Search in sources :

Example 6 with NO_MATCH

use of io.trino.sql.planner.assertions.MatchResult.NO_MATCH 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)

Example 7 with NO_MATCH

use of io.trino.sql.planner.assertions.MatchResult.NO_MATCH in project trino by trinodb.

the class SemiJoinMatcher 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());
    SemiJoinNode semiJoinNode = (SemiJoinNode) node;
    if (!(symbolAliases.get(sourceSymbolAlias).equals(semiJoinNode.getSourceJoinSymbol().toSymbolReference()) && symbolAliases.get(filteringSymbolAlias).equals(semiJoinNode.getFilteringSourceJoinSymbol().toSymbolReference()))) {
        return NO_MATCH;
    }
    if (distributionType.isPresent() && !distributionType.equals(semiJoinNode.getDistributionType())) {
        return NO_MATCH;
    }
    if (hasDynamicFilter.isPresent()) {
        if (hasDynamicFilter.get()) {
            if (semiJoinNode.getDynamicFilterId().isEmpty()) {
                return NO_MATCH;
            }
            DynamicFilterId dynamicFilterId = semiJoinNode.getDynamicFilterId().get();
            List<DynamicFilters.Descriptor> matchingDescriptors = searchFrom(semiJoinNode.getSource()).where(FilterNode.class::isInstance).findAll().stream().flatMap(filterNode -> extractExpressions(filterNode).stream()).flatMap(expression -> extractDynamicFilters(expression).getDynamicConjuncts().stream()).filter(descriptor -> descriptor.getId().equals(dynamicFilterId)).collect(toImmutableList());
            boolean sourceSymbolsMatch = matchingDescriptors.stream().map(descriptor -> Symbol.from(descriptor.getInput())).allMatch(sourceSymbol -> symbolAliases.get(sourceSymbolAlias).equals(sourceSymbol.toSymbolReference()));
            if (!matchingDescriptors.isEmpty() && sourceSymbolsMatch) {
                return match(outputAlias, semiJoinNode.getSemiJoinOutput().toSymbolReference());
            }
            return NO_MATCH;
        }
        if (semiJoinNode.getDynamicFilterId().isPresent()) {
            return NO_MATCH;
        }
    }
    return match(outputAlias, semiJoinNode.getSemiJoinOutput().toSymbolReference());
}
Also used : Symbol(io.trino.sql.planner.Symbol) MatchResult.match(io.trino.sql.planner.assertions.MatchResult.match) PlanNodeSearcher.searchFrom(io.trino.sql.planner.optimizations.PlanNodeSearcher.searchFrom) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DynamicFilters.extractDynamicFilters(io.trino.sql.DynamicFilters.extractDynamicFilters) SemiJoinNode(io.trino.sql.planner.plan.SemiJoinNode) StatsProvider(io.trino.cost.StatsProvider) FilterNode(io.trino.sql.planner.plan.FilterNode) DynamicFilterId(io.trino.sql.planner.plan.DynamicFilterId) PlanNode(io.trino.sql.planner.plan.PlanNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) DynamicFilters(io.trino.sql.DynamicFilters) Objects.requireNonNull(java.util.Objects.requireNonNull) ExpressionExtractor.extractExpressions(io.trino.sql.planner.ExpressionExtractor.extractExpressions) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) NO_MATCH(io.trino.sql.planner.assertions.MatchResult.NO_MATCH) Session(io.trino.Session) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) FilterNode(io.trino.sql.planner.plan.FilterNode) SemiJoinNode(io.trino.sql.planner.plan.SemiJoinNode) DynamicFilterId(io.trino.sql.planner.plan.DynamicFilterId)

Example 8 with NO_MATCH

use of io.trino.sql.planner.assertions.MatchResult.NO_MATCH in project trino by trinodb.

the class TableWriterMatcher 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());
    TableWriterNode tableWriterNode = (TableWriterNode) node;
    if (!tableWriterNode.getColumnNames().equals(columnNames)) {
        return NO_MATCH;
    }
    if (!columns.stream().map(s -> Symbol.from(symbolAliases.get(s))).collect(toImmutableList()).equals(tableWriterNode.getColumns())) {
        return NO_MATCH;
    }
    return match();
}
Also used : Symbol(io.trino.sql.planner.Symbol) List(java.util.List) MatchResult.match(io.trino.sql.planner.assertions.MatchResult.match) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Metadata(io.trino.metadata.Metadata) TableWriterNode(io.trino.sql.planner.plan.TableWriterNode) NO_MATCH(io.trino.sql.planner.assertions.MatchResult.NO_MATCH) StatsProvider(io.trino.cost.StatsProvider) Session(io.trino.Session) PlanNode(io.trino.sql.planner.plan.PlanNode) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TableWriterNode(io.trino.sql.planner.plan.TableWriterNode)

Example 9 with NO_MATCH

use of io.trino.sql.planner.assertions.MatchResult.NO_MATCH in project trino by trinodb.

the class AggregationMatcher 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());
    AggregationNode aggregationNode = (AggregationNode) node;
    if (groupId.isPresent() != aggregationNode.getGroupIdSymbol().isPresent()) {
        return NO_MATCH;
    }
    if (!matches(groupingSets.getGroupingKeys(), aggregationNode.getGroupingKeys(), symbolAliases)) {
        return NO_MATCH;
    }
    if (groupingSets.getGroupingSetCount() != aggregationNode.getGroupingSetCount()) {
        return NO_MATCH;
    }
    if (!groupingSets.getGlobalGroupingSets().equals(aggregationNode.getGlobalGroupingSets())) {
        return NO_MATCH;
    }
    Set<Symbol> actualMasks = aggregationNode.getAggregations().values().stream().filter(aggregation -> aggregation.getMask().isPresent()).map(aggregation -> aggregation.getMask().get()).collect(toImmutableSet());
    Set<Symbol> expectedMasks = masks.stream().map(name -> new Symbol(symbolAliases.get(name).getName())).collect(toImmutableSet());
    if (!actualMasks.equals(expectedMasks)) {
        return NO_MATCH;
    }
    if (step != aggregationNode.getStep()) {
        return NO_MATCH;
    }
    if (!matches(preGroupedSymbols, aggregationNode.getPreGroupedSymbols(), symbolAliases)) {
        return NO_MATCH;
    }
    if (!preGroupedSymbols.isEmpty() && !aggregationNode.isStreamable()) {
        return NO_MATCH;
    }
    return match();
}
Also used : Symbol(io.trino.sql.planner.Symbol) MatchResult.match(io.trino.sql.planner.assertions.MatchResult.match) Step(io.trino.sql.planner.plan.AggregationNode.Step) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) StatsProvider(io.trino.cost.StatsProvider) PlanNode(io.trino.sql.planner.plan.PlanNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Metadata(io.trino.metadata.Metadata) AggregationNode(io.trino.sql.planner.plan.AggregationNode) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NO_MATCH(io.trino.sql.planner.assertions.MatchResult.NO_MATCH) Session(io.trino.Session) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Symbol(io.trino.sql.planner.Symbol) AggregationNode(io.trino.sql.planner.plan.AggregationNode)

Example 10 with NO_MATCH

use of io.trino.sql.planner.assertions.MatchResult.NO_MATCH in project trino by trinodb.

the class WindowMatcher 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());
    WindowNode windowNode = (WindowNode) node;
    if (!prePartitionedInputs.map(expectedInputs -> expectedInputs.stream().map(alias -> alias.toSymbol(symbolAliases)).collect(toImmutableSet()).equals(windowNode.getPrePartitionedInputs())).orElse(true)) {
        return NO_MATCH;
    }
    if (!specification.map(expectedSpecification -> expectedSpecification.getExpectedValue(symbolAliases).equals(windowNode.getSpecification())).orElse(true)) {
        return NO_MATCH;
    }
    if (!preSortedOrderPrefix.map(Integer.valueOf(windowNode.getPreSortedOrderPrefix())::equals).orElse(true)) {
        return NO_MATCH;
    }
    if (!hashSymbol.map(expectedHashSymbol -> expectedHashSymbol.map(alias -> alias.toSymbol(symbolAliases)).equals(windowNode.getHashSymbol())).orElse(true)) {
        return NO_MATCH;
    }
    /*
         * Window functions produce a symbol (the result of the function call) that we might
         * want to bind to an alias so we can reference it further up the tree. As such,
         * they need to be matched with an Alias matcher so we can bind the symbol if desired.
         */
    return match();
}
Also used : MatchResult.match(io.trino.sql.planner.assertions.MatchResult.match) ResolvedFunction(io.trino.metadata.ResolvedFunction) Set(java.util.Set) StatsProvider(io.trino.cost.StatsProvider) SortOrder(io.trino.spi.connector.SortOrder) PlanNode(io.trino.sql.planner.plan.PlanNode) Preconditions.checkState(com.google.common.base.Preconditions.checkState) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) List(java.util.List) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) NO_MATCH(io.trino.sql.planner.assertions.MatchResult.NO_MATCH) WindowNode(io.trino.sql.planner.plan.WindowNode) LinkedList(java.util.LinkedList) FunctionCall(io.trino.sql.tree.FunctionCall) Session(io.trino.Session) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) WindowNode(io.trino.sql.planner.plan.WindowNode)

Aggregations

MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)13 Preconditions.checkState (com.google.common.base.Preconditions.checkState)13 Session (io.trino.Session)13 StatsProvider (io.trino.cost.StatsProvider)13 Metadata (io.trino.metadata.Metadata)13 NO_MATCH (io.trino.sql.planner.assertions.MatchResult.NO_MATCH)13 PlanNode (io.trino.sql.planner.plan.PlanNode)13 List (java.util.List)13 Optional (java.util.Optional)11 Objects.requireNonNull (java.util.Objects.requireNonNull)10 MatchResult.match (io.trino.sql.planner.assertions.MatchResult.match)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7 Symbol (io.trino.sql.planner.Symbol)7 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)6 Set (java.util.Set)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Expression (io.trino.sql.tree.Expression)4 Map (java.util.Map)4 PlanMatchPattern.node (io.trino.sql.planner.assertions.PlanMatchPattern.node)3 ImmutableList (com.google.common.collect.ImmutableList)2