Search in sources :

Example 1 with UnionNode

use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.

the class PushTopNThroughUnion method apply.

@Override
public Result apply(TopNNode topNNode, Captures captures, Context context) {
    UnionNode unionNode = captures.get(CHILD);
    ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
    for (PlanNode source : unionNode.getSources()) {
        SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
        Set<VariableReferenceExpression> sourceOutputVariables = ImmutableSet.copyOf(source.getOutputVariables());
        for (VariableReferenceExpression unionOutput : unionNode.getOutputVariables()) {
            Set<VariableReferenceExpression> inputVariables = ImmutableSet.copyOf(unionNode.getVariableMapping().get(unionOutput));
            VariableReferenceExpression unionInput = getLast(intersection(inputVariables, sourceOutputVariables));
            symbolMapper.put(unionOutput, unionInput);
        }
        sources.add(symbolMapper.build().map(topNNode, source, context.getIdAllocator().getNextId()));
    }
    return Result.ofPlanNode(new UnionNode(unionNode.getSourceLocation(), unionNode.getId(), sources.build(), unionNode.getOutputVariables(), unionNode.getVariableMapping()));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) SymbolMapper(com.facebook.presto.sql.planner.optimizations.SymbolMapper) ImmutableList(com.google.common.collect.ImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression)

Example 2 with UnionNode

use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.

the class TestTypeValidator method testValidUnion.

@Test
public void testValidUnion() {
    VariableReferenceExpression output = variableAllocator.newVariable("output", DATE);
    PlanNode node = new UnionNode(Optional.empty(), newId(), ImmutableList.of(baseTableScan, baseTableScan), ImmutableList.of(output), ImmutableMap.of(output, ImmutableList.of(variableD, variableD)));
    assertTypesValid(node);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test)

Example 3 with UnionNode

use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.

the class TestTypeValidator method testInvalidUnion.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of variable 'output(_[0-9]+)?' is expected to be date, but the actual type is bigint")
public void testInvalidUnion() {
    VariableReferenceExpression output = variableAllocator.newVariable("output", DATE);
    PlanNode node = new UnionNode(Optional.empty(), newId(), ImmutableList.of(baseTableScan, baseTableScan), ImmutableList.of(output), ImmutableMap.of(output, ImmutableList.of(variableD, // should be a symbol with DATE type
    variableA)));
    assertTypesValid(node);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test)

Example 4 with UnionNode

use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.

the class TestEffectivePredicateExtractor method testUnion.

@Test
public void testUnion() {
    PlanNode node = new UnionNode(Optional.empty(), newId(), ImmutableList.of(filter(baseTableScan, greaterThan(AV, bigintLiteral(10))), filter(baseTableScan, and(greaterThan(AV, bigintLiteral(10)), lessThan(AV, bigintLiteral(100)))), filter(baseTableScan, and(greaterThan(AV, bigintLiteral(10)), lessThan(AV, bigintLiteral(100))))), ImmutableList.of(AV), ImmutableMap.of(AV, ImmutableList.of(BV, CV, EV)));
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Only the common conjuncts can be inferred through a Union
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(greaterThan(AV, bigintLiteral(10))));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 5 with UnionNode

use of com.facebook.presto.spi.plan.UnionNode in project presto by prestodb.

the class TestDruidPlanOptimizer method testUnionAll.

@Test
public void testUnionAll() {
    PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
    PlanVariableAllocator variableAllocator = new PlanVariableAllocator();
    AggregationNode aggregationOne = simpleAggregationSum(planBuilder, tableScan(planBuilder, druidTableOne, city, fare), variableAllocator, ImmutableList.of(city), fare);
    AggregationNode aggregationTwo = simpleAggregationSum(planBuilder, tableScan(planBuilder, druidTableTwo, city, fare), variableAllocator, ImmutableList.of(city), fare);
    VariableReferenceExpression groupByColumn = variableAllocator.newVariable(city.getColumnName(), city.getColumnType());
    VariableReferenceExpression sumColumn = variableAllocator.newVariable(fare.getColumnName(), fare.getColumnType());
    PlanNode originalPlan = new UnionNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), ImmutableList.of(aggregationOne, aggregationTwo), ImmutableList.of(groupByColumn, sumColumn), ImmutableMap.of(groupByColumn, Stream.concat(aggregationOne.getGroupingKeys().stream(), aggregationTwo.getGroupingKeys().stream()).collect(toImmutableList()), sumColumn, ImmutableList.of(Iterables.getOnlyElement(aggregationOne.getAggregations().keySet()), Iterables.getOnlyElement(aggregationTwo.getAggregations().keySet()))));
    PlanNode optimizedPlan = getOptimizedPlan(planBuilder, originalPlan);
    PlanMatchPattern tableScanMatcherOne = DruidTableScanMatcher.match(druidTableOne.getTableName(), "SELECT \"city\", sum(fare) FROM \"realtimeOnly\" GROUP BY \"city\"");
    PlanMatchPattern tableScanMatcherTwo = DruidTableScanMatcher.match(druidTableTwo.getTableName(), "SELECT \"city\", sum(fare) FROM \"hybrid\" GROUP BY \"city\"");
    assertPlanMatch(optimizedPlan, PlanMatchPattern.union(tableScanMatcherOne, tableScanMatcherTwo), typeProvider);
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) UnionNode(com.facebook.presto.spi.plan.UnionNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PlanMatchPattern(com.facebook.presto.sql.planner.assertions.PlanMatchPattern) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) PlanVariableAllocator(com.facebook.presto.sql.planner.PlanVariableAllocator) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Aggregations

UnionNode (com.facebook.presto.spi.plan.UnionNode)10 PlanNode (com.facebook.presto.spi.plan.PlanNode)9 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)7 Test (org.testng.annotations.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 Map (java.util.Map)2 Type (com.facebook.presto.common.type.Type)1 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 LimitNode (com.facebook.presto.spi.plan.LimitNode)1 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)1 PlanVariableAllocator (com.facebook.presto.sql.planner.PlanVariableAllocator)1 PlanMatchPattern (com.facebook.presto.sql.planner.assertions.PlanMatchPattern)1 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)1 SymbolMapper (com.facebook.presto.sql.planner.optimizations.SymbolMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1