public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, Optional<GroupBy> groupBy, Optional<Expression> having, Optional<OrderBy> orderBy, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, orderBy, limit)); }
@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 PlanBuilder limit(PlanBuilder subPlan, QuerySpecification node) { return limit(subPlan, node.getOrderBy(), node.getLimit()); }
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(); }
node.getWhere().ifPresent(where -> analyzeWhere(node, sourceScope, where)); if (node.getOrderBy().isPresent()) { orderByScope = Optional.of(computeAndAssignOrderByScope(node.getOrderBy().get(), sourceScope, outputScope)); orderByExpressions = analyzeOrderBy(node, orderByScope.get(), outputExpressions); node.getHaving().ifPresent(sourceExpressions::add); analyzeWindowFunctions(node, outputExpressions, orderByExpressions); if (analysis.isAggregation(node) && node.getOrderBy().isPresent()) { computeAndAssignOrderByScopeWithAggregation(node.getOrderBy().get(), sourceScope, outputScope, orderByAggregations, groupByExpressions, orderByGroupingOperations);
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); }
for (SelectItem item : node.getSelect().getSelectItems()) { if (item instanceof AllColumns) { throw new SemanticException(MISSING_TABLE, item, "Table '%s' not found", starPrefix.get()); if (!node.getFrom().isPresent()) { throw new SemanticException(WILDCARD_WITHOUT_FROM, item, "SELECT * not allowed in queries without FROM clause"); 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());
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); }
private void analyzeWindowFunctions(QuerySpecification node, List<Expression> outputExpressions, List<Expression> orderByExpressions) { analysis.setWindowFunctions(node, analyzeWindowFunctions(node, outputExpressions)); if (node.getOrderBy().isPresent()) { analysis.setOrderByWindowFunctions(node.getOrderBy().get(), analyzeWindowFunctions(node, orderByExpressions)); } }
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 analyzeHaving(QuerySpecification node, Scope scope) { if (node.getHaving().isPresent()) { Expression predicate = node.getHaving().get(); ExpressionAnalysis expressionAnalysis = analyzeExpression(predicate, scope); expressionAnalysis.getWindowFunctions().stream() .findFirst() .ifPresent(function -> { throw new SemanticException(NESTED_WINDOW, function.getNode(), "HAVING clause cannot contain window functions"); }); analysis.recordSubqueries(node, expressionAnalysis); Type predicateType = expressionAnalysis.getType(predicate); if (!predicateType.equals(BOOLEAN) && !predicateType.equals(UNKNOWN)) { throw new SemanticException(TYPE_MISMATCH, predicate, "HAVING clause must evaluate to a boolean: actual type %s", predicateType); } analysis.setHaving(node, predicate); } }
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(); }
node.getWhere().ifPresent(where -> analyzeWhere(node, sourceScope, where)); if (node.getOrderBy().isPresent()) { orderByScope = Optional.of(computeAndAssignOrderByScope(node.getOrderBy().get(), sourceScope, outputScope)); orderByExpressions = analyzeOrderBy(node, orderByScope.get(), outputExpressions); node.getHaving().ifPresent(sourceExpressions::add); analyzeWindowFunctions(node, outputExpressions, orderByExpressions); if (analysis.isAggregation(node) && node.getOrderBy().isPresent()) { computeAndAssignOrderByScopeWithAggregation(node.getOrderBy().get(), sourceScope, outputScope, orderByAggregations, groupByExpressions, orderByGroupingOperations);
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); }
for (SelectItem item : node.getSelect().getSelectItems()) { if (item instanceof AllColumns) { throw new SemanticException(MISSING_TABLE, item, "Table '%s' not found", starPrefix.get()); if (!node.getFrom().isPresent()) { throw new SemanticException(WILDCARD_WITHOUT_FROM, item, "SELECT * not allowed in queries without FROM clause"); 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());
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); }
private void analyzeWindowFunctions(QuerySpecification node, List<Expression> outputExpressions, List<Expression> orderByExpressions) { analysis.setWindowFunctions(node, analyzeWindowFunctions(node, outputExpressions)); if (node.getOrderBy().isPresent()) { analysis.setOrderByWindowFunctions(node.getOrderBy().get(), analyzeWindowFunctions(node, orderByExpressions)); } }
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; }