From 0a171adb7a751607746a88982c01768b8ba330a9 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Mon, 23 May 2022 19:24:59 +0800 Subject: [PATCH] Never rename column when retrieving JDBC metadata --- .../client/data/ClickHouseRowBinaryProcessor.java | 3 +-- .../client/data/ClickHouseSimpleResponse.java | 2 +- .../jdbc/ClickHouseDatabaseMetaData.java | 14 +++++++++++--- .../jdbc/internal/ClickHouseConnectionImpl.java | 5 ++++- .../jdbc/internal/ClickHouseStatementImpl.java | 15 +++++++++------ 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseRowBinaryProcessor.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseRowBinaryProcessor.java index dce29b064..1c19a27dd 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseRowBinaryProcessor.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseRowBinaryProcessor.java @@ -547,8 +547,7 @@ protected List readColumns() throws IOException { .getOption(ClickHouseClientOption.RENAME_RESPONSE_COLUMN); List columns = new ArrayList<>(size); for (int i = 0; i < size; i++) { - // a bit risky here - what if ClickHouse support user type? - columns.add(ClickHouseColumn.of(m.rename(names[i]), input.readAsciiString())); + columns.add(ClickHouseColumn.of(m.rename(names[i]), input.readUnicodeString())); } return columns; diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseSimpleResponse.java b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseSimpleResponse.java index 981726cf0..62e5eca49 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseSimpleResponse.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/data/ClickHouseSimpleResponse.java @@ -52,7 +52,7 @@ public static ClickHouseResponse of(ClickHouseConfig config, List columns, Object[][] values, ClickHouseResponseSummary summary) { if (columns == null || columns.isEmpty()) { - return EMPTY; + return ClickHouseResponse.EMPTY; } int size = columns.size(); diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaData.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaData.java index 981141e22..a16d67c82 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaData.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaData.java @@ -22,6 +22,8 @@ import com.clickhouse.client.ClickHouseParameterizedQuery; import com.clickhouse.client.ClickHouseUtils; import com.clickhouse.client.ClickHouseValues; +import com.clickhouse.client.config.ClickHouseClientOption; +import com.clickhouse.client.config.ClickHouseRenameMethod; import com.clickhouse.client.data.ClickHouseRecordTransformer; import com.clickhouse.client.data.ClickHouseSimpleResponse; import com.clickhouse.client.logging.Logger; @@ -66,7 +68,9 @@ protected ResultSet query(String sql, ClickHouseRecordTransformer func, boolean ClickHouseStatement stmt = connection.createStatement(); return new ClickHouseResultSet("", "", stmt, // load everything into memory - ClickHouseSimpleResponse.of(stmt.getRequest().query(sql).execute().get(), func)); + ClickHouseSimpleResponse.of(stmt.getRequest() + .option(ClickHouseClientOption.RENAME_RESPONSE_COLUMN, ClickHouseRenameMethod.NONE) + .query(sql).execute().get(), func)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw SqlExceptionUtils.forCancellation(e); @@ -1255,8 +1259,12 @@ public ResultSet getClientInfoProperties() throws SQLException { public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException { Map params = new HashMap<>(); - params.put("filter", ClickHouseChecker.isNullOrEmpty(schemaPattern) - || "system".contains(schemaPattern.toLowerCase(Locale.ROOT)) ? "1" : "0"); + boolean systemSchema = ClickHouseChecker.isNullOrEmpty(schemaPattern); + if (!systemSchema) { + String schemaPatternLower = schemaPattern.toLowerCase(Locale.ROOT); + systemSchema = "system".contains(schemaPatternLower) || "information_schema".contains(schemaPatternLower); + } + params.put("filter", systemSchema ? "1" : "0"); params.put("pattern", ClickHouseChecker.isNullOrEmpty(functionNamePattern) ? "'%'" : ClickHouseValues.convertToQuotedString(functionNamePattern)); diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java index 3df1afb69..a6890573e 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseConnectionImpl.java @@ -40,6 +40,7 @@ import com.clickhouse.client.ClickHouseValues; import com.clickhouse.client.ClickHouseVersion; import com.clickhouse.client.config.ClickHouseClientOption; +import com.clickhouse.client.config.ClickHouseRenameMethod; import com.clickhouse.client.http.config.ClickHouseHttpOption; import com.clickhouse.client.logging.Logger; import com.clickhouse.client.logging.LoggerFactory; @@ -65,7 +66,8 @@ public class ClickHouseConnectionImpl extends JdbcWrapper implements ClickHouseC protected static ClickHouseRecord getServerInfo(ClickHouseNode node, ClickHouseRequest request, boolean createDbIfNotExist) throws SQLException { - ClickHouseRequest newReq = request.copy(); + ClickHouseRequest newReq = request.copy().option(ClickHouseClientOption.RENAME_RESPONSE_COLUMN, + ClickHouseRenameMethod.NONE); if (!createDbIfNotExist) { // in case the database does not exist newReq.option(ClickHouseClientOption.DATABASE, ""); } @@ -189,6 +191,7 @@ protected List getTableColumns(String dbName, String tableName builder.append('`').append(ClickHouseUtils.escape(tableName, '`')).append('`').append(" WHERE 0"); List list; try (ClickHouseResponse resp = clientRequest.copy().format(ClickHouseFormat.RowBinaryWithNamesAndTypes) + .option(ClickHouseClientOption.RENAME_RESPONSE_COLUMN, ClickHouseRenameMethod.NONE) .query(builder.toString()).execute().get()) { list = resp.getColumns(); } catch (InterruptedException e) { diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java index f177b4f25..9099d2063 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java @@ -23,6 +23,7 @@ import com.clickhouse.client.ClickHouseResponse; import com.clickhouse.client.ClickHouseResponseSummary; import com.clickhouse.client.ClickHouseSerializer; +import com.clickhouse.client.ClickHouseUtils; import com.clickhouse.client.ClickHouseValue; import com.clickhouse.client.config.ClickHouseClientOption; import com.clickhouse.client.config.ClickHouseConfigChangeListener; @@ -123,18 +124,20 @@ protected ClickHouseResponse executeStatement(String stmt, } if (tables != null && !tables.isEmpty()) { List list = new ArrayList<>(tables.size()); + char quote = '`'; for (ClickHouseExternalTable t : tables) { if (t.isTempTable()) { if (!request.getSessionId().isPresent()) { request.session(UUID.randomUUID().toString()); } - request.query("drop temporary table if exists `" + t.getName() + "`").execute().get(); - request.query("create temporary table `" + t.getName() + "`(" + t.getStructure() + ")") - .execute().get(); - request.write() - .table(t.getName()) + String tableName = new StringBuilder().append(quote) + .append(ClickHouseUtils.escape(t.getName(), quote)).append(quote).toString(); + request.query("drop temporary table if exists ".concat(tableName)).executeAndWait(); + request.query("create temporary table " + tableName + "(" + t.getStructure() + ")") + .executeAndWait(); + request.write().table(tableName) // .format(t.getFormat() != null ? t.getFormat() : ClickHouseFormat.RowBinary) - .data(t.getContent()).sendAndWait(); + .data(t.getContent()).send().get(); } else { list.add(t); }