Search in sources :

Example 1 with ExpressionAnalyzer

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;
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) RowType(com.facebook.presto.type.RowType) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) FunctionType(com.facebook.presto.type.FunctionType) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.type.ArrayType) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer)

Example 2 with ExpressionAnalyzer

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();
}
Also used : ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer)

Example 3 with ExpressionAnalyzer

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();
}
Also used : ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer)

Example 4 with ExpressionAnalyzer

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;
}
Also used : SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) SqlFunctionUtils.getSqlFunctionExpression(com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer)

Example 5 with ExpressionAnalyzer

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);
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) FunctionImplementationType(com.facebook.presto.spi.function.FunctionImplementationType) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) OperatorType(com.facebook.presto.common.function.OperatorType) FunctionType(com.facebook.presto.common.type.FunctionType) RowType(com.facebook.presto.common.type.RowType) LiteralEncoder.isSupportedLiteralType(com.facebook.presto.sql.planner.LiteralEncoder.isSupportedLiteralType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Type(com.facebook.presto.common.type.Type) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) SqlFunctionUtils.getSqlFunctionExpression(com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Aggregations

ExpressionAnalyzer (com.facebook.presto.sql.analyzer.ExpressionAnalyzer)6 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 ArithmeticUnaryExpression (com.facebook.presto.sql.tree.ArithmeticUnaryExpression)3 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)3 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 Expression (com.facebook.presto.sql.tree.Expression)3 IfExpression (com.facebook.presto.sql.tree.IfExpression)3 InListExpression (com.facebook.presto.sql.tree.InListExpression)3 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)3 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)3 NotExpression (com.facebook.presto.sql.tree.NotExpression)3 NullIfExpression (com.facebook.presto.sql.tree.NullIfExpression)3 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)3 SearchedCaseExpression (com.facebook.presto.sql.tree.SearchedCaseExpression)3 SimpleCaseExpression (com.facebook.presto.sql.tree.SimpleCaseExpression)3 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)3 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)3 CanonicalizeExpressionRewriter.canonicalizeExpression (com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression)2 SqlFunctionUtils.getSqlFunctionExpression (com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression)2