diff --git a/client-v2/pom.xml b/client-v2/pom.xml
index 1c6408799..18bd3d6a9 100644
--- a/client-v2/pom.xml
+++ b/client-v2/pom.xml
@@ -82,6 +82,12 @@
compile
+
+
+ com.google.guava
+ guava
+ ${guava.version}
+
diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java
index 0d837f0a1..e559080f9 100644
--- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java
+++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java
@@ -74,12 +74,12 @@ private boolean readBlock() throws IOException {
List names = new ArrayList<>(nColumns);
List types = new ArrayList<>(nColumns);
currentBlock = new Block(names, types, nRows);
- TableSchema schema = new TableSchema();
+ List columns = new ArrayList<>(nColumns);
for (int i = 0; i < nColumns; i++) {
- schema.addColumn(BinaryStreamReader.readString(input),
+ ClickHouseColumn column = ClickHouseColumn.of(BinaryStreamReader.readString(input),
BinaryStreamReader.readString(input));
- ClickHouseColumn column = schema.getColumns().get(i);
+ columns.add(column);
names.add(column.getColumnName());
types.add(column.getDataType().name());
@@ -101,6 +101,7 @@ private boolean readBlock() throws IOException {
}
currentBlock.add(values);
}
+ TableSchema schema = new TableSchema(columns);
setSchema(schema);
diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesAndTypesFormatReader.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesAndTypesFormatReader.java
index e5272602f..7eaa13d06 100644
--- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesAndTypesFormatReader.java
+++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryWithNamesAndTypesFormatReader.java
@@ -5,6 +5,7 @@
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.client.api.query.QuerySettings;
+import com.clickhouse.data.ClickHouseColumn;
import java.io.EOFException;
import java.io.IOException;
@@ -24,8 +25,7 @@ public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySett
private void readSchema() {
try {
- TableSchema headerSchema = new TableSchema();
- List columns = new ArrayList<>();
+ List names = new ArrayList<>();
int nCol;
try {
nCol = BinaryStreamReader.readVarInt(input);
@@ -34,14 +34,16 @@ private void readSchema() {
return;
}
for (int i = 0; i < nCol; i++) {
- columns.add(binaryStreamReader.readString());
+ names.add(binaryStreamReader.readString());
}
+ List columns = new ArrayList<>(nCol);
for (int i = 0; i < nCol; i++) {
- headerSchema.addColumn(columns.get(i), binaryStreamReader.readString());
+ columns.add(ClickHouseColumn.of(names.get(i), binaryStreamReader.readString()));
}
- setSchema(headerSchema);
+ TableSchema schema = new TableSchema(columns);
+ setSchema(schema);
} catch (IOException e) {
throw new ClientException("Failed to read header", e);
}
diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/TableSchemaParser.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/TableSchemaParser.java
index d396b27cb..1b7bbd627 100644
--- a/client-v2/src/main/java/com/clickhouse/client/api/internal/TableSchemaParser.java
+++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/TableSchemaParser.java
@@ -1,35 +1,38 @@
package com.clickhouse.client.api.internal;
import com.clickhouse.client.api.metadata.TableSchema;
+import com.clickhouse.data.ClickHouseColumn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Properties;
public class TableSchemaParser {
public static TableSchema readTSKV(InputStream content, String table, String sqlQuery, String database) {
- TableSchema schema = new TableSchema();
- schema.setTableName(table);
- schema.setQuery(sqlQuery);
- schema.setDatabaseName(database);
Properties p = new Properties();
try (BufferedReader r = new BufferedReader(new InputStreamReader(content))) {
String line;
+ List columns = new ArrayList<>();
while ((line = r.readLine()) != null) {
p.clear();
- int lineLength = line.length();
if (!line.trim().isEmpty()) {
p.load(new StringReader(line.replaceAll("\t", "\n")));
- schema.addColumn(p.getProperty("name"), p.getProperty("type"), p.getProperty("default_type"));
+ ClickHouseColumn column = ClickHouseColumn.of(p.getProperty("name"), p.getProperty("type"));
+ String defaultType = p.getProperty("default_type");
+ column.setHasDefault(defaultType != null && !defaultType.isEmpty());
+ columns.add(column);
}
}
+
+ return new TableSchema(table, sqlQuery, database, columns);
} catch (IOException e) {
throw new RuntimeException("Failed to parse table schema", e);
}
- return schema;
}
}
diff --git a/client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java b/client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java
index 0381e768e..f902ef521 100644
--- a/client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java
+++ b/client-v2/src/main/java/com/clickhouse/client/api/metadata/TableSchema.java
@@ -1,9 +1,11 @@
package com.clickhouse.client.api.metadata;
-import com.clickhouse.client.api.ClientException;
import com.clickhouse.data.ClickHouseColumn;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -11,27 +13,35 @@
public class TableSchema {
- private String tableName = "";
+ private final String tableName;
- private String query = "";
+ private final String query;
- private String databaseName = "";
+ private final String databaseName;
- private List columns;
+ private final List columns;
- private List columnsView;
-
- private Map> metadata;
-
- private Map colIndex;
+ private final Map colIndex;
private boolean hasDefaults = false;
- public TableSchema() {
- this.metadata = new HashMap<>();
- this.columns = new ArrayList<>();
- this.columnsView = Collections.unmodifiableList(this.columns);
- this.colIndex = new HashMap<>();
+ public TableSchema(Collection columns) {
+ this("", "", "", columns);
+ }
+ public TableSchema(String tableName, String query, String databaseName, Collection columns) {
+ this.tableName = tableName;
+ this.databaseName = databaseName;
+ this.query = query;
+ this.columns = ImmutableList.copyOf(columns);
+ ImmutableMap.Builder colIndexMapBuilder = ImmutableMap.builder();
+ for (int i = 0; i < this.columns.size(); i++) {
+ ClickHouseColumn column= this.columns.get(i);
+ if (column.hasDefault()) {
+ this.hasDefaults = true;
+ }
+ colIndexMapBuilder.put(this.columns.get(i).getColumnName(), i);
+ }
+ this.colIndex = colIndexMapBuilder.build();
}
/**
@@ -40,7 +50,7 @@ public TableSchema() {
* @return - collection of columns in the table
*/
public List getColumns() {
- return columnsView;
+ return columns;
}
public String getDatabaseName() {
@@ -51,14 +61,6 @@ public String getTableName() {
return tableName;
}
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
-
- public void setDatabaseName(String databaseName) {
- this.databaseName = databaseName;
- }
-
public boolean hasDefaults() {
return hasDefaults;
}
@@ -67,30 +69,19 @@ public String getQuery() {
return query;
}
- public void setQuery(String query) {
- this.query = query;
- }
-
- public void addColumn(String name, String type) {
- addColumn(name, type, "");
- }
- public void addColumn(String name, String type, String defaultType) {
- ClickHouseColumn column = ClickHouseColumn.of(name, type);
- if (defaultType.toUpperCase().contains("DEFAULT")) {
- hasDefaults = true;
- column.setHasDefault(true);
- }
- columns.add(column);
-
- Map columnMetadata = metadata.computeIfAbsent(name, k -> new HashMap<>());
- columnMetadata.put("type", type);
- colIndex.put(name, columns.size() - 1);
- }
-
public ClickHouseColumn getColumnByName(String name) {
return columns.get(nameToIndex(name));
}
+ /**
+ * Returns column by index. Index starts with 1.
+ * @param colIndex - column index;
+ * @return
+ */
+ public ClickHouseColumn getColumnByIndex(int colIndex) {
+ return columns.get(colIndex - 1);
+ }
+
/**
* Takes absolute index (starting from 0) and returns corresponding column.
*
@@ -134,7 +125,6 @@ public String toString() {
"tableName='" + tableName + '\'' +
", databaseName='" + databaseName + '\'' +
", columns=" + columns +
- ", metadata=" + metadata +
", colIndex=" + colIndex +
", hasDefaults=" + hasDefaults +
'}';
diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java
index 23acee8e8..ebffeb57d 100644
--- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java
+++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java
@@ -27,6 +27,7 @@
import com.clickhouse.client.api.query.QueryResponse;
import com.clickhouse.client.api.query.QuerySettings;
import com.clickhouse.client.api.query.Records;
+import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.ClickHouseVersion;
@@ -440,10 +441,13 @@ public void testBinaryStreamReader() throws Exception {
Future response = client.query("SELECT col1, col3, hostname() as host FROM " + table, settings);
QueryResponse queryResponse = response.get();
- TableSchema schema = new TableSchema();
- schema.addColumn("col1", "UInt32");
- schema.addColumn("col3", "String");
- schema.addColumn("host", "String");
+
+ TableSchema schema = new TableSchema(
+ Arrays.asList(
+ ClickHouseColumn.of("col1", "UInt32"),
+ ClickHouseColumn.of("col3", "String"),
+ ClickHouseColumn.of("host", "String")
+ ));
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse, schema);
int rowsCount = 0;
while (reader.next() != null) {
@@ -472,10 +476,6 @@ public void testRowStreamReader() throws Exception {
Future response = client.query("SELECT col1, col3, hostname() as host FROM " + table, settings);
QueryResponse queryResponse = response.get();
- TableSchema schema = new TableSchema();
- schema.addColumn("col1", "UInt32");
- schema.addColumn("col3", "String");
- schema.addColumn("host", "String");
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(queryResponse);
Map record;
diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
index 0049f1a23..ec0a89b02 100644
--- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
+++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
@@ -17,6 +17,7 @@
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.client.api.query.QueryResponse;
import com.clickhouse.data.ClickHouseColumn;
+import com.clickhouse.data.ClickHouseDataType;
import com.clickhouse.jdbc.internal.ExceptionUtils;
import com.clickhouse.jdbc.internal.JdbcUtils;
import com.clickhouse.jdbc.types.Array;
@@ -431,7 +432,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
@Override
public Object getObject(int columnIndex) throws SQLException {
- return getObject(columnIndexToName(columnIndex));
+ return getObject(columnIndex, JdbcUtils.convertToJavaClass(getSchema().getColumnByIndex(columnIndex).getDataType()));
}
@Override
@@ -959,7 +960,8 @@ public Statement getStatement() throws SQLException {
@Override
public Object getObject(int columnIndex, Map> map) throws SQLException {
- return getObject(columnIndexToName(columnIndex), map);
+ ClickHouseDataType type = getSchema().getColumnByIndex(columnIndex).getDataType();
+ return getObject(columnIndex, map.get(JdbcUtils.convertToSqlType(type).getName()));
}
@Override
@@ -1493,7 +1495,24 @@ public void updateNClob(String columnLabel, Reader reader) throws SQLException {
@Override
public T getObject(int columnIndex, Class type) throws SQLException {
- return getObject(columnIndexToName(columnIndex), type);
+ checkClosed();
+ try {
+ if (reader.hasValue(columnIndex)) {
+ wasNull = false;
+ if (type == null) {//As a fallback, try to get the value as is
+ return reader.readValue(columnIndex);
+ }
+
+ return (T) JdbcUtils.convert(reader.readValue(columnIndex), type);
+ } else {
+ wasNull = true;
+ return null;
+ }
+ } catch (Exception e) {
+ throw ExceptionUtils.toSqlState(String.format("Method: getObject(\"%s\", %s) encountered an exception.",
+ reader.getSchema().columnIndexToName(columnIndex), type),
+ String.format("SQL: [%s]", parentStatement.getLastSql()), e);
+ }
}
@Override
diff --git a/performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java b/performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java
index b9f4766d0..aeea46d41 100644
--- a/performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java
+++ b/performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java
@@ -39,15 +39,15 @@ public static void main(String[] args) throws Exception {
Map argMap = parseArguments(args);
Options opt = new OptionsBuilder()
- .include(QueryClient.class.getName())
- .include(InsertClient.class.getName())
- .include(ConcurrentInsertClient.class.getName())
- .include(ConcurrentQueryClient.class.getName())
- .include(Compression.class.getName())
- .include(Serializers.class.getName())
- .include(Deserializers.class.getName())
- .include(MixedWorkload.class.getName())
- .include(DataTypes.class.getName())
+// .include(QueryClient.class.getName())
+// .include(InsertClient.class.getName())
+// .include(ConcurrentInsertClient.class.getName())
+// .include(ConcurrentQueryClient.class.getName())
+// .include(Compression.class.getName())
+// .include(Serializers.class.getName())
+// .include(Deserializers.class.getName())
+// .include(MixedWorkload.class.getName())
+// .include(DataTypes.class.getName())
.include(JDBCQuery.class.getName())
.include(JDBCInsert.class.getName())
.forks(1) // must be a fork. No fork only for debugging
@@ -55,7 +55,7 @@ public static void main(String[] args) throws Exception {
.timeUnit(TimeUnit.MILLISECONDS)
.addProfiler(GCProfiler.class)
.addProfiler(MemPoolProfiler.class)
- .warmupIterations(3)
+ .warmupIterations(1)
.warmupTime(TimeValue.seconds(5))
.measurementIterations(10)
.jvmArgs("-Xms8g", "-Xmx8g")
diff --git a/performance/src/test/com/clickhouse/benchmark/clients/JDBCQuery.java b/performance/src/test/com/clickhouse/benchmark/clients/JDBCQuery.java
index 51fd0aa12..bea11c29f 100644
--- a/performance/src/test/com/clickhouse/benchmark/clients/JDBCQuery.java
+++ b/performance/src/test/com/clickhouse/benchmark/clients/JDBCQuery.java
@@ -1,5 +1,6 @@
package com.clickhouse.benchmark.clients;
+import com.clickhouse.data.ClickHouseColumn;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
import org.slf4j.Logger;
@@ -9,19 +10,22 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.List;
+import java.util.stream.Collectors;
import static com.clickhouse.benchmark.BenchmarkRunner.getSelectQuery;
public class JDBCQuery extends BenchmarkBase {
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCQuery.class);
+
void selectData(Connection connection, DataState dataState, Blackhole blackhole) throws SQLException {
String sql = getSelectQuery(dataState.tableNameFilled);
- try (Statement stmt = connection.createStatement()) {
- try (ResultSet rs = stmt.executeQuery(sql)) {
- while (rs.next()) {
- for (int i = 1; i <= dataState.dataSet.getSchema().getColumns().size(); i++) {
- blackhole.consume(rs.getObject(i));
- }
+ try (Statement stmt = connection.createStatement();
+ ResultSet rs = stmt.executeQuery(sql)) {
+ int nCol = dataState.dataSet.getSchema().getColumns().size();
+ while (rs.next()) {
+ for (int i = 1; i <= nCol; i++) {
+ blackhole.consume(rs.getObject(i));
}
}
}
@@ -37,4 +41,25 @@ public void selectJDBCV2(DataState dataState, Blackhole blackhole) throws SQLExc
selectData(jdbcV2, dataState, blackhole);
}
+ void selectDataUseNames(Connection connection, DataState dataState, Blackhole blackhole) throws SQLException {
+ String sql = getSelectQuery(dataState.tableNameFilled);
+ try (Statement stmt = connection.createStatement();
+ ResultSet rs = stmt.executeQuery(sql)) {
+ while (rs.next()) {
+ for (ClickHouseColumn col : dataState.dataSet.getSchema().getColumns()) {
+ blackhole.consume(rs.getObject(col.getColumnName()));
+ }
+ }
+ }
+ }
+
+ @Benchmark
+ public void selectJDBCV1UseNames(DataState dataState, Blackhole blackhole) throws SQLException {
+ selectDataUseNames(jdbcV1, dataState, blackhole);
+ }
+
+ @Benchmark
+ public void selectJDBCV2UseName(DataState dataState, Blackhole blackhole) throws SQLException {
+ selectDataUseNames(jdbcV2, dataState, blackhole);
+ }
}
diff --git a/performance/src/test/com/clickhouse/benchmark/data/FileDataSet.java b/performance/src/test/com/clickhouse/benchmark/data/FileDataSet.java
index 5829f0f22..3ee0e26cb 100644
--- a/performance/src/test/com/clickhouse/benchmark/data/FileDataSet.java
+++ b/performance/src/test/com/clickhouse/benchmark/data/FileDataSet.java
@@ -24,7 +24,7 @@ public class FileDataSet implements DataSet{
private final String name;
private final String createTableStmt;
- private TableSchema schema = new TableSchema();
+ private TableSchema schema = null;
private final Map metadata = new HashMap<>();
@@ -176,10 +176,11 @@ public ClickHouseDataProcessor getClickHouseDataProcessor() {
@Override
public void setClickHouseDataProcessor(ClickHouseDataProcessor dataProcessor) {
this.dataProcessor = dataProcessor;
- this.schema = new TableSchema();
+ List columns = new ArrayList<>();
for (ClickHouseColumn column : dataProcessor.getColumns()) {
- schema.addColumn(column.getColumnName(), column.getOriginalTypeName());
+ columns.add(ClickHouseColumn.of(column.getColumnName(), column.getOriginalTypeName()));
}
+ this.schema = new TableSchema(columns);
}
@Override
diff --git a/performance/src/test/com/clickhouse/benchmark/data/SimpleDataSet.java b/performance/src/test/com/clickhouse/benchmark/data/SimpleDataSet.java
index 5b0b2aad6..4208fb49d 100644
--- a/performance/src/test/com/clickhouse/benchmark/data/SimpleDataSet.java
+++ b/performance/src/test/com/clickhouse/benchmark/data/SimpleDataSet.java
@@ -1,6 +1,7 @@
package com.clickhouse.benchmark.data;
import com.clickhouse.client.api.metadata.TableSchema;
+import com.clickhouse.data.ClickHouseColumn;
import com.clickhouse.data.ClickHouseDataProcessor;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.ClickHouseRecord;
@@ -11,6 +12,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -99,25 +101,24 @@ public ClickHouseFormat getFormat() {
@Override
public TableSchema getSchema() {
- TableSchema schema = new TableSchema();
- schema.setTableName(tableName);
- schema.addColumn("trip_id", "UInt32");
- schema.addColumn("pickup_datetime", "DateTime DEFAULT now()");
- schema.addColumn("dropoff_datetime", "DateTime");
- schema.addColumn("pickup_longitude", "Nullable(Float64)");
- schema.addColumn("pickup_latitude", "Nullable(Float64)");
- schema.addColumn("dropoff_longitude", "Nullable(Float64)");
- schema.addColumn("dropoff_latitude", "Nullable(Float64)");
- schema.addColumn("passenger_count", "UInt8");
- schema.addColumn("trip_distance", "Float32");
- schema.addColumn("fare_amount", "Float32");
- schema.addColumn("extra", "Float32");
- schema.addColumn("tip_amount", "Float32");
- schema.addColumn("tolls_amount", "Float32");
- schema.addColumn("total_amount", "Float32");
- schema.addColumn("payment_type", "Enum('CSH' = 1, 'CRE' = 2, 'NOC' = 3, 'DIS' = 4, 'UNK' = 5)");
- schema.addColumn("pickup_ntaname", "LowCardinality(String)");
- schema.addColumn("dropoff_ntaname", "LowCardinality(String)");
+ TableSchema schema = new TableSchema(tableName, "", "", Arrays.asList(
+ ClickHouseColumn.of("trip_id", "UInt32"),
+ ClickHouseColumn.of("pickup_datetime", "DateTime DEFAULT now()"),
+ ClickHouseColumn.of("dropoff_datetime", "DateTime"),
+ ClickHouseColumn.of("pickup_longitude", "Nullable(Float64)"),
+ ClickHouseColumn.of("pickup_latitude", "Nullable(Float64)"),
+ ClickHouseColumn.of("dropoff_longitude", "Nullable(Float64)"),
+ ClickHouseColumn.of("dropoff_latitude", "Nullable(Float64)"),
+ ClickHouseColumn.of("passenger_count", "UInt8"),
+ ClickHouseColumn.of("trip_distance", "Float32"),
+ ClickHouseColumn.of("fare_amount", "Float32"),
+ ClickHouseColumn.of("extra", "Float32"),
+ ClickHouseColumn.of("tip_amount", "Float32"),
+ ClickHouseColumn.of("tolls_amount", "Float32"),
+ ClickHouseColumn.of("total_amount", "Float32"),
+ ClickHouseColumn.of("payment_type", "Enum('CSH' = 1, 'CRE' = 2, 'NOC' = 3, 'DIS' = 4, 'UNK' = 5)"),
+ ClickHouseColumn.of("pickup_ntaname", "LowCardinality(String)"),
+ ClickHouseColumn.of("dropoff_ntaname", "LowCardinality(String)")));
return schema;
}
diff --git a/pom.xml b/pom.xml
index 48f65a7e2..721b0af86 100644
--- a/pom.xml
+++ b/pom.xml
@@ -144,6 +144,7 @@
2.16.0
3.3.1
1.37
+ 33.4.6-jre
1.8
false