use of io.trino.spi.type.CharType in project trino by trinodb.
the class TypeUtils method jdbcObjectToTrinoNative.
private static Object jdbcObjectToTrinoNative(ConnectorSession session, Object jdbcObject, Type type) {
if (jdbcObject == null) {
return null;
}
if (BOOLEAN.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type)) {
return jdbcObject;
}
if (TINYINT.equals(type)) {
return (long) (byte) jdbcObject;
}
if (SMALLINT.equals(type)) {
return (long) (short) jdbcObject;
}
if (INTEGER.equals(type)) {
return (long) (int) jdbcObject;
}
if (type instanceof ArrayType) {
return jdbcObjectArrayToBlock(session, ((ArrayType) type).getElementType(), (Object[]) jdbcObject);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal value = (BigDecimal) jdbcObject;
if (decimalType.isShort()) {
return encodeShortScaledValue(value, decimalType.getScale());
}
return encodeScaledValue(value, decimalType.getScale());
}
if (REAL.equals(type)) {
return (long) floatToRawIntBits((float) jdbcObject);
}
if (DATE.equals(type)) {
long localMillis = ((Date) jdbcObject).getTime();
// Convert it to a ~midnight in UTC.
long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
// convert to days
return MILLISECONDS.toDays(utcMillis);
}
if (type instanceof VarcharType) {
return utf8Slice((String) jdbcObject);
}
if (type instanceof CharType) {
return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", type, jdbcObject.getClass()));
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class TypeUtils method trinoNativeToJdbcObject.
private static Object trinoNativeToJdbcObject(ConnectorSession session, Type type, Object object) {
if (object == null) {
return null;
}
if (DOUBLE.equals(type) || BOOLEAN.equals(type) || BIGINT.equals(type)) {
return object;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) object);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = ((Int128) object).toBigInteger();
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(type)) {
return intBitsToFloat(toIntExact((long) object));
}
if (TINYINT.equals(type)) {
return SignedBytes.checkedCast((long) object);
}
if (SMALLINT.equals(type)) {
return Shorts.checkedCast((long) object);
}
if (INTEGER.equals(type)) {
return toIntExact((long) object);
}
if (DATE.equals(type)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) object);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (type instanceof VarcharType || type instanceof CharType) {
return ((Slice) object).toStringUtf8();
}
if (type instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) type).getElementType(), (Block) object);
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + type);
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class TypeUtils method jdbcObjectToTrinoNative.
private static Object jdbcObjectToTrinoNative(ConnectorSession session, Object jdbcObject, Type type) {
if (jdbcObject == null) {
return null;
}
if (BOOLEAN.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type)) {
return jdbcObject;
}
if (TINYINT.equals(type)) {
return (long) (byte) jdbcObject;
}
if (SMALLINT.equals(type)) {
return (long) (short) jdbcObject;
}
if (INTEGER.equals(type)) {
return (long) (int) jdbcObject;
}
if (type instanceof ArrayType) {
return jdbcObjectArrayToBlock(session, ((ArrayType) type).getElementType(), (Object[]) jdbcObject);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal value = (BigDecimal) jdbcObject;
if (decimalType.isShort()) {
return encodeShortScaledValue(value, decimalType.getScale());
}
return encodeScaledValue(value, decimalType.getScale());
}
if (REAL.equals(type)) {
return (long) floatToRawIntBits((float) jdbcObject);
}
if (DATE.equals(type)) {
long localMillis = ((Date) jdbcObject).getTime();
// Convert it to a ~midnight in UTC.
long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
// convert to days
return MILLISECONDS.toDays(utcMillis);
}
if (type instanceof VarcharType) {
return utf8Slice((String) jdbcObject);
}
if (type instanceof CharType) {
return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", type, jdbcObject.getClass()));
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class H2QueryRunner method rowMapper.
private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
return (resultSet, context) -> {
int count = resultSet.getMetaData().getColumnCount();
checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
List<Object> row = new ArrayList<>(count);
for (int i = 1; i <= count; i++) {
Type type = types.get(i - 1);
if (BOOLEAN.equals(type)) {
boolean booleanValue = resultSet.getBoolean(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(booleanValue);
}
} else if (TINYINT.equals(type)) {
byte byteValue = resultSet.getByte(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(byteValue);
}
} else if (SMALLINT.equals(type)) {
short shortValue = resultSet.getShort(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(shortValue);
}
} else if (INTEGER.equals(type)) {
int intValue = resultSet.getInt(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(intValue);
}
} else if (BIGINT.equals(type)) {
long longValue = resultSet.getLong(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(longValue);
}
} else if (REAL.equals(type)) {
float floatValue = resultSet.getFloat(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(floatValue);
}
} else if (DOUBLE.equals(type)) {
double doubleValue = resultSet.getDouble(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(doubleValue);
}
} else if (JSON.equals(type)) {
String stringValue = resultSet.getString(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(jsonParse(utf8Slice(stringValue)).toStringUtf8());
}
} else if (type instanceof VarcharType) {
String stringValue = resultSet.getString(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(stringValue);
}
} else if (type instanceof CharType) {
String stringValue = resultSet.getString(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(padSpaces(stringValue, (CharType) type));
}
} else if (VARBINARY.equals(type)) {
byte[] bytes = resultSet.getBytes(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(bytes);
}
} else if (DATE.equals(type)) {
// resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
LocalDate dateValue = resultSet.getObject(i, LocalDate.class);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(dateValue);
}
} else if (type instanceof TimeType) {
// resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
LocalTime timeValue = resultSet.getObject(i, LocalTime.class);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(timeValue);
}
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
} else if (type instanceof TimestampType) {
// resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
LocalDateTime timestampValue;
try {
timestampValue = resultSet.getObject(i, LocalDateTime.class);
} catch (SQLException first) {
// H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
try {
timestampValue = Optional.ofNullable(resultSet.getObject(i, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
} catch (RuntimeException e) {
first.addSuppressed(e);
throw first;
}
}
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(timestampValue);
}
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
// This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
throw new UnsupportedOperationException();
} else if (UUID.equals(type)) {
java.util.UUID value = (java.util.UUID) resultSet.getObject(i);
row.add(value);
} else if (UNKNOWN.equals(type)) {
Object objectValue = resultSet.getObject(i);
checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
row.add(null);
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimalValue = resultSet.getBigDecimal(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision())));
}
} else if (type instanceof ArrayType) {
Array array = resultSet.getArray(i);
if (resultSet.wasNull()) {
row.add(null);
} else {
row.add(newArrayList((Object[]) array.getArray()));
}
} else {
throw new AssertionError("unhandled type: " + type);
}
}
return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
};
}
use of io.trino.spi.type.CharType in project trino by trinodb.
the class PostgreSqlClient method toWriteMapping.
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
if (type == BOOLEAN) {
return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
}
if (type == TINYINT) {
// PostgreSQL has no type corresponding to tinyint
return WriteMapping.longMapping("smallint", 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 (VARBINARY.equals(type)) {
return WriteMapping.sliceMapping("bytea", varbinaryWriteFunction());
}
if (type == DATE) {
return WriteMapping.longMapping("date", dateWriteFunctionUsingLocalDate());
}
if (type instanceof TimeType) {
TimeType timeType = (TimeType) type;
if (timeType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
return WriteMapping.longMapping(format("time(%s)", timeType.getPrecision()), timeWriteFunction(timeType.getPrecision()));
}
return WriteMapping.longMapping(format("time(%s)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), timeWriteFunction(POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION));
}
if (type instanceof TimestampType) {
TimestampType timestampType = (TimestampType) type;
if (timestampType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
verify(timestampType.getPrecision() <= TimestampType.MAX_SHORT_PRECISION);
return WriteMapping.longMapping(format("timestamp(%s)", timestampType.getPrecision()), PostgreSqlClient::shortTimestampWriteFunction);
}
verify(timestampType.getPrecision() > TimestampType.MAX_SHORT_PRECISION);
return WriteMapping.objectMapping(format("timestamp(%s)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), longTimestampWriteFunction());
}
if (type instanceof TimestampWithTimeZoneType) {
TimestampWithTimeZoneType timestampWithTimeZoneType = (TimestampWithTimeZoneType) type;
if (timestampWithTimeZoneType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
String dataType = format("timestamptz(%d)", timestampWithTimeZoneType.getPrecision());
if (timestampWithTimeZoneType.getPrecision() <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
return WriteMapping.longMapping(dataType, shortTimestampWithTimeZoneWriteFunction());
}
return WriteMapping.objectMapping(dataType, longTimestampWithTimeZoneWriteFunction());
}
return WriteMapping.objectMapping(format("timestamptz(%d)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), longTimestampWithTimeZoneWriteFunction());
}
if (type.equals(jsonType)) {
return WriteMapping.sliceMapping("jsonb", typedVarcharWriteFunction("json"));
}
if (type.equals(uuidType)) {
return WriteMapping.sliceMapping("uuid", uuidWriteFunction());
}
if (type instanceof ArrayType && getArrayMapping(session) == AS_ARRAY) {
Type elementType = ((ArrayType) type).getElementType();
String elementDataType = toWriteMapping(session, elementType).getDataType();
return WriteMapping.objectMapping(elementDataType + "[]", arrayWriteFunction(session, elementType, getArrayElementPgTypeName(session, this, elementType)));
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Aggregations