Search in sources :

Example 1 with IrRowPattern

use of io.trino.sql.planner.rowpattern.ir.IrRowPattern 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)

Example 2 with IrRowPattern

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

the class TestIrRowPatternOptimization method assertFlattened.

private void assertFlattened(IrRowPattern pattern, IrRowPattern expected) {
    IrRowPattern flattened = IrRowPatternFlattener.optimize(pattern);
    assertEquals(flattened, expected);
}
Also used : IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern)

Example 3 with IrRowPattern

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

the class TestIrRowPatternOptimization method assertOptimized.

private void assertOptimized(IrRowPattern pattern, IrRowPattern expected) {
    IrRowPattern optimized = IrPatternAlternationOptimizer.optimize(pattern);
    assertEquals(optimized, expected);
}
Also used : IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern)

Example 4 with IrRowPattern

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

the class RowPatternToIrRewriter method visitQuantifiedPattern.

@Override
protected IrRowPattern visitQuantifiedPattern(QuantifiedPattern node, Void context) {
    IrRowPattern pattern = process(node.getPattern());
    IrQuantifier quantifier = rewritePatternQuantifier(node.getPatternQuantifier());
    return new IrQuantified(pattern, quantifier);
}
Also used : IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) IrQuantifier(io.trino.sql.planner.rowpattern.ir.IrQuantifier) IrQuantified(io.trino.sql.planner.rowpattern.ir.IrQuantified)

Example 5 with IrRowPattern

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

the class TestRowPatternSerialization method testRowPatternRoundtrip.

@Test
public void testRowPatternRoundtrip() {
    JsonCodec<IrRowPattern> codec = new JsonCodecFactory(new ObjectMapperProvider()).jsonCodec(IrRowPattern.class);
    assertJsonRoundTrip(codec, start());
    assertJsonRoundTrip(codec, end());
    assertJsonRoundTrip(codec, empty());
    assertJsonRoundTrip(codec, excluded(empty()));
    assertJsonRoundTrip(codec, label("name"));
    assertJsonRoundTrip(codec, label(""));
    assertJsonRoundTrip(codec, label("^"));
    assertJsonRoundTrip(codec, label("$"));
    assertJsonRoundTrip(codec, alternation(empty(), empty(), empty()));
    assertJsonRoundTrip(codec, concatenation(empty(), empty(), empty()));
    assertJsonRoundTrip(codec, permutation(empty(), empty(), empty()));
    assertJsonRoundTrip(codec, starQuantified(empty(), true));
    assertJsonRoundTrip(codec, concatenation(alternation(starQuantified(start(), true), plusQuantified(end(), false), questionMarkQuantified(empty(), true)), concatenation(excluded(rangeQuantified(empty(), 0, Optional.empty(), true)), rangeQuantified(label("name_0"), 5, Optional.empty(), false), label("name_1")), permutation(alternation(label("name_2"), rangeQuantified(label("name_3"), 0, Optional.of(5), true)), rangeQuantified(concatenation(label("name_4"), label("name_5")), 5, Optional.of(10), false))));
}
Also used : IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) JsonCodecFactory(io.airlift.json.JsonCodecFactory) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) Test(org.testng.annotations.Test)

Aggregations

IrRowPattern (io.trino.sql.planner.rowpattern.ir.IrRowPattern)6 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 JsonCodecFactory (io.airlift.json.JsonCodecFactory)1 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)1 RowType (io.trino.spi.type.RowType)1 Type (io.trino.spi.type.Type)1 RelationType (io.trino.sql.analyzer.RelationType)1 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)1 AggregationNode.singleGroupingSet (io.trino.sql.planner.plan.AggregationNode.singleGroupingSet)1 Measure (io.trino.sql.planner.plan.PatternRecognitionNode.Measure)1 ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)1 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)1 IrQuantified (io.trino.sql.planner.rowpattern.ir.IrQuantified)1 IrQuantifier (io.trino.sql.planner.rowpattern.ir.IrQuantifier)1 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)1