Search in sources :

Example 1 with GroupingOperation

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

the class QueryPlanner method handleGroupingOperations.

private PlanBuilder handleGroupingOperations(PlanBuilder subPlan, QuerySpecification node, Optional<VariableReferenceExpression> groupIdVariable, List<Set<FieldId>> groupingSets) {
    if (analysis.getGroupingOperations(node).isEmpty()) {
        return subPlan;
    }
    TranslationMap newTranslations = subPlan.copyTranslations();
    Assignments.Builder projections = Assignments.builder();
    projections.putAll(identitiesAsSymbolReferences(subPlan.getRoot().getOutputVariables()));
    List<Set<Integer>> descriptor = groupingSets.stream().map(set -> set.stream().map(FieldId::getFieldIndex).collect(toImmutableSet())).collect(toImmutableList());
    for (GroupingOperation groupingOperation : analysis.getGroupingOperations(node)) {
        Expression rewritten = GroupingOperationRewriter.rewriteGroupingOperation(groupingOperation, descriptor, analysis.getColumnReferenceFields(), groupIdVariable);
        Type coercion = analysis.getCoercion(groupingOperation);
        VariableReferenceExpression variable = variableAllocator.newVariable(rewritten, analysis.getTypeWithCoercions(groupingOperation));
        if (coercion != null) {
            rewritten = new Cast(rewritten, coercion.getTypeSignature().toString(), false, metadata.getFunctionAndTypeManager().isTypeOnlyCoercion(analysis.getType(groupingOperation), coercion));
        }
        projections.put(variable, castToRowExpression(rewritten));
        newTranslations.put(groupingOperation, variable);
    }
    return new PlanBuilder(newTranslations, new ProjectNode(subPlan.getRoot().getSourceLocation(), idAllocator.getNextId(), subPlan.getRoot(), projections.build(), LOCAL));
}
Also used : FINAL(com.facebook.presto.spi.plan.LimitNode.Step.FINAL) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) SortNode(com.facebook.presto.sql.planner.plan.SortNode) OriginalExpressionUtils(com.facebook.presto.sql.relational.OriginalExpressionUtils) FrameBound(com.facebook.presto.sql.tree.FrameBound) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Field(com.facebook.presto.sql.analyzer.Field) WindowNodeUtil.toBoundType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toBoundType) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) Delete(com.facebook.presto.sql.tree.Delete) Map(java.util.Map) LOCAL(com.facebook.presto.spi.plan.ProjectNode.Locality.LOCAL) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) CallExpression(com.facebook.presto.spi.relation.CallExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) OffsetNode(com.facebook.presto.sql.planner.plan.OffsetNode) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AssignmentUtils.identitiesAsSymbolReferences(com.facebook.presto.sql.planner.plan.AssignmentUtils.identitiesAsSymbolReferences) RelationId(com.facebook.presto.sql.analyzer.RelationId) ImmutableSet(com.google.common.collect.ImmutableSet) Query(com.facebook.presto.sql.tree.Query) WindowNodeUtil.toWindowType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toWindowType) SortOrder(com.facebook.presto.common.block.SortOrder) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Ordering(com.facebook.presto.spi.plan.Ordering) ExpressionTreeUtils(com.facebook.presto.sql.analyzer.ExpressionTreeUtils) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Node(com.facebook.presto.sql.tree.Node) Set(java.util.Set) SortItem(com.facebook.presto.sql.tree.SortItem) Sets(com.google.common.collect.Sets) LimitNode(com.facebook.presto.spi.plan.LimitNode) SystemSessionProperties.isSkipRedundantSort(com.facebook.presto.SystemSessionProperties.isSkipRedundantSort) List(java.util.List) Window(com.facebook.presto.sql.tree.Window) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) ExpressionTreeUtils.getSourceLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getSourceLocation) FieldId(com.facebook.presto.sql.analyzer.FieldId) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) PlannerUtils.toOrderingScheme(com.facebook.presto.sql.planner.PlannerUtils.toOrderingScheme) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) PlannerUtils.toSortOrder(com.facebook.presto.sql.planner.PlannerUtils.toSortOrder) GroupIdNode(com.facebook.presto.sql.planner.plan.GroupIdNode) Assignments(com.facebook.presto.spi.plan.Assignments) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WindowFrame(com.facebook.presto.sql.tree.WindowFrame) FilterNode(com.facebook.presto.spi.plan.FilterNode) AssignmentUtils(com.facebook.presto.sql.planner.plan.AssignmentUtils) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) TableHandle(com.facebook.presto.spi.TableHandle) Cast(com.facebook.presto.sql.tree.Cast) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation) OrderBy(com.facebook.presto.sql.tree.OrderBy) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) Session(com.facebook.presto.Session) NodeLocation(com.facebook.presto.sql.tree.NodeLocation) NodeUtils.getSortItemsFromOrderBy(com.facebook.presto.sql.NodeUtils.getSortItemsFromOrderBy) RelationType(com.facebook.presto.sql.analyzer.RelationType) Offset(com.facebook.presto.sql.tree.Offset) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) NodeRef(com.facebook.presto.sql.tree.NodeRef) Streams.stream(com.google.common.collect.Streams.stream) Scope(com.facebook.presto.sql.analyzer.Scope) PlanNode(com.facebook.presto.spi.plan.PlanNode) AggregationNode.groupingSets(com.facebook.presto.spi.plan.AggregationNode.groupingSets) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) FieldReference(com.facebook.presto.sql.tree.FieldReference) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) OriginalExpressionUtils.asSymbolReference(com.facebook.presto.sql.relational.OriginalExpressionUtils.asSymbolReference) Metadata(com.facebook.presto.metadata.Metadata) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) Cast(com.facebook.presto.sql.tree.Cast) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Assignments(com.facebook.presto.spi.plan.Assignments) WindowNodeUtil.toBoundType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toBoundType) WindowNodeUtil.toWindowType(com.facebook.presto.sql.planner.optimizations.WindowNodeUtil.toWindowType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation)

Example 2 with GroupingOperation

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

the class TestSqlParser method testSelectWithGroupBy.

@Test
public void testSelectWithGroupBy() {
    assertStatement("SELECT * FROM table1 GROUP BY a", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new SimpleGroupBy(ImmutableList.of(new Identifier("a")))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 GROUP BY a, b", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new SimpleGroupBy(ImmutableList.of(new Identifier("a"))), new SimpleGroupBy(ImmutableList.of(new Identifier("b")))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 GROUP BY ()", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new SimpleGroupBy(ImmutableList.of())))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 GROUP BY GROUPING SETS (a)", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a"))))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT a, b, GROUPING(a, b) FROM table1 GROUP BY GROUPING SETS ((a), (b))", new Query(Optional.empty(), new QuerySpecification(selectList(DereferenceExpression.from(QualifiedName.of("a")), DereferenceExpression.from(QualifiedName.of("b")), new GroupingOperation(Optional.empty(), ImmutableList.of(QualifiedName.of("a"), QualifiedName.of("b")))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a")), ImmutableList.of(new Identifier("b"))))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 GROUP BY ALL GROUPING SETS ((a, b), (a), ()), CUBE (c), ROLLUP (d)", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(false, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableList.of(new Identifier("a")), ImmutableList.of())), new Cube(ImmutableList.of(new Identifier("c"))), new Rollup(ImmutableList.of(new Identifier("d")))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 GROUP BY DISTINCT GROUPING SETS ((a, b), (a), ()), CUBE (c), ROLLUP (d)", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.of(new GroupBy(true, ImmutableList.of(new GroupingSets(ImmutableList.of(ImmutableList.of(new Identifier("a"), new Identifier("b")), ImmutableList.of(new Identifier("a")), ImmutableList.of())), new Cube(ImmutableList.of(new Identifier("c"))), new Rollup(ImmutableList.of(new Identifier("d")))))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty(), Optional.empty()));
}
Also used : GroupingSets(com.facebook.presto.sql.tree.GroupingSets) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) GroupBy(com.facebook.presto.sql.tree.GroupBy) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) Rollup(com.facebook.presto.sql.tree.Rollup) Cube(com.facebook.presto.sql.tree.Cube) AllColumns(com.facebook.presto.sql.tree.AllColumns) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation) Test(org.testng.annotations.Test)

Example 3 with GroupingOperation

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

the class GroupingOperationRewriter method rewriteGroupingOperation.

public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Multimap<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<VariableReferenceExpression> groupIdVariable) {
    requireNonNull(groupIdVariable, "groupIdVariable is null");
    // See SQL:2011:4.16.2 and SQL:2011:6.9.10.
    if (groupingSets.size() == 1) {
        return new LongLiteral("0");
    } else {
        checkState(groupIdVariable.isPresent(), "groupId symbol is missing");
        RelationId relationId = checkAndGetColumnReferenceField(expression.getGroupingColumns().get(0), columnReferenceFields).getRelationId();
        List<Integer> columns = expression.getGroupingColumns().stream().map(NodeRef::of).peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")).map(columnReferenceFields::get).flatMap(Collection::stream).map(fieldId -> translateFieldToInteger(fieldId, relationId)).collect(toImmutableList());
        List<Expression> groupingResults = groupingSets.stream().map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))).map(LongLiteral::new).collect(toImmutableList());
        // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1
        return new SubscriptExpression(new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, createSymbolReference(groupIdVariable.get()), new GenericLiteral("BIGINT", "1")));
    }
}
Also used : SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ExpressionTreeUtils.checkAndGetColumnReferenceField(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.checkAndGetColumnReferenceField) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Multimap(com.google.common.collect.Multimap) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NodeRef(com.facebook.presto.sql.tree.NodeRef) List(java.util.List) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Expression(com.facebook.presto.sql.tree.Expression) ADD(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) FieldId(com.facebook.presto.sql.analyzer.FieldId) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Optional(java.util.Optional) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) Collection(java.util.Collection) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral)

Aggregations

VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 FieldId (com.facebook.presto.sql.analyzer.FieldId)2 RelationId (com.facebook.presto.sql.analyzer.RelationId)2 GroupingOperation (com.facebook.presto.sql.tree.GroupingOperation)2 Session (com.facebook.presto.Session)1 SystemSessionProperties.isSkipRedundantSort (com.facebook.presto.SystemSessionProperties.isSkipRedundantSort)1 SortOrder (com.facebook.presto.common.block.SortOrder)1 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 Type (com.facebook.presto.common.type.Type)1 VARBINARY (com.facebook.presto.common.type.VarbinaryType.VARBINARY)1 Metadata (com.facebook.presto.metadata.Metadata)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 TableHandle (com.facebook.presto.spi.TableHandle)1 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)1 AggregationNode.groupingSets (com.facebook.presto.spi.plan.AggregationNode.groupingSets)1 AggregationNode.singleGroupingSet (com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 FilterNode (com.facebook.presto.spi.plan.FilterNode)1