Search in sources :

Example 1 with RelationId

use of com.facebook.presto.sql.analyzer.RelationId 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)1 ExpressionTreeUtils.checkAndGetColumnReferenceField (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.checkAndGetColumnReferenceField)1 ExpressionTreeUtils.createSymbolReference (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference)1 FieldId (com.facebook.presto.sql.analyzer.FieldId)1 RelationId (com.facebook.presto.sql.analyzer.RelationId)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 ADD (com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD)1 ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)1 Expression (com.facebook.presto.sql.tree.Expression)1 GenericLiteral (com.facebook.presto.sql.tree.GenericLiteral)1 GroupingOperation (com.facebook.presto.sql.tree.GroupingOperation)1 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)1 NodeRef (com.facebook.presto.sql.tree.NodeRef)1 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Multimap (com.google.common.collect.Multimap)1 Collection (java.util.Collection)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1