private boolean hasAggregates(QuerySpecification node) { ImmutableList.Builder<Node> toExtractBuilder = ImmutableList.builder(); toExtractBuilder.addAll(node.getSelect().getSelectItems().stream() .filter(SingleColumn.class::isInstance) .collect(toImmutableList())); toExtractBuilder.addAll(getSortItemsFromOrderBy(node.getOrderBy())); node.getHaving().ifPresent(toExtractBuilder::add); List<FunctionCall> aggregates = extractAggregateFunctions(toExtractBuilder.build(), metadata.getFunctionRegistry()); return !aggregates.isEmpty(); }
private List<Expression> analyzeOrderBy(QuerySpecification node, Scope orderByScope, List<Expression> outputExpressions) { checkState(node.getOrderBy().isPresent(), "orderBy is absent"); List<SortItem> sortItems = getSortItemsFromOrderBy(node.getOrderBy()); if (node.getSelect().isDistinct()) { verifySelectDistinct(node, outputExpressions); } return analyzeOrderBy(node, sortItems, orderByScope); }
private PlanBuilder distinct(PlanBuilder subPlan, QuerySpecification node) { if (node.getSelect().isDistinct()) { return subPlan.withNewRoot( new AggregationNode( idAllocator.getNextId(), subPlan.getRoot(), ImmutableMap.of(), singleGroupingSet(subPlan.getRoot().getOutputSymbols()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty())); } return subPlan; }
private void verifySelectDistinct(QuerySpecification node, List<Expression> outputExpressions) { for (SortItem item : node.getOrderBy().get().getSortItems()) { Expression expression = item.getSortKey(); if (expression instanceof LongLiteral) { continue; } Expression rewrittenOrderByExpression = ExpressionTreeRewriter.rewriteWith(new OrderByExpressionRewriter(extractNamedOutputExpressions(node.getSelect())), expression); int index = outputExpressions.indexOf(rewrittenOrderByExpression); if (index == -1) { throw new SemanticException(ORDER_BY_MUST_BE_IN_SELECT, node.getSelect(), "For SELECT DISTINCT, ORDER BY expressions must appear in select list"); } if (!isDeterministic(expression)) { throw new SemanticException(NONDETERMINISTIC_ORDER_BY_EXPRESSION_WITH_SELECT_DISTINCT, expression, "Non deterministic ORDER BY expression is not supported with SELECT DISTINCT"); } } }
@Override protected Void visitQuerySpecification(QuerySpecification node, Integer indent) process(node.getSelect(), indent);
QuerySpecification querySpecification = (QuerySpecification) innerQuery; innerQuery = new QuerySpecification( querySpecification.getSelect(), querySpecification.getFrom(), querySpecification.getWhere(),
@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; }
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"); }
for (SelectItem item : node.getSelect().getSelectItems()) { if (item instanceof AllColumns) { if (node.getSelect().isDistinct() && !type.isComparable()) { throw new SemanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s)", type); if (node.getSelect().isDistinct() && !type.isComparable()) { throw new SemanticException(TYPE_MISMATCH, node.getSelect(), "DISTINCT can only be applied to comparable types (actual: %s): %s", type, column.getExpression());
for (SelectItem item : node.getSelect().getSelectItems()) { if (item instanceof AllColumns) {
process(node.getSelect(), indentLevel);
new QuerySpecification( getLocation(context), query.getSelect(), query.getFrom(), query.getWhere(),
private ResponseQuery parseQuerySpecification(QuerySpecification queryBody, Optional<String> limitOutside, List<SortItem> orderByOutside, Map<String, NodeLocation> with) { Function<Node, Integer> mapper = item -> { if (item instanceof GroupingElement) { return findSelectIndex(((GroupingElement) item).enumerateGroupingSets(), queryBody.getSelect().getSelectItems()).orElse(null); } else if (item instanceof LongLiteral) { return Ints.checkedCast(((LongLiteral) item).getValue());
@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; }
private PlanBuilder distinct(PlanBuilder subPlan, QuerySpecification node, List<FieldOrExpression> outputs, List<FieldOrExpression> orderBy) { if (node.getSelect().isDistinct()) { checkState(outputs.containsAll(orderBy), "Expected ORDER BY terms to be in SELECT. Broken analysis"); AggregationNode aggregation = new AggregationNode(idAllocator.getNextId(), subPlan.getRoot(), subPlan.getRoot().getOutputSymbols(), ImmutableMap.<Symbol, FunctionCall>of(), ImmutableMap.<Symbol, Signature>of(), ImmutableMap.<Symbol, Symbol>of(), AggregationNode.Step.SINGLE, Optional.empty(), 1.0, Optional.empty()); return new PlanBuilder(subPlan.getTranslations(), aggregation, subPlan.getSampleWeight()); } return subPlan; }
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()); } }
@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; }
@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; }
private List<FunctionCall> extractAggregates(QuerySpecification node) { AggregateExtractor extractor = new AggregateExtractor(metadata); for (SelectItem item : node.getSelect().getSelectItems()) { if (item instanceof SingleColumn) { extractor.process(((SingleColumn) item).getExpression(), null); } } for (SortItem item : node.getOrderBy()) { extractor.process(item.getSortKey(), null); } if (node.getHaving().isPresent()) { extractor.process(node.getHaving().get(), null); } List<FunctionCall> aggregates = extractor.getAggregates(); analysis.setAggregates(node, aggregates); return aggregates; }