Search in sources :

Example 1 with GroupingElement

use of io.prestosql.sql.tree.GroupingElement in project hetu-core by openlookeng.

the class ExpressionFormatter method formatGroupBy.

static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters) {
    ImmutableList.Builder<String> resultStrings = ImmutableList.builder();
    for (GroupingElement groupingElement : groupingElements) {
        String result = "";
        if (groupingElement instanceof SimpleGroupBy) {
            List<Expression> columns = groupingElement.getExpressions();
            if (columns.size() == 1) {
                result = formatExpression(getOnlyElement(columns), parameters);
            } else {
                result = formatGroupingSet(columns, parameters);
            }
        } else if (groupingElement instanceof GroupingSets) {
            result = format("GROUPING SETS (%s)", Joiner.on(", ").join(((GroupingSets) groupingElement).getSets().stream().map(e -> formatGroupingSet(e, parameters)).iterator()));
        } else if (groupingElement instanceof Cube) {
            result = format("CUBE %s", formatGroupingSet(groupingElement.getExpressions(), parameters));
        } else if (groupingElement instanceof Rollup) {
            result = format("ROLLUP %s", formatGroupingSet(groupingElement.getExpressions(), parameters));
        }
        resultStrings.add(result);
    }
    return Joiner.on(", ").join(resultStrings.build());
}
Also used : GroupingSets(io.prestosql.sql.tree.GroupingSets) Parameter(io.prestosql.sql.tree.Parameter) DecimalFormatSymbols(java.text.DecimalFormatSymbols) QualifiedName(io.prestosql.sql.tree.QualifiedName) FieldReference(io.prestosql.sql.tree.FieldReference) Cast(io.prestosql.sql.tree.Cast) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Rollup(io.prestosql.sql.tree.Rollup) WindowFrame(io.prestosql.sql.tree.WindowFrame) Locale(java.util.Locale) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) ArrayConstructor(io.prestosql.sql.tree.ArrayConstructor) Window(io.prestosql.sql.tree.Window) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) Identifier(io.prestosql.sql.tree.Identifier) Cube(io.prestosql.sql.tree.Cube) WhenClause(io.prestosql.sql.tree.WhenClause) BindExpression(io.prestosql.sql.tree.BindExpression) Row(io.prestosql.sql.tree.Row) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) GroupingOperation(io.prestosql.sql.tree.GroupingOperation) IsNotNullPredicate(io.prestosql.sql.tree.IsNotNullPredicate) GroupingElement(io.prestosql.sql.tree.GroupingElement) NullLiteral(io.prestosql.sql.tree.NullLiteral) TimeLiteral(io.prestosql.sql.tree.TimeLiteral) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) List(java.util.List) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) CharLiteral(io.prestosql.sql.tree.CharLiteral) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) SymbolReference(io.prestosql.sql.tree.SymbolReference) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) StringLiteral(io.prestosql.sql.tree.StringLiteral) AtTimeZone(io.prestosql.sql.tree.AtTimeZone) Optional(java.util.Optional) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) FrameBound(io.prestosql.sql.tree.FrameBound) IfExpression(io.prestosql.sql.tree.IfExpression) GroupingSets(io.prestosql.sql.tree.GroupingSets) Joiner(com.google.common.base.Joiner) InPredicate(io.prestosql.sql.tree.InPredicate) Extract(io.prestosql.sql.tree.Extract) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) CurrentPath(io.prestosql.sql.tree.CurrentPath) AllColumns(io.prestosql.sql.tree.AllColumns) DecimalLiteral(io.prestosql.sql.tree.DecimalLiteral) Node(io.prestosql.sql.tree.Node) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) IntervalLiteral(io.prestosql.sql.tree.IntervalLiteral) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) LikePredicate(io.prestosql.sql.tree.LikePredicate) AstVisitor(io.prestosql.sql.tree.AstVisitor) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) SortItem(io.prestosql.sql.tree.SortItem) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) TimestampLiteral(io.prestosql.sql.tree.TimestampLiteral) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) CurrentTime(io.prestosql.sql.tree.CurrentTime) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) CharMatcher(com.google.common.base.CharMatcher) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) OrderBy(io.prestosql.sql.tree.OrderBy) DecimalFormat(java.text.DecimalFormat) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) CurrentUser(io.prestosql.sql.tree.CurrentUser) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) PrimitiveIterator(java.util.PrimitiveIterator) Collectors.toList(java.util.stream.Collectors.toList) ExistsPredicate(io.prestosql.sql.tree.ExistsPredicate) TryExpression(io.prestosql.sql.tree.TryExpression) BinaryLiteral(io.prestosql.sql.tree.BinaryLiteral) Format(io.prestosql.sql.tree.Format) Expression(io.prestosql.sql.tree.Expression) GroupingElement(io.prestosql.sql.tree.GroupingElement) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) Rollup(io.prestosql.sql.tree.Rollup) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) BindExpression(io.prestosql.sql.tree.BindExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) TryExpression(io.prestosql.sql.tree.TryExpression) Expression(io.prestosql.sql.tree.Expression) Cube(io.prestosql.sql.tree.Cube) ImmutableList(com.google.common.collect.ImmutableList)

Example 2 with GroupingElement

use of io.prestosql.sql.tree.GroupingElement in project hetu-core by openlookeng.

the class TreePrinter method print.

public void print(Node root) {
    AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<Void, Integer>() {

        @Override
        protected Void visitNode(Node node, Integer indentLevel) {
            throw new UnsupportedOperationException("not yet implemented: " + node);
        }

        @Override
        protected Void visitQuery(Query node, Integer indentLevel) {
            print(indentLevel, "Query ");
            Integer tmpIndentLevel = indentLevel;
            tmpIndentLevel++;
            print(tmpIndentLevel, "QueryBody");
            process(node.getQueryBody(), tmpIndentLevel);
            if (node.getOrderBy().isPresent()) {
                print(tmpIndentLevel, "OrderBy");
                process(node.getOrderBy().get(), tmpIndentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(tmpIndentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        @Override
        protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
            print(indentLevel, "QuerySpecification ");
            Integer tmpIndentLevel = indentLevel;
            tmpIndentLevel++;
            process(node.getSelect(), tmpIndentLevel);
            if (node.getFrom().isPresent()) {
                print(tmpIndentLevel, "From");
                process(node.getFrom().get(), tmpIndentLevel + 1);
            }
            if (node.getWhere().isPresent()) {
                print(tmpIndentLevel, "Where");
                process(node.getWhere().get(), tmpIndentLevel + 1);
            }
            if (node.getGroupBy().isPresent()) {
                String distinct = "";
                if (node.getGroupBy().get().isDistinct()) {
                    distinct = "[DISTINCT]";
                }
                print(tmpIndentLevel, "GroupBy" + distinct);
                for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
                    print(tmpIndentLevel, "SimpleGroupBy");
                    if (groupingElement instanceof SimpleGroupBy) {
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    } else if (groupingElement instanceof GroupingSets) {
                        print(tmpIndentLevel + 1, "GroupingSets");
                        for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
                            print(tmpIndentLevel + 2, "GroupingSet[");
                            for (Expression expression : set) {
                                process(expression, tmpIndentLevel + 3);
                            }
                            print(tmpIndentLevel + 2, "]");
                        }
                    } else if (groupingElement instanceof Cube) {
                        print(tmpIndentLevel + 1, "Cube");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    } else if (groupingElement instanceof Rollup) {
                        print(tmpIndentLevel + 1, "Rollup");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    }
                }
            }
            if (node.getHaving().isPresent()) {
                print(tmpIndentLevel, "Having");
                process(node.getHaving().get(), tmpIndentLevel + 1);
            }
            if (node.getOrderBy().isPresent()) {
                print(tmpIndentLevel, "OrderBy");
                process(node.getOrderBy().get(), tmpIndentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(tmpIndentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
            for (SortItem sortItem : node.getSortItems()) {
                process(sortItem, indentLevel);
            }
            return null;
        }

        @Override
        protected Void visitSelect(Select node, Integer indentLevel) {
            String distinct = "";
            if (node.isDistinct()) {
                distinct = "[DISTINCT]";
            }
            print(indentLevel, "Select" + distinct);
            // visit children
            super.visitSelect(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAllColumns(AllColumns node, Integer indent) {
            if (node.getPrefix().isPresent()) {
                print(indent, node.getPrefix() + ".*");
            } else {
                print(indent, "*");
            }
            return null;
        }

        @Override
        protected Void visitSingleColumn(SingleColumn node, Integer indent) {
            if (node.getAlias().isPresent()) {
                print(indent, "Alias: " + node.getAlias().get());
            }
            // visit children
            super.visitSingleColumn(node, indent + 1);
            return null;
        }

        @Override
        protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitComparisonExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitArithmeticBinary(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitLogicalBinaryExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) {
            print(indentLevel, "String[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) {
            print(indentLevel, "Binary[" + node.toHexString() + "]");
            return null;
        }

        @Override
        protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) {
            print(indentLevel, "Boolean[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) {
            print(indentLevel, "Long[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) {
            print(indentLevel, "LIKE");
            super.visitLikePredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitIdentifier(Identifier node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "FunctionCall[" + name + "]");
            super.visitFunctionCall(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTable(Table node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "Table[" + name + "]");
            return null;
        }

        @Override
        protected Void visitValues(Values node, Integer indentLevel) {
            print(indentLevel, "Values");
            super.visitValues(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitRow(Row node, Integer indentLevel) {
            print(indentLevel, "Row");
            super.visitRow(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) {
            print(indentLevel, "Alias[" + node.getAlias() + "]");
            super.visitAliasedRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) {
            print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]");
            super.visitSampledRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitTableSubquery(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitInPredicate(InPredicate node, Integer indentLevel) {
            print(indentLevel, "IN");
            super.visitInPredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitSubqueryExpression(node, indentLevel + 1);
            return null;
        }
    };
    printer.process(root, 0);
}
Also used : ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) Query(io.prestosql.sql.tree.Query) Rollup(io.prestosql.sql.tree.Rollup) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Node(io.prestosql.sql.tree.Node) Values(io.prestosql.sql.tree.Values) AllColumns(io.prestosql.sql.tree.AllColumns) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Identifier(io.prestosql.sql.tree.Identifier) List(java.util.List) FunctionCall(io.prestosql.sql.tree.FunctionCall) SampledRelation(io.prestosql.sql.tree.SampledRelation) GroupingSets(io.prestosql.sql.tree.GroupingSets) OrderBy(io.prestosql.sql.tree.OrderBy) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) Table(io.prestosql.sql.tree.Table) LongLiteral(io.prestosql.sql.tree.LongLiteral) QualifiedName(io.prestosql.sql.tree.QualifiedName) DefaultTraversalVisitor(io.prestosql.sql.tree.DefaultTraversalVisitor) SingleColumn(io.prestosql.sql.tree.SingleColumn) LikePredicate(io.prestosql.sql.tree.LikePredicate) TableSubquery(io.prestosql.sql.tree.TableSubquery) InPredicate(io.prestosql.sql.tree.InPredicate) GroupingElement(io.prestosql.sql.tree.GroupingElement) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) BinaryLiteral(io.prestosql.sql.tree.BinaryLiteral) StringLiteral(io.prestosql.sql.tree.StringLiteral) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) Expression(io.prestosql.sql.tree.Expression) Cube(io.prestosql.sql.tree.Cube) Select(io.prestosql.sql.tree.Select) Row(io.prestosql.sql.tree.Row) AliasedRelation(io.prestosql.sql.tree.AliasedRelation)

Aggregations

AllColumns (io.prestosql.sql.tree.AllColumns)2 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)2 BinaryLiteral (io.prestosql.sql.tree.BinaryLiteral)2 BooleanLiteral (io.prestosql.sql.tree.BooleanLiteral)2 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)2 Cube (io.prestosql.sql.tree.Cube)2 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)2 Expression (io.prestosql.sql.tree.Expression)2 FunctionCall (io.prestosql.sql.tree.FunctionCall)2 GroupingElement (io.prestosql.sql.tree.GroupingElement)2 GroupingSets (io.prestosql.sql.tree.GroupingSets)2 Identifier (io.prestosql.sql.tree.Identifier)2 InPredicate (io.prestosql.sql.tree.InPredicate)2 LikePredicate (io.prestosql.sql.tree.LikePredicate)2 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)2 LongLiteral (io.prestosql.sql.tree.LongLiteral)2 Node (io.prestosql.sql.tree.Node)2 OrderBy (io.prestosql.sql.tree.OrderBy)2 QualifiedName (io.prestosql.sql.tree.QualifiedName)2 Rollup (io.prestosql.sql.tree.Rollup)2