Search in sources :

Example 6 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class TestExpressionCompiler method smokedTest.

@Test
public void smokedTest() throws Exception {
    assertExecute("cast(true as boolean)", BOOLEAN, true);
    assertExecute("true", BOOLEAN, true);
    assertExecute("false", BOOLEAN, false);
    assertExecute("42", INTEGER, 42);
    assertExecute("'foo'", createVarcharType(3), "foo");
    assertExecute("4.2E0", DOUBLE, 4.2);
    assertExecute("10000000000 + 1", BIGINT, 10000000001L);
    assertExecute("4.2", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
    assertExecute("DECIMAL '4.2'", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
    assertExecute("X' 1 f'", VARBINARY, new SqlVarbinary(Slices.wrappedBuffer((byte) 0x1f).getBytes()));
    assertExecute("X' '", VARBINARY, new SqlVarbinary(new byte[0]));
    assertExecute("bound_integer", INTEGER, 1234);
    assertExecute("bound_long", BIGINT, 1234L);
    assertExecute("bound_string", VARCHAR, "hello");
    assertExecute("bound_double", DOUBLE, 12.34);
    assertExecute("bound_boolean", BOOLEAN, true);
    assertExecute("bound_timestamp", BIGINT, new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis());
    assertExecute("bound_pattern", VARCHAR, "%el%");
    assertExecute("bound_null_string", VARCHAR, null);
    assertExecute("bound_timestamp_with_timezone", TIMESTAMP_WITH_TIME_ZONE, new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z")));
    assertExecute("bound_binary_literal", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xab }));
    // todo enable when null output type is supported
    // assertExecute("null", null);
    Futures.allAsList(futures).get();
}
Also used : SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 7 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class KuduPageSink method appendColumn.

private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
    Block block = page.getBlock(channel);
    Type type = columnTypes.get(destChannel);
    if (block.isNull(position)) {
        row.setNull(destChannel);
    } else if (TIMESTAMP.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position) * 1000);
    } else if (REAL.equals(type)) {
        row.addFloat(destChannel, intBitsToFloat((int) type.getLong(block, position)));
    } else if (BIGINT.equals(type)) {
        row.addLong(destChannel, type.getLong(block, position));
    } else if (INTEGER.equals(type)) {
        row.addInt(destChannel, (int) type.getLong(block, position));
    } else if (SMALLINT.equals(type)) {
        row.addShort(destChannel, (short) type.getLong(block, position));
    } else if (TINYINT.equals(type)) {
        row.addByte(destChannel, (byte) type.getLong(block, position));
    } else if (BOOLEAN.equals(type)) {
        row.addBoolean(destChannel, type.getBoolean(block, position));
    } else if (DOUBLE.equals(type)) {
        row.addDouble(destChannel, type.getDouble(block, position));
    } else if (isVarcharType(type)) {
        Type originalType = originalColumnTypes.get(destChannel);
        if (DATE.equals(originalType)) {
            SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession.getSqlFunctionProperties(), block, position);
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
            byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
            row.addStringUtf8(destChannel, bytes);
        } else {
            row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
        }
    } else if (VARBINARY.equals(type)) {
        row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
    } else if (type instanceof DecimalType) {
        SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession.getSqlFunctionProperties(), block, position);
        row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
    } else {
        throw new UnsupportedOperationException("Type is not supported: " + type);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DecimalType(com.facebook.presto.common.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) Type(com.facebook.presto.common.type.Type) SqlDate(com.facebook.presto.common.type.SqlDate) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal)

Example 8 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class AbstractTestFunctions method decimal.

protected static SqlDecimal decimal(String decimalString) {
    DecimalParseResult parseResult = Decimals.parseIncludeLeadingZerosInPrecision(decimalString);
    BigInteger unscaledValue;
    if (parseResult.getType().isShort()) {
        unscaledValue = BigInteger.valueOf((Long) parseResult.getObject());
    } else {
        unscaledValue = Decimals.decodeUnscaledValue((Slice) parseResult.getObject());
    }
    return new SqlDecimal(unscaledValue, parseResult.getType().getPrecision(), parseResult.getType().getScale());
}
Also used : Slice(io.airlift.slice.Slice) DecimalParseResult(com.facebook.presto.common.type.DecimalParseResult) BigInteger(java.math.BigInteger) SqlDecimal(com.facebook.presto.common.type.SqlDecimal)

Example 9 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class StructuralTestUtil method appendToBlockBuilder.

public static void appendToBlockBuilder(Type type, Object element, BlockBuilder blockBuilder) {
    Class<?> javaType = type.getJavaType();
    if (element == null) {
        blockBuilder.appendNull();
    } else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY) && element instanceof Iterable<?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object subElement : (Iterable<?>) element) {
            appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
        }
        blockBuilder.closeEntry();
    } else if (type.getTypeSignature().getBase().equals(StandardTypes.ROW) && element instanceof Iterable<?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        int field = 0;
        for (Object subElement : (Iterable<?>) element) {
            appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
            field++;
        }
        blockBuilder.closeEntry();
    } else if (type.getTypeSignature().getBase().equals(StandardTypes.MAP) && element instanceof Map<?, ?>) {
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) element).entrySet()) {
            appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
            appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
        }
        blockBuilder.closeEntry();
    } else if (javaType == boolean.class) {
        type.writeBoolean(blockBuilder, (Boolean) element);
    } else if (javaType == long.class) {
        if (element instanceof SqlDecimal) {
            type.writeLong(blockBuilder, ((SqlDecimal) element).getUnscaledValue().longValue());
        } else if (REAL.equals(type)) {
            type.writeLong(blockBuilder, floatToRawIntBits(((Number) element).floatValue()));
        } else {
            type.writeLong(blockBuilder, ((Number) element).longValue());
        }
    } else if (javaType == double.class) {
        type.writeDouble(blockBuilder, ((Number) element).doubleValue());
    } else if (javaType == Slice.class) {
        if (element instanceof String) {
            type.writeSlice(blockBuilder, Slices.utf8Slice(element.toString()));
        } else if (element instanceof byte[]) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) element));
        } else if (element instanceof SqlDecimal) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) element).getUnscaledValue()));
        } else {
            type.writeSlice(blockBuilder, (Slice) element);
        }
    } else {
        type.writeObject(blockBuilder, element);
    }
}
Also used : Slice(io.airlift.slice.Slice) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) Map(java.util.Map) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 10 with SqlDecimal

use of com.facebook.presto.common.type.SqlDecimal in project presto by prestodb.

the class OrcTester method testValue.

private static boolean testValue(Type type, Object value, TupleDomainFilter filter) {
    if (value == null) {
        return filter.testNull();
    }
    if (filter == IS_NULL) {
        return false;
    }
    if (filter == IS_NOT_NULL) {
        return true;
    }
    if (type == BOOLEAN) {
        return filter.testBoolean((Boolean) value);
    }
    if (type == TINYINT || type == BIGINT || type == INTEGER || type == SMALLINT) {
        return filter.testLong(((Number) value).longValue());
    }
    if (type == REAL) {
        return filter.testFloat(((Number) value).floatValue());
    }
    if (type == DOUBLE) {
        return filter.testDouble((double) value);
    }
    if (type == DATE) {
        return filter.testLong(((SqlDate) value).getDays());
    }
    if (type == TIMESTAMP) {
        return filter.testLong(((SqlTimestamp) value).getMillisUtc());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
        if (decimalType.isShort()) {
            return filter.testLong(bigDecimal.unscaledValue().longValue());
        } else {
            Slice encodedDecimal = Decimals.encodeScaledValue(bigDecimal);
            return filter.testDecimal(encodedDecimal.getLong(0), encodedDecimal.getLong(Long.BYTES));
        }
    }
    if (type == VARCHAR) {
        return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
    }
    if (type instanceof CharType) {
        String charString = String.valueOf(value);
        return filter.testBytes(charString.getBytes(), 0, charString.length());
    }
    if (type == VARBINARY) {
        byte[] binary = ((SqlVarbinary) value).getBytes();
        return filter.testBytes(binary, 0, binary.length);
    }
    fail("Unsupported type: " + type);
    return false;
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) DecimalType(com.facebook.presto.common.type.DecimalType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) CharType(com.facebook.presto.common.type.CharType) BigDecimal(java.math.BigDecimal)

Aggregations

SqlDecimal (com.facebook.presto.common.type.SqlDecimal)21 ArrayList (java.util.ArrayList)10 DecimalType (com.facebook.presto.common.type.DecimalType)9 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigInteger (java.math.BigInteger)9 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)7 Type (com.facebook.presto.common.type.Type)7 Slice (io.airlift.slice.Slice)7 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)6 ArrayType (com.facebook.presto.common.type.ArrayType)6 SqlDate (com.facebook.presto.common.type.SqlDate)6 List (java.util.List)6 Map (java.util.Map)6 RowType (com.facebook.presto.common.type.RowType)5 VarcharType (com.facebook.presto.common.type.VarcharType)5 HashMap (java.util.HashMap)5 CharType (com.facebook.presto.common.type.CharType)4 MapType (com.facebook.presto.common.type.MapType)4 ImmutableMap (com.google.common.collect.ImmutableMap)4