Search in sources :

Example 11 with NullLiteral

use of io.trino.sql.tree.NullLiteral in project trino by trinodb.

the class TestMergeProjectWithValues method testNonDeterministicValues.

@Test
public void testNonDeterministicValues() {
    FunctionCall randomFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("random"), ImmutableList.of()).toQualifiedName(), ImmutableList.of());
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("rand"), expression("rand")), p.valuesOfExpressions(ImmutableList.of(p.symbol("rand")), ImmutableList.of(new Row(ImmutableList.of(randomFunction)))))).matches(values(ImmutableList.of("rand"), ImmutableList.of(ImmutableList.of(randomFunction))));
    // ValuesNode has multiple rows
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("output"), expression("value")), p.valuesOfExpressions(ImmutableList.of(p.symbol("value")), ImmutableList.of(new Row(ImmutableList.of(new NullLiteral())), new Row(ImmutableList.of(randomFunction)), new Row(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction))))))).matches(values(ImmutableList.of("output"), ImmutableList.of(ImmutableList.of(new NullLiteral()), ImmutableList.of(randomFunction), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction)))));
    // ValuesNode has multiple non-deterministic outputs
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("-a"), p.symbol("y"), expression("b")), p.valuesOfExpressions(ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of(new Row(ImmutableList.of(new DoubleLiteral("1e0"), randomFunction)), new Row(ImmutableList.of(randomFunction, new NullLiteral())), new Row(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction), new NullLiteral())))))).matches(values(ImmutableList.of("x", "y"), ImmutableList.of(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, new DoubleLiteral("1e0")), randomFunction), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction), new NullLiteral()), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, new ArithmeticUnaryExpression(MINUS, randomFunction)), new NullLiteral()))));
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) Cast(io.trino.sql.tree.Cast) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) StringLiteral(io.trino.sql.tree.StringLiteral) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CharLiteral(io.trino.sql.tree.CharLiteral) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) FunctionCall(io.trino.sql.tree.FunctionCall) Row(io.trino.sql.tree.Row) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 12 with NullLiteral

use of io.trino.sql.tree.NullLiteral in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testExpressionAndValuePointersRoundtrip.

@Test
public void testExpressionAndValuePointersRoundtrip() {
    ObjectMapperProvider provider = new ObjectMapperProvider();
    provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
    provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser())));
    JsonCodec<ExpressionAndValuePointers> codec = new JsonCodecFactory(provider).jsonCodec(ExpressionAndValuePointers.class);
    assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()));
    assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("classifier"), new SymbolReference("x")), new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()), new ArithmeticUnaryExpression(MINUS, new SymbolReference("match_number"))), ImmutableList.of(new Symbol("classifier"), new Symbol("x"), new Symbol("match_number")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), true, false, 2, 1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a"))), ImmutableSet.of(new Symbol("classifier")), ImmutableSet.of(new Symbol("match_number"))));
}
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) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) 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) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) FunctionCall(io.trino.sql.tree.FunctionCall) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 13 with NullLiteral

use of io.trino.sql.tree.NullLiteral 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)

Example 14 with NullLiteral

use of io.trino.sql.tree.NullLiteral 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 15 with NullLiteral

use of io.trino.sql.tree.NullLiteral in project trino by trinodb.

the class TestSqlParser method testCoalesce.

@Test
public void testCoalesce() {
    assertInvalidExpression("coalesce()", "The 'coalesce' function must have at least two arguments");
    assertInvalidExpression("coalesce(5)", "The 'coalesce' function must have at least two arguments");
    assertInvalidExpression("coalesce(1, 2) filter (where true)", "FILTER not valid for 'coalesce' function");
    assertInvalidExpression("coalesce(1, 2) OVER ()", "OVER clause not valid for 'coalesce' function");
    assertExpression("coalesce(13, 42)", new CoalesceExpression(new LongLiteral("13"), new LongLiteral("42")));
    assertExpression("coalesce(6, 7, 8)", new CoalesceExpression(new LongLiteral("6"), new LongLiteral("7"), new LongLiteral("8")));
    assertExpression("coalesce(13, null)", new CoalesceExpression(new LongLiteral("13"), new NullLiteral()));
    assertExpression("coalesce(null, 13)", new CoalesceExpression(new NullLiteral(), new LongLiteral("13")));
    assertExpression("coalesce(null, null)", new CoalesceExpression(new NullLiteral(), new NullLiteral()));
}
Also used : LongLiteral(io.trino.sql.tree.LongLiteral) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.junit.jupiter.api.Test)

Aggregations

NullLiteral (io.trino.sql.tree.NullLiteral)26 Expression (io.trino.sql.tree.Expression)13 Test (org.testng.annotations.Test)13 Symbol (io.trino.sql.planner.Symbol)12 Cast (io.trino.sql.tree.Cast)11 IfExpression (io.trino.sql.tree.IfExpression)10 ImmutableList (com.google.common.collect.ImmutableList)9 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)9 ImmutableMap (com.google.common.collect.ImmutableMap)7 Type (io.trino.spi.type.Type)7 ResolvedFunction (io.trino.metadata.ResolvedFunction)6 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)6 Assignments (io.trino.sql.planner.plan.Assignments)6 LongLiteral (io.trino.sql.tree.LongLiteral)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 ProjectNode (io.trino.sql.planner.plan.ProjectNode)5 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)5 FunctionCall (io.trino.sql.tree.FunctionCall)5 Row (io.trino.sql.tree.Row)5 Optional (java.util.Optional)5