private PlanBuilder planQueryBody(Query query) { RelationPlan relationPlan = new RelationPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, metadata, session) .process(query.getQueryBody(), null); return planBuilderFor(relationPlan); }
@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; }
@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"); } }
processRelation(node.getQueryBody(), indent);
QueryBody innerQuery = createSelectClause.getQueryBody(); com.facebook.presto.sql.tree.Query zeroRowsQuery; if (innerQuery instanceof QuerySpecification) {
@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()); }
@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; }
limit = Integer.parseInt(statement.getLimit().get()); if (statement.getQueryBody() instanceof QuerySpecification && ((QuerySpecification) statement.getQueryBody()).getLimit().isPresent()) { limit = Integer.parseInt(((QuerySpecification) statement.getQueryBody()).getLimit().get());
}).orElse(null); if (statement.getQueryBody() instanceof QuerySpecification) { return parseQuerySpecification((QuerySpecification) statement.getQueryBody(), statement.getLimit(), statement.getOrderBy().map(v -> v.getSortItems()).orElse(null), map); } else if (statement.getQueryBody() instanceof Union) { Relation relation = ((Union) statement.getQueryBody()).getRelations().get(0); while (relation instanceof Union) { relation = ((Union) relation).getRelations().get(0); statement.getQueryBody().getLocation().orElse(null), ImmutableList.of(), ImmutableList.of(), statement.getLimit().map(l -> Long.parseLong(l)).orElse(null));
processRelation(node.getQueryBody(), indent);
QueryBody queryBody = statement.getQueryBody(); if (queryBody instanceof QuerySpecification) { QuerySpecification node = (QuerySpecification) queryBody;
/** * Parses and executes the provided insert statement and returns 1 if execution was successful * @param sql * @param insert * @param index * @return the number of executed inserts * @throws SQLException */ public int execute(String sql, Insert insert, String index) throws SQLException{ if(insert.getQuery().getQueryBody() instanceof Values){ // parse one or multiple value sets (... VALUES (1,2,'a'), (2,4,'b'), ...) return this.insertFromValues(sql, insert, index, Utils.getIntProp(props, Utils.PROP_FETCH_SIZE, 2500)); }else if(insert.getQuery().getQueryBody() instanceof QuerySpecification){ // insert data based on a SELECT statement return this.insertFromSelect(sql, insert, index, Utils.getIntProp(props, Utils.PROP_FETCH_SIZE, 2500)); }else throw new SQLException("Unknown set of values to insert ("+insert.getQuery().getQueryBody()+")"); }
/** * Builds the request defined within the explain statement and returns its string representation * @param sql * @param explain * @param indexes * @return * @throws SQLException */ public String explain(String sql, Explain explain, String... indexes) throws SQLException { com.facebook.presto.sql.tree.Statement explanSt = explain.getStatement(); if(!(explanSt instanceof Query)) throw new SQLException("Can only EXPLAIN SELECT ... statements"); this.buildRequest(sql, ((Query)explanSt).getQueryBody(), indexes); return this.request.toString(); }
@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; }
@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 R visitQuery(Query node, C context) { if (node.getWith().isPresent()) { process(node.getWith().get(), context); } process(node.getQueryBody(), context); for (SortItem sortItem : node.getOrderBy()) { process(sortItem, context); } return null; }
private PlanBuilder planQueryBody(Query query) { RelationPlan relationPlan = new RelationPlanner(analysis, symbolAllocator, idAllocator, metadata, session) .process(query.getQueryBody(), null); TranslationMap translations = new TranslationMap(relationPlan, analysis); // Make field->symbol mapping from underlying relation plan available for translations // This makes it possible to rewrite FieldOrExpressions that reference fields from the QuerySpecification directly translations.setFieldMappings(relationPlan.getOutputSymbols()); return new PlanBuilder(translations, relationPlan.getRoot(), relationPlan.getSampleWeight()); }
@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()); }
@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(), body.getApproximate()); }