use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.
the class PushLimitThroughProject method apply.
@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
ProjectNode projectNode = captures.get(CHILD);
// for a LimitNode without ties, simply reorder the nodes
if (!parent.isWithTies()) {
return Result.ofPlanNode(transpose(parent, projectNode));
}
// for a LimitNode with ties, the tiesResolvingScheme must be rewritten in terms of symbols before projection
SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
for (Symbol symbol : parent.getTiesResolvingScheme().get().getOrderBy()) {
Expression expression = castToExpression(projectNode.getAssignments().get(symbol));
// if a symbol results from some computation, the translation fails
if (!(expression instanceof SymbolReference)) {
return Result.empty();
}
symbolMapper.put(symbol, SymbolUtils.from(expression));
}
LimitNode mappedLimitNode = symbolMapper.build().map(parent, projectNode.getSource());
return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedLimitNode)));
}
use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.
the class PushTopNThroughProject method apply.
@Override
public Result apply(TopNNode parent, Captures captures, Context context) {
ProjectNode projectNode = captures.get(PROJECT_CHILD);
// do not push topN between projection and filter(table scan) so that they can be merged into a PageProcessor
PlanNode projectSource = context.getLookup().resolve(projectNode.getSource());
if (projectSource instanceof FilterNode) {
PlanNode filterSource = context.getLookup().resolve(((FilterNode) projectSource).getSource());
if (filterSource instanceof TableScanNode) {
return Result.empty();
}
}
Optional<SymbolMapper> symbolMapper = symbolMapper(parent.getOrderingScheme().getOrderBy(), projectNode.getAssignments());
if (!symbolMapper.isPresent()) {
return Result.empty();
}
TopNNode mappedTopN = symbolMapper.get().map(parent, projectNode.getSource(), context.getIdAllocator().getNextId());
return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedTopN)));
}
use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.
the class PushPartialAggregationThroughExchange method pushPartial.
private PlanNode pushPartial(AggregationNode aggregation, ExchangeNode exchange, Context context) {
List<PlanNode> partials = new ArrayList<>();
for (int i = 0; i < exchange.getSources().size(); i++) {
PlanNode source = exchange.getSources().get(i);
SymbolMapper.Builder mappingsBuilder = SymbolMapper.builder();
for (int outputIndex = 0; outputIndex < exchange.getOutputSymbols().size(); outputIndex++) {
Symbol output = exchange.getOutputSymbols().get(outputIndex);
Symbol input = exchange.getInputs().get(i).get(outputIndex);
if (!output.equals(input)) {
mappingsBuilder.put(output, input);
}
}
SymbolMapper symbolMapper = mappingsBuilder.build();
if (symbolMapper.getTypes() == null) {
symbolMapper.setTypes(context.getSymbolAllocator().getTypes());
}
AggregationNode mappedPartial = symbolMapper.map(aggregation, source, context.getIdAllocator());
Assignments.Builder assignments = Assignments.builder();
for (Symbol output : aggregation.getOutputSymbols()) {
Symbol input = symbolMapper.map(output);
assignments.put(output, VariableReferenceSymbolConverter.toVariableReference(input, context.getSymbolAllocator().getTypes()));
}
partials.add(new ProjectNode(context.getIdAllocator().getNextId(), mappedPartial, assignments.build()));
}
for (PlanNode node : partials) {
verify(aggregation.getOutputSymbols().equals(node.getOutputSymbols()));
}
// Since this exchange source is now guaranteed to have the same symbols as the inputs to the the partial
// aggregation, we don't need to rewrite symbols in the partitioning function
PartitioningScheme partitioning = new PartitioningScheme(exchange.getPartitioningScheme().getPartitioning(), aggregation.getOutputSymbols(), exchange.getPartitioningScheme().getHashColumn(), exchange.getPartitioningScheme().isReplicateNullsAndAny(), exchange.getPartitioningScheme().getBucketToPartition());
return new ExchangeNode(context.getIdAllocator().getNextId(), exchange.getType(), exchange.getScope(), partitioning, partials, ImmutableList.copyOf(Collections.nCopies(partials.size(), aggregation.getOutputSymbols())), Optional.empty(), aggregation.getAggregationType());
}
use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.
the class PushTableWriteThroughUnion method rewriteSource.
private static TableWriterNode rewriteSource(TableWriterNode writerNode, UnionNode unionNode, int source, List<Map<Symbol, Symbol>> sourceMappings, Context context) {
Map<Symbol, Symbol> inputMappings = getInputSymbolMapping(unionNode, source);
ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
mappings.putAll(inputMappings);
ImmutableMap.Builder<Symbol, Symbol> outputMappings = ImmutableMap.builder();
for (Symbol outputSymbol : writerNode.getOutputSymbols()) {
if (inputMappings.containsKey(outputSymbol)) {
outputMappings.put(outputSymbol, inputMappings.get(outputSymbol));
} else {
Symbol newSymbol = context.getSymbolAllocator().newSymbol(outputSymbol);
outputMappings.put(outputSymbol, newSymbol);
mappings.put(outputSymbol, newSymbol);
}
}
sourceMappings.add(outputMappings.build());
ImmutableMap.Builder<String, String> stringMapping = ImmutableMap.builder();
mappings.build().forEach((symbol1, symbol2) -> stringMapping.put(symbol1.getName(), symbol2.getName()));
SymbolMapper symbolMapper = new SymbolMapper(stringMapping.build(), context.getSymbolAllocator().getTypes());
return symbolMapper.map(writerNode, unionNode.getSources().get(source), context.getIdAllocator().getNextId());
}
use of io.prestosql.sql.planner.optimizations.SymbolMapper in project hetu-core by openlookeng.
the class PushTopNThroughProject method symbolMapper.
private Optional<SymbolMapper> symbolMapper(List<Symbol> symbols, Assignments assignments) {
SymbolMapper.Builder mapper = SymbolMapper.builder();
for (Symbol symbol : symbols) {
if (isExpression(assignments.get(symbol))) {
Expression expression = castToExpression(assignments.get(symbol));
if (!(expression instanceof SymbolReference)) {
return Optional.empty();
}
mapper.put(symbol, SymbolUtils.from(expression));
} else {
RowExpression expression = assignments.get(symbol);
if (!(expression instanceof VariableReferenceExpression)) {
return Optional.empty();
}
mapper.put(symbol, new Symbol(((VariableReferenceExpression) expression).getName()));
}
}
return Optional.of(mapper.build());
}
Aggregations