use of io.trino.spi.type.TimestampType.TIMESTAMP_SECONDS in project trino by trinodb.
the class ClickHouseClient method toColumnMapping.
@Override
public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
String jdbcTypeName = typeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(JDBC_ERROR, "Type name is missing: " + typeHandle));
Optional<ColumnMapping> mapping = getForcedMappingToVarchar(typeHandle);
if (mapping.isPresent()) {
return mapping;
}
ClickHouseColumn column = ClickHouseColumn.of("", jdbcTypeName);
ClickHouseDataType columnDataType = column.getDataType();
switch(columnDataType) {
case UInt8:
return Optional.of(ColumnMapping.longMapping(SMALLINT, ResultSet::getShort, uInt8WriteFunction()));
case UInt16:
return Optional.of(ColumnMapping.longMapping(INTEGER, ResultSet::getInt, uInt16WriteFunction()));
case UInt32:
return Optional.of(ColumnMapping.longMapping(BIGINT, ResultSet::getLong, uInt32WriteFunction()));
case UInt64:
return Optional.of(ColumnMapping.objectMapping(UINT64_TYPE, longDecimalReadFunction(UINT64_TYPE, UNNECESSARY), uInt64WriteFunction()));
case IPv4:
return Optional.of(ipAddressColumnMapping("IPv4StringToNum(?)"));
case IPv6:
return Optional.of(ipAddressColumnMapping("IPv6StringToNum(?)"));
case Enum8:
case Enum16:
return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), varcharReadFunction(createUnboundedVarcharType()), varcharWriteFunction(), // TODO (https://github.com/trinodb/trino/issues/7100) Currently pushdown would not work and may require a custom bind expression
DISABLE_PUSHDOWN));
// FixedString(n)
case FixedString:
case String:
if (isMapStringAsVarchar(session)) {
return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), varcharReadFunction(createUnboundedVarcharType()), varcharWriteFunction(), DISABLE_PUSHDOWN));
}
// TODO (https://github.com/trinodb/trino/issues/7100) test & enable predicate pushdown
return Optional.of(varbinaryColumnMapping());
case UUID:
return Optional.of(uuidColumnMapping());
default:
}
switch(typeHandle.getJdbcType()) {
case Types.TINYINT:
return Optional.of(tinyintColumnMapping());
case Types.SMALLINT:
return Optional.of(smallintColumnMapping());
case Types.INTEGER:
return Optional.of(integerColumnMapping());
case Types.BIGINT:
return Optional.of(bigintColumnMapping());
case Types.FLOAT:
case Types.REAL:
return Optional.of(ColumnMapping.longMapping(REAL, (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)), realWriteFunction(), DISABLE_PUSHDOWN));
case Types.DOUBLE:
return Optional.of(doubleColumnMapping());
case Types.DECIMAL:
int decimalDigits = typeHandle.getRequiredDecimalDigits();
int precision = typeHandle.getRequiredColumnSize();
ColumnMapping decimalColumnMapping;
if (getDecimalRounding(session) == ALLOW_OVERFLOW && precision > Decimals.MAX_PRECISION) {
int scale = Math.min(decimalDigits, getDecimalDefaultScale(session));
decimalColumnMapping = decimalColumnMapping(createDecimalType(Decimals.MAX_PRECISION, scale), getDecimalRoundingMode(session));
} else {
decimalColumnMapping = decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0)));
}
return Optional.of(new ColumnMapping(decimalColumnMapping.getType(), decimalColumnMapping.getReadFunction(), decimalColumnMapping.getWriteFunction(), // TODO (https://github.com/trinodb/trino/issues/7100) fix, enable and test decimal pushdown
DISABLE_PUSHDOWN));
case Types.DATE:
return Optional.of(dateColumnMappingUsingLocalDate());
case Types.TIMESTAMP:
if (columnDataType == ClickHouseDataType.DateTime) {
verify(typeHandle.getRequiredDecimalDigits() == 0, "Expected 0 as timestamp precision, but got %s", typeHandle.getRequiredDecimalDigits());
return Optional.of(ColumnMapping.longMapping(TIMESTAMP_SECONDS, timestampReadFunction(TIMESTAMP_SECONDS), timestampSecondsWriteFunction()));
}
// TODO (https://github.com/trinodb/trino/issues/10537) Add support for Datetime64 type
return Optional.of(timestampColumnMappingUsingSqlTimestampWithRounding(TIMESTAMP_MILLIS));
}
return Optional.empty();
}
use of io.trino.spi.type.TimestampType.TIMESTAMP_SECONDS in project trino by trinodb.
the class OracleClient method toColumnMapping.
@Override
public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) {
String jdbcTypeName = typeHandle.getJdbcTypeName().orElseThrow(() -> new TrinoException(JDBC_ERROR, "Type name is missing: " + typeHandle));
Optional<ColumnMapping> mappingToVarchar = getForcedMappingToVarchar(typeHandle);
if (mappingToVarchar.isPresent()) {
return mappingToVarchar;
}
if (jdbcTypeName.equalsIgnoreCase("date")) {
return Optional.of(ColumnMapping.longMapping(TIMESTAMP_SECONDS, oracleTimestampReadFunction(), trinoTimestampToOracleDateWriteFunction(), FULL_PUSHDOWN));
}
switch(typeHandle.getJdbcType()) {
case Types.SMALLINT:
return Optional.of(ColumnMapping.longMapping(SMALLINT, ResultSet::getShort, smallintWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.BINARY_FLOAT:
return Optional.of(ColumnMapping.longMapping(REAL, (resultSet, columnIndex) -> floatToRawIntBits(resultSet.getFloat(columnIndex)), oracleRealWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.BINARY_DOUBLE:
case OracleTypes.FLOAT:
return Optional.of(ColumnMapping.doubleMapping(DOUBLE, ResultSet::getDouble, oracleDoubleWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.NUMBER:
int actualPrecision = typeHandle.getRequiredColumnSize();
int decimalDigits = typeHandle.getRequiredDecimalDigits();
// Map negative scale to decimal(p+s, 0).
int precision = actualPrecision + max(-decimalDigits, 0);
int scale = max(decimalDigits, 0);
Optional<Integer> numberDefaultScale = getNumberDefaultScale(session);
RoundingMode roundingMode = getNumberRoundingMode(session);
if (precision < scale) {
if (roundingMode == RoundingMode.UNNECESSARY) {
break;
}
scale = min(Decimals.MAX_PRECISION, scale);
precision = scale;
} else if (numberDefaultScale.isPresent() && precision == PRECISION_OF_UNSPECIFIED_NUMBER) {
precision = Decimals.MAX_PRECISION;
scale = numberDefaultScale.get();
} else if (precision > Decimals.MAX_PRECISION || actualPrecision <= 0) {
break;
}
DecimalType decimalType = createDecimalType(precision, scale);
// JDBC driver can return BigDecimal with lower scale than column's scale when there are trailing zeroes
if (decimalType.isShort()) {
return Optional.of(ColumnMapping.longMapping(decimalType, shortDecimalReadFunction(decimalType, roundingMode), shortDecimalWriteFunction(decimalType), FULL_PUSHDOWN));
}
return Optional.of(ColumnMapping.objectMapping(decimalType, longDecimalReadFunction(decimalType, roundingMode), longDecimalWriteFunction(decimalType), FULL_PUSHDOWN));
case OracleTypes.CHAR:
case OracleTypes.NCHAR:
CharType charType = createCharType(typeHandle.getRequiredColumnSize());
return Optional.of(ColumnMapping.sliceMapping(charType, charReadFunction(charType), oracleCharWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.VARCHAR:
case OracleTypes.NVARCHAR:
return Optional.of(ColumnMapping.sliceMapping(createVarcharType(typeHandle.getRequiredColumnSize()), (varcharResultSet, varcharColumnIndex) -> utf8Slice(varcharResultSet.getString(varcharColumnIndex)), varcharWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.CLOB:
case OracleTypes.NCLOB:
return Optional.of(ColumnMapping.sliceMapping(createUnboundedVarcharType(), (resultSet, columnIndex) -> utf8Slice(resultSet.getString(columnIndex)), varcharWriteFunction(), DISABLE_PUSHDOWN));
// Oracle's RAW(n)
case OracleTypes.VARBINARY:
case OracleTypes.BLOB:
return Optional.of(ColumnMapping.sliceMapping(VARBINARY, (resultSet, columnIndex) -> wrappedBuffer(resultSet.getBytes(columnIndex)), varbinaryWriteFunction(), DISABLE_PUSHDOWN));
case OracleTypes.TIMESTAMP:
return Optional.of(ColumnMapping.longMapping(TIMESTAMP_MILLIS, oracleTimestampReadFunction(), trinoTimestampToOracleTimestampWriteFunction(), FULL_PUSHDOWN));
case OracleTypes.TIMESTAMPTZ:
return Optional.of(oracleTimestampWithTimeZoneColumnMapping());
}
if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
return mapToUnboundedVarchar(typeHandle);
}
return Optional.empty();
}
Aggregations