use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class PruneValuesColumns 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 child = lookup.resolve(parent.getSource());
if (!(child instanceof ValuesNode)) {
return Optional.empty();
}
ValuesNode values = (ValuesNode) child;
Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
if (!dependencies.isPresent()) {
return Optional.empty();
}
List<Symbol> newOutputs = dependencies.get();
// 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] = values.getOutputSymbols().indexOf(newOutputs.get(i));
}
ImmutableList.Builder<List<Expression>> rowsBuilder = ImmutableList.builder();
for (List<Expression> row : values.getRows()) {
rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
}
return Optional.of(new ProjectNode(parent.getId(), new ValuesNode(values.getId(), newOutputs, rowsBuilder.build()), parent.getAssignments()));
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class TestVerifyOnlyOneOutputNode method testValidateSuccessful.
@Test
public void testValidateSuccessful() throws Exception {
// random seemingly valid plan
PlanNode root = new OutputNode(idAllocator.getNextId(), new ProjectNode(idAllocator.getNextId(), new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of()), Assignments.of()), ImmutableList.of(), ImmutableList.of());
new VerifyOnlyOneOutputNode().validate(root, null, null, null, null);
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class SimplifyCountOverConstant method apply.
@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
if (!(node instanceof AggregationNode)) {
return Optional.empty();
}
AggregationNode parent = (AggregationNode) node;
PlanNode input = lookup.resolve(parent.getSource());
if (!(input instanceof ProjectNode)) {
return Optional.empty();
}
ProjectNode child = (ProjectNode) input;
boolean changed = false;
Map<Symbol, AggregationNode.Aggregation> assignments = new LinkedHashMap<>(parent.getAssignments());
for (Entry<Symbol, AggregationNode.Aggregation> entry : parent.getAssignments().entrySet()) {
Symbol symbol = entry.getKey();
AggregationNode.Aggregation aggregation = entry.getValue();
if (isCountOverConstant(aggregation, child.getAssignments())) {
changed = true;
assignments.put(symbol, new AggregationNode.Aggregation(new FunctionCall(QualifiedName.of("count"), ImmutableList.of()), new Signature("count", AGGREGATE, parseTypeSignature(StandardTypes.BIGINT))));
}
}
if (!changed) {
return Optional.empty();
}
return Optional.of(new AggregationNode(node.getId(), child, assignments, parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class TestTypeValidator method testInvalidProject.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'expr(_[0-9]+)?' is expected to be bigint, but the actual type is integer")
public void testInvalidProject() throws Exception {
Expression expression1 = new Cast(columnB.toSymbolReference(), StandardTypes.INTEGER);
Expression expression2 = new Cast(columnA.toSymbolReference(), StandardTypes.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);
assertTypesValid(node);
}
use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.
the class TestTypeValidator method testValidProject.
@Test
public void testValidProject() throws Exception {
Expression expression1 = new Cast(columnB.toSymbolReference(), StandardTypes.BIGINT);
Expression expression2 = new Cast(columnC.toSymbolReference(), StandardTypes.BIGINT);
Assignments assignments = Assignments.builder().put(symbolAllocator.newSymbol(expression1, BIGINT), expression1).put(symbolAllocator.newSymbol(expression2, BIGINT), expression2).build();
PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
assertTypesValid(node);
}
Aggregations