use of io.prestosql.spi.block.BlockBuilder 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);
}
}
}
}
use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.
the class TestJsonType method createTestBlock.
public static Block createTestBlock() {
BlockBuilder blockBuilder = JSON.createBlockBuilder(null, 1);
Slice slice = Slices.utf8Slice("{\"x\":1, \"y\":2}");
JSON.writeSlice(blockBuilder, slice);
return blockBuilder.build();
}
use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.
the class TestLongDecimalType method createTestBlock.
public static Block createTestBlock() {
BlockBuilder blockBuilder = LONG_DECIMAL_TYPE.createBlockBuilder(null, 15);
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("-12345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("22345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("32345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("32345678901234567890.1234567890"));
writeBigDecimal(LONG_DECIMAL_TYPE, blockBuilder, new BigDecimal("42345678901234567890.1234567890"));
return blockBuilder.build();
}
use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.
the class TestMapOperators method assertMapHashOperator.
private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
MapType mapType = mapType(keyType, valueType);
BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
for (int i = 0; i < elements.size(); i += 2) {
appendToBlockBuilder(keyType, elements.get(i), singleMapWriter);
appendToBlockBuilder(valueType, elements.get(i + 1), singleMapWriter);
}
mapArrayBuilder.closeEntry();
long hashResult = mapType.hash(mapArrayBuilder.build(), 0);
assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
use of io.prestosql.spi.block.BlockBuilder in project hetu-core by openlookeng.
the class TestRowOperators method assertRowHashOperator.
private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) {
checkArgument(types.size() == elements.size(), "types and elements must have the same size");
RowType rowType = RowType.anonymous(types);
BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < types.size(); i++) {
appendToBlockBuilder(types.get(i), elements.get(i), singleRowBlockWriter);
}
blockBuilder.closeEntry();
assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(blockBuilder.build(), 0));
}
Aggregations