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()); }
public TermsBuilder parse(List<GroupingElement> elements, QueryState state){ List<Column> groups = new ArrayList<Column>(); for(GroupingElement grouping : elements){ for(Set<Expression> expressions : grouping.enumerateGroupingSets()){ for(Expression e : expressions) groups.add((Column) e.accept(this, state) ); } } // to find case sensitive group by definitions which ES needs for(Column groupby : groups){ if(groupby.getOp() != Operation.NONE){ state.addException("Can not use function '"+groupby.getAggName()+"' as GROUP BY, please use an alias to group by a function"); return null; } } Heading.fixColumnReferences(state.originalSql()+";", "group by.+", "\\W", groups); for(Column g : groups){ Column s = state.getHeading().getColumnByLabel(g.getAggName()); if(s == null ){ state.addException("Group by '"+g.getColumn()+"' not defined in SELECT"); }else{ // add column from select to this group (when referenced through an alias) g.setColumn(s.getColumn()); } } return buildAggregationQuery(groups, 0, state); }
private List<List<FieldOrExpression>> analyzeGroupBy(QuerySpecification node, RelationType tupleDescriptor, AnalysisContext context, List<FieldOrExpression> outputExpressions) { List<Set<Set<Expression>>> enumeratedGroupingSets = node.getGroupBy().stream() .map(GroupingElement::enumerateGroupingSets) .distinct() .collect(toImmutableList()); // compute cross product of enumerated grouping sets, if there are any List<List<Expression>> computedGroupingSets = ImmutableList.of(); if (!enumeratedGroupingSets.isEmpty()) { computedGroupingSets = Sets.cartesianProduct(enumeratedGroupingSets).stream() .map(groupingSetList -> groupingSetList.stream() .flatMap(Collection::stream) .distinct() .collect(toImmutableList())) .distinct() .collect(toImmutableList()); } // if there are aggregates, but no grouping columns, create a grand total grouping set if (computedGroupingSets.isEmpty() && !extractAggregates(node).isEmpty()) { computedGroupingSets = ImmutableList.of(ImmutableList.of()); } if (computedGroupingSets.size() > 1) { throw new SemanticException(NOT_SUPPORTED, node, "Grouping by multiple sets of columns is not yet supported"); } List<List<FieldOrExpression>> analyzedGroupingSets = computedGroupingSets.stream() .map(groupingSet -> analyzeGroupingColumns(groupingSet, node, tupleDescriptor, context, outputExpressions)) .collect(toImmutableList()); analysis.setGroupingSets(node, analyzedGroupingSets); return analyzedGroupingSets; }
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()); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, List<GroupingElement> groupBy, Optional<Expression> having, List<SortItem> ordering, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, ordering, limit)); }
static String formatGroupBy(List<GroupingElement> groupingElements, int indent) { return formatGroupBy(groupingElements, Optional.empty(), indent); }