Search in sources :

Example 1 with DecimalLiteral

use of com.facebook.presto.sql.tree.DecimalLiteral in project presto by prestodb.

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 FunctionCall(QualifiedName.of("$internal$try"), ImmutableList.of(new LambdaExpression(ImmutableList.of(), new DecimalLiteral("2")))));
    assertEquals(DesugarTryExpressionRewriter.rewrite(before), after);
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) TryExpression(com.facebook.presto.sql.tree.TryExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) BaseRuleTest(com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 2 with DecimalLiteral

use of com.facebook.presto.sql.tree.DecimalLiteral in project presto by prestodb.

the class LiteralEncoder method toExpression.

@Deprecated
public Expression toExpression(Object object, Type type, boolean typeOnly) {
    requireNonNull(type, "type is null");
    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, typeOnly);
    }
    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());
    }
    checkArgument(Primitives.wrap(type.getJavaType()).isInstance(object), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
    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 FunctionCall(QualifiedName.of("nan"), ImmutableList.of());
        }
        if (value.equals(Double.NEGATIVE_INFINITY)) {
            return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
        }
        if (value.equals(Double.POSITIVE_INFINITY)) {
            return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
        }
        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 FunctionCall(QualifiedName.of("nan"), ImmutableList.of()), StandardTypes.REAL);
        }
        if (value.equals(Float.NEGATIVE_INFINITY)) {
            return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
        }
        if (value.equals(Float.POSITIVE_INFINITY)) {
            return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), 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.getLengthSafe() == SliceUtf8.countCodePoints(value)) {
            return stringLiteral;
        }
        return new Cast(stringLiteral, type.getDisplayName(), false, typeOnly);
    }
    if (type instanceof CharType) {
        StringLiteral stringLiteral = new StringLiteral(((Slice) object).toStringUtf8());
        return new Cast(stringLiteral, type.getDisplayName(), false, typeOnly);
    }
    if (type.equals(BOOLEAN)) {
        return new BooleanLiteral(object.toString());
    }
    if (type.equals(DATE)) {
        return new GenericLiteral("DATE", new SqlDate(toIntExact((Long) object)).toString());
    }
    if (object instanceof Block) {
        SliceOutput output = new DynamicSliceOutput(toIntExact(((Block) object).getSizeInBytes()));
        BlockSerdeUtil.writeBlock(blockEncodingSerde, output, (Block) object);
        object = output.slice();
    // This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
    }
    if (type instanceof EnumType) {
        return new EnumLiteral(type.getTypeSignature().toString(), object);
    }
    Signature signature = getMagicLiteralFunctionSignature(type);
    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>)
        FunctionCall fromBase64 = new FunctionCall(QualifiedName.of("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
        return new FunctionCall(QualifiedName.of(signature.getNameSuffix()), ImmutableList.of(fromBase64));
    }
    Expression rawLiteral = toExpression(object, typeForMagicLiteral(type), typeOnly);
    return new FunctionCall(QualifiedName.of(signature.getNameSuffix()), ImmutableList.of(rawLiteral));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) VarcharType(com.facebook.presto.common.type.VarcharType) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) VarcharEnumType(com.facebook.presto.common.type.VarcharEnumType) EnumType(com.facebook.presto.common.type.EnumType) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Expression(com.facebook.presto.sql.tree.Expression) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) TypeSignature(com.facebook.presto.common.type.TypeSignature) Signature(com.facebook.presto.spi.function.Signature) DecimalType(com.facebook.presto.common.type.DecimalType) Block(com.facebook.presto.common.block.Block) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) CharType(com.facebook.presto.common.type.CharType) EnumLiteral(com.facebook.presto.sql.tree.EnumLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 3 with DecimalLiteral

use of com.facebook.presto.sql.tree.DecimalLiteral in project presto by prestodb.

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);
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Test(org.testng.annotations.Test)

Example 4 with DecimalLiteral

use of com.facebook.presto.sql.tree.DecimalLiteral in project presto by prestodb.

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");
}
Also used : DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) Test(org.testng.annotations.Test)

Aggregations

DecimalLiteral (com.facebook.presto.sql.tree.DecimalLiteral)4 Test (org.testng.annotations.Test)3 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)2 Expression (com.facebook.presto.sql.tree.Expression)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 GenericLiteral (com.facebook.presto.sql.tree.GenericLiteral)2 NullLiteral (com.facebook.presto.sql.tree.NullLiteral)2 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)2 Block (com.facebook.presto.common.block.Block)1 CharType (com.facebook.presto.common.type.CharType)1 DecimalType (com.facebook.presto.common.type.DecimalType)1 EnumType (com.facebook.presto.common.type.EnumType)1 SqlDate (com.facebook.presto.common.type.SqlDate)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 VarcharEnumType (com.facebook.presto.common.type.VarcharEnumType)1 VarcharType (com.facebook.presto.common.type.VarcharType)1 Signature (com.facebook.presto.spi.function.Signature)1 RowExpression (com.facebook.presto.spi.relation.RowExpression)1 BaseRuleTest (com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1