Search in sources :

Example 6 with GenericLiteral

use of com.facebook.presto.sql.tree.GenericLiteral 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

GenericLiteral (com.facebook.presto.sql.tree.GenericLiteral)6 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)4 Expression (com.facebook.presto.sql.tree.Expression)4 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)4 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)4 BooleanLiteral (com.facebook.presto.sql.tree.BooleanLiteral)3 NullLiteral (com.facebook.presto.sql.tree.NullLiteral)3 Slice (io.airlift.slice.Slice)3 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ExpressionTreeUtils.createSymbolReference (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference)2 ArithmeticUnaryExpression (com.facebook.presto.sql.tree.ArithmeticUnaryExpression)2 Cast (com.facebook.presto.sql.tree.Cast)2 DecimalLiteral (com.facebook.presto.sql.tree.DecimalLiteral)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 Session (com.facebook.presto.Session)1 Block (com.facebook.presto.common.block.Block)1 CharType (com.facebook.presto.common.type.CharType)1 DecimalType (com.facebook.presto.common.type.DecimalType)1 EnumType (com.facebook.presto.common.type.EnumType)1 SqlDate (com.facebook.presto.common.type.SqlDate)1