use of io.trino.sql.planner.rowpattern.ValuePointer in project trino by trinodb.
the class TestPatternRecognitionNodeSerialization method testScalarValuePointerRoundtrip.
@Test
public void testScalarValuePointerRoundtrip() {
JsonCodec<ValuePointer> codec = new JsonCodecFactory(new ObjectMapperProvider()).jsonCodec(ValuePointer.class);
assertJsonRoundTrip(codec, new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), false, false, 5, 5), new Symbol("input_symbol")));
assertJsonRoundTrip(codec, new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), true, true, 1, -1), new Symbol("input_symbol")));
}
use of io.trino.sql.planner.rowpattern.ValuePointer 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());
}
use of io.trino.sql.planner.rowpattern.ValuePointer 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());
}
use of io.trino.sql.planner.rowpattern.ValuePointer 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")));
}
Aggregations