Search in sources :

Example 96 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestSimplifyExpressions method assertSimplifies.

private static void assertSimplifies(String expression, String expected) {
    Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression));
    Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected));
    assertEquals(normalize(simplifyExpressions(actualExpression)), normalize(expectedExpression));
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ExpressionUtils.binaryExpression(com.facebook.presto.sql.ExpressionUtils.binaryExpression)

Example 97 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestExpressionEquivalence method assertEquivalent.

private static void assertEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
    Expression leftExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(left));
    Expression rightExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(right));
    Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
    Map<Symbol, Type> types = symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType));
    assertTrue(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, leftExpression, rightExpression, types), String.format("Expected (%s) and (%s) to be equivalent", left, right));
    assertTrue(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, rightExpression, leftExpression, types), String.format("Expected (%s) and (%s) to be equivalent", right, left));
}
Also used : Type(com.facebook.presto.spi.type.Type) Expression(com.facebook.presto.sql.tree.Expression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 98 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class TestExpressionEquivalence method assertNotEquivalent.

private static void assertNotEquivalent(@Language("SQL") String left, @Language("SQL") String right) {
    Expression leftExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(left));
    Expression rightExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(right));
    Set<Symbol> symbols = extractUnique(ImmutableList.of(leftExpression, rightExpression));
    Map<Symbol, Type> types = symbols.stream().collect(toMap(identity(), TestExpressionEquivalence::generateType));
    assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, leftExpression, rightExpression, types), String.format("Expected (%s) and (%s) to not be equivalent", left, right));
    assertFalse(EQUIVALENCE.areExpressionsEquivalent(TEST_SESSION, rightExpression, leftExpression, types), String.format("Expected (%s) and (%s) to not be equivalent", right, left));
}
Also used : Type(com.facebook.presto.spi.type.Type) Expression(com.facebook.presto.sql.tree.Expression) Symbol(com.facebook.presto.sql.planner.Symbol)

Example 99 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class FunctionAssertions method compileScanFilterProject.

private SourceOperatorFactory compileScanFilterProject(Expression filter, Expression projection, ExpressionCompiler compiler) {
    filter = new SymbolToInputRewriter(INPUT_MAPPING).rewrite(filter);
    projection = new SymbolToInputRewriter(INPUT_MAPPING).rewrite(projection);
    IdentityLinkedHashMap<Expression, Type> expressionTypes = getExpressionTypesFromInput(TEST_SESSION, metadata, SQL_PARSER, INPUT_TYPES, ImmutableList.of(filter, projection), emptyList());
    try {
        Supplier<CursorProcessor> cursorProcessor = compiler.compileCursorProcessor(toRowExpression(filter, expressionTypes), ImmutableList.of(toRowExpression(projection, expressionTypes)), SOURCE_ID);
        Supplier<PageProcessor> pageProcessor = compiler.compilePageProcessor(toRowExpression(filter, expressionTypes), ImmutableList.of(toRowExpression(projection, expressionTypes)));
        return new ScanFilterAndProjectOperator.ScanFilterAndProjectOperatorFactory(0, new PlanNodeId("test"), SOURCE_ID, PAGE_SOURCE_PROVIDER, cursorProcessor, pageProcessor, ImmutableList.of(), ImmutableList.of(expressionTypes.get(projection)));
    } catch (Throwable e) {
        if (e instanceof UncheckedExecutionException) {
            e = e.getCause();
        }
        throw new RuntimeException("Error compiling " + projection + ": " + e.getMessage(), e);
    }
}
Also used : CursorProcessor(com.facebook.presto.operator.CursorProcessor) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) Type(com.facebook.presto.spi.type.Type) PageProcessor(com.facebook.presto.operator.PageProcessor) GenericPageProcessor(com.facebook.presto.operator.GenericPageProcessor) RowExpression(com.facebook.presto.sql.relational.RowExpression) CanonicalizeExpressions.canonicalizeExpression(com.facebook.presto.sql.planner.optimizations.CanonicalizeExpressions.canonicalizeExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) SymbolToInputRewriter(com.facebook.presto.sql.planner.SymbolToInputRewriter)

Example 100 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class FunctionAssertions method createExpression.

public static Expression createExpression(String expression, Metadata metadata, Map<Symbol, Type> symbolTypes) {
    Expression parsedExpression = SQL_PARSER.createExpression(expression);
    parsedExpression = rewriteIdentifiersToSymbolReferences(parsedExpression);
    final ExpressionAnalysis analysis = analyzeExpressionsWithSymbols(TEST_SESSION, metadata, SQL_PARSER, symbolTypes, ImmutableList.of(parsedExpression), emptyList(), 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.getColumnReferences().contains(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 canonicalizeExpression(rewrittenExpression);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) Type(com.facebook.presto.spi.type.Type) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) RowExpression(com.facebook.presto.sql.relational.RowExpression) CanonicalizeExpressions.canonicalizeExpression(com.facebook.presto.sql.planner.optimizations.CanonicalizeExpressions.canonicalizeExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ExpressionAnalysis(com.facebook.presto.sql.analyzer.ExpressionAnalysis)

Aggregations

Expression (com.facebook.presto.sql.tree.Expression)137 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)74 Test (org.testng.annotations.Test)46 NotExpression (com.facebook.presto.sql.tree.NotExpression)42 InListExpression (com.facebook.presto.sql.tree.InListExpression)40 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)33 Type (com.facebook.presto.spi.type.Type)26 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)26 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)25 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)22 ImmutableList (com.google.common.collect.ImmutableList)22 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)19 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)18 Cast (com.facebook.presto.sql.tree.Cast)17 ArrayList (java.util.ArrayList)17 ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)16 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)16 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)16 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)16 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)15