use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class PushLimitThroughProject method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof LimitNode)) {
return Optional.empty();
}
LimitNode parent = (LimitNode) node;
PlanNode child = lookup.resolve(parent.getSource());
if (!(child instanceof ProjectNode)) {
return Optional.empty();
}
return Optional.of(transpose(parent, child));
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class QueryPlanner method project.
private PlanBuilder project(PlanBuilder subPlan, Iterable<Expression> expressions) {
TranslationMap outputTranslations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
Assignments.Builder projections = Assignments.builder();
for (Expression expression : expressions) {
Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
Symbol symbol = symbolAllocator.newSymbol(rewritten, analysis.getTypeWithCoercions(expression));
projections.put(symbol, subPlan.rewrite(rewritten));
outputTranslations.addIntermediateMapping(expression, rewritten);
outputTranslations.put(rewritten, symbol);
}
return new PlanBuilder(outputTranslations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()), analysis.getParameters());
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class PlanBuilder method appendProjections.
public PlanBuilder appendProjections(Iterable<Expression> expressions, SymbolAllocator symbolAllocator, PlanNodeIdAllocator idAllocator) {
TranslationMap translations = copyTranslations();
Assignments.Builder projections = Assignments.builder();
// add an identity projection for underlying plan
for (Symbol symbol : getRoot().getOutputSymbols()) {
projections.put(symbol, symbol.toSymbolReference());
}
ImmutableMap.Builder<Symbol, Expression> newTranslations = ImmutableMap.builder();
ParameterRewriter parameterRewriter = new ParameterRewriter(parameters, getAnalysis());
for (Expression expression : expressions) {
Expression rewritten = ExpressionTreeRewriter.rewriteWith(parameterRewriter, expression);
translations.addIntermediateMapping(expression, rewritten);
Symbol symbol = symbolAllocator.newSymbol(rewritten, getAnalysis().getTypeWithCoercions(expression));
projections.put(symbol, translations.rewrite(rewritten));
newTranslations.put(symbol, rewritten);
}
// Now append the new translations into the TranslationMap
for (Map.Entry<Symbol, Expression> entry : newTranslations.build().entrySet()) {
translations.put(entry.getValue(), entry.getKey());
}
return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), getRoot(), projections.build()), parameters);
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class QueryPlanner method explicitCoercionFields.
private PlanBuilder explicitCoercionFields(PlanBuilder subPlan, Iterable<Expression> alreadyCoerced, Iterable<? extends Expression> uncoerced) {
TranslationMap translations = new TranslationMap(subPlan.getRelationPlan(), analysis, lambdaDeclarationToSymbolMap);
Assignments.Builder projections = Assignments.builder();
projections.putAll(coerce(uncoerced, subPlan, translations));
for (Expression expression : alreadyCoerced) {
Symbol symbol = symbolAllocator.newSymbol(expression, analysis.getType(expression));
Expression parametersReplaced = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
translations.addIntermediateMapping(expression, parametersReplaced);
Expression rewritten = subPlan.rewrite(expression);
projections.put(symbol, rewritten);
translations.put(parametersReplaced, symbol);
}
return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), projections.build()), analysis.getParameters());
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class PruneTableScanColumns method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof ProjectNode)) {
return Optional.empty();
}
ProjectNode parent = (ProjectNode) node;
PlanNode source = lookup.resolve(parent.getSource());
if (!(source instanceof TableScanNode)) {
return Optional.empty();
}
TableScanNode child = (TableScanNode) source;
Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
if (!dependencies.isPresent()) {
return Optional.empty();
}
List<Symbol> newOutputs = dependencies.get();
return Optional.of(new ProjectNode(parent.getId(), new TableScanNode(child.getId(), child.getTable(), newOutputs, newOutputs.stream().collect(Collectors.toMap(Function.identity(), e -> child.getAssignments().get(e))), child.getLayout(), child.getCurrentConstraint(), child.getOriginalConstraint()), parent.getAssignments()));
}
Aggregations