Search in sources :

Example 1 with RowsPerMatch

use of io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch in project trino by trinodb.

the class PatternRecognitionMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    PatternRecognitionNode patternRecognitionNode = (PatternRecognitionNode) node;
    boolean specificationMatches = specification.map(expected -> expected.getExpectedValue(symbolAliases).equals(patternRecognitionNode.getSpecification())).orElse(true);
    if (!specificationMatches) {
        return NO_MATCH;
    }
    if (frame.isPresent()) {
        if (patternRecognitionNode.getCommonBaseFrame().isEmpty()) {
            return NO_MATCH;
        }
        if (!frame.get().getExpectedValue(symbolAliases).equals(patternRecognitionNode.getCommonBaseFrame().get())) {
            return NO_MATCH;
        }
    }
    if (rowsPerMatch != patternRecognitionNode.getRowsPerMatch()) {
        return NO_MATCH;
    }
    if (!skipToLabel.equals(patternRecognitionNode.getSkipToLabel())) {
        return NO_MATCH;
    }
    if (skipToPosition != patternRecognitionNode.getSkipToPosition()) {
        return NO_MATCH;
    }
    if (initial != patternRecognitionNode.isInitial()) {
        return NO_MATCH;
    }
    if (!pattern.equals(patternRecognitionNode.getPattern())) {
        return NO_MATCH;
    }
    if (!subsets.equals(patternRecognitionNode.getSubsets())) {
        return NO_MATCH;
    }
    if (variableDefinitions.size() != patternRecognitionNode.getVariableDefinitions().size()) {
        return NO_MATCH;
    }
    for (Map.Entry<IrLabel, ExpressionAndValuePointers> entry : variableDefinitions.entrySet()) {
        IrLabel name = entry.getKey();
        ExpressionAndValuePointers actual = patternRecognitionNode.getVariableDefinitions().get(name);
        if (actual == null) {
            return NO_MATCH;
        }
        ExpressionAndValuePointers expected = entry.getValue();
        ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
        if (!ExpressionAndValuePointersEquivalence.equivalent(actual, expected, (actualSymbol, expectedSymbol) -> verifier.process(actualSymbol.toSymbolReference(), expectedSymbol.toSymbolReference()))) {
            return NO_MATCH;
        }
    }
    return match();
}
Also used : SkipTo(io.trino.sql.tree.SkipTo) Type(io.trino.spi.type.Type) HashMap(java.util.HashMap) PlanNode(io.trino.sql.planner.plan.PlanNode) ONE(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ONE) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) LinkedList(java.util.LinkedList) FunctionCall(io.trino.sql.tree.FunctionCall) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) PatternRecognitionExpressionRewriter.rewrite(io.trino.sql.planner.assertions.PatternRecognitionExpressionRewriter.rewrite) RowsPerMatch(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch) MatchResult.match(io.trino.sql.planner.assertions.MatchResult.match) ImmutableMap(com.google.common.collect.ImmutableMap) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ExpressionAndValuePointersEquivalence(io.trino.sql.planner.rowpattern.ExpressionAndValuePointersEquivalence) Set(java.util.Set) PAST_LAST(io.trino.sql.tree.SkipTo.Position.PAST_LAST) StatsProvider(io.trino.cost.StatsProvider) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) AbstractMap(java.util.AbstractMap) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Metadata(io.trino.metadata.Metadata) IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) Optional(java.util.Optional) NO_MATCH(io.trino.sql.planner.assertions.MatchResult.NO_MATCH) Expression(io.trino.sql.tree.Expression) WindowNode(io.trino.sql.planner.plan.WindowNode) Session(io.trino.Session) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractMap(java.util.AbstractMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Aggregations

MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 Session (io.trino.Session)1 StatsProvider (io.trino.cost.StatsProvider)1 Metadata (io.trino.metadata.Metadata)1 Type (io.trino.spi.type.Type)1 NO_MATCH (io.trino.sql.planner.assertions.MatchResult.NO_MATCH)1 MatchResult.match (io.trino.sql.planner.assertions.MatchResult.match)1 PatternRecognitionExpressionRewriter.rewrite (io.trino.sql.planner.assertions.PatternRecognitionExpressionRewriter.rewrite)1 PlanMatchPattern.node (io.trino.sql.planner.assertions.PlanMatchPattern.node)1 PatternRecognitionNode (io.trino.sql.planner.plan.PatternRecognitionNode)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 WindowNode (io.trino.sql.planner.plan.WindowNode)1 ExpressionAndValuePointersEquivalence (io.trino.sql.planner.rowpattern.ExpressionAndValuePointersEquivalence)1 ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)1 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)1 IrRowPattern (io.trino.sql.planner.rowpattern.ir.IrRowPattern)1 Expression (io.trino.sql.tree.Expression)1