use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class PushTopNThroughUnion method apply.
@Override
public Result apply(TopNNode topNNode, Captures captures, Context context) {
UnionNode unionNode = captures.get(CHILD);
ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
for (PlanNode source : unionNode.getSources()) {
SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
Set<VariableReferenceExpression> sourceOutputVariables = ImmutableSet.copyOf(source.getOutputVariables());
for (VariableReferenceExpression unionOutput : unionNode.getOutputVariables()) {
Set<VariableReferenceExpression> inputVariables = ImmutableSet.copyOf(unionNode.getVariableMapping().get(unionOutput));
VariableReferenceExpression unionInput = getLast(intersection(inputVariables, sourceOutputVariables));
symbolMapper.put(unionOutput, unionInput);
}
sources.add(symbolMapper.build().map(topNNode, source, context.getIdAllocator().getNextId()));
}
return Result.ofPlanNode(new UnionNode(unionNode.getSourceLocation(), unionNode.getId(), sources.build(), unionNode.getOutputVariables(), unionNode.getVariableMapping()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class RowExpressionRewriteRuleSet method translateStatisticAggregation.
private Optional<StatisticAggregations> translateStatisticAggregation(StatisticAggregations statisticAggregations, Rule.Context context) {
ImmutableMap.Builder<VariableReferenceExpression, AggregationNode.Aggregation> rewrittenAggregation = builder();
boolean changed = false;
for (Map.Entry<VariableReferenceExpression, AggregationNode.Aggregation> entry : statisticAggregations.getAggregations().entrySet()) {
AggregationNode.Aggregation rewritten = rewriteAggregation(entry.getValue(), context);
rewrittenAggregation.put(entry.getKey(), rewritten);
if (!rewritten.equals(entry.getValue())) {
changed = true;
}
}
if (changed) {
return Optional.of(new StatisticAggregations(rewrittenAggregation.build(), statisticAggregations.getGroupingVariables()));
}
return Optional.empty();
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class PruneValuesColumns method pushDownProjectOff.
@Override
protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, PlanVariableAllocator variableAllocator, ValuesNode valuesNode, Set<VariableReferenceExpression> referencedOutputs) {
List<VariableReferenceExpression> newOutputs = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
List<VariableReferenceExpression> newOutputVariables = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
// for each output of project, the corresponding column in the values node
int[] mapping = new int[newOutputs.size()];
for (int i = 0; i < mapping.length; i++) {
mapping[i] = valuesNode.getOutputVariables().indexOf(newOutputs.get(i));
}
ImmutableList.Builder<List<RowExpression>> rowsBuilder = ImmutableList.builder();
for (List<RowExpression> row : valuesNode.getRows()) {
rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
}
return Optional.of(new ValuesNode(valuesNode.getSourceLocation(), valuesNode.getId(), newOutputVariables, rowsBuilder.build()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class PushAggregationThroughOuterJoin method inlineOrderByVariables.
private static OrderingScheme inlineOrderByVariables(Map<VariableReferenceExpression, VariableReferenceExpression> variableMapping, OrderingScheme orderingScheme) {
// This is a logic expanded from ExpressionTreeRewriter::rewriteSortItems
ImmutableList.Builder<VariableReferenceExpression> orderBy = ImmutableList.builder();
ImmutableMap.Builder<VariableReferenceExpression, SortOrder> ordering = new ImmutableMap.Builder<>();
for (VariableReferenceExpression variable : orderingScheme.getOrderByVariables()) {
VariableReferenceExpression translated = variableMapping.get(variable);
orderBy.add(translated);
ordering.put(translated, orderingScheme.getOrdering(variable));
}
ImmutableMap<VariableReferenceExpression, SortOrder> orderingMap = ordering.build();
return new OrderingScheme(orderBy.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class RewriteAggregationIfToFilter method shouldRewriteAggregation.
private boolean shouldRewriteAggregation(Aggregation aggregation, ProjectNode sourceProject) {
if (functionAndTypeManager.getFunctionMetadata(aggregation.getFunctionHandle()).isCalledOnNullInput()) {
// This rewrite will filter out the null values. It could change the behavior if the aggregation is also applied on NULLs.
return false;
}
if (!(aggregation.getArguments().size() == 1 && aggregation.getArguments().get(0) instanceof VariableReferenceExpression)) {
// Currently we only handle aggregation with a single VariableReferenceExpression. The detailed expressions are in a project node below this aggregation.
return false;
}
if (aggregation.getFilter().isPresent() || aggregation.getMask().isPresent()) {
// Do not rewrite the aggregation if it already has a filter or mask.
return false;
}
RowExpression sourceExpression = sourceProject.getAssignments().get((VariableReferenceExpression) aggregation.getArguments().get(0));
if (sourceExpression instanceof CallExpression) {
CallExpression callExpression = (CallExpression) sourceExpression;
if (callExpression.getArguments().size() == 1 && standardFunctionResolution.isCastFunction(callExpression.getFunctionHandle())) {
// If the expression is CAST(), check the expression inside.
sourceExpression = callExpression.getArguments().get(0);
}
}
if (!(sourceExpression instanceof SpecialFormExpression) || !rowExpressionDeterminismEvaluator.isDeterministic(sourceExpression)) {
return false;
}
SpecialFormExpression expression = (SpecialFormExpression) sourceExpression;
// Only rewrite the aggregation if the else branch is not present or the else result is NULL.
return expression.getForm() == IF && Expressions.isNull(expression.getArguments().get(2));
}
Aggregations