use of io.trino.spi.type.CharType in project trino by trinodb.
the class OrcTester method preprocessWriteValueHive.
private static Object preprocessWriteValueHive(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
}
if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
}
if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
}
if (type.equals(INTEGER)) {
return ((Number) value).intValue();
}
if (type.equals(BIGINT)) {
return ((Number) value).longValue();
}
if (type.equals(REAL)) {
return ((Number) value).floatValue();
}
if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
}
if (type instanceof VarcharType) {
return value;
}
if (type instanceof CharType) {
return new HiveChar((String) value, ((CharType) type).getLength());
}
if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
}
if (type.equals(DATE)) {
return Date.ofEpochDay(((SqlDate) value).getDays());
}
if (type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS) || type.equals(TIMESTAMP_NANOS)) {
LocalDateTime dateTime = ((SqlTimestamp) value).toLocalDateTime();
return Timestamp.ofEpochSecond(dateTime.toEpochSecond(ZoneOffset.UTC), dateTime.getNano());
}
if (type.equals(TIMESTAMP_TZ_MILLIS) || type.equals(TIMESTAMP_TZ_MICROS) || type.equals(TIMESTAMP_TZ_NANOS)) {
SqlTimestampWithTimeZone timestamp = (SqlTimestampWithTimeZone) value;
int nanosOfMilli = roundDiv(timestamp.getPicosOfMilli(), PICOSECONDS_PER_NANOSECOND);
return Timestamp.ofEpochMilli(timestamp.getEpochMillis(), nanosOfMilli);
}
if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
}
if (type instanceof ArrayType) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueHive(elementType, element)).collect(toList());
}
if (type instanceof MapType) {
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(preprocessWriteValueHive(keyType, entry.getKey()), preprocessWriteValueHive(valueType, entry.getValue()));
}
return newMap;
}
if (type instanceof RowType) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueHive(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class ClickHouseClient method toWriteMapping.
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
if (type == BOOLEAN) {
// ClickHouse is no separate type for boolean values. Use UInt8 type, restricted to the values 0 or 1.
return WriteMapping.booleanMapping("UInt8", booleanWriteFunction());
}
if (type == TINYINT) {
return WriteMapping.longMapping("Int8", tinyintWriteFunction());
}
if (type == SMALLINT) {
return WriteMapping.longMapping("Int16", smallintWriteFunction());
}
if (type == INTEGER) {
return WriteMapping.longMapping("Int32", integerWriteFunction());
}
if (type == BIGINT) {
return WriteMapping.longMapping("Int64", bigintWriteFunction());
}
if (type == REAL) {
return WriteMapping.longMapping("Float32", realWriteFunction());
}
if (type == DOUBLE) {
return WriteMapping.doubleMapping("Float64", 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 || type instanceof VarcharType) {
// The String type replaces the types VARCHAR, BLOB, CLOB, and others from other DBMSs.
return WriteMapping.sliceMapping("String", varcharWriteFunction());
}
if (type instanceof VarbinaryType) {
// Strings of an arbitrary length. The length is not limited
return WriteMapping.sliceMapping("String", varbinaryWriteFunction());
}
if (type == DATE) {
// TODO (https://github.com/trinodb/trino/issues/10055) Deny unsupported dates to prevent inserting wrong values. 2106-02-07 is max value in version 20.8
return WriteMapping.longMapping("Date", dateWriteFunctionUsingLocalDate());
}
if (type == TIMESTAMP_SECONDS) {
return WriteMapping.longMapping("DateTime", timestampSecondsWriteFunction());
}
if (type.equals(uuidType)) {
return WriteMapping.sliceMapping("UUID", uuidWriteFunction());
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type);
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class TypeCoercion method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getBaseName();
String toTypeBaseName = toType.getBaseName();
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR)) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME)) {
Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getBaseName());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class BinaryColumnReader method readValue.
@Override
protected void readValue(BlockBuilder blockBuilder, Type type) {
Binary binary = valuesReader.readBytes();
Slice value;
if (binary.length() == 0) {
value = EMPTY_SLICE;
} else {
value = wrappedBuffer(binary.getBytes());
}
if (type instanceof VarcharType) {
value = truncateToLength(value, type);
}
if (type instanceof CharType) {
value = truncateToLengthAndTrimSpaces(value, type);
}
type.writeSlice(blockBuilder, value);
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class ParquetSchemaConverter method getPrimitiveType.
private org.apache.parquet.schema.Type getPrimitiveType(Type type, String name, List<String> parent) {
List<String> fullName = ImmutableList.<String>builder().addAll(parent).add(name).build();
primitiveTypes.put(fullName, type);
if (BOOLEAN.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.BOOLEAN, OPTIONAL).named(name);
}
if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT32, OPTIONAL).named(name);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.getPrecision() <= 9) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
} else if (decimalType.isShort()) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT64).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
} else {
return Types.optional(PrimitiveType.PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY).length(16).as(OriginalType.DECIMAL).precision(decimalType.getPrecision()).scale(decimalType.getScale()).named(name);
}
}
if (DATE.equals(type)) {
return Types.optional(PrimitiveType.PrimitiveTypeName.INT32).as(OriginalType.DATE).named(name);
}
if (BIGINT.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).named(name);
}
if (type instanceof TimestampType) {
TimestampType timestampType = (TimestampType) type;
if (timestampType.getPrecision() <= 3) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.MILLIS)).named(name);
}
if (timestampType.getPrecision() <= 6) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.MICROS)).named(name);
}
if (timestampType.getPrecision() <= 9) {
// even though it can only hold values within 1677-09-21 00:12:43 and 2262-04-11 23:47:16 range.
return Types.primitive(PrimitiveType.PrimitiveTypeName.INT64, OPTIONAL).as(LogicalTypeAnnotation.timestampType(false, LogicalTypeAnnotation.TimeUnit.NANOS)).named(name);
}
}
if (DOUBLE.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.DOUBLE, OPTIONAL).named(name);
}
if (RealType.REAL.equals(type)) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.FLOAT, OPTIONAL).named(name);
}
if (type instanceof VarcharType || type instanceof CharType) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).as(LogicalTypeAnnotation.stringType()).named(name);
}
if (type instanceof VarbinaryType) {
return Types.primitive(PrimitiveType.PrimitiveTypeName.BINARY, OPTIONAL).named(name);
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported primitive type: %s", type));
}
Aggregations