Search in sources :

Example 6 with DecimalLiteral

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

the class TestDesugarTryExpressionRewriter method testTryExpressionDesugaringRewriter.

@Test
public void testTryExpressionDesugaringRewriter() {
    // 1 + try(2)
    Expression initial = new ArithmeticBinaryExpression(ADD, new DecimalLiteral("1"), new TryExpression(new DecimalLiteral("2")));
    Expression rewritten = DesugarTryExpressionRewriter.rewrite(initial, tester().getMetadata(), tester().getTypeAnalyzer(), tester().getSession(), new SymbolAllocator());
    // 1 + try_function(() -> 2)
    Expression expected = new ArithmeticBinaryExpression(ADD, new DecimalLiteral("1"), new TestingFunctionResolution().functionCallBuilder(QualifiedName.of(TryFunction.NAME)).addArgument(new FunctionType(ImmutableList.of(), createDecimalType(1)), new LambdaExpression(ImmutableList.of(), new DecimalLiteral("2"))).build());
    ExpressionVerifier verifier = new ExpressionVerifier(new SymbolAliases());
    assertTrue(verifier.process(rewritten, expected));
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TestingFunctionResolution(io.trino.metadata.TestingFunctionResolution) LambdaExpression(io.trino.sql.tree.LambdaExpression) Expression(io.trino.sql.tree.Expression) TryExpression(io.trino.sql.tree.TryExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) SymbolAliases(io.trino.sql.planner.assertions.SymbolAliases) FunctionType(io.trino.type.FunctionType) ExpressionVerifier(io.trino.sql.planner.assertions.ExpressionVerifier) DecimalLiteral(io.trino.sql.tree.DecimalLiteral) TryExpression(io.trino.sql.tree.TryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 7 with DecimalLiteral

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

the class QueryPlanner method planFrameOffset.

private FrameOffsetPlanAndSymbol planFrameOffset(PlanBuilder subPlan, Optional<Symbol> frameOffset) {
    if (frameOffset.isEmpty()) {
        return new FrameOffsetPlanAndSymbol(subPlan, Optional.empty());
    }
    Symbol offsetSymbol = frameOffset.get();
    Type offsetType = symbolAllocator.getTypes().get(offsetSymbol);
    // Append filter to validate offset values. They mustn't be negative or null.
    Expression zeroOffset = zeroOfType(offsetType);
    ResolvedFunction fail = plannerContext.getMetadata().resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
    Expression predicate = new IfExpression(new ComparisonExpression(GREATER_THAN_OR_EQUAL, offsetSymbol.toSymbolReference(), zeroOffset), TRUE_LITERAL, new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral("Window frame offset value must not be negative or null"), toSqlType(VARCHAR)))), toSqlType(BOOLEAN)));
    subPlan = subPlan.withNewRoot(new FilterNode(idAllocator.getNextId(), subPlan.getRoot(), predicate));
    if (offsetType.equals(BIGINT)) {
        return new FrameOffsetPlanAndSymbol(subPlan, Optional.of(offsetSymbol));
    }
    Expression offsetToBigint;
    if (offsetType instanceof DecimalType && !((DecimalType) offsetType).isShort()) {
        String maxBigint = Long.toString(Long.MAX_VALUE);
        int maxBigintPrecision = maxBigint.length();
        int actualPrecision = ((DecimalType) offsetType).getPrecision();
        if (actualPrecision < maxBigintPrecision) {
            offsetToBigint = new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT));
        } else if (actualPrecision > maxBigintPrecision) {
            // If the offset value exceeds max bigint, it implies that the frame bound falls beyond the partition bound.
            // In such case, the frame bound is set to the partition bound. Passing max bigint as the offset value has
            // the same effect. The offset value can be truncated to max bigint for the purpose of cast.
            offsetToBigint = new GenericLiteral("BIGINT", maxBigint);
        } else {
            offsetToBigint = new IfExpression(new ComparisonExpression(LESS_THAN_OR_EQUAL, offsetSymbol.toSymbolReference(), new DecimalLiteral(maxBigint)), new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT)), new GenericLiteral("BIGINT", maxBigint));
        }
    } else {
        offsetToBigint = new Cast(offsetSymbol.toSymbolReference(), toSqlType(BIGINT), false, typeCoercion.isTypeOnlyCoercion(offsetType, BIGINT));
    }
    Symbol coercedOffsetSymbol = symbolAllocator.newSymbol(offsetToBigint, BIGINT);
    subPlan = subPlan.withNewRoot(new ProjectNode(idAllocator.getNextId(), subPlan.getRoot(), Assignments.builder().putIdentities(subPlan.getRoot().getOutputSymbols()).put(coercedOffsetSymbol, offsetToBigint).build()));
    return new FrameOffsetPlanAndSymbol(subPlan, Optional.of(coercedOffsetSymbol));
}
Also used : Cast(io.trino.sql.tree.Cast) IfExpression(io.trino.sql.tree.IfExpression) ResolvedFunction(io.trino.metadata.ResolvedFunction) FilterNode(io.trino.sql.planner.plan.FilterNode) GenericLiteral(io.trino.sql.tree.GenericLiteral) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) RelationType(io.trino.sql.analyzer.RelationType) ExpressionAnalyzer.isNumericType(io.trino.sql.analyzer.ExpressionAnalyzer.isNumericType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) StringLiteral(io.trino.sql.tree.StringLiteral) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) LambdaExpression(io.trino.sql.tree.LambdaExpression) DecimalLiteral(io.trino.sql.tree.DecimalLiteral) DecimalType(io.trino.spi.type.DecimalType) ProjectNode(io.trino.sql.planner.plan.ProjectNode) FunctionCall(io.trino.sql.tree.FunctionCall)

Example 8 with DecimalLiteral

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

DecimalLiteral (io.trino.sql.tree.DecimalLiteral)8 LongLiteral (io.trino.sql.tree.LongLiteral)4 Test (org.testng.annotations.Test)4 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TEST_SESSION (io.trino.SessionTestUtils.TEST_SESSION)2 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)2 ResolvedFunction (io.trino.metadata.ResolvedFunction)2 SortOrder (io.trino.spi.connector.SortOrder)2 DecimalType (io.trino.spi.type.DecimalType)2 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)2 INTEGER (io.trino.spi.type.IntegerType.INTEGER)2 Type (io.trino.spi.type.Type)2 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)2 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)2 CREATED (io.trino.sql.planner.LogicalPlanner.Stage.CREATED)2 BasePlanTest (io.trino.sql.planner.assertions.BasePlanTest)2 PlanMatchPattern (io.trino.sql.planner.assertions.PlanMatchPattern)2 PlanMatchPattern.anyTree (io.trino.sql.planner.assertions.PlanMatchPattern.anyTree)2 PlanMatchPattern.expression (io.trino.sql.planner.assertions.PlanMatchPattern.expression)2