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));
}
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));
}
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"));
}
Aggregations