Search in sources :

Example 26 with PlanBuilder

use of com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder in project presto by prestodb.

the class TestSchedulingOrderVisitor method testIndexJoinOrder.

@Test
public void testIndexJoinOrder() {
    PlanBuilder planBuilder = new PlanBuilder(TEST_SESSION, new PlanNodeIdAllocator(), METADATA);
    TableScanNode a = planBuilder.tableScan(emptyList(), emptyMap());
    TableScanNode b = planBuilder.tableScan(emptyList(), emptyMap());
    List<PlanNodeId> order = scheduleOrder(planBuilder.indexJoin(IndexJoinNode.Type.INNER, a, b));
    assertEquals(order, ImmutableList.of(b.getId(), a.getId()));
}
Also used : PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Example 27 with PlanBuilder

use of com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder in project presto by prestodb.

the class TestDruidQueryBase method tableScan.

protected TableScanNode tableScan(PlanBuilder planBuilder, DruidTableHandle connectorTableHandle, DruidColumnHandle... columnHandles) {
    List<VariableReferenceExpression> variables = Arrays.stream(columnHandles).map(ch -> new VariableReferenceExpression(Optional.empty(), ch.getColumnName().toLowerCase(ENGLISH), ch.getColumnType())).collect(toImmutableList());
    ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> assignments = ImmutableMap.builder();
    for (int i = 0; i < variables.size(); ++i) {
        assignments.put(variables.get(i), columnHandles[i]);
    }
    TableHandle tableHandle = new TableHandle(druidConnectorId, connectorTableHandle, TestingTransactionHandle.create(), Optional.empty());
    return new TableScanNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), tableHandle, variables, assignments.build(), TupleDomain.all(), TupleDomain.all());
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) WarningCollector(com.facebook.presto.spi.WarningCollector) FINAL(com.facebook.presto.spi.plan.LimitNode.Step.FINAL) Arrays(java.util.Arrays) MetadataManager(com.facebook.presto.metadata.MetadataManager) SqlToRowExpressionTranslator(com.facebook.presto.sql.relational.SqlToRowExpressionTranslator) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) StandardFunctionResolution(com.facebook.presto.spi.function.StandardFunctionResolution) Collectors.toMap(java.util.stream.Collectors.toMap) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) SESSION(com.facebook.presto.testing.TestingConnectorSession.SESSION) Map(java.util.Map) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) ENGLISH(java.util.Locale.ENGLISH) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Ordering(com.facebook.presto.spi.plan.Ordering) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SqlParser(com.facebook.presto.sql.parser.SqlParser) ConnectorSession(com.facebook.presto.spi.ConnectorSession) LimitNode(com.facebook.presto.spi.plan.LimitNode) List(java.util.List) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ExpressionAnalyzer.getExpressionTypes(com.facebook.presto.sql.analyzer.ExpressionAnalyzer.getExpressionTypes) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) ConnectorId(com.facebook.presto.spi.ConnectorId) ParsingOptions(com.facebook.presto.sql.parser.ParsingOptions) TABLE_COLUMN(com.facebook.presto.druid.DruidQueryGeneratorContext.Origin.TABLE_COLUMN) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) IntStream(java.util.stream.IntStream) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assignments(com.facebook.presto.spi.plan.Assignments) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) TestingSession(com.facebook.presto.testing.TestingSession) LinkedHashMap(java.util.LinkedHashMap) FilterNode(com.facebook.presto.spi.plan.FilterNode) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) TableHandle(com.facebook.presto.spi.TableHandle) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Session(com.facebook.presto.Session) TestingTransactionHandle(com.facebook.presto.testing.TestingTransactionHandle) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DERIVED(com.facebook.presto.druid.DruidQueryGeneratorContext.Origin.DERIVED) NodeRef(com.facebook.presto.sql.tree.NodeRef) PlanNode(com.facebook.presto.spi.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) TopNNode(com.facebook.presto.spi.plan.TopNNode) REGULAR(com.facebook.presto.druid.DruidColumnHandle.DruidColumnType.REGULAR) Metadata(com.facebook.presto.metadata.Metadata) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) TableHandle(com.facebook.presto.spi.TableHandle) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 28 with PlanBuilder

use of com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder 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)

Example 29 with PlanBuilder

use of com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder in project presto by prestodb.

the class TestDruidQueryGenerator method testDistinctSelection.

@Test
public void testDistinctSelection() {
    PlanNode justScan = buildPlan(planBuilder -> tableScan(planBuilder, druidTable, regionId, secondsSinceEpoch, city, fare));
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggBuilder.source(justScan).singleGroupingSet(variable("region.id"))), "SELECT \"region.Id\", count(*) FROM \"realtimeOnly\" GROUP BY \"region.Id\"");
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) BiConsumer(java.util.function.BiConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) PlanNode(com.facebook.presto.spi.plan.PlanNode) Test(org.testng.annotations.Test)

Example 30 with PlanBuilder

use of com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder in project presto by prestodb.

the class TestDruidQueryGenerator method testCountStar.

@Test
public void testCountStar() {
    BiConsumer<PlanBuilder, PlanBuilder.AggregationBuilder> aggregationFunctionBuilder = (planBuilder, aggregationBuilder) -> aggregationBuilder.addAggregation(planBuilder.variable("agg"), getRowExpression("count(*)", defaultSessionHolder));
    PlanNode justScan = buildPlan(planBuilder -> tableScan(planBuilder, druidTable, regionId, secondsSinceEpoch, city, fare));
    PlanNode filter = buildPlan(planBuilder -> filter(planBuilder, tableScan(planBuilder, druidTable, regionId, secondsSinceEpoch, city, fare), getRowExpression("fare > 3", defaultSessionHolder)));
    PlanNode anotherFilter = buildPlan(planBuilder -> filter(planBuilder, tableScan(planBuilder, druidTable, regionId, secondsSinceEpoch, city, fare), getRowExpression("secondssinceepoch between 200 and 300 and \"region.id\" >= 40", defaultSessionHolder)));
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(justScan).globalGrouping())), "SELECT count(*) FROM \"realtimeOnly\"");
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(filter).globalGrouping())), "SELECT count(*) FROM \"realtimeOnly\" WHERE (\"fare\" > 3)");
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(filter).singleGroupingSet(variable("region.id")))), "SELECT \"region.Id\", count(*) FROM \"realtimeOnly\" WHERE (\"fare\" > 3) GROUP BY \"region.Id\"");
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(justScan).singleGroupingSet(variable("region.id")))), "SELECT \"region.Id\", count(*) FROM \"realtimeOnly\" GROUP BY \"region.Id\"");
    testDQL(planBuilder -> limit(planBuilder, 5L, planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(justScan).singleGroupingSet(variable("region.id"))))), "SELECT \"region.Id\", count(*) FROM \"realtimeOnly\" GROUP BY \"region.Id\" LIMIT 5");
    testDQL(planBuilder -> planBuilder.aggregation(aggBuilder -> aggregationFunctionBuilder.accept(planBuilder, aggBuilder.source(anotherFilter).singleGroupingSet(variable("region.id"), variable("city")))), "SELECT \"region.Id\", \"city\", count(*) FROM \"realtimeOnly\" WHERE ((\"secondsSinceEpoch\" BETWEEN 200 AND 300) AND (\"region.Id\" >= 40)) GROUP BY \"region.Id\", \"city\"");
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) BiConsumer(java.util.function.BiConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) PlanNode(com.facebook.presto.spi.plan.PlanNode) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Aggregations

PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)70 Test (org.testng.annotations.Test)56 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)31 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)25 ImmutableList (com.google.common.collect.ImmutableList)23 ImmutableMap (com.google.common.collect.ImmutableMap)23 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)21 TopNNode (com.facebook.presto.spi.plan.TopNNode)20 Map (java.util.Map)19 Function (java.util.function.Function)19 Assert.assertEquals (org.testng.Assert.assertEquals)19 Ordering (com.facebook.presto.spi.plan.Ordering)18 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)18 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)18 Optional (java.util.Optional)18 BiConsumer (java.util.function.BiConsumer)18 Collectors (java.util.stream.Collectors)18 PinotColumnHandle (com.facebook.presto.pinot.PinotColumnHandle)16 SortOrder (com.facebook.presto.common.block.SortOrder)15