Search in sources :

Example 1 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class OrcTester method decodeRecordReaderValue.

private static Object decodeRecordReaderValue(Type type, Object inputActualValue) {
    Object actualValue = inputActualValue;
    if (actualValue instanceof BooleanWritable) {
        actualValue = ((BooleanWritable) actualValue).get();
    } else if (actualValue instanceof ByteWritable) {
        actualValue = ((ByteWritable) actualValue).get();
    } else if (actualValue instanceof BytesWritable) {
        actualValue = new SqlVarbinary(((BytesWritable) actualValue).copyBytes());
    } else if (actualValue instanceof DateWritableV2) {
        actualValue = new SqlDate(((DateWritableV2) actualValue).getDays());
    } else if (actualValue instanceof DoubleWritable) {
        actualValue = ((DoubleWritable) actualValue).get();
    } else if (actualValue instanceof FloatWritable) {
        actualValue = ((FloatWritable) actualValue).get();
    } else if (actualValue instanceof IntWritable) {
        actualValue = ((IntWritable) actualValue).get();
    } else if (actualValue instanceof HiveCharWritable) {
        actualValue = ((HiveCharWritable) actualValue).getPaddedValue().toString();
    } else if (actualValue instanceof LongWritable) {
        actualValue = ((LongWritable) actualValue).get();
    } else if (actualValue instanceof ShortWritable) {
        actualValue = ((ShortWritable) actualValue).get();
    } else if (actualValue instanceof HiveDecimalWritable) {
        DecimalType decimalType = (DecimalType) type;
        HiveDecimalWritable writable = (HiveDecimalWritable) actualValue;
        // writable messes with the scale so rescale the values to the Presto type
        BigInteger rescaledValue = rescale(writable.getHiveDecimal().unscaledValue(), writable.getScale(), decimalType.getScale());
        actualValue = new SqlDecimal(rescaledValue, decimalType.getPrecision(), decimalType.getScale());
    } else if (actualValue instanceof Text) {
        actualValue = actualValue.toString();
    } else if (actualValue instanceof TimestampWritableV2) {
        actualValue = sqlTimestampOf(((TimestampWritableV2) actualValue).getTimestamp().toEpochMilli());
    } else if (actualValue instanceof OrcStruct) {
        List<Object> fields = new ArrayList<>();
        OrcStruct structObject = (OrcStruct) actualValue;
        for (int fieldId = 0; fieldId < structObject.getNumFields(); fieldId++) {
            fields.add(OrcUtil.getFieldValue(structObject, fieldId));
        }
        actualValue = decodeRecordReaderStruct(type, fields);
    } else if (actualValue instanceof List) {
        actualValue = decodeRecordReaderList(type, ((List<?>) actualValue));
    } else if (actualValue instanceof Map) {
        actualValue = decodeRecordReaderMap(type, (Map<?, ?>) actualValue);
    }
    return actualValue;
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) SqlDecimal(io.prestosql.spi.type.SqlDecimal) Text(org.apache.hadoop.io.Text) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) FloatWritable(org.apache.hadoop.io.FloatWritable) OrcStruct(org.apache.hadoop.hive.ql.io.orc.OrcStruct) BooleanWritable(org.apache.hadoop.io.BooleanWritable) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) BigInteger(java.math.BigInteger) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) IntWritable(org.apache.hadoop.io.IntWritable)

Example 2 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class OrcTester method testRow.

// later we can extend for multiple columns
private static boolean testRow(List<Type> types, List<?> values, int row, Map<Integer, TupleDomainFilter> columnFilters) {
    for (int column = 0; column < types.size(); column++) {
        TupleDomainFilter filter = columnFilters.get(column);
        if (filter == null) {
            continue;
        }
        Object value = values.get(row);
        if (value == null) {
            if (!filter.testNull()) {
                return false;
            }
        } else {
            Type type = types.get(column);
            if (type == BOOLEAN) {
                if (!filter.testBoolean((Boolean) value)) {
                    return false;
                }
            } else if (type == BIGINT || type == INTEGER || type == SMALLINT) {
                if (!filter.testLong(((Number) value).longValue())) {
                    return false;
                }
            } else if (type == DATE) {
                if (!filter.testLong(((SqlDate) value).getDays())) {
                    return false;
                }
            } else if (type == TIMESTAMP) {
                return filter.testLong(((SqlTimestamp) value).getMillis());
            } else if (type == VARCHAR) {
                return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
            } else if (type instanceof CharType) {
                String charString = String.valueOf(value);
                return filter.testBytes(charString.getBytes(StandardCharsets.UTF_8), 0, charString.length());
            } else if (type == VARBINARY) {
                byte[] binary = ((SqlVarbinary) value).getBytes();
                return filter.testBytes(binary, 0, binary.length);
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
                if (decimalType.isShort()) {
                    return filter.testLong(bigDecimal.unscaledValue().longValue());
                }
            } else if (type == DOUBLE) {
                if (!filter.testDouble((double) value)) {
                    return false;
                }
            } else {
                fail("Unsupported type: " + type);
            }
        }
    }
    return true;
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) BigDecimal(java.math.BigDecimal) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 3 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillis();
            type.writeLong(blockBuilder, millis);
        } else {
            String baseType = type.getTypeSignature().getBase();
            if (StandardTypes.ARRAY.equals(baseType)) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.MAP.equals(baseType)) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (StandardTypes.ROW.equals(baseType)) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlDecimal(io.prestosql.spi.type.SqlDecimal) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Entry(java.util.Map.Entry) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) CharType(io.prestosql.spi.type.CharType) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 4 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class Validator method convertJdbcResultSet.

private List<List<Object>> convertJdbcResultSet(ResultSet resultSet) throws SQLException, VerifierException {
    int rowCount = 0;
    int columnCount = resultSet.getMetaData().getColumnCount();
    ImmutableList.Builder<List<Object>> rows = ImmutableList.builder();
    while (resultSet.next()) {
        List<Object> row = new ArrayList<>();
        for (int i = 1; i <= columnCount; i++) {
            Object object = resultSet.getObject(i);
            if (object instanceof BigDecimal) {
                if (((BigDecimal) object).scale() <= 0) {
                    object = ((BigDecimal) object).longValueExact();
                } else {
                    object = ((BigDecimal) object).doubleValue();
                }
            }
            if (object instanceof Array) {
                object = ((Array) object).getArray();
            }
            if (object instanceof byte[]) {
                object = new SqlVarbinary((byte[]) object);
            }
            row.add(object);
        }
        rows.add(unmodifiableList(row));
        rowCount++;
        if (rowCount > maxRowCount) {
            throw new VerifierException("More than '" + maxRowCount + "' rows, failing query");
        }
    }
    return rows.build();
}
Also used : Array(java.sql.Array) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Collections.unmodifiableList(java.util.Collections.unmodifiableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BigDecimal(java.math.BigDecimal)

Example 5 with SqlVarbinary

use of io.prestosql.spi.type.SqlVarbinary in project hetu-core by openlookeng.

the class TestUuidOperators method testUUIDToVarbinaryCast.

@Test
public void testUUIDToVarbinaryCast() {
    assertFunction("CAST(UUID '00000000-0000-0000-0000-000000000000' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("00000000000000000000000000000000")));
    assertFunction("CAST(UUID '6b5f5b65-67e4-43b0-8ee3-586cd49f58a0' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("B043E467655B5F6BA0589FD46C58E38E")));
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) Test(org.testng.annotations.Test)

Aggregations

SqlVarbinary (io.prestosql.spi.type.SqlVarbinary)26 DecimalType (io.prestosql.spi.type.DecimalType)9 ImmutableList (com.google.common.collect.ImmutableList)8 SqlDate (io.prestosql.spi.type.SqlDate)8 SqlDecimal (io.prestosql.spi.type.SqlDecimal)8 SqlTimestamp (io.prestosql.spi.type.SqlTimestamp)8 List (java.util.List)8 Test (org.testng.annotations.Test)8 Type (io.prestosql.spi.type.Type)7 Collectors.toList (java.util.stream.Collectors.toList)7 ArrayList (java.util.ArrayList)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Slice (io.airlift.slice.Slice)5 BlockBuilder (io.prestosql.spi.block.BlockBuilder)5 CharType (io.prestosql.spi.type.CharType)5 VarcharType (io.prestosql.spi.type.VarcharType)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3