static String formatGroupBy(List<GroupingElement> groupingElements, Optional<List<Expression>> parameters, int indent) { ImmutableList.Builder<String> resultStrings = ImmutableList.builder(); for (GroupingElement groupingElement : groupingElements) { String result = ""; if (groupingElement instanceof SimpleGroupBy) { Set<Expression> columns = ImmutableSet.copyOf(((SimpleGroupBy) groupingElement).getColumnExpressions()); if (columns.size() == 1) { result = formatExpression(getOnlyElement(columns), parameters, indent); } else { result = formatGroupingSet(columns, parameters, indent); } } else if (groupingElement instanceof GroupingSets) { result = format("GROUPING SETS (%s)", Joiner.on(", ").join( ((GroupingSets) groupingElement).getSets().stream() .map(ExpressionFormatter::formatGroupingSet) .iterator())); } else if (groupingElement instanceof Cube) { result = format("CUBE %s", formatGroupingSet(((Cube) groupingElement).getColumns())); } else if (groupingElement instanceof Rollup) { result = format("ROLLUP %s", formatGroupingSet(((Rollup) groupingElement).getColumns())); } resultStrings.add(result); } return Joiner.on(", ").join(resultStrings.build()); }
static String formatGroupBy(List<GroupingElement> groupingElements) { ImmutableList.Builder<String> resultStrings = ImmutableList.builder(); for (GroupingElement groupingElement : groupingElements) { String result = ""; if (groupingElement instanceof SimpleGroupBy) { Set<Expression> columns = ImmutableSet.copyOf(((SimpleGroupBy) groupingElement).getColumnExpressions()); if (columns.size() == 1) { result = formatExpression(getOnlyElement(columns)); } else { result = formatGroupingSet(columns); } } else if (groupingElement instanceof GroupingSets) { result = format("GROUPING SETS (%s)", Joiner.on(", ").join( groupingElement.enumerateGroupingSets().stream() .map(ExpressionFormatter::formatGroupingSet) .iterator())); } else if (groupingElement instanceof Cube) { result = format("CUBE %s", formatGroupingSet(((Cube) groupingElement).getColumns())); } else if (groupingElement instanceof Rollup) { result = format("ROLLUP %s", formatGroupingSet(((Rollup) groupingElement).getColumns())); } resultStrings.add(result); } return Joiner.on(", ").join(resultStrings.build()); }
@Override public Node visitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext context) { return new SimpleGroupBy(getLocation(context), visit(context.groupingExpressions().expression(), Expression.class)); }