use of io.prestosql.sql.tree.DecimalLiteral in project hetu-core by openlookeng.
the class TestSqlParser method testNumbers.
@Test
public void testNumbers() {
assertExpression("9223372036854775807", new LongLiteral("9223372036854775807"));
assertExpression("-9223372036854775808", new LongLiteral("-9223372036854775808"));
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"));
}
use of io.prestosql.sql.tree.DecimalLiteral in project hetu-core by openlookeng.
the class TestDesugarTryExpressionRewriter method testTryExpressionDesugaringRewriter.
@Test
public void testTryExpressionDesugaringRewriter() {
// 1 + try(2)
Expression before = new ArithmeticBinaryExpression(ADD, new DecimalLiteral("1"), new TryExpression(new DecimalLiteral("2")));
// 1 + try_function(() -> 2)
Expression after = new ArithmeticBinaryExpression(ADD, new DecimalLiteral("1"), new FunctionCallBuilder(tester().getMetadata()).setName(QualifiedName.of(TryFunction.NAME)).addArgument(new FunctionType(ImmutableList.of(), createDecimalType(1)), new LambdaExpression(ImmutableList.of(), new DecimalLiteral("2"))).build());
assertEquals(DesugarTryExpressionRewriter.rewrite(before, tester().getMetadata(), tester().getTypeAnalyzer(), tester().getSession(), new PlanSymbolAllocator()), after);
}
use of io.prestosql.sql.tree.DecimalLiteral in project hetu-core by openlookeng.
the class TestScalarStatsCalculator method testLiteral.
@Test
public void testLiteral() {
assertCalculate(new GenericLiteral("TINYINT", "7")).distinctValuesCount(1.0).lowValue(7).highValue(7).nullsFraction(0.0);
assertCalculate(new GenericLiteral("SMALLINT", "8")).distinctValuesCount(1.0).lowValue(8).highValue(8).nullsFraction(0.0);
assertCalculate(new GenericLiteral("INTEGER", "9")).distinctValuesCount(1.0).lowValue(9).highValue(9).nullsFraction(0.0);
assertCalculate(new GenericLiteral("BIGINT", Long.toString(Long.MAX_VALUE))).distinctValuesCount(1.0).lowValue(Long.MAX_VALUE).highValue(Long.MAX_VALUE).nullsFraction(0.0);
assertCalculate(new DoubleLiteral("7.5")).distinctValuesCount(1.0).lowValue(7.5).highValue(7.5).nullsFraction(0.0);
assertCalculate(new DecimalLiteral("75.5")).distinctValuesCount(1.0).lowValue(75.5).highValue(75.5).nullsFraction(0.0);
assertCalculate(new StringLiteral("blah")).distinctValuesCount(1.0).lowValueUnknown().highValueUnknown().nullsFraction(0.0);
assertCalculate(new NullLiteral()).distinctValuesCount(0.0).lowValueUnknown().highValueUnknown().nullsFraction(1.0);
}
use of io.prestosql.sql.tree.DecimalLiteral in project hetu-core by openlookeng.
the class LiteralEncoder method toExpression.
public Expression toExpression(Object inputObject, Type type) {
requireNonNull(type, "type is null");
Object object = inputObject;
if (object instanceof Expression) {
return (Expression) object;
}
if (object == null) {
if (type.equals(UNKNOWN)) {
return new NullLiteral();
}
return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, true);
}
// AbstractIntType internally uses long as javaType. So specially handled for AbstractIntType types.
Class<?> wrap = Primitives.wrap(type.getJavaType());
checkArgument(wrap.isInstance(object) || (type instanceof AbstractIntType && wrap == Long.class && Integer.class.isInstance(object)), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
if (type.equals(TINYINT)) {
return new GenericLiteral("TINYINT", object.toString());
}
if (type.equals(SMALLINT)) {
return new GenericLiteral("SMALLINT", object.toString());
}
if (type.equals(INTEGER)) {
return new LongLiteral(object.toString());
}
if (type.equals(BIGINT)) {
LongLiteral expression = new LongLiteral(object.toString());
if (expression.getValue() >= Integer.MIN_VALUE && expression.getValue() <= Integer.MAX_VALUE) {
return new GenericLiteral("BIGINT", object.toString());
}
return new LongLiteral(object.toString());
}
if (type.equals(DOUBLE)) {
Double value = (Double) object;
// When changing this, don't forget about similar code for REAL below
if (value.isNaN()) {
return new FunctionCallBuilder(metadata).setName(QualifiedName.of("nan")).build();
}
if (value.equals(Double.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new FunctionCallBuilder(metadata).setName(QualifiedName.of("infinity")).build());
}
if (value.equals(Double.POSITIVE_INFINITY)) {
return new FunctionCallBuilder(metadata).setName(QualifiedName.of("infinity")).build();
}
return new DoubleLiteral(object.toString());
}
if (type.equals(REAL)) {
Float value = intBitsToFloat(((Long) object).intValue());
// WARNING for ORC predicate code as above (for double)
if (value.isNaN()) {
return new Cast(new FunctionCallBuilder(metadata).setName(QualifiedName.of("nan")).build(), StandardTypes.REAL);
}
if (value.equals(Float.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new Cast(new FunctionCallBuilder(metadata).setName(QualifiedName.of("infinity")).build(), StandardTypes.REAL));
}
if (value.equals(Float.POSITIVE_INFINITY)) {
return new Cast(new FunctionCallBuilder(metadata).setName(QualifiedName.of("infinity")).build(), StandardTypes.REAL);
}
return new GenericLiteral("REAL", value.toString());
}
if (type instanceof DecimalType) {
String string;
if (isShortDecimal(type)) {
string = Decimals.toString((long) object, ((DecimalType) type).getScale());
} else {
string = Decimals.toString((Slice) object, ((DecimalType) type).getScale());
}
return new Cast(new DecimalLiteral(string), type.getDisplayName());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
Slice value = (Slice) object;
StringLiteral stringLiteral = new StringLiteral(value.toStringUtf8());
if (!varcharType.isUnbounded() && varcharType.getBoundedLength() == SliceUtf8.countCodePoints(value)) {
return stringLiteral;
}
return new Cast(stringLiteral, type.getDisplayName(), false, true);
}
if (type instanceof CharType) {
StringLiteral stringLiteral = new StringLiteral(((Slice) object).toStringUtf8());
return new Cast(stringLiteral, type.getDisplayName(), false, true);
}
if (type.equals(BOOLEAN)) {
return new BooleanLiteral(object.toString());
}
if (type.equals(DATE)) {
return new GenericLiteral("DATE", new SqlDate(toIntExact((Long) object)).toString());
}
if (type.equals(TIMESTAMP)) {
return new GenericLiteral("TIMESTAMP", new SqlTimestamp((Long) object).toString());
}
if (object instanceof Block) {
SliceOutput output = new DynamicSliceOutput(toIntExact(((Block) object).getSizeInBytes()));
BlockSerdeUtil.writeBlock(metadata.getFunctionAndTypeManager().getBlockEncodingSerde(), output, (Block) object);
object = output.slice();
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}
Signature signature = LiteralFunction.getLiteralFunctionSignature(type);
Type argumentType = typeForLiteralFunctionArgument(type);
Expression argument;
if (object instanceof Slice) {
// HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
// able to encode it in the plan that gets sent to workers.
// We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
Slice encoded = VarbinaryFunctions.toBase64((Slice) object);
argument = new FunctionCallBuilder(metadata).setName(QualifiedName.of("from_base64")).addArgument(VARCHAR, new StringLiteral(encoded.toStringUtf8())).build();
} else {
argument = toExpression(object, argumentType);
}
return new FunctionCallBuilder(metadata).setName(QualifiedName.of(signature.getNameSuffix())).addArgument(argumentType, argument).build();
}
use of io.prestosql.sql.tree.DecimalLiteral in project hetu-core by openlookeng.
the class TestSqlParser method testDecimal.
@Test
public void testDecimal() {
assertExpression("DECIMAL '12.34'", new DecimalLiteral("12.34"));
assertExpression("DECIMAL '12.'", new DecimalLiteral("12."));
assertExpression("DECIMAL '12'", new DecimalLiteral("12"));
assertExpression("DECIMAL '.34'", new DecimalLiteral(".34"));
assertExpression("DECIMAL '+12.34'", new DecimalLiteral("+12.34"));
assertExpression("DECIMAL '+12'", new DecimalLiteral("+12"));
assertExpression("DECIMAL '-12.34'", new DecimalLiteral("-12.34"));
assertExpression("DECIMAL '-12'", new DecimalLiteral("-12"));
assertExpression("DECIMAL '+.34'", new DecimalLiteral("+.34"));
assertExpression("DECIMAL '-.34'", new DecimalLiteral("-.34"));
assertInvalidExpression("123.", "Unexpected decimal literal: 123.");
assertInvalidExpression("123.0", "Unexpected decimal literal: 123.0");
assertInvalidExpression(".5", "Unexpected decimal literal: .5");
assertInvalidExpression("123.5", "Unexpected decimal literal: 123.5");
}
Aggregations