private static boolean canOptimizeWindowFunction(WindowNode node) { if (node.getWindowFunctions().size() != 1) { return false; } Symbol rowNumberSymbol = getOnlyElement(node.getWindowFunctions().entrySet()).getKey(); return isRowNumberSignature(node.getWindowFunctions().get(rowNumberSymbol).getSignature()); }
public static boolean dependsOn(WindowNode parent, WindowNode child) { return parent.getPartitionBy().stream().anyMatch(child.getCreatedSymbols()::contains) || (parent.getOrderingScheme().isPresent() && parent.getOrderingScheme().get().getOrderBy().stream().anyMatch(child.getCreatedSymbols()::contains)) || parent.getWindowFunctions().values().stream() .map(WindowNode.Function::getFunctionCall) .map(SymbolsExtractor::extractUnique) .flatMap(Collection::stream) .anyMatch(child.getCreatedSymbols()::contains); } }
@Override public Void visitWindow(WindowNode node, Void context) { visitPlan(node, context); checkWindowFunctions(node.getWindowFunctions()); return null; }
private TopNRowNumberNode convertToTopNRowNumber(WindowNode windowNode, int limit) { return new TopNRowNumberNode(idAllocator.getNextId(), windowNode.getSource(), windowNode.getSpecification(), getOnlyElement(windowNode.getWindowFunctions().keySet()), limit, false, Optional.empty()); }
@Override public PlanNode visitWindow(WindowNode node, RewriteContext<Void> context) { checkState(node.getWindowFunctions().size() == 1, "WindowFilterPushdown requires that WindowNodes contain exactly one window function"); PlanNode rewrittenSource = context.rewrite(node.getSource()); if (canReplaceWithRowNumber(node)) { return new RowNumberNode(idAllocator.getNextId(), rewrittenSource, node.getPartitionBy(), getOnlyElement(node.getWindowFunctions().keySet()), Optional.empty(), Optional.empty()); } return replaceChildren(node, ImmutableList.of(rewrittenSource)); }
printStats(indent + 2, node.getId()); for (Map.Entry<Symbol, WindowNode.Function> entry : node.getWindowFunctions().entrySet()) { FunctionCall call = entry.getValue().getFunctionCall(); String frameInfo = formatFrame(entry.getValue().getFrame());
@Override public PlanNode visitWindow(WindowNode node, RewriteContext<Context> context) { if (!node.getWindowFunctions().values().stream() .map(function -> function.getFunctionCall().getName()) .allMatch(metadata.getFunctionRegistry()::isAggregationFunction)) { return node; } // Don't need this restriction if we can prove that all order by symbols are deterministically produced if (node.getOrderingScheme().isPresent()) { return node; } // Only RANGE frame type currently supported for aggregation functions because it guarantees the // same value for each peer group. // ROWS frame type requires the ordering to be fully deterministic (e.g. deterministically sorted on all columns) if (node.getFrames().stream().map(WindowNode.Frame::getType).anyMatch(type -> type != WindowFrame.Type.RANGE)) { // TODO: extract frames of type RANGE and allow optimization on them return node; } // Lookup symbols can only be passed through if they are part of the partitioning Set<Symbol> partitionByLookupSymbols = context.get().getLookupSymbols().stream() .filter(node.getPartitionBy()::contains) .collect(toImmutableSet()); if (partitionByLookupSymbols.isEmpty()) { return node; } return context.defaultRewrite(node, new Context(partitionByLookupSymbols, context.get().getSuccess())); }
@Override protected Optional<PlanNode> manipulateAdjacentWindowNodes(WindowNode parent, WindowNode child, Context context) { if (!child.getSpecification().equals(parent.getSpecification()) || dependsOn(parent, child)) { return Optional.empty(); } ImmutableMap.Builder<Symbol, WindowNode.Function> functionsBuilder = ImmutableMap.builder(); functionsBuilder.putAll(parent.getWindowFunctions()); functionsBuilder.putAll(child.getWindowFunctions()); WindowNode mergedWindowNode = new WindowNode( parent.getId(), child.getSource(), parent.getSpecification(), functionsBuilder.build(), parent.getHashSymbol(), parent.getPrePartitionedInputs(), parent.getPreSortedOrderPrefix()); return Optional.of( restrictOutputs(context.getIdAllocator(), mergedWindowNode, ImmutableSet.copyOf(parent.getOutputSymbols())) .orElse(mergedWindowNode)); } }
for (Map.Entry<Symbol, WindowNode.Function> entry : node.getWindowFunctions().entrySet()) { Symbol symbol = entry.getKey(); WindowNode.Function function = entry.getValue();
@Override public PlanNode visitFilter(FilterNode node, RewriteContext<Void> context) { PlanNode source = context.rewrite(node.getSource()); TupleDomain<Symbol> tupleDomain = fromPredicate(metadata, session, node.getPredicate(), types).getTupleDomain(); if (source instanceof RowNumberNode) { Symbol rowNumberSymbol = ((RowNumberNode) source).getRowNumberSymbol(); OptionalInt upperBound = extractUpperBound(tupleDomain, rowNumberSymbol); if (upperBound.isPresent()) { source = mergeLimit(((RowNumberNode) source), upperBound.getAsInt()); return rewriteFilterSource(node, source, rowNumberSymbol, upperBound.getAsInt()); } } else if (source instanceof WindowNode && canOptimizeWindowFunction((WindowNode) source) && isOptimizeTopNRowNumber(session)) { WindowNode windowNode = (WindowNode) source; Symbol rowNumberSymbol = getOnlyElement(windowNode.getWindowFunctions().entrySet()).getKey(); OptionalInt upperBound = extractUpperBound(tupleDomain, rowNumberSymbol); if (upperBound.isPresent()) { source = convertToTopNRowNumber(windowNode, upperBound.getAsInt()); return rewriteFilterSource(node, source, rowNumberSymbol, upperBound.getAsInt()); } } return replaceChildren(node, ImmutableList.of(source)); }
windowNode.getWindowFunctions(), referencedOutputs::contains);
child.getNode(), node.getSpecification(), node.getWindowFunctions(), node.getHashSymbol(), prePartitionedInputs,
@Override public PlanNode visitWindow(WindowNode node, RewriteContext<Void> context) { PlanNode source = context.rewrite(node.getSource()); ImmutableMap.Builder<Symbol, WindowNode.Function> functions = ImmutableMap.builder(); for (Map.Entry<Symbol, WindowNode.Function> entry : node.getWindowFunctions().entrySet()) { Symbol symbol = entry.getKey(); FunctionCall canonicalFunctionCall = (FunctionCall) canonicalize(entry.getValue().getFunctionCall()); Signature signature = entry.getValue().getSignature(); WindowNode.Frame canonicalFrame = canonicalize(entry.getValue().getFrame()); functions.put(canonicalize(symbol), new WindowNode.Function(canonicalFunctionCall, signature, canonicalFrame)); } return new WindowNode( node.getId(), source, canonicalizeAndDistinct(node.getSpecification()), functions.build(), canonicalize(node.getHashSymbol()), canonicalize(node.getPrePartitionedInputs()), node.getPreSortedOrderPrefix()); }
@Override public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) { Optional<Symbol> result = Optional.empty(); if (!(node instanceof WindowNode)) { return result; } WindowNode windowNode = (WindowNode) node; FunctionCall expectedCall = callMaker.getExpectedValue(symbolAliases); Optional<WindowNode.Frame> expectedFrame = frameMaker.map(maker -> maker.getExpectedValue(symbolAliases)); List<Symbol> matchedOutputs = windowNode.getWindowFunctions().entrySet().stream() .filter(assignment -> expectedCall.equals(assignment.getValue().getFunctionCall()) && signature.map(assignment.getValue().getSignature()::equals).orElse(true) && expectedFrame.map(assignment.getValue().getFrame()::equals).orElse(true)) .map(Map.Entry::getKey) .collect(toImmutableList()); checkState(matchedOutputs.size() <= 1, "Ambiguous function calls in %s", windowNode); if (matchedOutputs.isEmpty()) { return Optional.empty(); } return Optional.of(matchedOutputs.get(0)); }
@Override public Void visitWindow(WindowNode node, Set<Symbol> boundSymbols) { PlanNode source = node.getSource(); source.accept(this, boundSymbols); // visit child Set<Symbol> inputs = createInputs(source, boundSymbols); checkDependencies(inputs, node.getPartitionBy(), "Invalid node. Partition by symbols (%s) not in source plan output (%s)", node.getPartitionBy(), node.getSource().getOutputSymbols()); if (node.getOrderingScheme().isPresent()) { checkDependencies( inputs, node.getOrderingScheme().get().getOrderBy(), "Invalid node. Order by symbols (%s) not in source plan output (%s)", node.getOrderingScheme().get().getOrderBy(), node.getSource().getOutputSymbols()); } ImmutableList.Builder<Symbol> bounds = ImmutableList.builder(); for (WindowNode.Frame frame : node.getFrames()) { if (frame.getStartValue().isPresent()) { bounds.add(frame.getStartValue().get()); } if (frame.getEndValue().isPresent()) { bounds.add(frame.getEndValue().get()); } } checkDependencies(inputs, bounds.build(), "Invalid node. Frame bounds (%s) not in source plan output (%s)", bounds.build(), node.getSource().getOutputSymbols()); for (WindowNode.Function function : node.getWindowFunctions().values()) { Set<Symbol> dependencies = SymbolsExtractor.extractUnique(function.getFunctionCall()); checkDependencies(inputs, dependencies, "Invalid node. Window function dependencies (%s) not in source plan output (%s)", dependencies, node.getSource().getOutputSymbols()); } return null; }
@Override public PlanWithProperties visitWindow(WindowNode node, HashComputationSet parentPreference) { if (node.getPartitionBy().isEmpty()) { return planSimpleNodeWithProperties(node, parentPreference, true); } Optional<HashComputation> hashComputation = computeHash(node.getPartitionBy()); PlanWithProperties child = planAndEnforce( node.getSource(), new HashComputationSet(hashComputation), true, parentPreference.withHashComputation(node, hashComputation)); Symbol hashSymbol = child.getRequiredHashSymbol(hashComputation.get()); return new PlanWithProperties( new WindowNode( node.getId(), child.getNode(), node.getSpecification(), node.getWindowFunctions(), Optional.of(hashSymbol), node.getPrePartitionedInputs(), node.getPreSortedOrderPrefix()), child.getHashSymbols()); }
assertEquals(actualNode.getWindowFunctions(), windowNode.getWindowFunctions()); assertEquals(actualNode.getFrames(), windowNode.getFrames()); assertEquals(actualNode.getHashSymbol(), windowNode.getHashSymbol());
private static boolean canOptimizeWindowFunction(WindowNode node) { if (node.getWindowFunctions().size() != 1) { return false; } Symbol rowNumberSymbol = getOnlyElement(node.getWindowFunctions().entrySet()).getKey(); return isRowNumberSignature(node.getSignatures().get(rowNumberSymbol)); }
@Override public Void visitWindow(WindowNode node, Void context) { // visit child node.getSource().accept(this, context); builder.addAll(node.getWindowFunctions().keySet()); return null; }
private TopNRowNumberNode convertToTopNRowNumber(WindowNode windowNode, int limit) { return new TopNRowNumberNode(idAllocator.getNextId(), windowNode.getSource(), windowNode.getPartitionBy(), windowNode.getOrderBy(), windowNode.getOrderings(), getOnlyElement(windowNode.getWindowFunctions().keySet()), limit, false, Optional.empty()); }