use of io.trino.spi.type.TimeWithTimeZoneType in project trino by trinodb.
the class AbstractRowEncoder method appendColumnValue.
@Override
public void appendColumnValue(Block block, int position) {
checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
Type type = columnHandles.get(currentColumnIndex).getType();
if (block.isNull(position)) {
appendNullValue();
} else if (type == BOOLEAN) {
appendBoolean(type.getBoolean(block, position));
} else if (type == BIGINT) {
appendLong(type.getLong(block, position));
} else if (type == INTEGER) {
appendInt(toIntExact(type.getLong(block, position)));
} else if (type == SMALLINT) {
appendShort(Shorts.checkedCast(type.getLong(block, position)));
} else if (type == TINYINT) {
appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
} else if (type == DOUBLE) {
appendDouble(type.getDouble(block, position));
} else if (type == REAL) {
appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (type instanceof VarcharType) {
appendString(type.getSlice(block, position).toStringUtf8());
} else if (type instanceof VarbinaryType) {
appendByteBuffer(type.getSlice(block, position).toByteBuffer());
} else if (type == DATE) {
appendSqlDate((SqlDate) type.getObjectValue(session, block, position));
} else if (type instanceof TimeType) {
appendSqlTime((SqlTime) type.getObjectValue(session, block, position));
} else if (type instanceof TimeWithTimeZoneType) {
appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampType) {
appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session, block, position));
} else if (type instanceof TimestampWithTimeZoneType) {
appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session, block, position));
} else if (type instanceof ArrayType) {
appendArray((List<Object>) type.getObjectValue(session, block, position));
} else if (type instanceof MapType) {
appendMap((Map<Object, Object>) type.getObjectValue(session, block, position));
} else if (type instanceof RowType) {
appendRow((List<Object>) type.getObjectValue(session, block, position));
} else {
throw new UnsupportedOperationException(format("Unsupported type '%s' for column '%s'", type, columnHandles.get(currentColumnIndex).getName()));
}
currentColumnIndex++;
}
use of io.trino.spi.type.TimeWithTimeZoneType in project TiBigData by tidb-incubator.
the class TypeHelpers method toSqlString.
public static String toSqlString(Type type) {
if (type instanceof TimeWithTimeZoneType || type instanceof TimestampWithTimeZoneType) {
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
if (type instanceof TimestampType) {
return format("timestamp(%s)", ((TimestampType) type).getPrecision());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return "longtext";
}
Integer length = varcharType.getLength().orElseThrow(IllegalStateException::new);
if (length <= 255) {
return "tinytext";
}
if (length <= 65535) {
return "text";
}
if (length <= 16777215) {
return "mediumtext";
}
return "longtext";
}
if (type instanceof CharType) {
int length = ((CharType) type).getLength();
if (length <= 255) {
return "char(" + length + ")";
}
return "text";
}
if (type instanceof DecimalType) {
return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
}
if (type instanceof TimeType) {
return format("time(%s)", ((TimeType) type).getPrecision());
}
String sqlType = SQL_TYPES.get(type);
if (sqlType != null) {
return sqlType;
}
return type.getDisplayName();
}
use of io.trino.spi.type.TimeWithTimeZoneType 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.TimeWithTimeZoneType in project trino by trinodb.
the class MaterializedResult method writeValue.
private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
if (value == null) {
blockBuilder.appendNull();
} else if (BIGINT.equals(type)) {
type.writeLong(blockBuilder, (Long) value);
} else if (INTEGER.equals(type)) {
type.writeLong(blockBuilder, (Integer) value);
} else if (SMALLINT.equals(type)) {
type.writeLong(blockBuilder, (Short) value);
} else if (TINYINT.equals(type)) {
type.writeLong(blockBuilder, (Byte) value);
} else if (REAL.equals(type)) {
type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
} else if (DOUBLE.equals(type)) {
type.writeDouble(blockBuilder, (Double) value);
} else if (BOOLEAN.equals(type)) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (JSON.equals(type)) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (type instanceof VarcharType) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (type instanceof CharType) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (VARBINARY.equals(type)) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
} else if (DATE.equals(type)) {
int days = ((SqlDate) value).getDays();
type.writeLong(blockBuilder, days);
} else if (type instanceof TimeType) {
SqlTime time = (SqlTime) value;
type.writeLong(blockBuilder, time.getPicos());
} else if (type instanceof TimeWithTimeZoneType) {
long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
} else if (type instanceof TimestampType) {
long micros = ((SqlTimestamp) value).getEpochMicros();
if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
type.writeLong(blockBuilder, micros);
} else {
type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
}
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
} else if (type instanceof ArrayType) {
List<?> list = (List<?>) value;
Type elementType = ((ArrayType) type).getElementType();
BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
for (Object element : list) {
writeValue(elementType, arrayBlockBuilder, element);
}
blockBuilder.closeEntry();
} else if (type instanceof MapType) {
Map<?, ?> map = (Map<?, ?>) value;
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Entry<?, ?> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
List<?> row = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
for (int field = 0; field < row.size(); field++) {
writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
}
blockBuilder.closeEntry();
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
Aggregations