Search in sources :

Example 1 with ExpressionAnalysis

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

the class FunctionAssertions method createExpression.

public static Expression createExpression(Session session, String expression, Metadata metadata, TypeProvider symbolTypes) {
    Expression parsedExpression = SQL_PARSER.createExpression(expression, createParsingOptions(session));
    parsedExpression = rewriteIdentifiersToSymbolReferences(parsedExpression);
    final ExpressionAnalysis analysis = analyzeExpressions(session, metadata, SQL_PARSER, symbolTypes, ImmutableList.of(parsedExpression), ImmutableList.of(), WarningCollector.NOOP, false);
    Expression rewrittenExpression = 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 coercion = analysis.getCoercion(node);
            if (coercion != null) {
                rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(node));
            }
            return rewrittenExpression;
        }

        @Override
        public Expression rewriteDereferenceExpression(DereferenceExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
            if (analysis.isColumnReference(node)) {
                return rewriteExpression(node, context, treeRewriter);
            }
            Expression rewrittenExpression = treeRewriter.defaultRewrite(node, context);
            // cast expression if coercion is registered
            Type coercion = analysis.getCoercion(node);
            if (coercion != null) {
                rewrittenExpression = new Cast(rewrittenExpression, coercion.getTypeSignature().toString());
            }
            return rewrittenExpression;
        }
    }, parsedExpression);
    return CanonicalizeExpressionRewriter.rewrite(rewrittenExpression, session, metadata, new TypeAnalyzer(SQL_PARSER, metadata), symbolTypes);
}
Also used : Cast(io.prestosql.sql.tree.Cast) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) Expression(io.prestosql.sql.tree.Expression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) ExpressionAnalysis(io.prestosql.sql.analyzer.ExpressionAnalysis) TypeAnalyzer(io.prestosql.sql.planner.TypeAnalyzer)

Aggregations

RowExpression (io.prestosql.spi.relation.RowExpression)1 RowType (io.prestosql.spi.type.RowType)1 Type (io.prestosql.spi.type.Type)1 ExpressionAnalysis (io.prestosql.sql.analyzer.ExpressionAnalysis)1 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)1 Cast (io.prestosql.sql.tree.Cast)1 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)1 Expression (io.prestosql.sql.tree.Expression)1