Search in sources :

Example 21 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class TestPinotPlanOptimizer method testDistinctCountPushdownNoOverride.

private void testDistinctCountPushdownNoOverride(PinotConfig pinotConfig) {
    PlanBuilder planBuilder = createPlanBuilder(new SessionHolder(pinotConfig));
    Map<VariableReferenceExpression, PinotColumnHandle> leftColumnHandleMap = ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "regionid", regionId.getDataType()), regionId);
    PlanNode leftJustScan = tableScan(planBuilder, pinotTable, leftColumnHandleMap);
    PlanNode leftAggregation = planBuilder.aggregation(aggBuilder -> aggBuilder.source(leftJustScan).addAggregation(planBuilder.variable("approx_distinct(regionid)"), getRowExpression("approx_distinct(regionid)", defaultSessionHolder), Optional.empty(), Optional.empty(), false, Optional.empty()).globalGrouping());
    PlanNode optimized = getOptimizedPlan(pinotConfig, planBuilder, leftAggregation);
    assertPlanMatch(optimized, PinotTableScanMatcher.match(pinotTable, Optional.of("SELECT DISTINCTCOUNTHLL\\(regionId\\) FROM hybrid"), Optional.of(false), leftAggregation.getOutputVariables(), useSqlSyntax()), typeProvider);
    PlanNode optimizedPlan = getOptimizedPlan(planBuilder, limit(planBuilder, 50L, tableScan(planBuilder, pinotTable, distinctCountDim)));
    assertPlanMatch(optimizedPlan, PinotTableScanMatcher.match(pinotTable, Optional.of("SELECT distinctCountDim FROM hybrid LIMIT 50"), Optional.of(false), optimizedPlan.getOutputVariables(), useSqlSyntax()), typeProvider);
}
Also used : PinotColumnHandle(com.facebook.presto.pinot.PinotColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)

Example 22 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class TestPinotPlanOptimizer method testSetOperationQueryWithSubQueriesPushdown.

@Test
public void testSetOperationQueryWithSubQueriesPushdown() {
    PlanBuilder planBuilder = createPlanBuilder(defaultSessionHolder);
    Map<VariableReferenceExpression, PinotColumnHandle> leftColumnHandleMap = ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "regionid", regionId.getDataType()), regionId);
    PlanNode leftJustScan = tableScan(planBuilder, pinotTable, leftColumnHandleMap);
    PlanNode leftMarkDistinct = markDistinct(planBuilder, variable("regionid$distinct"), ImmutableList.of(variable("regionid")), leftJustScan);
    PlanNode leftAggregation = planBuilder.aggregation(aggBuilder -> aggBuilder.source(leftMarkDistinct).addAggregation(planBuilder.variable("count(regionid)"), getRowExpression("count(regionid)", defaultSessionHolder), Optional.empty(), Optional.empty(), false, Optional.of(variable("regionid$distinct"))).globalGrouping());
    Map<VariableReferenceExpression, PinotColumnHandle> rightColumnHandleMap = ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "regionid_33", regionId.getDataType()), regionId);
    PlanNode rightJustScan = tableScan(planBuilder, pinotTable, rightColumnHandleMap);
    PlanNode rightMarkDistinct = markDistinct(planBuilder, variable("regionid$distinct_62"), ImmutableList.of(variable("regionid")), rightJustScan);
    PlanNode rightAggregation = planBuilder.aggregation(aggBuilder -> aggBuilder.source(rightMarkDistinct).addAggregation(planBuilder.variable("count(regionid_33)"), getRowExpression("count(regionid_33)", defaultSessionHolder), Optional.empty(), Optional.empty(), false, Optional.of(variable("regionid$distinct_62"))).globalGrouping());
    validateSetOperationOptimizer(planBuilder, planBuilder.union(ArrayListMultimap.create(), ImmutableList.of(leftAggregation, rightAggregation)));
    validateSetOperationOptimizer(planBuilder, planBuilder.intersect(ArrayListMultimap.create(), ImmutableList.of(leftAggregation, rightAggregation)));
    validateSetOperationOptimizer(planBuilder, planBuilder.except(ArrayListMultimap.create(), ImmutableList.of(leftAggregation, rightAggregation)));
}
Also used : PinotColumnHandle(com.facebook.presto.pinot.PinotColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder) Test(org.testng.annotations.Test)

Example 23 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class TestPinotPlanOptimizer method testDistinctCountInSubQueryPushdown.

private void testDistinctCountInSubQueryPushdown(String distinctCountFunctionName, PinotConfig pinotConfig) {
    PlanBuilder planBuilder = createPlanBuilder(new SessionHolder(pinotConfig));
    Map<VariableReferenceExpression, PinotColumnHandle> leftColumnHandleMap = ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "regionid", regionId.getDataType()), regionId);
    PlanNode leftJustScan = tableScan(planBuilder, pinotTable, leftColumnHandleMap);
    PlanNode leftMarkDistinct = markDistinct(planBuilder, variable("regionid$distinct"), ImmutableList.of(variable("regionid")), leftJustScan);
    PlanNode leftAggregation = planBuilder.aggregation(aggBuilder -> aggBuilder.source(leftMarkDistinct).addAggregation(planBuilder.variable("count(regionid)"), getRowExpression("count(regionid)", defaultSessionHolder), Optional.empty(), Optional.empty(), false, Optional.of(variable("regionid$distinct"))).globalGrouping());
    PlanNode optimized = getOptimizedPlan(pinotConfig, planBuilder, leftAggregation);
    assertPlanMatch(optimized, PinotTableScanMatcher.match(pinotTable, Optional.of(String.format("SELECT %s\\(regionId\\) FROM hybrid", distinctCountFunctionName)), Optional.of(false), leftAggregation.getOutputVariables(), useSqlSyntax()), typeProvider);
    Map<VariableReferenceExpression, PinotColumnHandle> rightColumnHandleMap = ImmutableMap.of(new VariableReferenceExpression(Optional.empty(), "regionid_33", regionId.getDataType()), regionId);
    PlanNode rightJustScan = tableScan(planBuilder, pinotTable, rightColumnHandleMap);
    PlanNode rightMarkDistinct = markDistinct(planBuilder, variable("regionid$distinct_62"), ImmutableList.of(variable("regionid")), rightJustScan);
    PlanNode rightAggregation = planBuilder.aggregation(aggBuilder -> aggBuilder.source(rightMarkDistinct).addAggregation(planBuilder.variable("count(regionid_33)"), getRowExpression("count(regionid_33)", defaultSessionHolder), Optional.empty(), Optional.empty(), false, Optional.of(variable("regionid$distinct_62"))).globalGrouping());
    optimized = getOptimizedPlan(pinotConfig, planBuilder, rightAggregation);
    assertPlanMatch(optimized, PinotTableScanMatcher.match(pinotTable, Optional.of(String.format("SELECT %s\\(regionId\\) FROM hybrid", distinctCountFunctionName)), Optional.of(false), rightAggregation.getOutputVariables(), useSqlSyntax()), typeProvider);
}
Also used : PinotColumnHandle(com.facebook.presto.pinot.PinotColumnHandle) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PlanBuilder(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)

Example 24 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class PinotPushdownUtils method computeAggregationNodes.

public static List<AggregationColumnNode> computeAggregationNodes(AggregationNode aggregationNode) {
    int groupByKeyIndex = 0;
    ImmutableList.Builder<AggregationColumnNode> nodeBuilder = ImmutableList.builder();
    for (VariableReferenceExpression outputColumn : aggregationNode.getOutputVariables()) {
        AggregationNode.Aggregation aggregation = aggregationNode.getAggregations().get(outputColumn);
        if (aggregation != null) {
            if (aggregation.getFilter().isPresent() || aggregation.isDistinct() || aggregation.getOrderBy().isPresent()) {
                throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Unsupported aggregation node " + aggregationNode);
            }
            if (aggregation.getMask().isPresent()) {
                // E.g. `SELECT count(distinct COL_A), sum(COL_B) FROM myTable` to Pinot as `SELECT distinctCount(COL_A), sum(COL_B) FROM myTable`
                if (aggregation.getCall().getDisplayName().equalsIgnoreCase(COUNT_FUNCTION_NAME) && aggregation.getMask().get().getName().contains(DISTINCT_MASK)) {
                    nodeBuilder.add(new AggregationFunctionColumnNode(outputColumn, new CallExpression(aggregation.getCall().getSourceLocation(), PINOT_DISTINCT_COUNT_FUNCTION_NAME, aggregation.getCall().getFunctionHandle(), aggregation.getCall().getType(), aggregation.getCall().getArguments())));
                    continue;
                }
                // Pinot doesn't support push down aggregation functions other than count on top of distinct function.
                throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Unsupported aggregation node with mask " + aggregationNode);
            }
            if (handlePushDownSingleDistinctCount(nodeBuilder, aggregationNode, outputColumn, aggregation)) {
                continue;
            }
            nodeBuilder.add(new AggregationFunctionColumnNode(outputColumn, aggregation.getCall()));
        } else {
            // group by output
            VariableReferenceExpression inputColumn = aggregationNode.getGroupingKeys().get(groupByKeyIndex);
            nodeBuilder.add(new GroupByColumnNode(inputColumn, outputColumn));
            groupByKeyIndex++;
        }
    }
    return nodeBuilder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 25 with VariableReferenceExpression

use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.

the class PinotQueryGeneratorContext method getAssignments.

public LinkedHashMap<VariableReferenceExpression, PinotColumnHandle> getAssignments(boolean isSqlSyntax) {
    LinkedHashMap<VariableReferenceExpression, PinotColumnHandle> result = new LinkedHashMap<>();
    LinkedHashSet<VariableReferenceExpression> outputFields = new LinkedHashSet<>();
    if (!isSqlSyntax) {
        outputFields.addAll(groupByColumns);
    }
    outputFields.addAll(outputs.stream().filter(variable -> !hiddenColumnSet.contains(variable)).collect(Collectors.toList()));
    outputFields.stream().forEach(variable -> {
        Selection selection = selections.get(variable);
        PinotColumnHandle handle = selection.getOrigin() == Origin.TABLE_COLUMN ? new PinotColumnHandle(selection.getDefinition(), variable.getType(), PinotColumnHandle.PinotColumnType.REGULAR) : new PinotColumnHandle(variable, PinotColumnHandle.PinotColumnType.DERIVED);
        result.put(variable, handle);
    });
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PinotColumnHandle(com.facebook.presto.pinot.PinotColumnHandle) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)340 Test (org.testng.annotations.Test)129 ImmutableList (com.google.common.collect.ImmutableList)109 RowExpression (com.facebook.presto.spi.relation.RowExpression)93 ImmutableMap (com.google.common.collect.ImmutableMap)89 PlanNode (com.facebook.presto.spi.plan.PlanNode)85 Optional (java.util.Optional)84 Map (java.util.Map)73 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)61 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)58 List (java.util.List)58 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)52 CallExpression (com.facebook.presto.spi.relation.CallExpression)49 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)48 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)45 Expression (com.facebook.presto.sql.tree.Expression)44 Assignments (com.facebook.presto.spi.plan.Assignments)42 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)42 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)42 ColumnHandle (com.facebook.presto.spi.ColumnHandle)40