Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/main/sphinx/connector/exasol.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ Trino data type mapping:
-
* - `DATE`
- `DATE`
-
* - `TIMESTAMP(n)`
- `TIMESTAMP(n)`
-
* - `TIMESTAMP(n) WITH LOCAL TIME ZONE`
- `TIMESTAMP(n)`
-
* - `HASHTYPE`
- `VARBINARY`
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import io.trino.plugin.jdbc.JdbcTypeHandle;
import io.trino.plugin.jdbc.LongReadFunction;
import io.trino.plugin.jdbc.LongWriteFunction;
import io.trino.plugin.jdbc.ObjectReadFunction;
import io.trino.plugin.jdbc.ObjectWriteFunction;
import io.trino.plugin.jdbc.QueryBuilder;
import io.trino.plugin.jdbc.SliceReadFunction;
import io.trino.plugin.jdbc.SliceWriteFunction;
Expand All @@ -43,12 +45,18 @@
import io.trino.spi.connector.ColumnPosition;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.type.LongTimestamp;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.Type;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
Expand All @@ -57,20 +65,27 @@
import java.util.Set;
import java.util.function.BiFunction;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.plugin.jdbc.PredicatePushdownController.FULL_PUSHDOWN;
import static io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.fromLongTrinoTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.fromTrinoTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.integerColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.toLongTrinoTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.toTrinoTimestamp;
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling;
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.connector.ConnectorMetadata.MODIFYING_ROWS_MESSAGE;
import static io.trino.spi.type.DateType.DATE;
import static io.trino.spi.type.DecimalType.createDecimalType;
import static io.trino.spi.type.TimestampType.createTimestampType;
import static io.trino.spi.type.VarbinaryType.VARBINARY;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
Expand All @@ -79,11 +94,15 @@
public class ExasolClient
extends BaseJdbcClient
{
private static final int EXASOL_TIMESTAMP_WITH_TIMEZONE = 124;

private static final Set<String> INTERNAL_SCHEMAS = ImmutableSet.<String>builder()
.add("EXA_STATISTICS")
.add("SYS")
.build();

private static final int MAX_EXASOL_TIMESTAMP_PRECISION = 9;

@Inject
public ExasolClient(
BaseJdbcConfig config,
Expand Down Expand Up @@ -239,8 +258,13 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect
// String data is sorted by its binary representation.
// https://docs.exasol.com/db/latest/sql/select.htm#UsageNotes
return Optional.of(defaultVarcharColumnMapping(typeHandle.requiredColumnSize(), true));
// DATE, TIMESTAMP and TIMESTAMP WITH LOCAL TIME ZONE types are described here in more details:
// https://docs.exasol.com/db/latest/sql_references/data_types/datatypedetails.htm
case Types.DATE:
return Optional.of(dateColumnMapping());
case Types.TIMESTAMP:
case EXASOL_TIMESTAMP_WITH_TIMEZONE:
return Optional.of(timestampColumnMapping(typeHandle));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The https://docs.exasol.com/db/latest/sql_references/data_types/datatypedetails.htm#Dateandtimedatatypes shows that timestamp with local time zone type actually has timezone concept, but just store as UTC.

How about support only TIMESTAMP type first in this single pr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The https://docs.exasol.com/db/latest/sql_references/data_types/datatypedetails.htm#Dateandtimedatatypes shows that timestamp with local time zone type actually has timezone concept, but just store as UTC.

Yes, it has some differences in the time zone behavior, than usual TIMESTAMP, which are reflected in the tests, but when we get the actual column value with trino connector reader we don't know, in which time zone it was saved. So if we map to Trino "Timestamp With Time Zone" we must provide some time zone, which could be hardcoded UTC or JVM Time Zone. But this approach is more fragile and in some extreme cases like DLT, could even provide incorrect mapping. So the solution of mapping to Trino Timestamp is more simple and less error-prone.

How about support only TIMESTAMP type first in this single pr?

The implementations are actually very similar and the second PR would be a change of just two lines of code in the main source and one line in documentation (the tests are also very similar for each type).
This is due to a more simple solution, which maps to Trino Timestamp for both Exasol Types.

Copy link
Member Author

@skyglass skyglass Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your review, @chenjian2664 👍

The new PR, which contains only TIMESTAMP changes, has been created:
#26963

I will close this PR

}

if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
Expand All @@ -256,6 +280,128 @@ private boolean isHashType(JdbcTypeHandle typeHandle)
&& typeHandle.jdbcTypeName().get().equalsIgnoreCase("HASHTYPE");
}

private static ColumnMapping timestampColumnMapping(JdbcTypeHandle typeHandle)
{
int timestampPrecision = typeHandle.requiredDecimalDigits();
TimestampType timestampType = createTimestampType(timestampPrecision);
if (timestampType.isShort()) {
return ColumnMapping.longMapping(
timestampType,
longTimestampReadFunction(timestampType),
longTimestampWriteFunction(timestampType),
FULL_PUSHDOWN);
}
return ColumnMapping.objectMapping(
timestampType,
objectTimestampReadFunction(timestampType),
objectTimestampWriteFunction(timestampType),
FULL_PUSHDOWN);
}

private static LongReadFunction longTimestampReadFunction(TimestampType timestampType)
{
return (resultSet, columnIndex) -> {
Timestamp timestamp = resultSet.getTimestamp(columnIndex);
return toTrinoTimestamp(timestampType, timestamp.toLocalDateTime());
};
}

private static LongWriteFunction longTimestampWriteFunction(TimestampType timestampType)
{
return new LongWriteFunction()
{
@Override
public String getBindExpression()
{
return getTimestampBindExpression(timestampType.getPrecision());
}

@Override
public void set(PreparedStatement statement, int index, long epochMicros)
throws SQLException
{
LocalDateTime localDateTime = fromTrinoTimestamp(epochMicros);
Timestamp timestampValue = Timestamp.valueOf(localDateTime);
statement.setTimestamp(index, timestampValue);
}

@Override
public void setNull(PreparedStatement statement, int index)
throws SQLException
{
statement.setNull(index, Types.VARCHAR);
}
};
}

private static ObjectReadFunction objectTimestampReadFunction(TimestampType timestampType)
{
verifyObjectTimestampPrecision(timestampType);
return ObjectReadFunction.of(
LongTimestamp.class,
(resultSet, columnIndex) -> {
Timestamp timestamp = resultSet.getTimestamp(columnIndex);
return toLongTrinoTimestamp(timestampType, timestamp.toLocalDateTime());
});
}

private static ObjectWriteFunction objectTimestampWriteFunction(TimestampType timestampType)
{
int precision = timestampType.getPrecision();
verifyObjectTimestampPrecision(timestampType);

return new ObjectWriteFunction() {
@Override
public Class<?> getJavaType()
{
return LongTimestamp.class;
}

@Override
public void set(PreparedStatement statement, int index, Object value)
throws SQLException
{
LocalDateTime localDateTime = fromLongTrinoTimestamp((LongTimestamp) value, precision);
Timestamp timestamp = Timestamp.valueOf(localDateTime);
statement.setTimestamp(index, timestamp);
}

@Override
public String getBindExpression()
{
return getTimestampBindExpression(timestampType.getPrecision());
}

@Override
public void setNull(PreparedStatement statement, int index)
throws SQLException
{
statement.setNull(index, Types.VARCHAR);
}
};
}

private static void verifyObjectTimestampPrecision(TimestampType timestampType)
{
int precision = timestampType.getPrecision();
checkArgument(precision > TimestampType.MAX_SHORT_PRECISION && precision <= MAX_EXASOL_TIMESTAMP_PRECISION,
"Precision is out of range: %s", precision);
}

/**
* Returns a {@code TO_TIMESTAMP} bind expression using the appropriate format model
* based on the given fractional seconds precision.
* See for more details: <a href="https://docs.exasol.com/db/latest/sql_references/formatmodels.htm">Date/time format models</a>
*/
private static String getTimestampBindExpression(int precision)
{
checkArgument(precision >= 0, "Precision is negative: %s", precision);
if (precision == 0) {
return "TO_TIMESTAMP(?, 'YYYY-MM-DD HH24:MI:SS')";
}
return format("TO_TIMESTAMP(?, 'YYYY-MM-DD HH24:MI:SS.FF%d')", precision);
}

private static ColumnMapping dateColumnMapping()
{
// Exasol driver does not support LocalDate
Expand Down
Loading