Search in sources :

Example 1 with ExpressionAnalyzer

use of io.prestosql.sql.analyzer.ExpressionAnalyzer in project hetu-core by openlookeng.

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 (!new TypeCoercion(metadata::getType).canCoerce(actualType, expectedType)) {
        throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, String.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(io.prestosql.sql.tree.NodeRef) RowType(io.prestosql.spi.type.RowType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) LiteralFunction.isSupportedLiteralType(io.prestosql.metadata.LiteralFunction.isSupportedLiteralType) FunctionType(io.prestosql.spi.type.FunctionType) CanonicalizeExpressionRewriter.canonicalizeExpression(io.prestosql.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer) TypeCoercion(io.prestosql.type.TypeCoercion) SemanticException(io.prestosql.sql.analyzer.SemanticException)

Example 2 with ExpressionAnalyzer

use of io.prestosql.sql.analyzer.ExpressionAnalyzer in project hetu-core by openlookeng.

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(), metadata);
    // 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(rewrite, Scope.create());
    // expressionInterpreter/optimizer only understands a subset of expression types
    // TODO: remove this when the new expression tree is implemented
    Expression canonicalized = canonicalizeExpression(rewrite, analyzer.getExpressionTypes(), metadata);
    // 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 : CanonicalizeExpressionRewriter.canonicalizeExpression(io.prestosql.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer)

Example 3 with ExpressionAnalyzer

use of io.prestosql.sql.analyzer.ExpressionAnalyzer in project hetu-core by openlookeng.

the class TestingRowExpressionTranslator method getExpressionTypes.

private Map<NodeRef<Expression>, Type> getExpressionTypes(Expression expression, TypeProvider typeProvider) {
    ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries(metadata, 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(io.prestosql.sql.analyzer.ExpressionAnalyzer)

Example 4 with ExpressionAnalyzer

use of io.prestosql.sql.analyzer.ExpressionAnalyzer in project hetu-core by openlookeng.

the class TestSqlToRowExpressionTranslator method getExpressionTypes.

private Map<NodeRef<Expression>, Type> getExpressionTypes(Expression expression) {
    ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries(metadata, TEST_SESSION, TypeProvider.empty(), emptyList(), node -> new IllegalStateException("Unexpected node: %s" + node), WarningCollector.NOOP, false);
    expressionAnalyzer.analyze(expression, Scope.create());
    return expressionAnalyzer.getExpressionTypes();
}
Also used : ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer)

Example 5 with ExpressionAnalyzer

use of io.prestosql.sql.analyzer.ExpressionAnalyzer in project hetu-core by openlookeng.

the class FilterStatsCalculator method getExpressionTypes.

private Map<NodeRef<Expression>, Type> getExpressionTypes(Session session, Expression expression, TypeProvider types) {
    ExpressionAnalyzer expressionAnalyzer = ExpressionAnalyzer.createWithoutSubqueries(metadata, session, types, emptyList(), node -> new IllegalStateException("Unexpected node: %s" + node), WarningCollector.NOOP, false);
    expressionAnalyzer.analyze(expression, Scope.create());
    return expressionAnalyzer.getExpressionTypes();
}
Also used : ExpressionAnalyzer(io.prestosql.sql.analyzer.ExpressionAnalyzer)

Aggregations

ExpressionAnalyzer (io.prestosql.sql.analyzer.ExpressionAnalyzer)5 CanonicalizeExpressionRewriter.canonicalizeExpression (io.prestosql.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression)2 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)2 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)2 BindExpression (io.prestosql.sql.tree.BindExpression)2 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)2 Expression (io.prestosql.sql.tree.Expression)2 IfExpression (io.prestosql.sql.tree.IfExpression)2 InListExpression (io.prestosql.sql.tree.InListExpression)2 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)2 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)2 NotExpression (io.prestosql.sql.tree.NotExpression)2 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)2 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)2 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)2 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)2 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)2 SubscriptExpression (io.prestosql.sql.tree.SubscriptExpression)2