From b685d9afb7c1ebb27313f322d37516001c96762f Mon Sep 17 00:00:00 2001 From: Mayank Vadariya <48036907+mayankvadariya@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:35:15 -0400 Subject: [PATCH] Upgrade ClickHouse JDBC driver to 0.5.0 --- .../plugin/clickhouse/ClickHouseClient.java | 55 +++++++++++++++++-- pom.xml | 2 +- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/ClickHouseClient.java b/plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/ClickHouseClient.java index fa7aa58efaef..fb50bcc386b7 100644 --- a/plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/ClickHouseClient.java +++ b/plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/ClickHouseClient.java @@ -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; @@ -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; @@ -243,6 +245,26 @@ public boolean supportsAggregationPushdown(ConnectorSession session, JdbcTableHa return preventTextualTypeAggregationPushdown(groupingSets); } + @Override + public ResultSet getTables(Connection connection, Optional schemaName, Optional 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 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())); @@ -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(); } @@ -278,6 +303,26 @@ protected void copyTableSchema(ConnectorSession session, Connection connection, } } + @Override + public Collection listSchemas(Connection connection) + { + // for Clickhouse, we need to list catalogs instead of schemas + try (ResultSet resultSet = connection.getMetaData().getCatalogs()) { + ImmutableSet.Builder 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 getTableComment(ResultSet resultSet) throws SQLException @@ -315,7 +360,7 @@ public Map 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()) { @@ -487,11 +532,9 @@ protected Optional> 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 diff --git a/pom.xml b/pom.xml index a311ca4dbe41..a8b0235145dc 100644 --- a/pom.xml +++ b/pom.xml @@ -415,7 +415,7 @@ com.clickhouse clickhouse-jdbc - 0.4.6 + 0.5.0 all