@Override protected Void visitQuery(Query node, Integer indent) if (node.getWith().isPresent()) { With with = node.getWith().get(); append(indent, "WITH"); if (with.isRecursive()) { processRelation(node.getQueryBody(), indent); if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), indent); if (node.getLimit().isPresent()) { append(indent, "LIMIT " + node.getLimit().get()) .append('\n');
@Override public Node visitQuery(SqlBaseParser.QueryContext context) { Query body = (Query) visit(context.queryNoWith()); return new Query( getLocation(context), visitIfPresent(context.with(), With.class), body.getQueryBody(), body.getOrderBy(), body.getLimit()); }
private PlanBuilder limit(PlanBuilder subPlan, Query node) { return limit(subPlan, node.getOrderBy(), node.getLimit()); }
@Override protected R visitQuery(Query node, C context) { if (node.getWith().isPresent()) { process(node.getWith().get(), context); } process(node.getQueryBody(), context); if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), context); } return null; }
@Override protected Void visitQuery(Query node, Integer indentLevel) { print(indentLevel, "Query "); indentLevel++; print(indentLevel, "QueryBody"); process(node.getQueryBody(), indentLevel); if (node.getOrderBy().isPresent()) { print(indentLevel, "OrderBy"); process(node.getOrderBy().get(), indentLevel + 1); } if (node.getLimit().isPresent()) { print(indentLevel, "Limit: " + node.getLimit().get()); } return null; }
private PlanBuilder planQueryBody(Query query) { RelationPlan relationPlan = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, metadata, session) .process(query.getQueryBody(), null); return planBuilderFor(relationPlan); }
public static Query query(QueryBody body) { return new Query( Optional.empty(), body, Optional.empty(), Optional.empty()); } }
QueryBody innerQuery = createSelectClause.getQueryBody(); com.facebook.presto.sql.tree.Query zeroRowsQuery; if (innerQuery instanceof QuerySpecification) { Optional.of("0")); zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.empty()); zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.of("0"));
@Override protected Node visitShowStats(ShowStats node, Void context) { checkState(queryExplainer.isPresent(), "Query explainer must be provided for SHOW STATS SELECT"); if (node.getRelation() instanceof TableSubquery) { Query query = ((TableSubquery) node.getRelation()).getQuery(); QuerySpecification specification = (QuerySpecification) query.getQueryBody(); Plan plan = queryExplainer.get().getLogicalPlan(session, new Query(Optional.empty(), specification, Optional.empty(), Optional.empty()), parameters, warningCollector); validateShowStatsSubquery(node, query, specification, plan); Table table = (Table) specification.getFrom().get(); Constraint<ColumnHandle> constraint = getConstraint(plan); return rewriteShowStats(node, table, constraint); } else if (node.getRelation() instanceof Table) { Table table = (Table) node.getRelation(); return rewriteShowStats(node, table, Constraint.alwaysTrue()); } else { throw new IllegalArgumentException("Expected either TableSubquery or Table as relation"); } }
@Override protected Scope visitQuery(Query node, Optional<Scope> scope) { Scope withScope = analyzeWith(node, scope); Scope queryBodyScope = process(node.getQueryBody(), withScope); if (node.getOrderBy().isPresent()) { analyzeOrderBy(node, queryBodyScope); } else { analysis.setOrderByExpressions(node, emptyList()); } // Input fields == Output fields analysis.setOutputExpressions(node, descriptorToFields(queryBodyScope)); Scope queryScope = Scope.builder() .withParent(withScope) .withRelationType(RelationId.of(node), queryBodyScope.getRelationType()) .build(); analysis.setScope(node, queryScope); return queryScope; }
if (statement.getLimit().isPresent()) { limit = Integer.parseInt(statement.getLimit().get()); if (statement.getQueryBody() instanceof QuerySpecification && ((QuerySpecification) statement.getQueryBody()).getLimit().isPresent()) { limit = Integer.parseInt(((QuerySpecification) statement.getQueryBody()).getLimit().get());
@Override protected RelationType visitQuery(Query node, AnalysisContext parentContext) { AnalysisContext context = new AnalysisContext(parentContext); if (node.getApproximate().isPresent()) { if (!experimentalSyntaxEnabled) { throw new SemanticException(NOT_SUPPORTED, node, "approximate queries are not enabled"); } context.setApproximate(true); } analyzeWith(node, context); RelationType descriptor = process(node.getQueryBody(), context); analyzeOrderBy(node, descriptor, context); // Input fields == Output fields analysis.setOutputDescriptor(node, descriptor); analysis.setOutputExpressions(node, descriptorToFields(descriptor)); analysis.setStatement(node); return descriptor; }
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"); }
private void analyzeOrderBy(Query node, Scope orderByScope) { checkState(node.getOrderBy().isPresent(), "orderBy is absent"); List<SortItem> sortItems = getSortItemsFromOrderBy(node.getOrderBy()); analyzeOrderBy(node, sortItems, orderByScope); }
public void validateQuery() { Statement query; synchronized (SQL_PARSER) { query = SQL_PARSER.createStatement(this.query, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE)); } checkState(query instanceof Query, "Expression is not query"); checkState((!((Query) query).getLimit().isPresent()), "The query of materialized view can't contain LIMIT statement"); // QueryBody queryBody = ((Query) query).getQueryBody(); // if (queryBody instanceof QuerySpecification) { // List<SelectItem> selectItems = ((QuerySpecification) queryBody).getSelect().getSelectItems(); // if (selectItems.stream().anyMatch(e -> e instanceof AllColumns)) { // throw new RakamException("Wildcard in select items is not supported in materialized views.", BAD_REQUEST); // } // // for (SelectItem selectItem : selectItems) { // SingleColumn selectColumn = (SingleColumn) selectItem; // if (!selectColumn.getAlias().isPresent() && !(selectColumn.getExpression() instanceof Identifier) // && !(selectColumn.getExpression() instanceof DereferenceExpression)) { // throw new RakamException(format("Column '%s' must have alias", selectColumn.getExpression().toString()), BAD_REQUEST); // } // } // } }
@Override protected RelationPlan visitQuery(Query node, Void context) { Optional<Double> approximationConfidence = node.getApproximate().map(Approximate::getConfidence).map(confidence -> Double.valueOf(confidence) / 100.0); PlanBuilder subPlan = new QueryPlanner(analysis, symbolAllocator, idAllocator, metadata, session, approximationConfidence).process(node, null); ImmutableList.Builder<Symbol> outputSymbols = ImmutableList.builder(); for (FieldOrExpression fieldOrExpression : analysis.getOutputExpressions(node)) { outputSymbols.add(subPlan.translate(fieldOrExpression)); } return new RelationPlan(subPlan.getRoot(), analysis.getOutputDescriptor(node), outputSymbols.build(), subPlan.getSampleWeight()); }
@Override protected R visitQuery(Query node, C context) { if (node.getWith().isPresent()) { process(node.getWith().get(), context); } process(node.getQueryBody(), context); if (node.getOrderBy().isPresent()) { process(node.getOrderBy().get(), context); } return null; }
@Override protected Void visitQuery(Query node, Integer indentLevel) { print(indentLevel, "Query "); indentLevel++; print(indentLevel, "QueryBody"); process(node.getQueryBody(), indentLevel); if (node.getOrderBy().isPresent()) { print(indentLevel, "OrderBy"); process(node.getOrderBy().get(), indentLevel + 1); } if (node.getLimit().isPresent()) { print(indentLevel, "Limit: " + node.getLimit().get()); } return null; }
QueryBody queryBody = statement.getQueryBody(); if (queryBody instanceof QuerySpecification) { QuerySpecification node = (QuerySpecification) queryBody;
@Override public Node visitShowStatsForQuery(SqlBaseParser.ShowStatsForQueryContext context) { QuerySpecification specification = (QuerySpecification) visitQuerySpecification(context.querySpecification()); Query query = new Query(Optional.empty(), specification, Optional.empty(), Optional.empty()); return new ShowStats(Optional.of(getLocation(context)), new TableSubquery(query)); }