use of com.facebook.presto.sql.analyzer.ExpressionAnalyzer in project presto by prestodb.
the class ExpressionInterpreter method evaluateConstantExpression.
public static Object evaluateConstantExpression(Expression expression, IdentityLinkedHashMap<Expression, Type> coercions, Metadata metadata, Session session, Set<Expression> columnReferences, List<Expression> parameters) {
requireNonNull(columnReferences, "columnReferences is null");
verifyExpressionIsConstant(columnReferences, expression);
// add coercions
Expression rewrite = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {
@Override
public Expression rewriteExpression(Expression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
// cast expression if coercion is registered
Type coerceToType = coercions.get(node);
if (coerceToType != null) {
rewrittenExpression = new Cast(rewrittenExpression, coerceToType.getTypeSignature().toString());
}
return rewrittenExpression;
}
}, expression);
// redo the analysis since above expression rewriter might create new expressions which do not have entries in the type map
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters);
analyzer.analyze(rewrite, Scope.builder().build());
// remove syntax sugar
rewrite = ExpressionTreeRewriter.rewriteWith(new DesugaringRewriter(analyzer.getExpressionTypes()), rewrite);
// expressionInterpreter/optimizer only understands a subset of expression types
// TODO: remove this when the new expression tree is implemented
Expression canonicalized = CanonicalizeExpressions.canonicalizeExpression(rewrite);
// The optimization above may have rewritten the expression tree which breaks all the identity maps, so redo the analysis
// to re-analyze coercions that might be necessary
analyzer = createConstantAnalyzer(metadata, session, parameters);
analyzer.analyze(canonicalized, Scope.builder().build());
// evaluate the expression
Object result = expressionInterpreter(canonicalized, metadata, session, analyzer.getExpressionTypes()).evaluate(0);
verify(!(result instanceof Expression), "Expression interpreter returned an unresolved expression");
return result;
}
use of com.facebook.presto.sql.analyzer.ExpressionAnalyzer in project presto by prestodb.
the class TestingRowExpressionTranslator method getExpressionTypes.
private Map<NodeRef<Expression>, Type> getExpressionTypes(Expression expression, TypeProvider typeProvider) {
ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries(metadata.getFunctionAndTypeManager(), TEST_SESSION, typeProvider, emptyList(), node -> new IllegalStateException("Unexpected node: %s" + node), WarningCollector.NOOP, false);
expressionAnalyzer.analyze(expression, Scope.create());
return expressionAnalyzer.getExpressionTypes();
}
use of com.facebook.presto.sql.analyzer.ExpressionAnalyzer in project presto by prestodb.
the class FilterStatsCalculator method getExpressionTypes.
private Map<NodeRef<Expression>, Type> getExpressionTypes(Session session, Expression expression, TypeProvider types) {
ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries(metadata.getFunctionAndTypeManager(), session, types, emptyList(), node -> new IllegalStateException("Unexpected node: %s" + node), WarningCollector.NOOP, false);
expressionAnalyzer.analyze(expression, Scope.create());
return expressionAnalyzer.getExpressionTypes();
}
use of com.facebook.presto.sql.analyzer.ExpressionAnalyzer in project presto by prestodb.
the class ExpressionInterpreter method evaluateConstantExpression.
private static Object evaluateConstantExpression(Expression expression, Map<NodeRef<Expression>, Type> coercions, Set<NodeRef<Expression>> typeOnlyCoercions, Metadata metadata, Session session, Set<NodeRef<Expression>> columnReferences, List<Expression> parameters) {
requireNonNull(columnReferences, "columnReferences is null");
verifyExpressionIsConstant(columnReferences, expression);
// add coercions
Expression rewrite = Coercer.addCoercions(expression, coercions, typeOnlyCoercions);
// redo the analysis since above expression rewriter might create new expressions which do not have entries in the type map
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
analyzer.analyze(rewrite, Scope.create());
// remove syntax sugar
rewrite = DesugarAtTimeZoneRewriter.rewrite(rewrite, analyzer.getExpressionTypes());
// expressionInterpreter/optimizer only understands a subset of expression types
// TODO: remove this when the new expression tree is implemented
Expression canonicalized = canonicalizeExpression(rewrite);
// The optimization above may have rewritten the expression tree which breaks all the identity maps, so redo the analysis
// to re-analyze coercions that might be necessary
analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
analyzer.analyze(canonicalized, Scope.create());
// evaluate the expression
Object result = expressionInterpreter(canonicalized, metadata, session, analyzer.getExpressionTypes()).evaluate();
verify(!(result instanceof Expression), "Expression interpreter returned an unresolved expression");
return result;
}
use of com.facebook.presto.sql.analyzer.ExpressionAnalyzer 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);
}
Aggregations