private void analyzeGroupingOperations(QuerySpecification node, List<Expression> outputExpressions, List<Expression> orderByExpressions) { List<GroupingOperation> groupingOperations = extractExpressions(Iterables.concat(outputExpressions, orderByExpressions), GroupingOperation.class); boolean isGroupingOperationPresent = !groupingOperations.isEmpty(); if (isGroupingOperationPresent && !node.getGroupBy().isPresent()) { throw new SemanticException( INVALID_PROCEDURE_ARGUMENTS, node, "A GROUPING() operation can only be used with a corresponding GROUPING SET/CUBE/ROLLUP/GROUP BY clause"); } analysis.setGroupingOperations(node, groupingOperations); }
if (node.getGroupBy().isPresent()) { append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n');
@Override protected R visitQuerySpecification(QuerySpecification node, C context) { process(node.getSelect(), context); if (node.getFrom().isPresent()) { process(node.getFrom().get(), context); } if (node.getWhere().isPresent()) { process(node.getWhere().get(), context); } if (node.getGroupBy().isPresent()) { process(node.getGroupBy().get(), context); } if (node.getHaving().isPresent()) { process(node.getHaving().get(), context); } if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), context); } return null; }
querySpecification.getFrom(), querySpecification.getWhere(), querySpecification.getGroupBy(), querySpecification.getHaving(), querySpecification.getOrderBy(),
private List<Expression> analyzeGroupBy(QuerySpecification node, Scope scope, List<Expression> outputExpressions) if (node.getGroupBy().isPresent()) { ImmutableList.Builder<Set<FieldId>> cubes = ImmutableList.builder(); ImmutableList.Builder<List<FieldId>> rollups = ImmutableList.builder(); ImmutableList.Builder<Expression> groupingExpressions = ImmutableList.builder(); checkGroupingSetsCount(node.getGroupBy().get()); for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) { if (groupingElement instanceof SimpleGroupBy) { for (Expression column : groupingElement.getExpressions()) {
List<List<Symbol>> groupingSets = ImmutableList.of(ImmutableList.of()); if (node.getGroupBy().isPresent()) { columnOnlyGroupingSets = enumerateGroupingSets(groupingSetAnalysis); if (node.getGroupBy().get().isDistinct()) { columnOnlyGroupingSets = columnOnlyGroupingSets.stream() .distinct()
if (node.getGroupBy().isPresent()) { String distinct = ""; if (node.getGroupBy().get().isDistinct()) { distinct = "[DISTINCT]"; for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) { print(indentLevel, "SimpleGroupBy"); if (groupingElement instanceof SimpleGroupBy) {
private void validateShowStatsSubquery(ShowStats node, Query query, QuerySpecification querySpecification, Plan plan) { // The following properties of SELECT subquery are required: // - only one relation in FROM // - only predicates that can be pushed down can be in the where clause // - no group by // - no having // - no set quantifier Optional<FilterNode> filterNode = searchFrom(plan.getRoot()) .where(FilterNode.class::isInstance) .findSingle(); check(!filterNode.isPresent(), node, "Only predicates that can be pushed down are supported in the SHOW STATS WHERE clause"); check(querySpecification.getFrom().isPresent(), node, "There must be exactly one table in query passed to SHOW STATS SELECT clause"); check(querySpecification.getFrom().get() instanceof Table, node, "There must be exactly one table in query passed to SHOW STATS SELECT clause"); check(!query.getWith().isPresent(), node, "WITH is not supported by SHOW STATS SELECT clause"); check(!querySpecification.getOrderBy().isPresent(), node, "ORDER BY is not supported in SHOW STATS SELECT clause"); check(!querySpecification.getLimit().isPresent(), node, "LIMIT is not supported by SHOW STATS SELECT clause"); check(!querySpecification.getHaving().isPresent(), node, "HAVING is not supported in SHOW STATS SELECT clause"); check(!querySpecification.getGroupBy().isPresent(), node, "GROUP BY is not supported in SHOW STATS SELECT clause"); check(!querySpecification.getSelect().isDistinct(), node, "DISTINCT is not supported by SHOW STATS SELECT clause"); List<SelectItem> selectItems = querySpecification.getSelect().getSelectItems(); check(selectItems.size() == 1 && selectItems.get(0) instanceof AllColumns, node, "Only SELECT * is supported in SHOW STATS SELECT clause"); }
query.getFrom(), query.getWhere(), query.getGroupBy(), query.getHaving(), orderBy,
@Override protected Void visitQuerySpecification(QuerySpecification node, Integer indent) { process(node.getSelect(), indent); if (node.getFrom().isPresent()) { append(indent, "FROM"); builder.append('\n'); append(indent, " "); process(node.getFrom().get(), indent); } builder.append('\n'); if (node.getWhere().isPresent()) { append(indent, "WHERE " + formatExpression(node.getWhere().get(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)) .append('\n'); } if (node.getGroupBy().isPresent()) { append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n'); } if (node.getHaving().isPresent()) { append(indent, "HAVING " + formatExpression(node.getHaving().get(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)) .append('\n'); } if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), indent); } if (node.getLimit().isPresent()) { append(indent, "LIMIT " + node.getLimit().get()) .append('\n'); } return null; }
List<GroupBy> groupBy = queryBody.getGroupBy().map(value -> value.getGroupingElements().stream() .map(item -> new GroupBy(mapper.apply(item), item.enumerateGroupingSets().toString())) .collect(Collectors.toList())).orElse(ImmutableList.of());
if (queryBody instanceof QuerySpecification) { QuerySpecification node = (QuerySpecification) queryBody; node.getGroupBy().ifPresent(groupBy -> { for (GroupingElement groupingElement : groupBy.getGroupingElements()) { for (Set<Expression> expressions : groupingElement.enumerateGroupingSets()) {
private List<List<FieldOrExpression>> analyzeGroupBy(QuerySpecification node, RelationType tupleDescriptor, AnalysisContext context, List<FieldOrExpression> outputExpressions) { List<Set<Expression>> computedGroupingSets = ImmutableList.of(); // empty list = no aggregations if (node.getGroupBy().isPresent()) { List<List<Set<Expression>>> enumeratedGroupingSets = node.getGroupBy().get().getGroupingElements().stream() .map(GroupingElement::enumerateGroupingSets) .collect(toImmutableList()); // compute cross product of enumerated grouping sets, if there are any computedGroupingSets = computeGroupingSetsCrossProduct(enumeratedGroupingSets, node.getGroupBy().get().isDistinct()); checkState(!computedGroupingSets.isEmpty(), "computed grouping sets cannot be empty"); } else if (!extractAggregates(node).isEmpty()) { // if there are aggregates, but no group by, create a grand total grouping set (global aggregation) computedGroupingSets = ImmutableList.of(ImmutableSet.of()); } List<List<FieldOrExpression>> analyzedGroupingSets = computedGroupingSets.stream() .map(groupingSet -> analyzeGroupingColumns(groupingSet, node, tupleDescriptor, context, outputExpressions)) .collect(toImmutableList()); analysis.setGroupingSets(node, analyzedGroupingSets); return analyzedGroupingSets; }
public class PrestoParserTest { public static void main(String[] args) { SqlParser parser = new SqlParser(); String sql = "select * from xyz where x=y group by x order by y limit 10"; Query query = (Query)parser.createStatement(sql); QuerySpecification body = (QuerySpecification)query.getQueryBody(); Select select = body.getSelect(); System.out.println("Columns = " + select.getSelectItems()); System.out.println("From = " + body.getFrom().get()); Optional<Expression> where = body.getWhere(); System.out.println("Where = " + where.get()); System.out.println("Group by = " + body.getGroupBy()); System.out.println("Order by = " + body.getOrderBy()); System.out.println("Limit = " + body.getLimit().get()); } }
if (node.getGroupBy().isPresent()) { append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n');
@Override protected R visitQuerySpecification(QuerySpecification node, C context) { process(node.getSelect(), context); if (node.getFrom().isPresent()) { process(node.getFrom().get(), context); } if (node.getWhere().isPresent()) { process(node.getWhere().get(), context); } if (node.getGroupBy().isPresent()) { process(node.getGroupBy().get(), context); } if (node.getHaving().isPresent()) { process(node.getHaving().get(), context); } if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), context); } return null; }
if (node.getGroupBy().isPresent()) { append(indent, "GROUP BY " + (node.getGroupBy().get().isDistinct() ? " DISTINCT " : "") + formatGroupBy(node.getGroupBy().get().getGroupingElements())).append('\n');
if (!node.getGroupBy().isEmpty()) { append(indent, "GROUP BY " + formatGroupBy(node.getGroupBy())).append('\n');
@Override protected R visitQuerySpecification(QuerySpecification node, C context) { process(node.getSelect(), context); if (node.getFrom().isPresent()) { process(node.getFrom().get(), context); } if (node.getWhere().isPresent()) { process(node.getWhere().get(), context); } if (node.getGroupBy().isPresent()) { for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) { process(groupingElement, context); } } if (node.getHaving().isPresent()) { process(node.getHaving().get(), context); } for (SortItem sortItem : node.getOrderBy()) { process(sortItem, context); } return null; }
query.getFrom(), query.getWhere(), query.getGroupBy(), query.getHaving(), orderBy,