Search in sources :

Example 21 with PlanVariableAllocator

use of com.facebook.presto.sql.planner.PlanVariableAllocator in project presto by prestodb.

the class TestSimplifyExpressions method assertSimplifies.

private static void assertSimplifies(String expression, String expected, String rowExpressionExpected) {
    Expression actualExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expression));
    Expression expectedExpression = rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expected));
    Expression rewritten = rewrite(actualExpression, TEST_SESSION, new PlanVariableAllocator(booleanVariablesFor(actualExpression)), METADATA, LITERAL_ENCODER, SQL_PARSER);
    assertEquals(normalize(rewritten), normalize(expectedExpression));
    TestingRowExpressionTranslator translator = new TestingRowExpressionTranslator(METADATA);
    RowExpression actualRowExpression = translator.translate(actualExpression, TypeProvider.viewOf(TYPES));
    RowExpression simplifiedRowExpression = SimplifyRowExpressions.rewrite(actualRowExpression, METADATA, TEST_SESSION.toConnectorSession());
    Expression expectedByRowExpression = Optional.ofNullable(rowExpressionExpected).map(expr -> rewriteIdentifiersToSymbolReferences(SQL_PARSER.createExpression(expr))).orElse(rewritten);
    RowExpression simplifiedByExpression = translator.translate(expectedByRowExpression, TypeProvider.viewOf(TYPES));
    assertEquals(simplifiedRowExpression, simplifiedByExpression);
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) IntStream(java.util.stream.IntStream) MetadataManager(com.facebook.presto.metadata.MetadataManager) Assert.assertEquals(org.testng.Assert.assertEquals) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) PrestoException(com.facebook.presto.spi.PrestoException) Function(java.util.function.Function) TEST_SESSION(com.facebook.presto.SessionTestUtils.TEST_SESSION) Collectors.toMap(java.util.stream.Collectors.toMap) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) Map(java.util.Map) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Cast(com.facebook.presto.sql.tree.Cast) Type(com.facebook.presto.common.type.Type) VariablesExtractor(com.facebook.presto.sql.planner.VariablesExtractor) RowExpression(com.facebook.presto.spi.relation.RowExpression) SimplifyExpressions.rewrite(com.facebook.presto.sql.planner.iterative.rule.SimplifyExpressions.rewrite) ExpressionUtils.extractPredicates(com.facebook.presto.sql.ExpressionUtils.extractPredicates) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) INVALID_CAST_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT) TestingRowExpressionTranslator(com.facebook.presto.sql.TestingRowExpressionTranslator) Streams(com.google.common.collect.Streams) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) ExpressionUtils.rewriteIdentifiersToSymbolReferences(com.facebook.presto.sql.ExpressionUtils.rewriteIdentifiersToSymbolReferences) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) LiteralEncoder(com.facebook.presto.sql.planner.LiteralEncoder) Stream(java.util.stream.Stream) Expression(com.facebook.presto.sql.tree.Expression) ExpressionUtils.binaryExpression(com.facebook.presto.sql.ExpressionUtils.binaryExpression) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) Optional(java.util.Optional) Comparator(java.util.Comparator) TestingRowExpressionTranslator(com.facebook.presto.sql.TestingRowExpressionTranslator) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ExpressionUtils.binaryExpression(com.facebook.presto.sql.ExpressionUtils.binaryExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator)

Example 22 with PlanVariableAllocator

use of com.facebook.presto.sql.planner.PlanVariableAllocator in project presto by prestodb.

the class TestStatisticsWriterNode method createTestDescriptor.

private static StatisticAggregationsDescriptor<VariableReferenceExpression> createTestDescriptor() {
    StatisticAggregationsDescriptor.Builder<VariableReferenceExpression> builder = StatisticAggregationsDescriptor.builder();
    PlanVariableAllocator variableAllocator = new PlanVariableAllocator();
    for (String column : COLUMNS) {
        for (ColumnStatisticType type : ColumnStatisticType.values()) {
            builder.addColumnStatistic(new ColumnStatisticMetadata(column, type), testVariable(variableAllocator));
        }
        builder.addGrouping(column, testVariable(variableAllocator));
    }
    builder.addTableStatistic(ROW_COUNT, testVariable(variableAllocator));
    return builder.build();
}
Also used : ColumnStatisticMetadata(com.facebook.presto.spi.statistics.ColumnStatisticMetadata) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ColumnStatisticType(com.facebook.presto.spi.statistics.ColumnStatisticType) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator)

Aggregations

PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)22 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)9 Test (org.testng.annotations.Test)8 PlanNodeIdAllocator (com.facebook.presto.spi.plan.PlanNodeIdAllocator)7 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)6 PlanNode (com.facebook.presto.spi.plan.PlanNode)5 Map (java.util.Map)5 Type (com.facebook.presto.common.type.Type)4 SqlParser (com.facebook.presto.sql.parser.SqlParser)4 ProjectionContext (com.facebook.presto.sql.planner.iterative.rule.PlanRemotePojections.ProjectionContext)4 Expression (com.facebook.presto.sql.tree.Expression)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 List (java.util.List)4 Session (com.facebook.presto.Session)3 Metadata (com.facebook.presto.metadata.Metadata)3 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 Optional (java.util.Optional)3 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)2