use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class RcFileTester method preprocessWriteValueOld.
private static Object preprocessWriteValueOld(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
} else if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
} else if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
} else if (type.equals(INTEGER)) {
return ((Number) value).intValue();
} else if (type.equals(BIGINT)) {
return ((Number) value).longValue();
} else if (type.equals(REAL)) {
return ((Number) value).floatValue();
} else if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
} else if (type instanceof VarcharType) {
return value;
} else if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
} else if (type.equals(DATE)) {
int days = ((SqlDate) value).getDays();
LocalDate localDate = LocalDate.ofEpochDay(days);
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
long millis = zonedDateTime.toEpochSecond() * 1000;
Date date = new Date(0);
// mills must be set separately to avoid masking
date.setTime(millis);
return date;
} else if (type.equals(TIMESTAMP)) {
long millisUtc = (int) ((SqlTimestamp) value).getMillisUtc();
return new Timestamp(millisUtc);
} else if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
} else if (type.getTypeSignature().getBase().equals(ARRAY)) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueOld(elementType, element)).collect(toList());
} else if (type.getTypeSignature().getBase().equals(MAP)) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> newMap = new HashMap<>();
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
newMap.put(preprocessWriteValueOld(keyType, entry.getKey()), preprocessWriteValueOld(valueType, entry.getValue()));
}
return newMap;
} else if (type.getTypeSignature().getBase().equals(ROW)) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueOld(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class RcFileTester method decodeRecordReaderValue.
private static Object decodeRecordReaderValue(Type type, Object actualValue) {
if (actualValue instanceof LazyPrimitive) {
actualValue = ((LazyPrimitive<?, ?>) actualValue).getWritableObject();
}
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 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;
if (SESSION.getSqlFunctionProperties().isLegacyTimestamp()) {
actualValue = new SqlTimestamp((timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L), UTC_KEY);
} else {
actualValue = new SqlTimestamp((timestamp.getSeconds() * 1000) + (timestamp.getNanos() / 1000000L));
}
} else if (actualValue instanceof StructObject) {
StructObject structObject = (StructObject) actualValue;
actualValue = decodeRecordReaderStruct(type, structObject.getFieldsAsList());
} else if (actualValue instanceof LazyBinaryArray) {
actualValue = decodeRecordReaderList(type, ((LazyBinaryArray) actualValue).getList());
} else if (actualValue instanceof LazyBinaryMap) {
actualValue = decodeRecordReaderMap(type, ((LazyBinaryMap) actualValue).getMap());
} else if (actualValue instanceof LazyArray) {
actualValue = decodeRecordReaderList(type, ((LazyArray) actualValue).getList());
} else if (actualValue instanceof LazyMap) {
actualValue = decodeRecordReaderMap(type, ((LazyMap) actualValue).getMap());
} else if (actualValue instanceof List) {
actualValue = decodeRecordReaderList(type, ((List<?>) actualValue));
}
return actualValue;
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class TestChecksumValidator method testRow.
@Test
public void testRow() {
List<Column> columns = ImmutableList.of(ROW_COLUMN);
ChecksumResult controlChecksum = new ChecksumResult(ROW_COLUMN_CHECKSUMS.size(), ROW_COLUMN_CHECKSUMS);
assertTrue(checksumValidator.getMismatchedColumns(columns, controlChecksum, controlChecksum).isEmpty());
// Mismatched different elements
ChecksumResult testChecksum = new ChecksumResult(ROW_COLUMN_CHECKSUMS.size(), merge(ROW_COLUMN_CHECKSUMS, ImmutableMap.<String, Object>builder().put("row.i$checksum", new SqlVarbinary(new byte[] { 0x1a })).put("row.r.b$checksum", new SqlVarbinary(new byte[] { 0x1d })).build()));
Column aFieldColumn = Column.create("row.i", new DereferenceExpression(ROW_COLUMN.getExpression(), new Identifier("i")), INTEGER);
Column rbFieldColumn = Column.create("row.r.b", new DereferenceExpression(new DereferenceExpression(ROW_COLUMN.getExpression(), new Identifier("r")), new Identifier("b")), BIGINT);
assertMismatchedColumns(columns, controlChecksum, testChecksum, aFieldColumn, rbFieldColumn);
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class TestChecksumValidator method testMap.
@Test
public void testMap() {
List<Column> columns = ImmutableList.of(MAP_COLUMN);
ChecksumResult controlChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0xa })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0xb })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0xc })).put("map$cardinality_sum", 3L).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).build());
// Matched
assertTrue(checksumValidator.getMismatchedColumns(columns, controlChecksum, controlChecksum).isEmpty());
// Mismatched map checksum
ChecksumResult testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0x1a })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0xb })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0xc })).put("map$cardinality_sum", 3L).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, MAP_COLUMN);
// Mismatched keys checksum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0xa })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0x1b })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0xc })).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map$cardinality_sum", 3L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, MAP_COLUMN);
// Mismatched values checksum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0xa })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0xb })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0x1c })).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map$cardinality_sum", 3L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, MAP_COLUMN);
// Mismatched cardinality checksum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0xa })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0xb })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0xc })).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0x1d })).put("map$cardinality_sum", 3L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, MAP_COLUMN);
// Mismatched cardinality sum
testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("map$checksum", new SqlVarbinary(new byte[] { 0xa })).put("map$keys_checksum", new SqlVarbinary(new byte[] { 0xb })).put("map$values_checksum", new SqlVarbinary(new byte[] { 0xc })).put("map$cardinality_checksum", new SqlVarbinary(new byte[] { 0xd })).put("map$cardinality_sum", 4L).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, MAP_COLUMN);
}
use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.
the class TestChecksumValidator method testSimple.
@Test
public void testSimple() {
List<Column> columns = ImmutableList.of(BIGINT_COLUMN, VARCHAR_COLUMN);
ChecksumResult controlChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("bigint$checksum", new SqlVarbinary(new byte[] { 0xa })).put("varchar$checksum", new SqlVarbinary(new byte[] { 0xb })).build());
// Matched
assertTrue(checksumValidator.getMismatchedColumns(columns, controlChecksum, controlChecksum).isEmpty());
// Mismatched
ChecksumResult testChecksum = new ChecksumResult(5, ImmutableMap.<String, Object>builder().put("bigint$checksum", new SqlVarbinary(new byte[] { 0x1a })).put("varchar$checksum", new SqlVarbinary(new byte[] { 0x1b })).build());
assertMismatchedColumns(columns, controlChecksum, testChecksum, BIGINT_COLUMN, VARCHAR_COLUMN);
}
Aggregations