Search in sources :

Example 1 with SymbolsExtractor

use of io.trino.sql.planner.SymbolsExtractor in project trino by trinodb.

the class MergePatternRecognitionNodes method extractPrerequisites.

/**
 * Extract project assignments producing symbols used by the PatternRecognitionNode's
 * window functions and measures. Exclude identity assignments.
 */
private static Assignments extractPrerequisites(PatternRecognitionNode node, ProjectNode project) {
    Assignments assignments = project.getAssignments();
    ImmutableSet.Builder<Symbol> inputsBuilder = ImmutableSet.builder();
    node.getWindowFunctions().values().stream().map(SymbolsExtractor::extractAll).forEach(inputsBuilder::addAll);
    node.getMeasures().values().stream().map(Measure::getExpressionAndValuePointers).map(ExpressionAndValuePointers::getInputSymbols).forEach(inputsBuilder::addAll);
    Set<Symbol> inputs = inputsBuilder.build();
    return assignments.filter(symbol -> !assignments.isIdentity(symbol)).filter(inputs::contains);
}
Also used : Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) Capture.newCapture(io.trino.matching.Capture.newCapture) PlanNode(io.trino.sql.planner.plan.PlanNode) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Rule(io.trino.sql.planner.iterative.Rule) SymbolsExtractor(io.trino.sql.planner.SymbolsExtractor) ProjectNode(io.trino.sql.planner.plan.ProjectNode) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Symbol(io.trino.sql.planner.Symbol) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ExpressionAndValuePointersEquivalence(io.trino.sql.planner.rowpattern.ExpressionAndValuePointersEquivalence) Collection(java.util.Collection) Assignments(io.trino.sql.planner.plan.Assignments) Function(io.trino.sql.planner.plan.WindowNode.Function) Set(java.util.Set) Streams(com.google.common.collect.Streams) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Capture(io.trino.matching.Capture) Patterns.patternRecognition(io.trino.sql.planner.plan.Patterns.patternRecognition) Pattern(io.trino.matching.Pattern) Patterns.source(io.trino.sql.planner.plan.Patterns.source) Captures(io.trino.matching.Captures) Util.restrictOutputs(io.trino.sql.planner.iterative.rule.Util.restrictOutputs) Patterns.project(io.trino.sql.planner.plan.Patterns.project) ImmutableSet(com.google.common.collect.ImmutableSet) Symbol(io.trino.sql.planner.Symbol) Assignments(io.trino.sql.planner.plan.Assignments) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure)

Example 2 with SymbolsExtractor

use of io.trino.sql.planner.SymbolsExtractor in project trino by trinodb.

the class MergePatternRecognitionNodes method dependsOnSourceCreatedOutputs.

/**
 * Check if parent node uses output symbols created by the child node (that is, the output symbols
 * of child node's window functions and measures), with an intervening projection between the parent
 * and child nodes. Only searches for dependencies in the window functions and measures of the parent
 * node. Other properties of the parent node, such as specification and frame, are supposed to be
 * identical to corresponding properties of the child node, as checked in the
 * `patternRecognitionSpecificationsMatch` call. As such, they cannot use symbols created by the child.
 */
private static boolean dependsOnSourceCreatedOutputs(PatternRecognitionNode parent, ProjectNode project, PatternRecognitionNode child) {
    Set<Symbol> sourceCreatedOutputs = child.getCreatedSymbols();
    Assignments assignments = project.getAssignments();
    ImmutableSet.Builder<Symbol> parentInputs = ImmutableSet.builder();
    parent.getWindowFunctions().values().stream().map(SymbolsExtractor::extractAll).forEach(parentInputs::addAll);
    parent.getMeasures().values().stream().map(Measure::getExpressionAndValuePointers).map(ExpressionAndValuePointers::getInputSymbols).forEach(parentInputs::addAll);
    return parentInputs.build().stream().map(assignments::get).map(SymbolsExtractor::extractAll).flatMap(Collection::stream).anyMatch(sourceCreatedOutputs::contains);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Symbol(io.trino.sql.planner.Symbol) Assignments(io.trino.sql.planner.plan.Assignments) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) SymbolsExtractor(io.trino.sql.planner.SymbolsExtractor)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)2 Symbol (io.trino.sql.planner.Symbol)2 SymbolsExtractor (io.trino.sql.planner.SymbolsExtractor)2 Assignments (io.trino.sql.planner.plan.Assignments)2 Measure (io.trino.sql.planner.plan.PatternRecognitionNode.Measure)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Streams (com.google.common.collect.Streams)1 Capture (io.trino.matching.Capture)1 Capture.newCapture (io.trino.matching.Capture.newCapture)1 Captures (io.trino.matching.Captures)1 Pattern (io.trino.matching.Pattern)1 Rule (io.trino.sql.planner.iterative.Rule)1 Util.restrictOutputs (io.trino.sql.planner.iterative.rule.Util.restrictOutputs)1 PatternRecognitionNode (io.trino.sql.planner.plan.PatternRecognitionNode)1 Patterns.patternRecognition (io.trino.sql.planner.plan.Patterns.patternRecognition)1 Patterns.project (io.trino.sql.planner.plan.Patterns.project)1 Patterns.source (io.trino.sql.planner.plan.Patterns.source)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 ProjectNode (io.trino.sql.planner.plan.ProjectNode)1