Skip to content

Commit

Permalink
Upgrade ClickHouse JDBC driver to 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankvadariya authored and ebyhr committed Oct 11, 2023
1 parent ee72515 commit b685d9a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -86,6 +87,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -243,6 +245,26 @@ public boolean supportsAggregationPushdown(ConnectorSession session, JdbcTableHa
return preventTextualTypeAggregationPushdown(groupingSets);
}

@Override
public ResultSet getTables(Connection connection, Optional<String> schemaName, Optional<String> tableName)
throws SQLException
{
// Clickhouse maps their "database" to SQL catalogs and does not have schemas
DatabaseMetaData metadata = connection.getMetaData();
return metadata.getTables(
schemaName.orElse(null),
null,
escapeObjectNameForMetadataQuery(tableName, metadata.getSearchStringEscape()).orElse(null),
getTableTypes().map(types -> types.toArray(String[]::new)).orElse(null));
}

@Override
protected String getTableSchemaName(ResultSet resultSet)
throws SQLException
{
return resultSet.getString("TABLE_CAT");
}

private static Optional<JdbcTypeHandle> toTypeHandle(DecimalType decimalType)
{
return Optional.of(new JdbcTypeHandle(Types.DECIMAL, Optional.of("Decimal"), Optional.of(decimalType.getPrecision()), Optional.of(decimalType.getScale()), Optional.empty(), Optional.empty()));
Expand All @@ -255,6 +277,9 @@ protected String quoted(@Nullable String catalog, @Nullable String schema, Strin
if (!isNullOrEmpty(schema)) {
sb.append(quoted(schema)).append(".");
}
else if (!isNullOrEmpty(catalog)) {
sb.append(quoted(catalog)).append(".");
}
sb.append(quoted(table));
return sb.toString();
}
Expand All @@ -278,6 +303,26 @@ protected void copyTableSchema(ConnectorSession session, Connection connection,
}
}

@Override
public Collection<String> listSchemas(Connection connection)
{
// for Clickhouse, we need to list catalogs instead of schemas
try (ResultSet resultSet = connection.getMetaData().getCatalogs()) {
ImmutableSet.Builder<String> schemaNames = ImmutableSet.builder();
while (resultSet.next()) {
String schemaName = resultSet.getString("TABLE_CAT");
// skip internal schemas
if (filterSchema(schemaName)) {
schemaNames.add(schemaName);
}
}
return schemaNames.build();
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

@Override
public Optional<String> getTableComment(ResultSet resultSet)
throws SQLException
Expand Down Expand Up @@ -315,7 +360,7 @@ public Map<String, Object> getTableProperties(ConnectorSession session, JdbcTabl
"SELECT engine, sorting_key, partition_key, primary_key, sampling_key " +
"FROM system.tables " +
"WHERE database = ? AND name = ?")) {
statement.setString(1, tableHandle.asPlainTable().getRemoteTableName().getSchemaName().orElse(null));
statement.setString(1, tableHandle.asPlainTable().getRemoteTableName().getCatalogName().orElse(null));
statement.setString(2, tableHandle.asPlainTable().getRemoteTableName().getTableName());

try (ResultSet resultSet = statement.executeQuery()) {
Expand Down Expand Up @@ -487,11 +532,9 @@ protected Optional<List<String>> getTableTypes()
protected void renameTable(ConnectorSession session, Connection connection, String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
throws SQLException
{
execute(session, connection, format("RENAME TABLE %s.%s TO %s.%s",
quoted(remoteSchemaName),
quoted(remoteTableName),
quoted(newRemoteSchemaName),
quoted(newRemoteTableName)));
execute(session, connection, format("RENAME TABLE %s TO %s",
quoted(catalogName, remoteSchemaName, remoteTableName),
quoted(catalogName, newRemoteSchemaName, newRemoteTableName)));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.4.6</version>
<version>0.5.0</version>
<classifier>all</classifier>
</dependency>

Expand Down

0 comments on commit b685d9a

Please sign in to comment.