use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class HivePageSource method createCoercer.
private static Optional<Function<Block, Block>> createCoercer(TypeManager typeManager, HiveType fromHiveType, HiveType toHiveType) {
if (fromHiveType.equals(toHiveType)) {
return Optional.empty();
}
Type fromType = fromHiveType.getType(typeManager);
Type toType = toHiveType.getType(typeManager);
if (toType instanceof VarcharType && (fromHiveType.equals(HIVE_BYTE) || fromHiveType.equals(HIVE_SHORT) || fromHiveType.equals(HIVE_INT) || fromHiveType.equals(HIVE_LONG))) {
return Optional.of(new IntegerNumberToVarcharCoercer<>(fromType, (VarcharType) toType));
}
if (fromType instanceof VarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return Optional.of(new VarcharToIntegerNumberCoercer<>((VarcharType) fromType, toType));
}
if (fromType instanceof VarcharType && toType instanceof VarcharType) {
VarcharType toVarcharType = (VarcharType) toType;
VarcharType fromVarcharType = (VarcharType) fromType;
if (narrowerThan(toVarcharType, fromVarcharType)) {
return Optional.of(new VarcharCoercer(fromVarcharType, toVarcharType));
}
return Optional.empty();
}
if (fromHiveType.equals(HIVE_BYTE) && (toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return Optional.of(new IntegerNumberUpscaleCoercer<>(fromType, toType));
}
if (fromHiveType.equals(HIVE_SHORT) && (toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return Optional.of(new IntegerNumberUpscaleCoercer<>(fromType, toType));
}
if (fromHiveType.equals(HIVE_INT) && toHiveType.equals(HIVE_LONG)) {
return Optional.of(new IntegerNumberUpscaleCoercer<>(fromType, toType));
}
if (fromHiveType.equals(HIVE_FLOAT) && toHiveType.equals(HIVE_DOUBLE)) {
return Optional.of(new FloatToDoubleCoercer());
}
if (fromHiveType.equals(HIVE_DOUBLE) && toHiveType.equals(HIVE_FLOAT)) {
return Optional.of(new DoubleToFloatCoercer());
}
if (fromType instanceof DecimalType && toType instanceof DecimalType) {
return Optional.of(createDecimalToDecimalCoercer((DecimalType) fromType, (DecimalType) toType));
}
if (fromType instanceof DecimalType && toType == DOUBLE) {
return Optional.of(createDecimalToDoubleCoercer((DecimalType) fromType));
}
if (fromType instanceof DecimalType && toType == REAL) {
return Optional.of(createDecimalToRealCoercer((DecimalType) fromType));
}
if (fromType == DOUBLE && toType instanceof DecimalType) {
return Optional.of(createDoubleToDecimalCoercer((DecimalType) toType));
}
if (fromType == REAL && toType instanceof DecimalType) {
return Optional.of(createRealToDecimalCoercer((DecimalType) toType));
}
if (isArrayType(fromType) && isArrayType(toType)) {
return Optional.of(new ListCoercer(typeManager, fromHiveType, toHiveType));
}
if (isMapType(fromType) && isMapType(toType)) {
return Optional.of(new MapCoercer(typeManager, fromHiveType, toHiveType));
}
if (isRowType(fromType) && isRowType(toType)) {
return Optional.of(new StructCoercer(typeManager, fromHiveType, toHiveType));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromHiveType, toHiveType));
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class MySqlClient method toWriteMapping.
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
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("float", 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 == DATE) {
return WriteMapping.longMapping("date", dateWriteFunctionUsingLocalDate());
}
if (type instanceof TimeType) {
TimeType timeType = (TimeType) type;
if (timeType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
return WriteMapping.longMapping(format("time(%s)", timeType.getPrecision()), timeWriteFunction(timeType.getPrecision()));
}
return WriteMapping.longMapping(format("time(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), timeWriteFunction(MAX_SUPPORTED_DATE_TIME_PRECISION));
}
if (TIME_WITH_TIME_ZONE.equals(type) || TIMESTAMP_TZ_MILLIS.equals(type)) {
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
if (type instanceof TimestampType) {
TimestampType timestampType = (TimestampType) type;
if (timestampType.getPrecision() <= MAX_SUPPORTED_DATE_TIME_PRECISION) {
verify(timestampType.getPrecision() <= TimestampType.MAX_SHORT_PRECISION);
return WriteMapping.longMapping(format("datetime(%s)", timestampType.getPrecision()), timestampWriteFunction(timestampType));
}
return WriteMapping.objectMapping(format("datetime(%s)", MAX_SUPPORTED_DATE_TIME_PRECISION), longTimestampWriteFunction(timestampType, MAX_SUPPORTED_DATE_TIME_PRECISION));
}
if (VARBINARY.equals(type)) {
return WriteMapping.sliceMapping("mediumblob", varbinaryWriteFunction());
}
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 = "longtext";
} else if (varcharType.getBoundedLength() <= 255) {
dataType = "tinytext";
} else if (varcharType.getBoundedLength() <= 65535) {
dataType = "text";
} else if (varcharType.getBoundedLength() <= 16777215) {
dataType = "mediumtext";
} else {
dataType = "longtext";
}
return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
}
if (type.equals(jsonType)) {
return WriteMapping.sliceMapping("json", varcharWriteFunction());
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class MongoPageSink method getObjectValue.
private Object getObjectValue(Type type, Block block, int position) {
if (block.isNull(position)) {
if (type.equals(OBJECT_ID)) {
return new ObjectId();
}
return null;
}
if (type.equals(OBJECT_ID)) {
return new ObjectId(block.getSlice(position, 0, block.getSliceLength(position)).getBytes());
}
if (type.equals(BooleanType.BOOLEAN)) {
return type.getBoolean(block, position);
}
if (type.equals(BigintType.BIGINT)) {
return type.getLong(block, position);
}
if (type.equals(IntegerType.INTEGER)) {
return toIntExact(type.getLong(block, position));
}
if (type.equals(SmallintType.SMALLINT)) {
return Shorts.checkedCast(type.getLong(block, position));
}
if (type.equals(TinyintType.TINYINT)) {
return SignedBytes.checkedCast(type.getLong(block, position));
}
if (type.equals(RealType.REAL)) {
return intBitsToFloat(toIntExact(type.getLong(block, position)));
}
if (type.equals(DoubleType.DOUBLE)) {
return type.getDouble(block, position);
}
if (type instanceof VarcharType) {
return type.getSlice(block, position).toStringUtf8();
}
if (type instanceof CharType) {
return padSpaces(type.getSlice(block, position), ((CharType) type)).toStringUtf8();
}
if (type.equals(VarbinaryType.VARBINARY)) {
return new Binary(type.getSlice(block, position).getBytes());
}
if (type.equals(DateType.DATE)) {
long days = type.getLong(block, position);
return new Date(TimeUnit.DAYS.toMillis(days));
}
if (type.equals(TimeType.TIME)) {
long picos = type.getLong(block, position);
return new Date(roundDiv(picos, PICOSECONDS_PER_MILLISECOND));
}
if (type.equals(TIMESTAMP_MILLIS)) {
long millisUtc = floorDiv(type.getLong(block, position), MICROSECONDS_PER_MILLISECOND);
return new Date(millisUtc);
}
if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_TZ_MILLIS)) {
long millisUtc = unpackMillisUtc(type.getLong(block, position));
return new Date(millisUtc);
}
if (type instanceof DecimalType) {
return readBigDecimal((DecimalType) type, block, position);
}
if (isJsonType(type)) {
String json = type.getSlice(block, position).toStringUtf8();
try {
return Document.parse(json);
} catch (BsonInvalidOperationException e) {
throw new TrinoException(NOT_SUPPORTED, "Can't convert json to MongoDB Document: " + json, e);
}
}
if (isArrayType(type)) {
Type elementType = type.getTypeParameters().get(0);
Block arrayBlock = block.getObject(position, Block.class);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
Object element = getObjectValue(elementType, arrayBlock, i);
list.add(element);
}
return unmodifiableList(list);
}
if (isMapType(type)) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Block mapBlock = block.getObject(position, Block.class);
// map type is converted into list of fixed keys document
List<Object> values = new ArrayList<>(mapBlock.getPositionCount() / 2);
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
Map<String, Object> mapValue = new HashMap<>();
mapValue.put("key", getObjectValue(keyType, mapBlock, i));
mapValue.put("value", getObjectValue(valueType, mapBlock, i + 1));
values.add(mapValue);
}
return unmodifiableList(values);
}
if (isRowType(type)) {
Block rowBlock = block.getObject(position, Block.class);
List<Type> fieldTypes = type.getTypeParameters();
if (fieldTypes.size() != rowBlock.getPositionCount()) {
throw new TrinoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
}
if (isImplicitRowType(type)) {
List<Object> rowValue = new ArrayList<>();
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
Object element = getObjectValue(fieldTypes.get(i), rowBlock, i);
rowValue.add(element);
}
return unmodifiableList(rowValue);
}
Map<String, Object> rowValue = new HashMap<>();
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
rowValue.put(type.getTypeSignature().getParameters().get(i).getNamedTypeSignature().getName().orElse("field" + i), getObjectValue(fieldTypes.get(i), rowBlock, i));
}
return unmodifiableMap(rowValue);
}
throw new TrinoException(NOT_SUPPORTED, "unsupported type: " + type);
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class KuduPageSink method appendColumn.
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
Block block = page.getBlock(channel);
Type type = columnTypes.get(destChannel);
if (block.isNull(position)) {
row.setNull(destChannel);
} else if (TIMESTAMP_MILLIS.equals(type)) {
row.addLong(destChannel, truncateEpochMicrosToMillis(type.getLong(block, position)));
} else if (REAL.equals(type)) {
row.addFloat(destChannel, intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (BIGINT.equals(type)) {
row.addLong(destChannel, type.getLong(block, position));
} else if (INTEGER.equals(type)) {
row.addInt(destChannel, toIntExact(type.getLong(block, position)));
} else if (SMALLINT.equals(type)) {
row.addShort(destChannel, Shorts.checkedCast(type.getLong(block, position)));
} else if (TINYINT.equals(type)) {
row.addByte(destChannel, SignedBytes.checkedCast(type.getLong(block, position)));
} else if (BOOLEAN.equals(type)) {
row.addBoolean(destChannel, type.getBoolean(block, position));
} else if (DOUBLE.equals(type)) {
row.addDouble(destChannel, type.getDouble(block, position));
} else if (type instanceof VarcharType) {
Type originalType = originalColumnTypes.get(destChannel);
if (DATE.equals(originalType)) {
SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession, block, position);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
row.addStringUtf8(destChannel, bytes);
} else {
row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
}
} else if (VARBINARY.equals(type)) {
row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
} else if (type instanceof DecimalType) {
SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession, block, position);
row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
} else {
throw new UnsupportedOperationException("Type is not supported: " + type);
}
}
use of io.trino.spi.type.VarcharType in project trino by trinodb.
the class DataType method varcharDataType.
private static DataType<String> varcharDataType(Optional<Integer> length, String properties) {
String prefix = length.map(size -> "varchar(" + size + ")").orElse("varchar");
String suffix = properties.isEmpty() ? "" : " " + properties;
VarcharType varcharType = length.map(VarcharType::createVarcharType).orElse(createUnboundedVarcharType());
return stringDataType(prefix + suffix, varcharType);
}
Aggregations