Skip to content

Commit

Permalink
Never rename column when retrieving JDBC metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed May 23, 2022
1 parent e495b45 commit 0a171ad
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@ protected List<ClickHouseColumn> readColumns() throws IOException {
.getOption(ClickHouseClientOption.RENAME_RESPONSE_COLUMN);
List<ClickHouseColumn> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static ClickHouseResponse of(ClickHouseConfig config, List<ClickHouseColu
public static ClickHouseResponse of(ClickHouseConfig config, List<ClickHouseColumn> columns, Object[][] values,
ClickHouseResponseSummary summary) {
if (columns == null || columns.isEmpty()) {
return EMPTY;
return ClickHouseResponse.EMPTY;
}

int size = columns.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1255,8 +1259,12 @@ public ResultSet getClientInfoProperties() throws SQLException {
public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern)
throws SQLException {
Map<String, String> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, "");
}
Expand Down Expand Up @@ -189,6 +191,7 @@ protected List<ClickHouseColumn> getTableColumns(String dbName, String tableName
builder.append('`').append(ClickHouseUtils.escape(tableName, '`')).append('`').append(" WHERE 0");
List<ClickHouseColumn> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,18 +124,20 @@ protected ClickHouseResponse executeStatement(String stmt,
}
if (tables != null && !tables.isEmpty()) {
List<ClickHouseExternalTable> 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);
}
Expand Down

0 comments on commit 0a171ad

Please sign in to comment.