use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class OrcTester method decodeRecordReaderValue.
private static Object decodeRecordReaderValue(Type type, Object actualValue) {
if (actualValue instanceof OrcLazyObject) {
try {
actualValue = ((OrcLazyObject) actualValue).materialize();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
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 DateWritable) {
actualValue = new SqlDate(((DateWritable) 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 TimestampWritable) {
TimestampWritable timestamp = (TimestampWritable) actualValue;
actualValue = sqlTimestampOf((timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L), SESSION);
} 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 com.facebook.hive.orc.OrcStruct) {
List<Object> fields = new ArrayList<>();
com.facebook.hive.orc.OrcStruct structObject = (com.facebook.hive.orc.OrcStruct) actualValue;
for (int fieldId = 0; fieldId < structObject.getNumFields(); fieldId++) {
fields.add(structObject.getFieldValue(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;
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class TestSelectiveOrcReader method testVarBinaries.
@Test
public void testVarBinaries() throws Exception {
tester.testRoundTrip(VARBINARY, createList(NUM_ROWS, i -> new SqlVarbinary(String.valueOf(i).getBytes(UTF_8))), stringIn(false, "10", "11"));
tester.testRoundTripTypes(ImmutableList.of(VARBINARY, VARBINARY), ImmutableList.of(createList(NUM_ROWS, i -> new SqlVarbinary(Ints.toByteArray(i))), Streams.stream(limit(cycle(ImmutableList.of("A", "B", "C")), NUM_ROWS)).map(String::getBytes).map(SqlVarbinary::new).collect(toImmutableList())), toSubfieldFilters(ImmutableMap.of(0, stringBetween(true, "10", "16"), 1, stringBetween(false, "A", "B"))));
tester.testRoundTrip(VARBINARY, createList(NUM_ROWS, i -> new SqlVarbinary(String.valueOf(i).getBytes(UTF_8))), bytesBetween(false, new byte[] { 8 }, new byte[] { 9 }));
tester.testRoundTrip(VARBINARY, nCopies(NUM_ROWS, new SqlVarbinary(new byte[0])), bytesBetween(false, new byte[0], new byte[] { 1 }));
tester.testRoundTrip(VARBINARY, ImmutableList.copyOf(limit(cycle(ImmutableList.of(1, 3, 5, 7, 11, 13, 17)), NUM_ROWS)).stream().map(String::valueOf).map(string -> string.getBytes(UTF_8)).map(SqlVarbinary::new).collect(toImmutableList()), bytesBetween(false, new byte[] { 1 }, new byte[] { 12 }));
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class ParquetTester 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).getUnscaledValue().longValue());
} else if (Decimals.isLongDecimal(type)) {
if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
} else {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).getUnscaledValue().longValue()));
}
} 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).getMillisUtc();
type.writeLong(blockBuilder, millis);
} else {
if (type instanceof ArrayType) {
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 (type instanceof MapType) {
Map<?, ?> map = (Map<?, ?>) value;
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
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);
}
}
}
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class ChecksumResult method fromResultSet.
public static Optional<ChecksumResult> fromResultSet(ResultSet resultSet) throws SQLException {
long rowCount = resultSet.getLong(1);
ResultSetMetaData metaData = resultSet.getMetaData();
Map<String, Object> checksums = new HashMap<>(metaData.getColumnCount());
for (int i = 2; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnName(i);
Object checksum = resultSet.getObject(i);
if (checksum == null) {
checksums.put(columnName, null);
continue;
}
if (metaData.getColumnType(i) == Types.DOUBLE) {
checkState(checksum instanceof Double, "Expecting double for column %s, found %s", columnName, checksum.getClass());
checksums.put(columnName, checksum);
} else if (metaData.getColumnType(i) == Types.BIGINT) {
checkState(checksum instanceof Long, "Expecting bigint for column %s, found %s", columnName, checksum.getClass());
checksums.put(columnName, checksum);
} else {
checkState(checksum instanceof byte[], "Expecting binary for column %s, found %s", columnName, checksum.getClass());
checksums.put(columnName, new SqlVarbinary((byte[]) checksum));
}
}
return Optional.of(new ChecksumResult(rowCount, checksums));
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class TestChecksumValidator method testArray.
@Test
public void testArray() {
List<Column> columns = ImmutableList.of(INT_ARRAY_COLUMN, MAP_ARRAY_COLUMN);
ChecksumResult controlChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("int_array$checksum", new SqlVarbinary(new byte[] { 0xa })).put("int_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xb })).put("int_array$cardinality_sum", 1L).put("map_array$checksum", new SqlVarbinary(new byte[] { 0xc })).put("map_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map_array$cardinality_sum", 2L).build());
// Matched
assertTrue(checksumValidator.getMismatchedColumns(columns, controlChecksum, controlChecksum).isEmpty());
// Mismatched different elements
ChecksumResult testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("int_array$checksum", new SqlVarbinary(new byte[] { 0x1a })).put("int_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xb })).put("int_array$cardinality_sum", 1L).put("map_array$checksum", new SqlVarbinary(new byte[] { 0x1c })).put("map_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map_array$cardinality_sum", 2L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, INT_ARRAY_COLUMN, MAP_ARRAY_COLUMN);
// Mismatched different cardinality checksum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("int_array$checksum", new SqlVarbinary(new byte[] { 0xa })).put("int_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0x1b })).put("int_array$cardinality_sum", 1L).put("map_array$checksum", new SqlVarbinary(new byte[] { 0xc })).put("map_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0x1d })).put("map_array$cardinality_sum", 2L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, INT_ARRAY_COLUMN, MAP_ARRAY_COLUMN);
// Mismatched different cardinality sum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("int_array$checksum", new SqlVarbinary(new byte[] { 0xa })).put("int_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xb })).put("int_array$cardinality_sum", 3L).put("map_array$checksum", new SqlVarbinary(new byte[] { 0xc })).put("map_array$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map_array$cardinality_sum", 4L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, INT_ARRAY_COLUMN, MAP_ARRAY_COLUMN);
}
Aggregations