Search in sources :

Example 1 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class MergePatternRecognitionNodes method equivalent.

private static boolean equivalent(Map<IrLabel, ExpressionAndValuePointers> parentVariableDefinitions, Map<IrLabel, ExpressionAndValuePointers> childVariableDefinitions) {
    if (!parentVariableDefinitions.keySet().equals(childVariableDefinitions.keySet())) {
        return false;
    }
    for (Map.Entry<IrLabel, ExpressionAndValuePointers> parentDefinition : parentVariableDefinitions.entrySet()) {
        IrLabel label = parentDefinition.getKey();
        ExpressionAndValuePointers parentExpression = parentDefinition.getValue();
        ExpressionAndValuePointers childExpression = childVariableDefinitions.get(label);
        if (!ExpressionAndValuePointersEquivalence.equivalent(parentExpression, childExpression)) {
            return false;
        }
    }
    return true;
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class PushDownProjectionsFromPatternRecognition method apply.

@Override
public Result apply(PatternRecognitionNode node, Captures captures, Context context) {
    Assignments.Builder assignments = Assignments.builder();
    Map<IrLabel, ExpressionAndValuePointers> rewrittenVariableDefinitions = rewriteVariableDefinitions(node.getVariableDefinitions(), assignments, context);
    Map<Symbol, Measure> rewrittenMeasureDefinitions = rewriteMeasureDefinitions(node.getMeasures(), assignments, context);
    if (assignments.build().isEmpty()) {
        return Result.empty();
    }
    assignments.putIdentities(node.getSource().getOutputSymbols());
    ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), assignments.build());
    PatternRecognitionNode patternRecognitionNode = new PatternRecognitionNode(node.getId(), projectNode, node.getSpecification(), node.getHashSymbol(), node.getPrePartitionedInputs(), node.getPreSortedOrderPrefix(), node.getWindowFunctions(), rewrittenMeasureDefinitions, node.getCommonBaseFrame(), node.getRowsPerMatch(), node.getSkipToLabel(), node.getSkipToPosition(), node.isInitial(), node.getPattern(), node.getSubsets(), rewrittenVariableDefinitions);
    return Result.ofPlanNode(restrictOutputs(context.getIdAllocator(), patternRecognitionNode, ImmutableSet.copyOf(node.getOutputSymbols())).orElse(patternRecognitionNode));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Symbol(io.trino.sql.planner.Symbol) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Assignments(io.trino.sql.planner.plan.Assignments) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) ProjectNode(io.trino.sql.planner.plan.ProjectNode)

Example 3 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class SymbolMapper method map.

public PatternRecognitionNode map(PatternRecognitionNode node, PlanNode source) {
    ImmutableMap.Builder<Symbol, WindowNode.Function> newFunctions = ImmutableMap.builder();
    node.getWindowFunctions().forEach((symbol, function) -> {
        List<Expression> newArguments = function.getArguments().stream().map(this::map).collect(toImmutableList());
        WindowNode.Frame newFrame = map(function.getFrame());
        newFunctions.put(map(symbol), new WindowNode.Function(function.getResolvedFunction(), newArguments, newFrame, function.isIgnoreNulls()));
    });
    ImmutableMap.Builder<Symbol, Measure> newMeasures = ImmutableMap.builder();
    node.getMeasures().forEach((symbol, measure) -> {
        ExpressionAndValuePointers newExpression = map(measure.getExpressionAndValuePointers());
        newMeasures.put(map(symbol), new Measure(newExpression, measure.getType()));
    });
    ImmutableMap.Builder<IrLabel, ExpressionAndValuePointers> newVariableDefinitions = ImmutableMap.builder();
    node.getVariableDefinitions().forEach((label, expression) -> newVariableDefinitions.put(label, map(expression)));
    return new PatternRecognitionNode(node.getId(), source, mapAndDistinct(node.getSpecification()), node.getHashSymbol().map(this::map), node.getPrePartitionedInputs().stream().map(this::map).collect(toImmutableSet()), node.getPreSortedOrderPrefix(), newFunctions.buildOrThrow(), newMeasures.buildOrThrow(), node.getCommonBaseFrame().map(this::map), node.getRowsPerMatch(), node.getSkipToLabel(), node.getSkipToPosition(), node.isInitial(), node.getPattern(), node.getSubsets(), newVariableDefinitions.buildOrThrow());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) WindowNode(io.trino.sql.planner.plan.WindowNode) Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Function(java.util.function.Function) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure)

Example 4 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class TestLogicalPlanner method testDoNotPlanUnreferencedRowPatternMeasures.

@Test
public void testDoNotPlanUnreferencedRowPatternMeasures() {
    // row pattern measure `label` is not referenced
    assertPlan("SELECT val OVER w " + "          FROM (VALUES (1, 90)) t(id, value) " + "          WINDOW w AS ( " + "                   ORDER BY id " + "                   MEASURES " + "                            RUNNING LAST(value) AS val, " + "                            CLASSIFIER() AS label " + "                   ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING " + "                   PATTERN (A+) " + "                   DEFINE A AS true " + "          )", output(project(patternRecognition(builder -> builder.specification(specification(ImmutableList.of(), ImmutableList.of("id"), ImmutableMap.of("id", ASC_NULLS_LAST))).addMeasure("val", "LAST(value)", INTEGER).rowsPerMatch(WINDOW).frame(windowFrame(ROWS, CURRENT_ROW, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty())).pattern(new IrQuantified(new IrLabel("A"), oneOrMore(true))).addVariableDefinition(new IrLabel("A"), "true"), values(ImmutableList.of("id", "value"), ImmutableList.of(ImmutableList.of(new LongLiteral("1"), new LongLiteral("90"))))))));
    // row pattern measure `label` is not referenced
    assertPlan("SELECT min(value) OVER w " + "          FROM (VALUES (1, 90)) t(id, value) " + "          WINDOW w AS ( " + "                   ORDER BY id " + "                   MEASURES CLASSIFIER() AS label " + "                   ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING " + "                   PATTERN (A+) " + "                   DEFINE A AS true " + "          )", output(project(patternRecognition(builder -> builder.specification(specification(ImmutableList.of(), ImmutableList.of("id"), ImmutableMap.of("id", ASC_NULLS_LAST))).addFunction("min", functionCall("min", ImmutableList.of("value"))).rowsPerMatch(WINDOW).frame(windowFrame(ROWS, CURRENT_ROW, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty())).pattern(new IrQuantified(new IrLabel("A"), oneOrMore(true))).addVariableDefinition(new IrLabel("A"), "true"), values(ImmutableList.of("id", "value"), ImmutableList.of(ImmutableList.of(new LongLiteral("1"), new LongLiteral("90"))))))));
}
Also used : CREATED(io.trino.sql.planner.LogicalPlanner.Stage.CREATED) PlanMatchPattern.aggregation(io.trino.sql.planner.assertions.PlanMatchPattern.aggregation) ROW_NUMBER(io.trino.sql.planner.plan.TopNRankingNode.RankingType.ROW_NUMBER) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) CorrelatedJoinNode(io.trino.sql.planner.plan.CorrelatedJoinNode) PlanNode(io.trino.sql.planner.plan.PlanNode) PlanMatchPattern.assignUniqueId(io.trino.sql.planner.assertions.PlanMatchPattern.assignUniqueId) LEFT(io.trino.sql.planner.plan.JoinNode.Type.LEFT) PlanMatchPattern.markDistinct(io.trino.sql.planner.assertions.PlanMatchPattern.markDistinct) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) AddLocalExchanges(io.trino.sql.planner.optimizations.AddLocalExchanges) LongLiteral(io.trino.sql.tree.LongLiteral) Slices(io.airlift.slice.Slices) Map(java.util.Map) LOCAL(io.trino.sql.planner.plan.ExchangeNode.Scope.LOCAL) TEST_SESSION(io.trino.SessionTestUtils.TEST_SESSION) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Assert.assertFalse(org.testng.Assert.assertFalse) TableScanNode(io.trino.sql.planner.plan.TableScanNode) ExplainAnalyzeNode(io.trino.sql.planner.plan.ExplainAnalyzeNode) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) Range(io.trino.spi.predicate.Range) PlanMatchPattern.topNRanking(io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking) Domain(io.trino.spi.predicate.Domain) StatisticsWriterNode(io.trino.sql.planner.plan.StatisticsWriterNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PlanMatchPattern.aliasToIndex(io.trino.sql.planner.assertions.PlanMatchPattern.aliasToIndex) SemiJoinNode(io.trino.sql.planner.plan.SemiJoinNode) QueryTemplate.queryTemplate(io.trino.tests.QueryTemplate.queryTemplate) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) ValueSet(io.trino.spi.predicate.ValueSet) PlanMatchPattern.strictConstrainedTableScan(io.trino.sql.planner.assertions.PlanMatchPattern.strictConstrainedTableScan) PlanMatchPattern.apply(io.trino.sql.planner.assertions.PlanMatchPattern.apply) ValuesNode(io.trino.sql.planner.plan.ValuesNode) TASK_CONCURRENCY(io.trino.SystemSessionProperties.TASK_CONCURRENCY) PlanMatchPattern.output(io.trino.sql.planner.assertions.PlanMatchPattern.output) REAL(io.trino.spi.type.RealType.REAL) Session(io.trino.Session) OPTIMIZE_HASH_GENERATION(io.trino.SystemSessionProperties.OPTIMIZE_HASH_GENERATION) PlanNodeSearcher.searchFrom(io.trino.sql.planner.optimizations.PlanNodeSearcher.searchFrom) PARTITIONED(io.trino.sql.planner.plan.JoinNode.DistributionType.PARTITIONED) LimitNode(io.trino.sql.planner.plan.LimitNode) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) REPLICATE(io.trino.sql.planner.plan.ExchangeNode.Type.REPLICATE) DISTRIBUTED_SORT(io.trino.SystemSessionProperties.DISTRIBUTED_SORT) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) PlanOptimizer(io.trino.sql.planner.optimizations.PlanOptimizer) ColumnHandle(io.trino.spi.connector.ColumnHandle) AggregationNode(io.trino.sql.planner.plan.AggregationNode) JOIN_REORDERING_STRATEGY(io.trino.SystemSessionProperties.JOIN_REORDERING_STRATEGY) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) MoreCollectors.toOptional(com.google.common.collect.MoreCollectors.toOptional) StringLiteral(io.trino.sql.tree.StringLiteral) MorePredicates(io.trino.util.MorePredicates) QueryTemplate(io.trino.tests.QueryTemplate) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) RANK(io.trino.sql.planner.plan.TopNRankingNode.RankingType.RANK) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) JoinDistributionType(io.trino.sql.planner.OptimizerConfig.JoinDistributionType) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) MorePredicates.isInstanceOfAny(io.trino.util.MorePredicates.isInstanceOfAny) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) ROWS(io.trino.sql.tree.WindowFrame.Type.ROWS) Domain.multipleValues(io.trino.spi.predicate.Domain.multipleValues) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) SUBQUERY_MULTIPLE_ROWS(io.trino.spi.StandardErrorCode.SUBQUERY_MULTIPLE_ROWS) PlanMatchPattern.strictTableScan(io.trino.sql.planner.assertions.PlanMatchPattern.strictTableScan) PlanMatchPattern.any(io.trino.sql.planner.assertions.PlanMatchPattern.any) CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) PlanMatchPattern.constrainedTableScan(io.trino.sql.planner.assertions.PlanMatchPattern.constrainedTableScan) REPLICATED(io.trino.sql.planner.plan.JoinNode.DistributionType.REPLICATED) FilterNode(io.trino.sql.planner.plan.FilterNode) PlanMatchPattern.limit(io.trino.sql.planner.assertions.PlanMatchPattern.limit) PlanMatchPattern.exchange(io.trino.sql.planner.assertions.PlanMatchPattern.exchange) PlanMatchPattern.equiJoinClause(io.trino.sql.planner.assertions.PlanMatchPattern.equiJoinClause) REPARTITION(io.trino.sql.planner.plan.ExchangeNode.Type.REPARTITION) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) JoinNode(io.trino.sql.planner.plan.JoinNode) INTEGER(io.trino.spi.type.IntegerType.INTEGER) FunctionCall(io.trino.sql.tree.FunctionCall) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) RowNumberSymbolMatcher(io.trino.sql.planner.assertions.RowNumberSymbolMatcher) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) RowType(io.trino.spi.type.RowType) MarkDistinctNode(io.trino.sql.planner.plan.MarkDistinctNode) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) EnforceSingleRowNode(io.trino.sql.planner.plan.EnforceSingleRowNode) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) IndexJoinNode(io.trino.sql.planner.plan.IndexJoinNode) FORCE_SINGLE_NODE_OUTPUT(io.trino.SystemSessionProperties.FORCE_SINGLE_NODE_OUTPUT) String.format(java.lang.String.format) IrQuantifier.oneOrMore(io.trino.sql.planner.rowpattern.ir.IrQuantifier.oneOrMore) GenericLiteral(io.trino.sql.tree.GenericLiteral) EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.EQUAL) List(java.util.List) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) GATHER(io.trino.sql.planner.plan.ExchangeNode.Type.GATHER) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.constrainedTableScanWithTableLayout(io.trino.sql.planner.assertions.PlanMatchPattern.constrainedTableScanWithTableLayout) FILTERING_SEMI_JOIN_TO_INNER(io.trino.SystemSessionProperties.FILTERING_SEMI_JOIN_TO_INNER) Entry(java.util.Map.Entry) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Optional(java.util.Optional) ExchangeNode(io.trino.sql.planner.plan.ExchangeNode) PlanMatchPattern.rowNumber(io.trino.sql.planner.assertions.PlanMatchPattern.rowNumber) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) PlanMatchPattern.anyNot(io.trino.sql.planner.assertions.PlanMatchPattern.anyNot) DESCENDING(io.trino.sql.tree.SortItem.Ordering.DESCENDING) PlanMatchPattern.semiJoin(io.trino.sql.planner.assertions.PlanMatchPattern.semiJoin) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) SINGLE(io.trino.sql.planner.plan.AggregationNode.Step.SINGLE) Assert.assertEquals(org.testng.Assert.assertEquals) IrQuantified(io.trino.sql.planner.rowpattern.ir.IrQuantified) OPTIMIZED(io.trino.sql.planner.LogicalPlanner.Stage.OPTIMIZED) SortNode(io.trino.sql.planner.plan.SortNode) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) Cast(io.trino.sql.tree.Cast) ImmutableList(com.google.common.collect.ImmutableList) JoinReorderingStrategy(io.trino.sql.planner.OptimizerConfig.JoinReorderingStrategy) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) DynamicFilterPattern(io.trino.sql.planner.assertions.PlanMatchPattern.DynamicFilterPattern) ProjectNode(io.trino.sql.planner.plan.ProjectNode) LAST(io.trino.sql.tree.SortItem.NullOrdering.LAST) PARTIAL(io.trino.sql.planner.plan.AggregationNode.Step.PARTIAL) PlanMatchPattern.identityProject(io.trino.sql.planner.assertions.PlanMatchPattern.identityProject) TopNNode(io.trino.sql.planner.plan.TopNNode) PlanMatchPattern.topN(io.trino.sql.planner.assertions.PlanMatchPattern.topN) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) TupleDomain(io.trino.spi.predicate.TupleDomain) PlanMatchPattern.singleGroupingSet(io.trino.sql.planner.assertions.PlanMatchPattern.singleGroupingSet) QualifiedName(io.trino.sql.tree.QualifiedName) Consumer(java.util.function.Consumer) DistributionType(io.trino.sql.planner.plan.SemiJoinNode.DistributionType) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) JOIN_DISTRIBUTION_TYPE(io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) CheckSubqueryNodesAreRewritten(io.trino.sql.planner.optimizations.CheckSubqueryNodesAreRewritten) REMOTE(io.trino.sql.planner.plan.ExchangeNode.Scope.REMOTE) DistinctLimitNode(io.trino.sql.planner.plan.DistinctLimitNode) Row(io.trino.sql.tree.Row) FINAL(io.trino.sql.planner.plan.AggregationNode.Step.FINAL) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) LongLiteral(io.trino.sql.tree.LongLiteral) IrQuantified(io.trino.sql.planner.rowpattern.ir.IrQuantified) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Example 5 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class RelationPlanner method planPatternRecognitionComponents.

public PatternRecognitionComponents planPatternRecognitionComponents(Function<Expression, Expression> expressionRewrite, List<SubsetDefinition> subsets, List<MeasureDefinition> measures, Optional<SkipTo> skipTo, Optional<PatternSearchMode> searchMode, RowPattern pattern, List<VariableDefinition> variableDefinitions) {
    // rewrite subsets
    ImmutableMap.Builder<IrLabel, Set<IrLabel>> rewrittenSubsets = ImmutableMap.builder();
    for (SubsetDefinition subsetDefinition : subsets) {
        IrLabel label = irLabel(subsetDefinition.getName());
        Set<IrLabel> elements = subsetDefinition.getIdentifiers().stream().map(RelationPlanner::irLabel).collect(toImmutableSet());
        rewrittenSubsets.put(label, elements);
    }
    // NOTE: There might be aggregate functions in measure definitions and variable definitions.
    // They are handled different than top level aggregations in a query:
    // 1. Their arguments are not pre-projected and replaced with single symbols. This is because the arguments might
    // not be eligible for pre-projection, when they contain references to CLASSIFIER() or MATCH_NUMBER() functions
    // which are evaluated at runtime. If some aggregation arguments can be pre-projected, it will be done in the
    // Optimizer.
    // 2. Their arguments do not need to be coerced by hand. Since the pattern aggregation arguments are rewritten as
    // parts of enclosing expressions, and not as standalone expressions, all necessary coercions will be applied by the
    // TranslationMap.
    // rewrite measures
    ImmutableMap.Builder<Symbol, Measure> rewrittenMeasures = ImmutableMap.builder();
    ImmutableList.Builder<Symbol> measureOutputs = ImmutableList.builder();
    for (MeasureDefinition measureDefinition : measures) {
        Type type = analysis.getType(measureDefinition.getExpression());
        Symbol symbol = symbolAllocator.newSymbol(measureDefinition.getName().getValue().toLowerCase(ENGLISH), type);
        Expression expression = expressionRewrite.apply(measureDefinition.getExpression());
        ExpressionAndValuePointers measure = LogicalIndexExtractor.rewrite(expression, rewrittenSubsets.buildOrThrow(), symbolAllocator, plannerContext.getMetadata());
        rewrittenMeasures.put(symbol, new Measure(measure, type));
        measureOutputs.add(symbol);
    }
    // rewrite pattern to IR
    IrRowPattern rewrittenPattern = RowPatternToIrRewriter.rewrite(pattern, analysis);
    // rewrite variable definitions
    ImmutableMap.Builder<IrLabel, ExpressionAndValuePointers> rewrittenVariableDefinitions = ImmutableMap.builder();
    for (VariableDefinition variableDefinition : variableDefinitions) {
        IrLabel label = irLabel(variableDefinition.getName());
        Expression expression = expressionRewrite.apply(variableDefinition.getExpression());
        ExpressionAndValuePointers definition = LogicalIndexExtractor.rewrite(expression, rewrittenSubsets.buildOrThrow(), symbolAllocator, plannerContext.getMetadata());
        rewrittenVariableDefinitions.put(label, definition);
    }
    // add `true` definition for undefined labels
    for (String label : analysis.getUndefinedLabels(pattern)) {
        rewrittenVariableDefinitions.put(irLabel(label), ExpressionAndValuePointers.TRUE);
    }
    return new PatternRecognitionComponents(rewrittenSubsets.buildOrThrow(), rewrittenMeasures.buildOrThrow(), measureOutputs.build(), skipTo.flatMap(SkipTo::getIdentifier).map(RelationPlanner::irLabel), skipTo.map(SkipTo::getPosition).orElse(PAST_LAST), searchMode.map(mode -> mode.getMode() == INITIAL).orElse(TRUE), rewrittenPattern, rewrittenVariableDefinitions.buildOrThrow());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) Set(java.util.Set) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) AggregationNode.singleGroupingSet(io.trino.sql.planner.plan.AggregationNode.singleGroupingSet) ImmutableSet(com.google.common.collect.ImmutableSet) VariableDefinition(io.trino.sql.tree.VariableDefinition) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) MeasureDefinition(io.trino.sql.tree.MeasureDefinition) SkipTo(io.trino.sql.tree.SkipTo) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) SubsetDefinition(io.trino.sql.tree.SubsetDefinition) RelationType(io.trino.sql.analyzer.RelationType) RowType(io.trino.spi.type.RowType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Type(io.trino.spi.type.Type) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure)

Aggregations

IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)22 Test (org.testng.annotations.Test)16 ImmutableMap (com.google.common.collect.ImmutableMap)15 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)15 ImmutableList (com.google.common.collect.ImmutableList)13 FunctionCall (io.trino.sql.tree.FunctionCall)13 ResolvedFunction (io.trino.metadata.ResolvedFunction)12 BIGINT (io.trino.spi.type.BigintType.BIGINT)12 WindowNode (io.trino.sql.planner.plan.WindowNode)12 Optional (java.util.Optional)12 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)11 ASC_NULLS_LAST (io.trino.spi.connector.SortOrder.ASC_NULLS_LAST)11 PlanMatchPattern.functionCall (io.trino.sql.planner.assertions.PlanMatchPattern.functionCall)11 PlanMatchPattern.patternRecognition (io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition)11 PlanMatchPattern.specification (io.trino.sql.planner.assertions.PlanMatchPattern.specification)11 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)11 PlanMatchPattern.windowFrame (io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame)11 Assignments (io.trino.sql.planner.plan.Assignments)11 CURRENT_ROW (io.trino.sql.tree.FrameBound.Type.CURRENT_ROW)11 UNBOUNDED_FOLLOWING (io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING)11