Search in sources :

Example 21 with StringLiteral

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

the class TestSqlParser method testCreateMaterializedView.

@Test
public void testCreateMaterializedView() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    Optional<NodeLocation> location = Optional.empty();
    assertStatement("CREATE MATERIALIZED VIEW a AS SELECT * FROM t", new CreateMaterializedView(location, QualifiedName.of("a"), query, false, false, new ArrayList<>(), Optional.empty()));
    Query query2 = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("catalog2", "schema2", "tab")));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, new ArrayList<>(), Optional.of("A simple materialized view")));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, new ArrayList<>(), Optional.of("A simple materialized view")));
    List<Property> properties = ImmutableList.of(new Property(new Identifier("partitioned_by"), new ArrayConstructor(ImmutableList.of(new StringLiteral("dateint")))));
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A simple materialized view'" + "WITH (partitioned_by = ARRAY ['dateint'])" + " AS SELECT * FROM catalog2.schema2.tab", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query2, true, false, properties, Optional.of("A simple materialized view")));
    Query query3 = new Query(Optional.of(new With(false, ImmutableList.of(new WithQuery(identifier("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("x"))), Optional.of(ImmutableList.of(identifier("t"), identifier("u")))), new WithQuery(identifier("b"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("a"))), Optional.empty())))), new Table(QualifiedName.of("b")), Optional.empty(), Optional.empty(), Optional.empty());
    assertStatement("CREATE OR REPLACE MATERIALIZED VIEW catalog.schema.matview COMMENT 'A partitioned materialized view' " + "WITH (partitioned_by = ARRAY ['dateint'])" + " AS WITH a (t, u) AS (SELECT * FROM x), b AS (SELECT * FROM a) TABLE b", new CreateMaterializedView(location, QualifiedName.of("catalog", "schema", "matview"), query3, true, false, properties, Optional.of("A partitioned materialized view")));
}
Also used : CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) ArrayList(java.util.ArrayList) AllColumns(io.trino.sql.tree.AllColumns) With(io.trino.sql.tree.With) CreateMaterializedView(io.trino.sql.tree.CreateMaterializedView) QueryUtil.quotedIdentifier(io.trino.sql.QueryUtil.quotedIdentifier) Identifier(io.trino.sql.tree.Identifier) NodeLocation(io.trino.sql.tree.NodeLocation) StringLiteral(io.trino.sql.tree.StringLiteral) WithQuery(io.trino.sql.tree.WithQuery) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) Property(io.trino.sql.tree.Property) Test(org.junit.jupiter.api.Test)

Example 22 with StringLiteral

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

the class TestSqlParser method testValues.

@Test
public void testValues() {
    Query valuesQuery = query(values(row(new StringLiteral("a"), new LongLiteral("1"), new DoubleLiteral("2.2")), row(new StringLiteral("b"), new LongLiteral("2"), new DoubleLiteral("3.3"))));
    assertStatement("VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0)", valuesQuery);
    assertStatement("SELECT * FROM (VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0))", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery)));
}
Also used : QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) AllColumns(io.trino.sql.tree.AllColumns) Test(org.junit.jupiter.api.Test)

Example 23 with StringLiteral

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

the class TestSqlParser method testArrayConstructor.

@Test
public void testArrayConstructor() {
    assertExpression("ARRAY []", new ArrayConstructor(ImmutableList.of()));
    assertExpression("ARRAY [1, 2]", new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))));
    assertExpression("ARRAY [1e0, 2.5e0]", new ArrayConstructor(ImmutableList.of(new DoubleLiteral("1.0"), new DoubleLiteral("2.5"))));
    assertExpression("ARRAY ['hi']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"))));
    assertExpression("ARRAY ['hi', 'hello']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"), new StringLiteral("hello"))));
}
Also used : StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.junit.jupiter.api.Test)

Example 24 with StringLiteral

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

the class TestComparisonStatsCalculator method symbolToLiteralEqualStats.

@Test
public void symbolToLiteralEqualStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    25.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.75).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal out of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    0.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(-2.5).highValue(-2.5).nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(0.0).highValue(0.0).nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
    // Column with values not representable as double (unknown range)
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("varchar"), new StringLiteral("blah"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("varchar", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(NEGATIVE_INFINITY).highValue(POSITIVE_INFINITY).nullsFraction(0.0);
    });
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) StringLiteral(io.trino.sql.tree.StringLiteral) SymbolReference(io.trino.sql.tree.SymbolReference) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Example 25 with StringLiteral

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

the class TestMergeProjectWithValues method testFailingExpression.

@Test
public void testFailingExpression() {
    FunctionCall failFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("fail"), fromTypes(VARCHAR)).toQualifiedName(), ImmutableList.of(new StringLiteral("message")));
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), failFunction), p.valuesOfExpressions(ImmutableList.of(p.symbol("a")), ImmutableList.of(new Row(ImmutableList.of(new LongLiteral("1"))))))).matches(values(ImmutableList.of("x"), ImmutableList.of(ImmutableList.of(failFunction))));
}
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) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) Row(io.trino.sql.tree.Row) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

StringLiteral (io.trino.sql.tree.StringLiteral)56 FunctionCall (io.trino.sql.tree.FunctionCall)25 LongLiteral (io.trino.sql.tree.LongLiteral)24 Test (org.junit.jupiter.api.Test)24 Test (org.testng.annotations.Test)19 Identifier (io.trino.sql.tree.Identifier)17 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)13 Expression (io.trino.sql.tree.Expression)12 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)9 AllColumns (io.trino.sql.tree.AllColumns)9 BooleanLiteral (io.trino.sql.tree.BooleanLiteral)9 Property (io.trino.sql.tree.Property)9 Cast (io.trino.sql.tree.Cast)8 SymbolReference (io.trino.sql.tree.SymbolReference)8 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)7 QualifiedName (io.trino.sql.tree.QualifiedName)7 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)6 CreateTable (io.trino.sql.tree.CreateTable)6 DoubleLiteral (io.trino.sql.tree.DoubleLiteral)6 GenericLiteral (io.trino.sql.tree.GenericLiteral)6