use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.
the class RelationPlanner method process.
private SetOperationPlan process(SetOperation node) {
List<Symbol> outputs = null;
ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
ImmutableListMultimap.Builder<Symbol, Symbol> symbolMapping = ImmutableListMultimap.builder();
List<RelationPlan> subPlans = node.getRelations().stream().map(relation -> processAndCoerceIfNecessary(relation, null)).collect(toImmutableList());
for (RelationPlan relationPlan : subPlans) {
List<Symbol> childOutputSymbols = relationPlan.getFieldMappings();
if (outputs == null) {
// Use the first Relation to derive output symbol names
RelationType descriptor = relationPlan.getDescriptor();
ImmutableList.Builder<Symbol> outputSymbolBuilder = ImmutableList.builder();
for (Field field : descriptor.getVisibleFields()) {
int fieldIndex = descriptor.indexOf(field);
Symbol symbol = childOutputSymbols.get(fieldIndex);
outputSymbolBuilder.add(planSymbolAllocator.newSymbol(symbol.getName(), planSymbolAllocator.getTypes().get(symbol)));
}
outputs = outputSymbolBuilder.build();
}
RelationType descriptor = relationPlan.getDescriptor();
checkArgument(descriptor.getVisibleFieldCount() == outputs.size(), "Expected relation to have %s symbols but has %s symbols", descriptor.getVisibleFieldCount(), outputs.size());
int fieldId = 0;
for (Field field : descriptor.getVisibleFields()) {
int fieldIndex = descriptor.indexOf(field);
symbolMapping.put(outputs.get(fieldId), childOutputSymbols.get(fieldIndex));
fieldId++;
}
sources.add(relationPlan.getRoot());
}
return new SetOperationPlan(sources.build(), symbolMapping.build());
}
use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.
the class RelationPlanner method visitUnnest.
@Override
protected RelationPlan visitUnnest(Unnest node, Void context) {
Scope scope = analysis.getScope(node);
ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
for (Field field : scope.getRelationType().getVisibleFields()) {
Symbol symbol = planSymbolAllocator.newSymbol(field);
outputSymbolsBuilder.add(symbol);
}
List<Symbol> unnestedSymbols = outputSymbolsBuilder.build();
// If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
ImmutableList.Builder<Symbol> argumentSymbols = ImmutableList.builder();
ImmutableList.Builder<RowExpression> values = ImmutableList.builder();
ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
Iterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
for (Expression expression : node.getExpressions()) {
Type type = analysis.getType(expression);
Expression rewritten = Coercer.addCoercions(expression, analysis);
rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), rewritten);
values.add(castToRowExpression(rewritten));
Symbol inputSymbol = planSymbolAllocator.newSymbol(rewritten, type);
argumentSymbols.add(inputSymbol);
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
if (elementType instanceof RowType) {
ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
}
unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
} else {
unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
}
} else if (type instanceof MapType) {
unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
} else {
throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
}
}
Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), argumentSymbols.build(), ImmutableList.of(values.build()));
UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestSymbols.build(), ordinalitySymbol);
return new RelationPlan(unnestNode, scope, unnestedSymbols);
}
use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.
the class RelationPlanner method addColumnMasks.
private RelationPlan addColumnMasks(Table table, RelationPlan plan) {
Map<String, List<Expression>> columnMasks = analysis.getColumnMasks(table);
PlanNode root = plan.getRoot();
List<Symbol> mappings = plan.getFieldMappings();
TranslationMap translations = new TranslationMap(plan, analysis, lambdaDeclarationToSymbolMap);
translations.setFieldMappings(mappings);
PlanBuilder planBuilder = new PlanBuilder(translations, root);
for (int i = 0; i < plan.getDescriptor().getAllFieldCount(); i++) {
Field field = plan.getDescriptor().getFieldByIndex(i);
for (Expression mask : columnMasks.getOrDefault(field.getName().get(), ImmutableList.of())) {
planBuilder = subqueryPlanner.handleSubqueries(planBuilder, mask, mask);
Map<Symbol, RowExpression> assignments = new LinkedHashMap<>();
for (Symbol symbol : root.getOutputSymbols()) {
assignments.put(symbol, castToRowExpression(toSymbolReference(symbol)));
}
assignments.put(mappings.get(i), castToRowExpression(translations.rewrite(mask)));
planBuilder = planBuilder.withNewRoot(new ProjectNode(idAllocator.getNextId(), planBuilder.getRoot(), Assignments.copyOf(assignments)));
}
}
return new RelationPlan(planBuilder.getRoot(), plan.getScope(), mappings);
}
Aggregations