Skip to content
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
6 changes: 6 additions & 0 deletions client-v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<scope>compile</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ private boolean readBlock() throws IOException {
List<String> names = new ArrayList<>(nColumns);
List<String> types = new ArrayList<>(nColumns);
currentBlock = new Block(names, types, nRows);
TableSchema schema = new TableSchema();
List<ClickHouseColumn> 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());
Expand All @@ -101,6 +101,7 @@ private boolean readBlock() throws IOException {
}
currentBlock.add(values);
}
TableSchema schema = new TableSchema(columns);

setSchema(schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +25,7 @@ public RowBinaryWithNamesAndTypesFormatReader(InputStream inputStream, QuerySett

private void readSchema() {
try {
TableSchema headerSchema = new TableSchema();
List<String> columns = new ArrayList<>();
List<String> names = new ArrayList<>();
int nCol;
try {
nCol = BinaryStreamReader.readVarInt(input);
Expand All @@ -34,14 +34,16 @@ private void readSchema() {
return;
}
for (int i = 0; i < nCol; i++) {
columns.add(binaryStreamReader.readString());
names.add(binaryStreamReader.readString());
}

List<ClickHouseColumn> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClickHouseColumn> 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
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;
import java.util.Map;

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<ClickHouseColumn> columns;
private final List<ClickHouseColumn> columns;

private List<ClickHouseColumn> columnsView;

private Map<String, Map<String, Object>> metadata;

private Map<String, Integer> colIndex;
private final Map<String, Integer> 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<ClickHouseColumn> columns) {
this("", "", "", columns);
}
public TableSchema(String tableName, String query, String databaseName, Collection<ClickHouseColumn> columns) {
this.tableName = tableName;
this.databaseName = databaseName;
this.query = query;
this.columns = ImmutableList.copyOf(columns);
ImmutableMap.Builder<String, Integer> 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();
}

/**
Expand All @@ -40,7 +50,7 @@ public TableSchema() {
* @return - collection of columns in the table
*/
public List<ClickHouseColumn> getColumns() {
return columnsView;
return columns;
}

public String getDatabaseName() {
Expand All @@ -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;
}
Expand All @@ -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<String, Object> 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.
*
Expand Down Expand Up @@ -134,7 +125,6 @@ public String toString() {
"tableName='" + tableName + '\'' +
", databaseName='" + databaseName + '\'' +
", columns=" + columns +
", metadata=" + metadata +
", colIndex=" + colIndex +
", hasDefaults=" + hasDefaults +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -440,10 +441,13 @@ public void testBinaryStreamReader() throws Exception {
Future<QueryResponse> 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) {
Expand Down Expand Up @@ -472,10 +476,6 @@ public void testRowStreamReader() throws Exception {
Future<QueryResponse> 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<String, Object> record;
Expand Down
25 changes: 22 additions & 3 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -959,7 +960,8 @@ public Statement getStatement() throws SQLException {

@Override
public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLException {
return getObject(columnIndexToName(columnIndex), map);
ClickHouseDataType type = getSchema().getColumnByIndex(columnIndex).getDataType();
return getObject(columnIndex, map.get(JdbcUtils.convertToSqlType(type).getName()));
}

@Override
Expand Down Expand Up @@ -1493,7 +1495,24 @@ public void updateNClob(String columnLabel, Reader reader) throws SQLException {

@Override
public <T> T getObject(int columnIndex, Class<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ public static void main(String[] args) throws Exception {
Map<String, String> 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
.mode(Mode.SampleTime)
.timeUnit(TimeUnit.MILLISECONDS)
.addProfiler(GCProfiler.class)
.addProfiler(MemPoolProfiler.class)
.warmupIterations(3)
.warmupIterations(1)
.warmupTime(TimeValue.seconds(5))
.measurementIterations(10)
.jvmArgs("-Xms8g", "-Xmx8g")
Expand Down
Loading