Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
Upgrade trino to 363 (#79)
Browse files Browse the repository at this point in the history
* Update to 363

* Pick up changes from 363

* Inline legacycolumnMapping method
  • Loading branch information
meneal authored Oct 18, 2021
1 parent 2211ac7 commit 7034138
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.trino</groupId>
<artifactId>trino-root</artifactId>
<version>359</version>
<version>363</version>
</parent>

<artifactId>trino-db2</artifactId>
Expand Down
93 changes: 88 additions & 5 deletions src/main/java/io/trino/plugin/db2/DB2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.type.Decimals;
import io.trino.spi.type.LongTimestamp;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.Type;
Expand All @@ -45,13 +46,32 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;
import static io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static io.trino.plugin.jdbc.StandardColumnMappings.fromLongTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.dateColumnMapping;
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.integerColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.realColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.smallintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.timeColumnMappingUsingSqlTime;
import static io.trino.plugin.jdbc.StandardColumnMappings.timestampWriteFunction;
import static io.trino.plugin.jdbc.StandardColumnMappings.timestampWriteFunctionUsingSqlTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.toLongTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.tinyintColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.toLongTrinoTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.toTrinoTimestamp;
import static io.trino.plugin.jdbc.StandardColumnMappings.varbinaryColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.varcharColumnMapping;
import static io.trino.plugin.jdbc.StandardColumnMappings.varcharWriteFunction;
import static io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling;
import static io.trino.plugin.jdbc.UnsupportedTypeHandling.CONVERT_TO_VARCHAR;
import static io.trino.spi.type.DecimalType.createDecimalType;
import static io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS;
import static io.trino.spi.type.VarcharType.createUnboundedVarcharType;
import static java.lang.Math.max;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -107,14 +127,77 @@ public Optional<ColumnMapping> toColumnMapping(ConnectorSession session, Connect
}

switch (typeHandle.getJdbcType()) {
case Types.BIT:
case Types.BOOLEAN:
return Optional.of(booleanColumnMapping());

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.REAL:
return Optional.of(realColumnMapping());

case Types.FLOAT:
case Types.DOUBLE:
return Optional.of(doubleColumnMapping());

case Types.NUMERIC:
case Types.DECIMAL:
int decimalDigits = typeHandle.getRequiredDecimalDigits();
int precision = typeHandle.getRequiredColumnSize() + max(-decimalDigits, 0); // Map decimal(p, -s) (negative scale) to decimal(p+s, 0).
if (precision > Decimals.MAX_PRECISION) {
break;
}
return Optional.of(decimalColumnMapping(createDecimalType(precision, max(decimalDigits, 0))));

case Types.CHAR:
case Types.NCHAR:
return Optional.of(defaultCharColumnMapping(typeHandle.getRequiredColumnSize(), false));

case Types.VARCHAR:
int columnSize = typeHandle.getRequiredColumnSize();
if (columnSize == -1) {
return Optional.of(varcharColumnMapping(createUnboundedVarcharType(), true));
}
return Optional.of(defaultVarcharColumnMapping(columnSize, true));

case Types.NVARCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
return Optional.of(defaultVarcharColumnMapping(typeHandle.getRequiredColumnSize(), false));

case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return Optional.of(varbinaryColumnMapping());

case Types.DATE:
return Optional.of(dateColumnMapping());

case Types.TIME:
// TODO Consider using `StandardColumnMappings.timeColumnMapping`
return Optional.of(timeColumnMappingUsingSqlTime());

case Types.TIMESTAMP:
TimestampType timestampType = typeHandle.getDecimalDigits()
.map(TimestampType::createTimestampType)
.orElse(TIMESTAMP_MILLIS);
return Optional.of(timestampColumnMapping(timestampType));
}

return super.legacyColumnMapping(session, connection, typeHandle);
if (getUnsupportedTypeHandling(session) == CONVERT_TO_VARCHAR) {
return mapToUnboundedVarchar(typeHandle);
}
return Optional.empty();
}

public static ColumnMapping timestampColumnMapping(TimestampType timestampType)
Expand Down Expand Up @@ -151,7 +234,7 @@ private static ObjectReadFunction longtimestampReadFunction(TimestampType timest
"Precision is out of range: %s", timestampType.getPrecision());
return ObjectReadFunction.of(
LongTimestamp.class,
(resultSet, columnIndex) -> toLongTimestamp(timestampType, resultSet.getTimestamp(columnIndex).toLocalDateTime()));
(resultSet, columnIndex) -> toLongTrinoTimestamp(timestampType, resultSet.getTimestamp(columnIndex).toLocalDateTime()));
}

/**
Expand All @@ -165,7 +248,7 @@ private static ObjectWriteFunction longTimestampWriteFunction(TimestampType time
checkArgument(timestampType.getPrecision() > TimestampType.MAX_SHORT_PRECISION, "Precision is out of range: %s", timestampType.getPrecision());
return ObjectWriteFunction.of(
LongTimestamp.class,
(statement, index, value) -> statement.setTimestamp(index, Timestamp.valueOf(fromLongTimestamp(value, timestampType.getPrecision()))));
(statement, index, value) -> statement.setTimestamp(index, Timestamp.valueOf(fromLongTrinoTimestamp(value, timestampType.getPrecision()))));
}

/**
Expand Down

0 comments on commit 7034138

Please sign in to comment.