Search in sources :

Example 1 with AggregationValuePointer

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

the class SymbolMapper method map.

private ExpressionAndValuePointers map(ExpressionAndValuePointers expressionAndValuePointers) {
    // Map only the input symbols of ValuePointers. These are the symbols produced by the source node.
    // Other symbols present in the ExpressionAndValuePointers structure are synthetic unique symbols
    // with no outer usage or dependencies.
    ImmutableList.Builder<ValuePointer> newValuePointers = ImmutableList.builder();
    for (ValuePointer valuePointer : expressionAndValuePointers.getValuePointers()) {
        if (valuePointer instanceof ScalarValuePointer) {
            ScalarValuePointer scalarValuePointer = (ScalarValuePointer) valuePointer;
            Symbol inputSymbol = scalarValuePointer.getInputSymbol();
            if (expressionAndValuePointers.getClassifierSymbols().contains(inputSymbol) || expressionAndValuePointers.getMatchNumberSymbols().contains(inputSymbol)) {
                newValuePointers.add(scalarValuePointer);
            } else {
                newValuePointers.add(new ScalarValuePointer(scalarValuePointer.getLogicalIndexPointer(), map(inputSymbol)));
            }
        } else {
            AggregationValuePointer aggregationValuePointer = (AggregationValuePointer) valuePointer;
            List<Expression> newArguments = aggregationValuePointer.getArguments().stream().map(expression -> ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {

                @Override
                public Expression rewriteSymbolReference(SymbolReference node, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
                    if (Symbol.from(node).equals(aggregationValuePointer.getClassifierSymbol()) || Symbol.from(node).equals(aggregationValuePointer.getMatchNumberSymbol())) {
                        return node;
                    }
                    return map(node);
                }
            }, expression)).collect(toImmutableList());
            newValuePointers.add(new AggregationValuePointer(aggregationValuePointer.getFunction(), aggregationValuePointer.getSetDescriptor(), newArguments, aggregationValuePointer.getClassifierSymbol(), aggregationValuePointer.getMatchNumberSymbol()));
        }
    }
    return new ExpressionAndValuePointers(expressionAndValuePointers.getExpression(), expressionAndValuePointers.getLayout(), newValuePointers.build(), expressionAndValuePointers.getClassifierSymbols(), expressionAndValuePointers.getMatchNumberSymbols());
}
Also used : TableExecuteNode(io.trino.sql.planner.plan.TableExecuteNode) Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) SymbolAllocator(io.trino.sql.planner.SymbolAllocator) LimitNode(io.trino.sql.planner.plan.LimitNode) TableWriterNode(io.trino.sql.planner.plan.TableWriterNode) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) HashMap(java.util.HashMap) StatisticAggregations(io.trino.sql.planner.plan.StatisticAggregations) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) PartitioningScheme(io.trino.sql.planner.PartitioningScheme) Function(java.util.function.Function) PlanNode(io.trino.sql.planner.plan.PlanNode) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) RowNumberNode(io.trino.sql.planner.plan.RowNumberNode) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) AggregationNode(io.trino.sql.planner.plan.AggregationNode) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ExpressionRewriter(io.trino.sql.tree.ExpressionRewriter) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) StatisticsWriterNode(io.trino.sql.planner.plan.StatisticsWriterNode) ExpressionTreeRewriter(io.trino.sql.tree.ExpressionTreeRewriter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TopNNode(io.trino.sql.planner.plan.TopNNode) Set(java.util.Set) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) OrderingScheme(io.trino.sql.planner.OrderingScheme) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) SortOrder(io.trino.spi.connector.SortOrder) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) AggregationNode.groupingSets(io.trino.sql.planner.plan.AggregationNode.groupingSets) SymbolReference(io.trino.sql.tree.SymbolReference) GroupIdNode(io.trino.sql.planner.plan.GroupIdNode) DistinctLimitNode(io.trino.sql.planner.plan.DistinctLimitNode) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) Entry(java.util.Map.Entry) TableFinishNode(io.trino.sql.planner.plan.TableFinishNode) TopNRankingNode(io.trino.sql.planner.plan.TopNRankingNode) Expression(io.trino.sql.tree.Expression) WindowNode(io.trino.sql.planner.plan.WindowNode) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Symbol(io.trino.sql.planner.Symbol) SymbolReference(io.trino.sql.tree.SymbolReference) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)

Example 2 with AggregationValuePointer

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

the class PushDownProjectionsFromPatternRecognition method rewrite.

private static ExpressionAndValuePointers rewrite(ExpressionAndValuePointers expression, Assignments.Builder assignments, Context context) {
    ImmutableList.Builder<ValuePointer> rewrittenPointers = ImmutableList.builder();
    for (ValuePointer valuePointer : expression.getValuePointers()) {
        if (valuePointer instanceof ScalarValuePointer) {
            rewrittenPointers.add(valuePointer);
        } else {
            AggregationValuePointer aggregationPointer = (AggregationValuePointer) valuePointer;
            Set<Symbol> runtimeEvaluatedSymbols = ImmutableSet.of(aggregationPointer.getClassifierSymbol(), aggregationPointer.getMatchNumberSymbol());
            List<Type> argumentTypes = aggregationPointer.getFunction().getSignature().getArgumentTypes();
            ImmutableList.Builder<Expression> rewrittenArguments = ImmutableList.builder();
            for (int i = 0; i < aggregationPointer.getArguments().size(); i++) {
                Expression argument = aggregationPointer.getArguments().get(i);
                if (argument instanceof SymbolReference || SymbolsExtractor.extractUnique(argument).stream().anyMatch(runtimeEvaluatedSymbols::contains)) {
                    rewrittenArguments.add(argument);
                } else {
                    Symbol symbol = context.getSymbolAllocator().newSymbol(argument, argumentTypes.get(i));
                    assignments.put(symbol, argument);
                    rewrittenArguments.add(symbol.toSymbolReference());
                }
            }
            rewrittenPointers.add(new AggregationValuePointer(aggregationPointer.getFunction(), aggregationPointer.getSetDescriptor(), rewrittenArguments.build(), aggregationPointer.getClassifierSymbol(), aggregationPointer.getMatchNumberSymbol()));
        }
    }
    return new ExpressionAndValuePointers(expression.getExpression(), expression.getLayout(), rewrittenPointers.build(), expression.getClassifierSymbols(), expression.getMatchNumberSymbols());
}
Also used : ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.trino.sql.planner.Symbol) SymbolReference(io.trino.sql.tree.SymbolReference) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) Type(io.trino.spi.type.Type) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)

Example 3 with AggregationValuePointer

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

the class TestPatternRecognitionNodeSerialization method testAggregationValuePointerRoundtrip.

@Test
public void testAggregationValuePointerRoundtrip() {
    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)));
    provider.setKeyDeserializers(ImmutableMap.of(TypeSignature.class, new TypeSignatureKeyDeserializer()));
    JsonCodec<ValuePointer> codec = new JsonCodecFactory(provider).jsonCodec(ValuePointer.class);
    ResolvedFunction countFunction = createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("count"), ImmutableList.of());
    assertJsonRoundTrip(codec, new AggregationValuePointer(countFunction, new AggregatedSetDescriptor(ImmutableSet.of(), false), ImmutableList.of(), new Symbol("classifier"), new Symbol("match_number")));
    ResolvedFunction maxFunction = createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("max"), fromTypes(BIGINT));
    assertJsonRoundTrip(codec, new AggregationValuePointer(maxFunction, new AggregatedSetDescriptor(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), true), ImmutableList.of(new NullLiteral()), new Symbol("classifier"), new Symbol("match_number")));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ResolvedFunction(io.trino.metadata.ResolvedFunction) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) Type(io.trino.spi.type.Type) TypeSignature(io.trino.spi.type.TypeSignature) AggregatedSetDescriptor(io.trino.sql.planner.rowpattern.AggregatedSetDescriptor) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) TypeDeserializer(io.trino.type.TypeDeserializer) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) TypeSignatureKeyDeserializer(io.trino.type.TypeSignatureKeyDeserializer) Test(org.testng.annotations.Test)

Aggregations

Symbol (io.trino.sql.planner.Symbol)3 AggregationValuePointer (io.trino.sql.planner.rowpattern.AggregationValuePointer)3 ScalarValuePointer (io.trino.sql.planner.rowpattern.ScalarValuePointer)3 ValuePointer (io.trino.sql.planner.rowpattern.ValuePointer)3 Expression (io.trino.sql.tree.Expression)3 ImmutableList (com.google.common.collect.ImmutableList)2 Type (io.trino.spi.type.Type)2 ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)2 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)2 SymbolReference (io.trino.sql.tree.SymbolReference)2 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.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 JsonCodecFactory (io.airlift.json.JsonCodecFactory)1 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)1 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 SortOrder (io.trino.spi.connector.SortOrder)1 TypeSignature (io.trino.spi.type.TypeSignature)1 SqlParser (io.trino.sql.parser.SqlParser)1