use of io.trino.spi.type.CharType in project trino by trinodb.
the class HiveWriteUtils method getField.
public static Object getField(DateTimeZone localZone, Type type, Block block, int position) {
if (block.isNull(position)) {
return null;
}
if (BOOLEAN.equals(type)) {
return type.getBoolean(block, position);
}
if (BIGINT.equals(type)) {
return type.getLong(block, position);
}
if (INTEGER.equals(type)) {
return toIntExact(type.getLong(block, position));
}
if (SMALLINT.equals(type)) {
return Shorts.checkedCast(type.getLong(block, position));
}
if (TINYINT.equals(type)) {
return SignedBytes.checkedCast(type.getLong(block, position));
}
if (REAL.equals(type)) {
return intBitsToFloat((int) type.getLong(block, position));
}
if (DOUBLE.equals(type)) {
return type.getDouble(block, position);
}
if (type instanceof VarcharType) {
return new Text(type.getSlice(block, position).getBytes());
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
return new Text(padSpaces(type.getSlice(block, position), charType).toStringUtf8());
}
if (VARBINARY.equals(type)) {
return type.getSlice(block, position).getBytes();
}
if (DATE.equals(type)) {
return Date.ofEpochDay(toIntExact(type.getLong(block, position)));
}
if (type instanceof TimestampType) {
return getHiveTimestamp(localZone, (TimestampType) type, block, position);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getHiveDecimal(decimalType, block, position);
}
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
Block arrayBlock = block.getObject(position, Block.class);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
list.add(getField(localZone, elementType, arrayBlock, i));
}
return unmodifiableList(list);
}
if (type instanceof MapType) {
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
Block mapBlock = block.getObject(position, Block.class);
Map<Object, Object> map = new HashMap<>();
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
map.put(getField(localZone, keyType, mapBlock, i), getField(localZone, valueType, mapBlock, i + 1));
}
return unmodifiableMap(map);
}
if (type instanceof RowType) {
List<Type> fieldTypes = type.getTypeParameters();
Block rowBlock = block.getObject(position, Block.class);
checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
row.add(getField(localZone, fieldTypes.get(i), rowBlock, i));
}
return unmodifiableList(row);
}
throw new TrinoException(NOT_SUPPORTED, "unsupported type: " + type);
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class SerDeUtils method serializePrimitive.
private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
requireNonNull(builder, "builder is null");
if (object == null) {
builder.appendNull();
return;
}
switch(inspector.getPrimitiveCategory()) {
case BOOLEAN:
type.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
return;
case BYTE:
type.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
return;
case SHORT:
type.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
return;
case INT:
type.writeLong(builder, ((IntObjectInspector) inspector).get(object));
return;
case LONG:
type.writeLong(builder, ((LongObjectInspector) inspector).get(object));
return;
case FLOAT:
type.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
return;
case DOUBLE:
type.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
return;
case STRING:
type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case VARCHAR:
type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
return;
case CHAR:
HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), ((CharType) type).getLength()));
return;
case DATE:
type.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
return;
case TIMESTAMP:
TimestampType timestampType = (TimestampType) type;
DecodedTimestamp timestamp = formatTimestamp(timestampType, object, (TimestampObjectInspector) inspector);
createTimestampEncoder(timestampType, DateTimeZone.UTC).write(timestamp, builder);
return;
case BINARY:
type.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case DECIMAL:
DecimalType decimalType = (DecimalType) type;
HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
if (decimalType.isShort()) {
type.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
} else {
type.writeObject(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
}
return;
case VOID:
case TIMESTAMPLOCALTZ:
case INTERVAL_YEAR_MONTH:
case INTERVAL_DAY_TIME:
case UNKNOWN:
}
throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class TestCharType method testGetObjectValue.
@Test
public void testGetObjectValue() {
CharType charType = createCharType(3);
for (int codePoint : ImmutableList.of(0, 1, 10, 17, (int) ' ', 127, 1011, 11_000, 65_891, MIN_SUPPLEMENTARY_CODE_POINT, MAX_CODE_POINT)) {
BlockBuilder blockBuilder = charType.createBlockBuilder(null, 1);
Slice slice = (codePoint != ' ') ? codePointToUtf8(codePoint) : EMPTY_SLICE;
blockBuilder.writeBytes(slice, 0, slice.length());
blockBuilder.closeEntry();
Block block = blockBuilder.build();
int codePointLengthInUtf16 = isSupplementaryCodePoint(codePoint) ? 2 : 1;
String objectValue = (String) charType.getObjectValue(SESSION, block, 0);
assertNotNull(objectValue);
assertEquals(objectValue.codePointAt(0), codePoint, "first code point");
assertEquals(objectValue.length(), codePointLengthInUtf16 + 2, "size");
for (int i = codePointLengthInUtf16; i < objectValue.length(); i++) {
assertEquals(objectValue.codePointAt(i), ' ');
}
}
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class DruidJdbcClient method legacyToWriteMapping.
private WriteMapping legacyToWriteMapping(Type type) {
// This method is copied from deprecated BaseJdbcClient.legacyToWriteMapping()
if (type == BOOLEAN) {
return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
}
if (type == TINYINT) {
return WriteMapping.longMapping("tinyint", tinyintWriteFunction());
}
if (type == SMALLINT) {
return WriteMapping.longMapping("smallint", smallintWriteFunction());
}
if (type == INTEGER) {
return WriteMapping.longMapping("integer", integerWriteFunction());
}
if (type == BIGINT) {
return WriteMapping.longMapping("bigint", bigintWriteFunction());
}
if (type == REAL) {
return WriteMapping.longMapping("real", realWriteFunction());
}
if (type == DOUBLE) {
return WriteMapping.doubleMapping("double precision", doubleWriteFunction());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
String dataType = format("decimal(%s, %s)", decimalType.getPrecision(), decimalType.getScale());
if (decimalType.isShort()) {
return WriteMapping.longMapping(dataType, shortDecimalWriteFunction(decimalType));
}
return WriteMapping.objectMapping(dataType, longDecimalWriteFunction(decimalType));
}
if (type instanceof CharType) {
return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
String dataType;
if (varcharType.isUnbounded()) {
dataType = "varchar";
} else {
dataType = "varchar(" + varcharType.getBoundedLength() + ")";
}
return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
}
if (type == VARBINARY) {
return WriteMapping.sliceMapping("varbinary", varbinaryWriteFunction());
}
if (type == DATE) {
return WriteMapping.longMapping("date", dateWriteFunctionUsingSqlDate());
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class FormatFunction method valueConverter.
private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionDependencies functionDependencies, Type type, int position) {
if (type.equals(UNKNOWN)) {
return (session, block) -> null;
}
if (type.equals(BOOLEAN)) {
return (session, block) -> type.getBoolean(block, position);
}
if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
return (session, block) -> type.getLong(block, position);
}
if (type.equals(REAL)) {
return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
}
if (type.equals(DOUBLE)) {
return (session, block) -> type.getDouble(block, position);
}
if (type.equals(DATE)) {
return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
}
if (type instanceof TimestampWithTimeZoneType) {
return (session, block) -> toZonedDateTime(((TimestampWithTimeZoneType) type), block, position);
}
if (type instanceof TimestampType) {
return (session, block) -> toLocalDateTime(((TimestampType) type), block, position);
}
if (type instanceof TimeType) {
return (session, block) -> toLocalTime(type.getLong(block, position));
}
// TODO: support TIME WITH TIME ZONE by https://github.com/trinodb/trino/issues/191 + mapping to java.time.OffsetTime
if (type.equals(JSON)) {
MethodHandle handle = functionDependencies.getFunctionInvoker(QualifiedName.of("json_format"), ImmutableList.of(JSON), simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
return (session, block) -> convertToString(handle, type.getSlice(block, position));
}
if (isShortDecimal(type)) {
int scale = ((DecimalType) type).getScale();
return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
}
if (isLongDecimal(type)) {
int scale = ((DecimalType) type).getScale();
return (session, block) -> new BigDecimal(((Int128) type.getObject(block, position)).toBigInteger(), scale);
}
if (type instanceof VarcharType) {
return (session, block) -> type.getSlice(block, position).toStringUtf8();
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
return (session, block) -> padSpaces(type.getSlice(block, position), charType).toStringUtf8();
}
BiFunction<ConnectorSession, Block, Object> function;
if (type.getJavaType() == long.class) {
function = (session, block) -> type.getLong(block, position);
} else if (type.getJavaType() == double.class) {
function = (session, block) -> type.getDouble(block, position);
} else if (type.getJavaType() == boolean.class) {
function = (session, block) -> type.getBoolean(block, position);
} else if (type.getJavaType() == Slice.class) {
function = (session, block) -> type.getSlice(block, position);
} else {
function = (session, block) -> type.getObject(block, position);
}
MethodHandle handle = functionDependencies.getCastInvoker(type, VARCHAR, simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
return (session, block) -> convertToString(handle, function.apply(session, block));
}
Aggregations