Search in sources :

Example 26 with ResolvedFunction

use of io.trino.metadata.ResolvedFunction in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testPatternRecognitionNodeRoundtrip.

@Test
public void testPatternRecognitionNodeRoundtrip() {
    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<PatternRecognitionNode> codec = new JsonCodecFactory(provider).jsonCodec(PatternRecognitionNode.class);
    ResolvedFunction rankFunction = createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("rank"), ImmutableList.of());
    // test remaining fields inside PatternRecognitionNode specific to pattern recognition:
    // windowFunctions, measures, commonBaseFrame, rowsPerMatch, skipToLabel, skipToPosition, initial, pattern, subsets, variableDefinitions
    PatternRecognitionNode node = new PatternRecognitionNode(new PlanNodeId("0"), new ValuesNode(new PlanNodeId("1"), 1), new Specification(ImmutableList.of(), Optional.empty()), Optional.empty(), ImmutableSet.of(), 0, ImmutableMap.of(new Symbol("rank"), new Function(rankFunction, ImmutableList.of(), new Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), false)), ImmutableMap.of(new Symbol("measure"), new Measure(new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), BOOLEAN)), Optional.of(new Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())), WINDOW, Optional.of(new IrLabel("B")), LAST, true, new IrConcatenation(ImmutableList.of(new IrLabel("A"), new IrLabel("B"), new IrLabel("C"))), ImmutableMap.of(new IrLabel("U"), ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), new IrLabel("V"), ImmutableSet.of(new IrLabel("B"), new IrLabel("C"))), ImmutableMap.of(new IrLabel("B"), new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), new IrLabel("C"), new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of())));
    PatternRecognitionNode roundtripNode = codec.fromJson(codec.toJson(node));
    assertEquals(roundtripNode.getMeasures(), node.getMeasures());
    assertEquals(roundtripNode.getRowsPerMatch(), node.getRowsPerMatch());
    assertEquals(roundtripNode.getSkipToLabel(), node.getSkipToLabel());
    assertEquals(roundtripNode.getSkipToPosition(), node.getSkipToPosition());
    assertEquals(roundtripNode.isInitial(), node.isInitial());
    assertEquals(roundtripNode.getPattern(), node.getPattern());
    assertEquals(roundtripNode.getSubsets(), node.getSubsets());
    assertEquals(roundtripNode.getVariableDefinitions(), node.getVariableDefinitions());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) Frame(io.trino.sql.planner.plan.WindowNode.Frame) ResolvedFunction(io.trino.metadata.ResolvedFunction) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) Specification(io.trino.sql.planner.plan.WindowNode.Specification) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) IrConcatenation(io.trino.sql.planner.rowpattern.ir.IrConcatenation) ResolvedFunction(io.trino.metadata.ResolvedFunction) Function(io.trino.sql.planner.plan.WindowNode.Function) Type(io.trino.spi.type.Type) TypeSignature(io.trino.spi.type.TypeSignature) 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) 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)

Example 27 with ResolvedFunction

use of io.trino.metadata.ResolvedFunction in project trino by trinodb.

the class ImplementExceptAll method apply.

@Override
public Result apply(ExceptNode node, Captures captures, Context context) {
    SetOperationNodeTranslator translator = new SetOperationNodeTranslator(context.getSession(), metadata, context.getSymbolAllocator(), context.getIdAllocator());
    SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlanForAll(node);
    // compute expected multiplicity for every row
    checkState(result.getCountSymbols().size() > 0, "ExceptNode translation result has no count symbols");
    ResolvedFunction greatest = metadata.resolveFunction(context.getSession(), QualifiedName.of("greatest"), fromTypes(BIGINT, BIGINT));
    Expression count = result.getCountSymbols().get(0).toSymbolReference();
    for (int i = 1; i < result.getCountSymbols().size(); i++) {
        count = new FunctionCall(greatest.toQualifiedName(), ImmutableList.of(new ArithmeticBinaryExpression(SUBTRACT, count, result.getCountSymbols().get(i).toSymbolReference()), new GenericLiteral("BIGINT", "0")));
    }
    // filter rows so that expected number of rows remains
    Expression removeExtraRows = new ComparisonExpression(LESS_THAN_OR_EQUAL, result.getRowNumberSymbol().toSymbolReference(), count);
    FilterNode filter = new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), removeExtraRows);
    // prune helper symbols
    ProjectNode project = new ProjectNode(context.getIdAllocator().getNextId(), filter, Assignments.identity(node.getOutputSymbols()));
    return Result.ofPlanNode(project);
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ResolvedFunction(io.trino.metadata.ResolvedFunction) FilterNode(io.trino.sql.planner.plan.FilterNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) FunctionCall(io.trino.sql.tree.FunctionCall) GenericLiteral(io.trino.sql.tree.GenericLiteral)

Example 28 with ResolvedFunction

use of io.trino.metadata.ResolvedFunction in project trino by trinodb.

the class TestPageProcessorCompiler method testNonDeterministicProject.

@Test
public void testNonDeterministicProject() {
    ResolvedFunction lessThan = functionResolution.resolveOperator(LESS_THAN, ImmutableList.of(BIGINT, BIGINT));
    CallExpression random = new CallExpression(functionResolution.resolveFunction(QualifiedName.of("random"), fromTypes(BIGINT)), singletonList(constant(10L, BIGINT)));
    InputReferenceExpression col0 = field(0, BIGINT);
    CallExpression lessThanRandomExpression = new CallExpression(lessThan, ImmutableList.of(col0, random));
    PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(lessThanRandomExpression), MAX_BATCH_SIZE).get();
    assertFalse(isDeterministic(lessThanRandomExpression));
    Page page = new Page(createLongDictionaryBlock(1, 100));
    Page outputPage = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertFalse(outputPage.getBlock(0) instanceof DictionaryBlock);
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) PageProcessor(io.trino.operator.project.PageProcessor) ResolvedFunction(io.trino.metadata.ResolvedFunction) DictionaryBlock(io.trino.spi.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(io.trino.block.BlockAssertions.createLongDictionaryBlock) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 29 with ResolvedFunction

use of io.trino.metadata.ResolvedFunction in project trino by trinodb.

the class TestPageProcessorCompiler method testNoCaching.

@Test
public void testNoCaching() {
    ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();
    ArrayType arrayType = new ArrayType(VARCHAR);
    ResolvedFunction resolvedFunction = functionResolution.resolveFunction(QualifiedName.of("concat"), fromTypes(arrayType, arrayType));
    projectionsBuilder.add(new CallExpression(resolvedFunction, ImmutableList.of(field(0, arrayType), field(1, arrayType))));
    ImmutableList<RowExpression> projections = projectionsBuilder.build();
    PageProcessor pageProcessor = compiler.compilePageProcessor(Optional.empty(), projections).get();
    PageProcessor pageProcessor2 = compiler.compilePageProcessor(Optional.empty(), projections).get();
    assertTrue(pageProcessor != pageProcessor2);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) PageProcessor(io.trino.operator.project.PageProcessor) ImmutableList(com.google.common.collect.ImmutableList) ResolvedFunction(io.trino.metadata.ResolvedFunction) RowExpression(io.trino.sql.relational.RowExpression) CallExpression(io.trino.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 30 with ResolvedFunction

use of io.trino.metadata.ResolvedFunction in project trino by trinodb.

the class PlanBuilder method aggregation.

public Aggregation aggregation(Expression expression, List<Type> inputTypes) {
    checkArgument(expression instanceof FunctionCall);
    FunctionCall aggregation = (FunctionCall) expression;
    ResolvedFunction resolvedFunction = metadata.resolveFunction(session, aggregation.getName(), TypeSignatureProvider.fromTypes(inputTypes));
    return new Aggregation(resolvedFunction, aggregation.getArguments(), aggregation.isDistinct(), aggregation.getFilter().map(Symbol::from), aggregation.getOrderBy().map(OrderingScheme::fromOrderBy), Optional.empty());
}
Also used : Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) ResolvedFunction(io.trino.metadata.ResolvedFunction) FunctionCall(io.trino.sql.tree.FunctionCall)

Aggregations

ResolvedFunction (io.trino.metadata.ResolvedFunction)51 Test (org.testng.annotations.Test)31 WindowNode (io.trino.sql.planner.plan.WindowNode)21 ImmutableList (com.google.common.collect.ImmutableList)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 Symbol (io.trino.sql.planner.Symbol)20 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)18 DEFAULT_FRAME (io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME)18 QualifiedName (io.trino.sql.tree.QualifiedName)18 Optional (java.util.Optional)18 OrderingScheme (io.trino.sql.planner.OrderingScheme)17 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)17 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)17 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)15 FunctionCall (io.trino.sql.tree.FunctionCall)14 SortOrder (io.trino.spi.connector.SortOrder)12 Type (io.trino.spi.type.Type)12 Expression (io.trino.sql.tree.Expression)11 BIGINT (io.trino.spi.type.BigintType.BIGINT)10 PlanMatchPattern.topNRanking (io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking)9