use of com.facebook.presto.sql.tree.NodeRef in project presto by prestodb.
the class TestExpressionInterpreter method optimize.
private static Object optimize(Expression expression) {
Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(TEST_SESSION, METADATA, SQL_PARSER, SYMBOL_TYPES, expression, emptyList(), WarningCollector.NOOP);
ExpressionInterpreter interpreter = expressionOptimizer(expression, METADATA, TEST_SESSION, expressionTypes);
return interpreter.optimize(variable -> {
Symbol symbol = new Symbol(variable.getName());
Object value = symbolConstant(symbol);
if (value == null) {
return symbol.toSymbolReference();
}
return value;
});
}
use of com.facebook.presto.sql.tree.NodeRef in project presto by prestodb.
the class SqlFunctionUtils method rewriteLambdaExpression.
private static Expression rewriteLambdaExpression(Expression sqlFunction, Map<String, VariableReferenceExpression> arguments, ExpressionAnalysis functionAnalysis, PlanVariableAllocator variableAllocator) {
Map<NodeRef<Identifier>, LambdaArgumentDeclaration> lambdaArgumentReferences = functionAnalysis.getLambdaArgumentReferences();
Map<NodeRef<Expression>, Type> expressionTypes = functionAnalysis.getExpressionTypes();
// Rewrite reference to LambdaArgumentDeclaration
Map<NodeRef<LambdaArgumentDeclaration>, VariableReferenceExpression> variables = expressionTypes.entrySet().stream().filter(entry -> entry.getKey().getNode() instanceof LambdaArgumentDeclaration).distinct().collect(toImmutableMap(entry -> NodeRef.of((LambdaArgumentDeclaration) entry.getKey().getNode()), entry -> variableAllocator.newVariable(((LambdaArgumentDeclaration) entry.getKey().getNode()).getName(), entry.getValue(), "lambda")));
Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>>() {
@Override
public Expression rewriteLambdaExpression(LambdaExpression node, Map<NodeRef<Identifier>, LambdaArgumentDeclaration> context, ExpressionTreeRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>> treeRewriter) {
return new LambdaExpression(node.getArguments().stream().map(argument -> new LambdaArgumentDeclaration(new Identifier(variables.get(NodeRef.of(argument)).getName()))).collect(toImmutableList()), treeRewriter.rewrite(node.getBody(), context));
}
@Override
public Expression rewriteIdentifier(Identifier node, Map<NodeRef<Identifier>, LambdaArgumentDeclaration> context, ExpressionTreeRewriter<Map<NodeRef<Identifier>, LambdaArgumentDeclaration>> treeRewriter) {
NodeRef<Identifier> ref = NodeRef.of(node);
if (context.containsKey(ref)) {
return createSymbolReference(variables.get(NodeRef.of(context.get(ref))));
}
return node;
}
}, sqlFunction, lambdaArgumentReferences);
// Rewrite function input referenced in lambda
rewritten = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Map<String, VariableReferenceExpression>>() {
@Override
public Expression rewriteIdentifier(Identifier node, Map<String, VariableReferenceExpression> context, ExpressionTreeRewriter<Map<String, VariableReferenceExpression>> treeRewriter) {
if (context.containsKey(node.getValue())) {
return createSymbolReference(context.get(node.getValue()));
}
return node;
}
}, rewritten, arguments);
// Desugar lambda capture
return LambdaCaptureDesugaringRewriter.rewrite(rewritten, variableAllocator);
}
use of com.facebook.presto.sql.tree.NodeRef in project presto by prestodb.
the class ExpressionInterpreter method evaluateConstantExpression.
public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) {
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
analyzer.analyze(expression, Scope.create());
Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression));
if (!metadata.getFunctionAndTypeManager().canCoerce(actualType, expectedType)) {
throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, format("Cannot cast type %s to %s", actualType.getTypeSignature(), expectedType.getTypeSignature()));
}
Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder().putAll(analyzer.getExpressionCoercions()).put(NodeRef.of(expression), expectedType).build();
return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), metadata, session, ImmutableSet.of(), parameters);
}
use of com.facebook.presto.sql.tree.NodeRef in project presto by prestodb.
the class GroupingOperationRewriter method rewriteGroupingOperation.
public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Multimap<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<VariableReferenceExpression> groupIdVariable) {
requireNonNull(groupIdVariable, "groupIdVariable is null");
// See SQL:2011:4.16.2 and SQL:2011:6.9.10.
if (groupingSets.size() == 1) {
return new LongLiteral("0");
} else {
checkState(groupIdVariable.isPresent(), "groupId symbol is missing");
RelationId relationId = checkAndGetColumnReferenceField(expression.getGroupingColumns().get(0), columnReferenceFields).getRelationId();
List<Integer> columns = expression.getGroupingColumns().stream().map(NodeRef::of).peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")).map(columnReferenceFields::get).flatMap(Collection::stream).map(fieldId -> translateFieldToInteger(fieldId, relationId)).collect(toImmutableList());
List<Expression> groupingResults = groupingSets.stream().map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))).map(LongLiteral::new).collect(toImmutableList());
// It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1
return new SubscriptExpression(new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, createSymbolReference(groupIdVariable.get()), new GenericLiteral("BIGINT", "1")));
}
}
use of com.facebook.presto.sql.tree.NodeRef in project presto by prestodb.
the class PlanBuilder method rowExpression.
public RowExpression rowExpression(String sql) {
Expression expression = expression(sql);
Map<NodeRef<Expression>, Type> expressionTypes = getExpressionTypes(session, metadata, new SqlParser(), getTypes(), expression, ImmutableList.of(), WarningCollector.NOOP);
return SqlToRowExpressionTranslator.translate(expression, expressionTypes, ImmutableMap.of(), metadata.getFunctionAndTypeManager(), session);
}
Aggregations