Search in sources :

Example 1 with Measure

use of io.trino.sql.planner.plan.PatternRecognitionNode.Measure 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 2 with Measure

use of io.trino.sql.planner.plan.PatternRecognitionNode.Measure 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 3 with Measure

use of io.trino.sql.planner.plan.PatternRecognitionNode.Measure 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 4 with Measure

use of io.trino.sql.planner.plan.PatternRecognitionNode.Measure in project trino by trinodb.

the class MeasureMatcher method getAssignedSymbol.

@Override
public Optional<Symbol> getAssignedSymbol(PlanNode node, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    Optional<Symbol> result = Optional.empty();
    if (!(node instanceof PatternRecognitionNode)) {
        return result;
    }
    PatternRecognitionNode patternRecognitionNode = (PatternRecognitionNode) node;
    Measure expectedMeasure = new Measure(rewrite(expression, subsets), type);
    for (Map.Entry<Symbol, Measure> assignment : patternRecognitionNode.getMeasures().entrySet()) {
        Measure actualMeasure = assignment.getValue();
        if (measuresEquivalent(actualMeasure, expectedMeasure, symbolAliases)) {
            checkState(result.isEmpty(), "Ambiguous measures in %s", patternRecognitionNode);
            result = Optional.of(assignment.getKey());
        }
    }
    return result;
}
Also used : PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Symbol(io.trino.sql.planner.Symbol) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) Map(java.util.Map)

Example 5 with Measure

use of io.trino.sql.planner.plan.PatternRecognitionNode.Measure in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testMeasureRoundtrip.

@Test
public void testMeasureRoundtrip() {
    ObjectMapperProvider provider = new ObjectMapperProvider();
    provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
    provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser()), Type.class, new TypeDeserializer(TESTING_TYPE_MANAGER)));
    JsonCodec<Measure> codec = new JsonCodecFactory(provider).jsonCodec(Measure.class);
    assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), BOOLEAN));
    assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("match_number"), new SymbolReference("x")), new GenericLiteral("BIGINT", "10"), new ArithmeticUnaryExpression(MINUS, new SymbolReference("y"))), ImmutableList.of(new Symbol("match_number"), new Symbol("x"), new Symbol("y")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_b"))), ImmutableSet.of(), ImmutableSet.of(new Symbol("match_number"))), BIGINT));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) IfExpression(io.trino.sql.tree.IfExpression) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) SymbolReference(io.trino.sql.tree.SymbolReference) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) GenericLiteral(io.trino.sql.tree.GenericLiteral) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Type(io.trino.spi.type.Type) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) TypeDeserializer(io.trino.type.TypeDeserializer) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

Measure (io.trino.sql.planner.plan.PatternRecognitionNode.Measure)9 Symbol (io.trino.sql.planner.Symbol)8 ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)6 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)6 PatternRecognitionNode (io.trino.sql.planner.plan.PatternRecognitionNode)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Expression (io.trino.sql.tree.Expression)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Type (io.trino.spi.type.Type)3 Assignments (io.trino.sql.planner.plan.Assignments)3 Function (io.trino.sql.planner.plan.WindowNode.Function)3 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 JsonCodecFactory (io.airlift.json.JsonCodecFactory)2 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)2 SqlParser (io.trino.sql.parser.SqlParser)2 SymbolsExtractor (io.trino.sql.planner.SymbolsExtractor)2 PlanNode (io.trino.sql.planner.plan.PlanNode)2 ProjectNode (io.trino.sql.planner.plan.ProjectNode)2