Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade ClickHouse JDBC driver to 0.5.0 #19342

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
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
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 new code are almost the same as MySqlClient.

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