Search in sources :

Example 11 with DoubleLiteral

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

the class TestComparisonStatsCalculator method symbolToLiteralNotEqualStats.

@Test
public void symbolToLiteralNotEqualStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    475.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(19.0).lowValue(0.0).highValue(5.0).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    731.25).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(39.0).lowValue(-10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal out of symbol range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls
    500.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(19.0).lowValue(0.0).highValue(5.0).nullsFraction(0.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls multiplied by ((distinct values - 1) / distinct values)
    882.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValue(15.0).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValue(-15.0).highValueUnknown().nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(NOT_EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValueUnknown().nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(NOT_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(NOT_EQUAL, new SymbolReference("varchar"), new StringLiteral("blah"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    882.0).symbolStats("varchar", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(49.0).lowValueUnknown().highValueUnknown().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 12 with DoubleLiteral

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

the class TestDereferencePushDown method testDereferencePushdownJoin.

@Test
public void testDereferencePushdownJoin() {
    // dereference pushdown + constant folding
    assertPlan("WITH t(msg) AS (VALUES ROW(CAST(ROW(1, 2.0) AS ROW(x BIGINT, y DOUBLE))))" + "SELECT b.msg.x " + "FROM t a, t b " + "WHERE a.msg.y = b.msg.y", output(project(ImmutableMap.of("b_x", expression("b_x")), filter("a_y = b_y", values(ImmutableList.of("b_x", "b_y", "a_y"), ImmutableList.of(ImmutableList.of(new GenericLiteral("BIGINT", "1"), new DoubleLiteral("2e0"), new DoubleLiteral("2e0"))))))));
    assertPlan("WITH t(msg) AS (VALUES ROW(CAST(ROW(1, 2.0) AS ROW(x BIGINT, y DOUBLE))))" + "SELECT a.msg.y " + "FROM t a JOIN t b ON a.msg.y = b.msg.y " + "WHERE a.msg.x > BIGINT '5'", output(ImmutableList.of("a_y"), join(INNER, ImmutableList.of(), values("a_y"), values())));
    assertPlan("WITH t(msg) AS (VALUES ROW(CAST(ROW(1, 2.0) AS ROW(x BIGINT, y DOUBLE))))" + "SELECT b.msg.x " + "FROM t a JOIN t b ON a.msg.y = b.msg.y " + "WHERE a.msg.x + b.msg.x < BIGINT '10'", output(ImmutableList.of("b_x"), join(INNER, ImmutableList.of(), project(filter("a_y = 2e0", values(ImmutableList.of("a_y"), ImmutableList.of(ImmutableList.of(new DoubleLiteral("2e0")))))), project(filter("b_y = 2e0", values(ImmutableList.of("b_x", "b_y"), ImmutableList.of(ImmutableList.of(new GenericLiteral("BIGINT", "1"), new DoubleLiteral("2e0")))))))));
}
Also used : DoubleLiteral(io.trino.sql.tree.DoubleLiteral) GenericLiteral(io.trino.sql.tree.GenericLiteral) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Example 13 with DoubleLiteral

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

the class TestSqlParser method testNumbers.

@Test
public void testNumbers() {
    assertExpression("9223372036854775807", new LongLiteral("9223372036854775807"));
    assertInvalidExpression("9223372036854775808", "Invalid numeric literal: 9223372036854775808");
    assertExpression("-9223372036854775808", new LongLiteral("-9223372036854775808"));
    assertInvalidExpression("-9223372036854775809", "Invalid numeric literal: -9223372036854775809");
    assertExpression("1E5", new DoubleLiteral("1E5"));
    assertExpression("1E-5", new DoubleLiteral("1E-5"));
    assertExpression(".1E5", new DoubleLiteral(".1E5"));
    assertExpression(".1E-5", new DoubleLiteral(".1E-5"));
    assertExpression("1.1E5", new DoubleLiteral("1.1E5"));
    assertExpression("1.1E-5", new DoubleLiteral("1.1E-5"));
    assertExpression("-1E5", new DoubleLiteral("-1E5"));
    assertExpression("-1E-5", new DoubleLiteral("-1E-5"));
    assertExpression("-.1E5", new DoubleLiteral("-.1E5"));
    assertExpression("-.1E-5", new DoubleLiteral("-.1E-5"));
    assertExpression("-1.1E5", new DoubleLiteral("-1.1E5"));
    assertExpression("-1.1E-5", new DoubleLiteral("-1.1E-5"));
    assertExpression(".1", new DecimalLiteral(".1"));
    assertExpression("1.2", new DecimalLiteral("1.2"));
    assertExpression("-1.2", new DecimalLiteral("-1.2"));
}
Also used : LongLiteral(io.trino.sql.tree.LongLiteral) DecimalLiteral(io.trino.sql.tree.DecimalLiteral) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.junit.jupiter.api.Test)

Aggregations

DoubleLiteral (io.trino.sql.tree.DoubleLiteral)13 Test (org.testng.annotations.Test)8 StringLiteral (io.trino.sql.tree.StringLiteral)7 SymbolReference (io.trino.sql.tree.SymbolReference)6 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)5 LongLiteral (io.trino.sql.tree.LongLiteral)5 Test (org.junit.jupiter.api.Test)4 DecimalLiteral (io.trino.sql.tree.DecimalLiteral)3 GenericLiteral (io.trino.sql.tree.GenericLiteral)3 NullLiteral (io.trino.sql.tree.NullLiteral)3 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)2 Symbol (io.trino.sql.planner.Symbol)2 PlanBuilder.expression (io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression)2 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)2 BooleanLiteral (io.trino.sql.tree.BooleanLiteral)2 Cast (io.trino.sql.tree.Cast)2 QualifiedName (io.trino.sql.tree.QualifiedName)2 ImmutableList (com.google.common.collect.ImmutableList)1 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)1 Slice (io.airlift.slice.Slice)1