Search in sources :

Example 6 with DoubleLiteral

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

the class TestComparisonStatsCalculator method symbolToLiteralGreaterThanStats.

@Test
public void symbolToLiteralGreaterThanStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls times range coverage (50%)
    250.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(10.0).lowValue(2.5).highValue(5.0).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range (whole range included)
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("-10.0"))).outputRowsCount(// all rows minus nulls times range coverage (100%)
    750.0).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(40.0).lowValue(-10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range (whole range excluded)
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by NDV (one value from edge is included as approximation)
    18.75).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal range out of symbol range
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls times range coverage (0%)
    0.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("leftOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (25% - heuristic)
    225.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (25% heuristic)
        12.5).lowValue(0.0).highValue(15.0).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("rightOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
    450.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
        25.0).lowValue(0.0).highValueUnknown().nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
    450.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
        25.0).lowValue(0.0).highValueUnknown().nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(GREATER_THAN, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) SymbolReference(io.trino.sql.tree.SymbolReference) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Example 7 with DoubleLiteral

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

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

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

the class TestSqlParser method testDouble.

@Test
public void testDouble() {
    assertExpression("123E7", new DoubleLiteral("123E7"));
    assertExpression("123.E7", new DoubleLiteral("123E7"));
    assertExpression("123.0E7", new DoubleLiteral("123E7"));
    assertExpression("123E+7", new DoubleLiteral("123E7"));
    assertExpression("123E-7", new DoubleLiteral("123E-7"));
    assertExpression("123.456E7", new DoubleLiteral("123.456E7"));
    assertExpression("123.456E+7", new DoubleLiteral("123.456E7"));
    assertExpression("123.456E-7", new DoubleLiteral("123.456E-7"));
    assertExpression(".4E42", new DoubleLiteral(".4E42"));
    assertExpression(".4E+42", new DoubleLiteral(".4E42"));
    assertExpression(".4E-42", new DoubleLiteral(".4E-42"));
}
Also used : DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.junit.jupiter.api.Test)

Example 10 with DoubleLiteral

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

the class TestComparisonStatsCalculator method symbolToLiteralLessThanStats.

@Test
public void symbolToLiteralLessThanStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls times range coverage (50%)
    250.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(10.0).lowValue(0.0).highValue(2.5).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range (whole range included)
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls times range coverage (100%)
    750.0).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(40.0).lowValue(-10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range (whole range excluded)
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("x"), new DoubleLiteral("-10.0"))).outputRowsCount(// all rows minus nulls divided by NDV (one value from edge is included as approximation)
    18.75).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(-10.0).highValue(-10.0).nullsFraction(0.0);
    });
    // Literal range out of symbol range
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("y"), new DoubleLiteral("-10.0"))).outputRowsCount(// all rows minus nulls times range coverage (0%)
    0.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("leftOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
    450.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
        25.0).lowValueUnknown().highValue(0.0).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("rightOpen"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (25% - heuristic)
    225.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (25% heuristic)
        12.5).lowValue(-15.0).highValue(0.0).nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls times range coverage (50% - heuristic)
    450.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(// (50% heuristic)
        25.0).lowValueUnknown().highValue(0.0).nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(LESS_THAN, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
}
Also used : ComparisonExpression(io.trino.sql.tree.ComparisonExpression) SymbolReference(io.trino.sql.tree.SymbolReference) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) Test(org.testng.annotations.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