use of com.facebook.presto.spi.plan.OrderingScheme in project presto by prestodb.
the class TestEffectivePredicateExtractor method testWindow.
@Test
public void testWindow() {
PlanNode node = new WindowNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10)))), new WindowNode.Specification(ImmutableList.of(AV), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(AV, SortOrder.ASC_NULLS_LAST))))), ImmutableMap.of(), Optional.empty(), ImmutableSet.of(), 0);
RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
// Pass through
assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10))));
}
use of com.facebook.presto.spi.plan.OrderingScheme in project presto by prestodb.
the class TestPruneWindowColumns method buildProjectedWindow.
private static PlanNode buildProjectedWindow(PlanBuilder p, Predicate<VariableReferenceExpression> projectionFilter, Predicate<VariableReferenceExpression> sourceFilter) {
VariableReferenceExpression orderKey = p.variable("orderKey");
VariableReferenceExpression partitionKey = p.variable("partitionKey");
VariableReferenceExpression hash = p.variable("hash");
VariableReferenceExpression startValue1 = p.variable("startValue1");
VariableReferenceExpression startValue2 = p.variable("startValue2");
VariableReferenceExpression endValue1 = p.variable("endValue1");
VariableReferenceExpression endValue2 = p.variable("endValue2");
VariableReferenceExpression input1 = p.variable("input1");
VariableReferenceExpression input2 = p.variable("input2");
VariableReferenceExpression unused = p.variable("unused");
VariableReferenceExpression output1 = p.variable("output1");
VariableReferenceExpression output2 = p.variable("output2");
List<VariableReferenceExpression> inputs = ImmutableList.of(orderKey, partitionKey, hash, startValue1, startValue2, endValue1, endValue2, input1, input2, unused);
List<VariableReferenceExpression> outputs = ImmutableList.<VariableReferenceExpression>builder().addAll(inputs).add(output1, output2).build();
List<VariableReferenceExpression> filteredInputs = inputs.stream().filter(sourceFilter).collect(toImmutableList());
return p.project(identityAssignmentsAsSymbolReferences(outputs.stream().filter(projectionFilter).collect(toImmutableList())), p.window(new WindowNode.Specification(ImmutableList.of(partitionKey), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(orderKey, SortOrder.ASC_NULLS_FIRST))))), ImmutableMap.of(output1, new WindowNode.Function(call(FUNCTION_NAME, FUNCTION_HANDLE, BIGINT, input1), new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue1), CURRENT_ROW, Optional.of(endValue1), Optional.of(new SymbolReference(startValue1.getName())).map(Expression::toString), Optional.of(new SymbolReference(endValue2.getName())).map(Expression::toString)), false), output2, new WindowNode.Function(call(FUNCTION_NAME, FUNCTION_HANDLE, BIGINT, input2), new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue2), CURRENT_ROW, Optional.of(endValue2), Optional.of(new SymbolReference(startValue2.getName())).map(Expression::toString), Optional.of(new SymbolReference(endValue2.getName())).map(Expression::toString)), false)), hash, p.values(filteredInputs, ImmutableList.of())));
}
use of com.facebook.presto.spi.plan.OrderingScheme in project presto by prestodb.
the class TestWindowNode method testSerializationRoundtrip.
@Test
public void testSerializationRoundtrip() {
VariableReferenceExpression windowVariable = variableAllocator.newVariable("sum", BIGINT);
FunctionHandle functionHandle = createTestMetadataManager().getFunctionAndTypeManager().lookupFunction("sum", fromTypes(BIGINT));
WindowNode.Frame frame = new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
PlanNodeId id = newId();
WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(columnA), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(columnB, SortOrder.ASC_NULLS_FIRST)))));
CallExpression call = call("sum", functionHandle, BIGINT, new VariableReferenceExpression(Optional.empty(), columnC.getName(), BIGINT));
Map<VariableReferenceExpression, WindowNode.Function> functions = ImmutableMap.of(windowVariable, new WindowNode.Function(call, frame, false));
Optional<VariableReferenceExpression> hashVariable = Optional.of(columnB);
Set<VariableReferenceExpression> prePartitionedInputs = ImmutableSet.of(columnA);
WindowNode windowNode = new WindowNode(Optional.empty(), id, sourceNode, specification, functions, hashVariable, prePartitionedInputs, 0);
String json = codec.toJson(windowNode);
WindowNode actualNode = codec.fromJson(json);
assertEquals(actualNode.getId(), windowNode.getId());
assertEquals(actualNode.getSpecification(), windowNode.getSpecification());
assertEquals(actualNode.getWindowFunctions(), windowNode.getWindowFunctions());
assertEquals(actualNode.getFrames(), windowNode.getFrames());
assertEquals(actualNode.getHashVariable(), windowNode.getHashVariable());
assertEquals(actualNode.getPrePartitionedInputs(), windowNode.getPrePartitionedInputs());
assertEquals(actualNode.getPreSortedOrderPrefix(), windowNode.getPreSortedOrderPrefix());
}
use of com.facebook.presto.spi.plan.OrderingScheme in project presto by prestodb.
the class SymbolMapper method map.
public TopNNode map(TopNNode node, PlanNode source, PlanNodeId newNodeId) {
ImmutableList.Builder<VariableReferenceExpression> variables = ImmutableList.builder();
ImmutableMap.Builder<VariableReferenceExpression, SortOrder> orderings = ImmutableMap.builder();
Set<VariableReferenceExpression> seenCanonicals = new HashSet<>(node.getOrderingScheme().getOrderByVariables().size());
for (VariableReferenceExpression variable : node.getOrderingScheme().getOrderByVariables()) {
VariableReferenceExpression canonical = map(variable);
if (seenCanonicals.add(canonical)) {
seenCanonicals.add(canonical);
variables.add(canonical);
orderings.put(canonical, node.getOrderingScheme().getOrdering(variable));
}
}
ImmutableMap<VariableReferenceExpression, SortOrder> orderingMap = orderings.build();
return new TopNNode(node.getSourceLocation(), newNodeId, source, node.getCount(), new OrderingScheme(variables.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList())), node.getStep());
}
use of com.facebook.presto.spi.plan.OrderingScheme in project presto by prestodb.
the class SymbolMapper method map.
public OrderingScheme map(OrderingScheme orderingScheme) {
// SymbolMapper inlines symbol with multiple level reference (SymbolInliner only inline single level).
ImmutableList.Builder<VariableReferenceExpression> orderBy = ImmutableList.builder();
HashMap<VariableReferenceExpression, SortOrder> orderingMap = new HashMap<>();
for (VariableReferenceExpression variable : orderingScheme.getOrderByVariables()) {
VariableReferenceExpression translated = map(variable);
// Some variables may become duplicates after canonicalization, so we put them only once.
if (!orderingMap.containsKey(translated)) {
orderBy.add(translated);
orderingMap.put(translated, orderingScheme.getOrdering(variable));
} else if (orderingMap.get(translated) != orderingScheme.getOrdering(variable)) {
warningCollector.add(new PrestoWarning(MULTIPLE_ORDER_BY, "Multiple ORDER BY for a variable were given, only first provided will be considered"));
}
}
return new OrderingScheme(orderBy.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList()));
}
Aggregations