private RelationPlan planImplicitTable() { List<Expression> emptyRow = ImmutableList.of(); Scope scope = Scope.create(); return new RelationPlan( new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of(emptyRow)), scope, ImmutableList.of()); }
return new RelationPlan(unnestNode, scope, unnestedSymbols);
private PlanBuilder planBuilderFor(PlanBuilder builder, Scope scope) { return planBuilderFor(new RelationPlan(builder.getRoot(), scope, builder.getRoot().getOutputSymbols())); }
@Override protected RelationPlan visitTable(Table node, Void context) { Query namedQuery = analysis.getNamedQuery(node); Scope scope = analysis.getScope(node); if (namedQuery != null) { RelationPlan subPlan = process(namedQuery, null); // Add implicit coercions if view query produces types that don't match the declared output types // of the view (e.g., if the underlying tables referenced by the view changed) Type[] types = scope.getRelationType().getAllFields().stream().map(Field::getType).toArray(Type[]::new); RelationPlan withCoercions = addCoercions(subPlan, types); return new RelationPlan(withCoercions.getRoot(), scope, withCoercions.getFieldMappings()); } TableHandle handle = analysis.getTableHandle(node); ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder(); ImmutableMap.Builder<Symbol, ColumnHandle> columns = ImmutableMap.builder(); for (Field field : scope.getRelationType().getAllFields()) { Symbol symbol = symbolAllocator.newSymbol(field.getName().get(), field.getType()); outputSymbolsBuilder.add(symbol); columns.put(symbol, analysis.getColumn(field)); } List<Symbol> outputSymbols = outputSymbolsBuilder.build(); PlanNode root = new TableScanNode(idAllocator.getNextId(), handle, outputSymbols, columns.build()); return new RelationPlan(root, scope, outputSymbols); }
@Override protected RelationPlan visitIntersect(Intersect node, Void context) { checkArgument(!node.getRelations().isEmpty(), "No relations specified for INTERSECT"); SetOperationPlan setOperationPlan = process(node); PlanNode planNode = new IntersectNode(idAllocator.getNextId(), setOperationPlan.getSources(), setOperationPlan.getSymbolMapping(), ImmutableList.copyOf(setOperationPlan.getSymbolMapping().keySet())); return new RelationPlan(planNode, analysis.getScope(node), planNode.getOutputSymbols()); }
@Override protected RelationPlan visitExcept(Except node, Void context) { checkArgument(!node.getRelations().isEmpty(), "No relations specified for EXCEPT"); SetOperationPlan setOperationPlan = process(node); PlanNode planNode = new ExceptNode(idAllocator.getNextId(), setOperationPlan.getSources(), setOperationPlan.getSymbolMapping(), ImmutableList.copyOf(setOperationPlan.getSymbolMapping().keySet())); return new RelationPlan(planNode, analysis.getScope(node), planNode.getOutputSymbols()); }
private RelationPlan planLateralJoin(Join join, RelationPlan leftPlan, Lateral lateral) { RelationPlan rightPlan = process(lateral.getQuery(), null); PlanBuilder leftPlanBuilder = initializePlanBuilder(leftPlan); PlanBuilder rightPlanBuilder = initializePlanBuilder(rightPlan); PlanBuilder planBuilder = subqueryPlanner.appendLateralJoin(leftPlanBuilder, rightPlanBuilder, lateral.getQuery(), true, LateralJoinNode.Type.INNER); List<Symbol> outputSymbols = ImmutableList.<Symbol>builder() .addAll(leftPlan.getRoot().getOutputSymbols()) .addAll(rightPlan.getRoot().getOutputSymbols()) .build(); return new RelationPlan(planBuilder.getRoot(), analysis.getScope(join), outputSymbols); }
@Override protected RelationPlan visitValues(Values node, Void context) { Scope scope = analysis.getScope(node); ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder(); for (Field field : scope.getRelationType().getVisibleFields()) { Symbol symbol = symbolAllocator.newSymbol(field); outputSymbolsBuilder.add(symbol); } ImmutableList.Builder<List<Expression>> rows = ImmutableList.builder(); for (Expression row : node.getRows()) { ImmutableList.Builder<Expression> values = ImmutableList.builder(); if (row instanceof Row) { for (Expression item : ((Row) row).getItems()) { Expression expression = Coercer.addCoercions(item, analysis); values.add(ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression)); } } else { Expression expression = Coercer.addCoercions(row, analysis); values.add(ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression)); } rows.add(values.build()); } ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), outputSymbolsBuilder.build(), rows.build()); return new RelationPlan(valuesNode, scope, outputSymbolsBuilder.build()); }
private RelationPlan createDeletePlan(Analysis analysis, Delete node) { DeleteNode deleteNode = new QueryPlanner(analysis, symbolAllocator, idAllocator, buildLambdaDeclarationToSymbolMap(analysis, symbolAllocator), metadata, session) .plan(node); TableFinishNode commitNode = new TableFinishNode( idAllocator.getNextId(), deleteNode, deleteNode.getTarget(), symbolAllocator.newSymbol("rows", BIGINT), Optional.empty(), Optional.empty()); return new RelationPlan(commitNode, analysis.getScope(node), commitNode.getOutputSymbols()); }
@Override protected RelationPlan visitUnion(Union node, Void context) { checkArgument(!node.getRelations().isEmpty(), "No relations specified for UNION"); SetOperationPlan setOperationPlan = process(node); PlanNode planNode = new UnionNode(idAllocator.getNextId(), setOperationPlan.getSources(), setOperationPlan.getSymbolMapping(), ImmutableList.copyOf(setOperationPlan.getSymbolMapping().keySet())); if (node.isDistinct()) { planNode = distinct(planNode); } return new RelationPlan(planNode, analysis.getScope(node), planNode.getOutputSymbols()); }
return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputSymbols());
private RelationPlan createExplainAnalyzePlan(Analysis analysis, Explain statement) { RelationPlan underlyingPlan = planStatementWithoutOutput(analysis, statement.getStatement()); PlanNode root = underlyingPlan.getRoot(); Scope scope = analysis.getScope(statement); Symbol outputSymbol = symbolAllocator.newSymbol(scope.getRelationType().getFieldByIndex(0)); root = new ExplainAnalyzeNode(idAllocator.getNextId(), root, outputSymbol, statement.isVerbose()); return new RelationPlan(root, scope, ImmutableList.of(outputSymbol)); }
Optional.of(result.getDescriptor())); return new RelationPlan(commitNode, analysis.getRootScope(), commitNode.getOutputSymbols()); Optional.empty(), Optional.empty()); return new RelationPlan(commitNode, analysis.getRootScope(), commitNode.getOutputSymbols());
RelationPlan relationPlan = new RelationPlan(tableScan, scope, outputSymbols.build());
@Override protected RelationPlan visitSampledRelation(SampledRelation node, Void context) { RelationPlan subPlan = process(node.getRelation(), context); double ratio = analysis.getSampleRatio(node); PlanNode planNode = new SampleNode(idAllocator.getNextId(), subPlan.getRoot(), ratio, SampleNode.Type.fromType(node.getType())); return new RelationPlan(planNode, analysis.getScope(node), subPlan.getFieldMappings()); }
@Override protected RelationPlan visitAliasedRelation(AliasedRelation node, Void context) { RelationPlan subPlan = process(node.getRelation(), context); PlanNode root = subPlan.getRoot(); List<Symbol> mappings = subPlan.getFieldMappings(); if (node.getColumnNames() != null) { ImmutableList.Builder<Symbol> newMappings = ImmutableList.<Symbol>builder(); Assignments.Builder assignments = Assignments.builder(); // project only the visible columns from the underlying relation for (int i = 0; i < subPlan.getDescriptor().getAllFieldCount(); i++) { Field field = subPlan.getDescriptor().getFieldByIndex(i); if (!field.isHidden()) { Symbol aliasedColumn = symbolAllocator.newSymbol(field); assignments.put(aliasedColumn, subPlan.getFieldMappings().get(i).toSymbolReference()); newMappings.add(aliasedColumn); } } root = new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), assignments.build()); mappings = newMappings.build(); } return new RelationPlan(root, analysis.getScope(node), mappings); }
public RelationPlan plan(Query query) { PlanBuilder builder = planQueryBody(query); List<Expression> orderBy = analysis.getOrderByExpressions(query); builder = handleSubqueries(builder, query, orderBy); List<Expression> outputs = analysis.getOutputExpressions(query); builder = handleSubqueries(builder, query, outputs); builder = project(builder, Iterables.concat(orderBy, outputs)); builder = sort(builder, query); builder = project(builder, analysis.getOutputExpressions(query)); builder = limit(builder, query); return new RelationPlan( builder.getRoot(), analysis.getScope(query), computeOutputs(builder, analysis.getOutputExpressions(query))); }
builder = limit(builder, node); return new RelationPlan( builder.getRoot(), analysis.getScope(node),
return new RelationPlan(projectNode, Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(newFields)).build(), newSymbols.build());
Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields)).build(); plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());