use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class CanonicalPlanGenerator method visitAggregation.
@Override
public Optional<PlanNode> visitAggregation(AggregationNode node, Map<VariableReferenceExpression, VariableReferenceExpression> context) {
Optional<PlanNode> source = node.getSource().accept(this, context);
if (!source.isPresent()) {
return Optional.empty();
}
// Steps to get canonical aggregations:
// 1. Transform aggregation into canonical form
// 2. Sort based on canonical aggregation expression
// 3. Get new variable reference for aggregation expression
// 4. Record mapping from original variable reference to the new one
List<AggregationReference> aggregationReferences = node.getAggregations().entrySet().stream().map(entry -> new AggregationReference(getCanonicalAggregation(entry.getValue(), context), entry.getKey())).sorted(comparing(aggregationReference -> aggregationReference.getAggregation().getCall().toString())).collect(toImmutableList());
ImmutableMap.Builder<VariableReferenceExpression, Aggregation> aggregations = ImmutableMap.builder();
for (AggregationReference aggregationReference : aggregationReferences) {
VariableReferenceExpression reference = variableAllocator.newVariable(aggregationReference.getAggregation().getCall());
context.put(aggregationReference.getVariableReferenceExpression(), reference);
aggregations.put(reference, aggregationReference.getAggregation());
}
return Optional.of(new AggregationNode(node.getSourceLocation(), planNodeidAllocator.getNextId(), source.get(), aggregations.build(), getCanonicalGroupingSetDescriptor(node.getGroupingSets(), context), node.getPreGroupedVariables().stream().map(context::get).collect(toImmutableList()), node.getStep(), node.getHashVariable().map(ignored -> variableAllocator.newHashVariable()), node.getGroupIdVariable().map(context::get)));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class CanonicalPlanGenerator method visitTableScan.
@Override
public Optional<PlanNode> visitTableScan(TableScanNode node, Map<VariableReferenceExpression, VariableReferenceExpression> context) {
List<ColumnReference> columnReferences = node.getAssignments().entrySet().stream().map(entry -> new ColumnReference(entry.getValue(), entry.getKey())).sorted(comparing(columnReference -> columnReference.getColumnHandle().toString())).collect(toImmutableList());
ImmutableList.Builder<VariableReferenceExpression> outputVariables = ImmutableList.builder();
ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> assignments = ImmutableMap.builder();
for (ColumnReference columnReference : columnReferences) {
VariableReferenceExpression reference = variableAllocator.newVariable(columnReference.getVariableReferenceExpression().getSourceLocation(), columnReference.getColumnHandle().toString(), columnReference.getVariableReferenceExpression().getType());
context.put(columnReference.getVariableReferenceExpression(), reference);
outputVariables.add(reference);
assignments.put(reference, columnReference.getColumnHandle());
}
return Optional.of(new CanonicalTableScanNode(node.getSourceLocation(), planNodeidAllocator.getNextId(), getCanonicalTableHandle(node.getTable()), outputVariables.build(), assignments.build()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class PlanBuilder method appendProjections.
public PlanBuilder appendProjections(Iterable<Expression> expressions, PlanVariableAllocator variableAllocator, PlanNodeIdAllocator idAllocator) {
TranslationMap translations = copyTranslations();
Assignments.Builder projections = Assignments.builder();
// add an identity projection for underlying plan
for (VariableReferenceExpression variable : getRoot().getOutputVariables()) {
projections.put(variable, castToRowExpression(createSymbolReference(variable)));
}
ImmutableMap.Builder<VariableReferenceExpression, Expression> newTranslations = ImmutableMap.builder();
for (Expression expression : expressions) {
VariableReferenceExpression variable = variableAllocator.newVariable(expression, getAnalysis().getTypeWithCoercions(expression));
projections.put(variable, castToRowExpression(translations.rewrite(expression)));
newTranslations.put(variable, expression);
}
// Now append the new translations into the TranslationMap
for (Map.Entry<VariableReferenceExpression, Expression> entry : newTranslations.build().entrySet()) {
translations.put(entry.getValue(), entry.getKey());
}
return new PlanBuilder(translations, new ProjectNode(idAllocator.getNextId(), getRoot(), projections.build()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class Partitioning method translateToCoalesce.
// Maps VariableReferenceExpression in both partitions to an COALESCE expression, keeps constant arguments unchanged.
public Optional<Partitioning> translateToCoalesce(Partitioning other, Metadata metadata, Session session) {
checkArgument(arePartitionHandlesCompatibleForCoalesce(this.handle, other.handle, metadata, session), "incompatible partitioning handles: cannot coalesce %s and %s", this.handle, other.handle);
checkArgument(this.arguments.size() == other.arguments.size(), "incompatible number of partitioning arguments: %s != %s", this.arguments.size(), other.arguments.size());
ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder();
for (int i = 0; i < this.arguments.size(); i++) {
RowExpression leftArgument = this.arguments.get(i);
RowExpression rightArgument = other.arguments.get(i);
if (leftArgument instanceof ConstantExpression) {
arguments.add(leftArgument);
} else if (rightArgument instanceof ConstantExpression) {
arguments.add(rightArgument);
} else if (leftArgument instanceof VariableReferenceExpression && rightArgument instanceof VariableReferenceExpression) {
VariableReferenceExpression leftVariable = (VariableReferenceExpression) leftArgument;
VariableReferenceExpression rightVariable = (VariableReferenceExpression) rightArgument;
checkArgument(leftVariable.getType().equals(rightVariable.getType()), "incompatible types: %s != %s", leftVariable.getType(), rightVariable.getType());
arguments.add(new SpecialFormExpression(COALESCE, leftVariable.getType(), ImmutableList.of(leftVariable, rightVariable)));
} else {
return Optional.empty();
}
}
return Optional.of(new Partitioning(metadata.isRefinedPartitioningOver(session, other.handle, this.handle) ? this.handle : other.handle, arguments.build()));
}
use of com.facebook.presto.spi.relation.VariableReferenceExpression in project presto by prestodb.
the class TestDruidQueryBase method tableScan.
protected TableScanNode tableScan(PlanBuilder planBuilder, DruidTableHandle connectorTableHandle, DruidColumnHandle... columnHandles) {
List<VariableReferenceExpression> variables = Arrays.stream(columnHandles).map(ch -> new VariableReferenceExpression(Optional.empty(), ch.getColumnName().toLowerCase(ENGLISH), ch.getColumnType())).collect(toImmutableList());
ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> assignments = ImmutableMap.builder();
for (int i = 0; i < variables.size(); ++i) {
assignments.put(variables.get(i), columnHandles[i]);
}
TableHandle tableHandle = new TableHandle(druidConnectorId, connectorTableHandle, TestingTransactionHandle.create(), Optional.empty());
return new TableScanNode(Optional.empty(), planBuilder.getIdAllocator().getNextId(), tableHandle, variables, assignments.build(), TupleDomain.all(), TupleDomain.all());
}
Aggregations