use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class SqlServerClient method toWriteMapping.
@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
if (type == BOOLEAN) {
return WriteMapping.booleanMapping("bit", 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 VarcharType) {
VarcharType varcharType = (VarcharType) type;
String dataType;
if (varcharType.isUnbounded() || varcharType.getBoundedLength() > 4000) {
dataType = "nvarchar(max)";
} else {
dataType = "nvarchar(" + varcharType.getBoundedLength() + ")";
}
return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
String dataType;
if (charType.getLength() > 4000) {
dataType = "nvarchar(max)";
} else {
dataType = "nchar(" + charType.getLength() + ")";
}
return WriteMapping.sliceMapping(dataType, charWriteFunction());
}
if (type instanceof VarbinaryType) {
return WriteMapping.sliceMapping("varbinary(max)", varbinaryWriteFunction());
}
if (type == DATE) {
return WriteMapping.longMapping("date", sqlServerDateWriteFunction());
}
if (type instanceof TimeType) {
TimeType timeType = (TimeType) type;
int precision = min(timeType.getPrecision(), MAX_SUPPORTED_TEMPORAL_PRECISION);
String dataType = format("time(%d)", precision);
return WriteMapping.longMapping(dataType, sqlServerTimeWriteFunction(precision));
}
if (type instanceof TimestampType) {
TimestampType timestampType = (TimestampType) type;
int precision = min(timestampType.getPrecision(), MAX_SUPPORTED_TEMPORAL_PRECISION);
String dataType = format("datetime2(%d)", precision);
if (timestampType.getPrecision() <= MAX_SHORT_PRECISION) {
return WriteMapping.longMapping(dataType, timestampWriteFunction(timestampType));
}
return WriteMapping.objectMapping(dataType, longTimestampWriteFunction(timestampType, precision));
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class TypeUtils method prestoNativeToJdbcObject.
private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) throws SQLException {
if (prestoNative == null) {
return null;
}
if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
return prestoNative;
}
if (prestoType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) prestoType;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = ((Int128) prestoNative).toBigInteger();
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(prestoType)) {
return intBitsToFloat(toIntExact((long) prestoNative));
}
if (TINYINT.equals(prestoType)) {
return SignedBytes.checkedCast((long) prestoNative);
}
if (SMALLINT.equals(prestoType)) {
return Shorts.checkedCast((long) prestoNative);
}
if (INTEGER.equals(prestoType)) {
return toIntExact((long) prestoNative);
}
if (DATE.equals(prestoType)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) prestoNative);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (prestoType instanceof TimestampType && ((TimestampType) prestoType).isShort()) {
return toPgTimestamp(fromTrinoTimestamp((long) prestoNative));
}
if (prestoType instanceof TimestampWithTimeZoneType) {
// PostgreSQL does not store zone, only the point in time
int precision = ((TimestampWithTimeZoneType) prestoType).getPrecision();
if (precision <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
long millisUtc = unpackMillisUtc((long) prestoNative);
return new Timestamp(millisUtc);
} else {
LongTimestampWithTimeZone value = (LongTimestampWithTimeZone) prestoNative;
long epochSeconds = floorDiv(value.getEpochMillis(), MILLISECONDS_PER_SECOND);
long nanosOfSecond = floorMod(value.getEpochMillis(), MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND + value.getPicosOfMilli() / PICOSECONDS_PER_NANOSECOND;
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds, nanosOfSecond), UTC_KEY.getZoneId());
}
}
if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
return ((Slice) prestoNative).toStringUtf8();
}
if (prestoType instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
Aggregations