use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.
the class ReplaceJoinOverConstantWithProject method appendProjection.
private ProjectNode appendProjection(PlanNode source, List<Symbol> sourceOutputs, PlanNode constantSource, List<Symbol> constantOutputs, PlanNodeIdAllocator idAllocator) {
ValuesNode values = (ValuesNode) constantSource;
Row row = (Row) getOnlyElement(values.getRows().get());
Map<Symbol, Expression> mapping = new HashMap<>();
for (int i = 0; i < values.getOutputSymbols().size(); i++) {
mapping.put(values.getOutputSymbols().get(i), row.getItems().get(i));
}
Assignments.Builder assignments = Assignments.builder().putIdentities(sourceOutputs);
constantOutputs.stream().forEach(symbol -> assignments.put(symbol, mapping.get(symbol)));
return new ProjectNode(idAllocator.getNextId(), source, assignments.build());
}
use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.
the class ReplaceRedundantJoinWithProject method appendNulls.
private static ProjectNode appendNulls(PlanNode source, List<Symbol> sourceOutputs, List<Symbol> nullSymbols, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
Assignments.Builder assignments = Assignments.builder().putIdentities(sourceOutputs);
nullSymbols.stream().forEach(symbol -> assignments.put(symbol, new Cast(new NullLiteral(), toSqlType(symbolAllocator.getTypes().get(symbol)))));
return new ProjectNode(idAllocator.getNextId(), source, assignments.build());
}
use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.
the class RemoveEmptyUnionBranches method apply.
@Override
public Result apply(UnionNode node, Captures captures, Context context) {
long emptyBranches = node.getSources().stream().filter(source -> isEmpty(source, context.getLookup())).count();
if (emptyBranches == 0) {
return Result.empty();
}
if (emptyBranches == node.getSources().size()) {
return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
}
ImmutableList.Builder<PlanNode> newSourcesBuilder = ImmutableList.builder();
ImmutableListMultimap.Builder<Symbol, Symbol> outputsToInputsBuilder = ImmutableListMultimap.builder();
for (int i = 0; i < node.getSources().size(); i++) {
PlanNode source = node.getSources().get(i);
if (!isEmpty(source, context.getLookup())) {
newSourcesBuilder.add(source);
for (Symbol column : node.getOutputSymbols()) {
outputsToInputsBuilder.put(column, node.getSymbolMapping().get(column).get(i));
}
}
}
List<PlanNode> newSources = newSourcesBuilder.build();
ListMultimap<Symbol, Symbol> outputsToInputs = outputsToInputsBuilder.build();
if (newSources.size() == 1) {
Assignments.Builder assignments = Assignments.builder();
outputsToInputs.entries().stream().forEach(entry -> assignments.put(entry.getKey(), entry.getValue().toSymbolReference()));
return Result.ofPlanNode(new ProjectNode(node.getId(), newSources.get(0), assignments.build()));
}
return Result.ofPlanNode(new UnionNode(node.getId(), newSources, outputsToInputs, node.getOutputSymbols()));
}
use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.
the class RemoveRedundantExists method apply.
@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
Assignments.Builder assignments = Assignments.builder();
assignments.putIdentities(node.getInput().getOutputSymbols());
Expression result;
if (QueryCardinalityUtil.isEmpty(node.getSubquery(), context.getLookup())) {
result = FALSE_LITERAL;
} else if (QueryCardinalityUtil.isAtLeastScalar(node.getSubquery(), context.getLookup())) {
result = TRUE_LITERAL;
} else {
return Result.empty();
}
for (Symbol output : node.getSubqueryAssignments().getOutputs()) {
assignments.put(output, result);
}
return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), assignments.build()));
}
use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.
the class TestTypeValidator method testInvalidProject.
@Test
public void testInvalidProject() {
Expression expression1 = new Cast(columnB.toSymbolReference(), toSqlType(INTEGER));
Expression expression2 = new Cast(columnA.toSymbolReference(), toSqlType(INTEGER));
Assignments assignments = Assignments.builder().put(symbolAllocator.newSymbol(expression1, BIGINT), // should be INTEGER
expression1).put(symbolAllocator.newSymbol(expression1, INTEGER), expression2).build();
PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
assertThatThrownBy(() -> assertTypesValid(node)).isInstanceOf(IllegalArgumentException.class).hasMessageMatching("type of symbol 'expr(_[0-9]+)?' is expected to be bigint, but the actual type is integer");
}
Aggregations