Skip to content

Commit cc8430d

Browse files
committed
added some tests and fixes
1 parent 4c02535 commit cc8430d

File tree

10 files changed

+225
-95
lines changed

10 files changed

+225
-95
lines changed

clickhouse-jdbc/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
<include>org.apache.httpcomponents.core5:httpcore5-h2</include>
382382
<include>org.lz4:lz4-pure-java</include>
383383
<include>com.clickhouse:jdbc-v2</include>
384+
<include>com.clickhouse:client-v2</include>
384385
</includes>
385386
</artifactSet>
386387
<relocations>

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaData.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,30 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
847847
params.put("defaultNonNull", String.valueOf(DatabaseMetaData.typeNoNulls));
848848
params.put("defaultType", String.valueOf(Types.OTHER));
849849
String sql = ClickHouseParameterizedQuery
850-
.apply("select :catalog as TABLE_CAT, :schema as TABLE_SCHEM, table as TABLE_NAME, "
851-
+ "name as COLUMN_NAME, toInt32(:defaultType) as DATA_TYPE, type as TYPE_NAME, toInt32(0) as COLUMN_SIZE, "
852-
+ "0 as BUFFER_LENGTH, cast(null as Nullable(Int32)) as DECIMAL_DIGITS, 10 as NUM_PREC_RADIX, "
853-
+ "toInt32(position(type, 'Nullable(') >= 1 ? :defaultNullable : :defaultNonNull) as NULLABLE, :comment as REMARKS, default_expression as COLUMN_DEF, "
854-
+ "0 as SQL_DATA_TYPE, 0 as SQL_DATETIME_SUB, cast(null as Nullable(Int32)) as CHAR_OCTET_LENGTH, position as ORDINAL_POSITION, "
855-
+ "position(type, 'Nullable(') >= 1 ? 'YES' : 'NO' as IS_NULLABLE, null as SCOPE_CATALOG, null as SCOPE_SCHEMA, null as SCOPE_TABLE, "
856-
+ "null as SOURCE_DATA_TYPE, 'NO' as IS_AUTOINCREMENT, 'NO' as IS_GENERATEDCOLUMN from system.columns "
857-
+ "where database like :database and table like :table and name like :column", params);
850+
.apply("select :catalog as TABLE_CAT, " +
851+
":schema as TABLE_SCHEM, " +
852+
"table as TABLE_NAME, " +
853+
"name as COLUMN_NAME, " +
854+
"toInt32(:defaultType) as DATA_TYPE, " +
855+
"type as TYPE_NAME, " +
856+
"toInt32(0) as COLUMN_SIZE, "
857+
+ "0 as BUFFER_LENGTH, " +
858+
"cast(null as Nullable(Int32)) as DECIMAL_DIGITS, " +
859+
"10 as NUM_PREC_RADIX, "
860+
+ "toInt32(position(type, 'Nullable(') >= 1 ? :defaultNullable : :defaultNonNull) as NULLABLE, " +
861+
":comment as REMARKS, " +
862+
"default_expression as COLUMN_DEF, "
863+
+ "0 as SQL_DATA_TYPE, " +
864+
"0 as SQL_DATETIME_SUB, " +
865+
"cast(null as Nullable(Int32)) as CHAR_OCTET_LENGTH, " +
866+
"position as ORDINAL_POSITION, "
867+
+ "position(type, 'Nullable(') >= 1 ? 'YES' : 'NO' as IS_NULLABLE, " +
868+
"null as SCOPE_CATALOG, null as SCOPE_SCHEMA, " +
869+
"null as SCOPE_TABLE, " +
870+
"null as SOURCE_DATA_TYPE, " +
871+
"'NO' as IS_AUTOINCREMENT, " +
872+
"'NO' as IS_GENERATEDCOLUMN " +
873+
" FROM system.columns WHERE database LIKE :database and table LIKE :table AND name LIKE :column", params);
858874
return query(sql, (i, r) -> {
859875
String typeName = r.getValue("TYPE_NAME").asString();
860876
try {

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDriver.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class ClickHouseDriver implements java.sql.Driver {
1919
}
2020

2121
public ClickHouseDriver() {
22-
log.debug("Creating a new instance of the 'proxy' ClickHouseDriver");
22+
// log.debug("Creating a new instance of the 'proxy' ClickHouseDriver");
23+
log.info("ClickHouse JDBC driver version: {}", ClickHouseDriver.class.getPackage().getImplementationVersion());
2324
urlFlagSent = false;
2425
this.driver = getDriver(null);
2526
}
@@ -50,18 +51,18 @@ public boolean isV2(String url) {
5051
log.debug("Checking if V2 driver is requested");
5152
boolean v2Flag = Boolean.parseBoolean(System.getProperty("clickhouse.jdbc.v2", "false"));
5253
if (v2Flag) {
53-
log.debug("V2 driver is requested through system property.");
54+
log.info("V2 driver is requested through system property.");
5455
return true;
5556
}
5657

5758
if (url != null && url.contains("clickhouse.jdbc.v2")) {
5859
urlFlagSent = true;
5960

6061
if (url.contains("clickhouse.jdbc.v2=true")) {
61-
log.debug("V2 driver is requested through URL.");
62+
log.info("V2 driver is requested through URL.");
6263
return true;
6364
} else {
64-
log.debug("V1 driver is requested through URL.");
65+
log.info("V1 driver is requested through URL.");
6566
return false;
6667
}
6768
}
@@ -72,12 +73,15 @@ public boolean isV2(String url) {
7273

7374
private java.sql.Driver getDriver(String url) {
7475
if (urlFlagSent && driver != null) {// if the URL flag was sent, we don't need to check the URL again
76+
7577
return driver;
7678
}
7779

7880
if (isV2(url)) {
81+
log.info("v2 driver");
7982
driver = new com.clickhouse.jdbc.Driver();
8083
} else {
84+
log.info("v1 driver");
8185
driver = new DriverV1();
8286
}
8387

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseDatabaseMetaDataTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ public void testTableComment() throws SQLException {
191191
}
192192
}
193193

194+
@Test(groups = "integration")
195+
public void testGetCatalogs() throws SQLException {
196+
Properties props = new Properties();
197+
// props.setProperty("databaseTerm", "schema");
198+
props.setProperty("externalDatabase", "false");
199+
try (ClickHouseConnection conn = newConnection(props)) {
200+
try (ResultSet rs = conn.getMetaData().getCatalogs()) {
201+
while (rs.next()) {
202+
log.debug("Catalog: %s", rs.getString("TABLE_CAT"));
203+
}
204+
205+
}
206+
}
207+
}
208+
194209
@Test(groups = "integration")
195210
public void testGetTables() throws SQLException {
196211
if (isCloud()) return; //TODO: testGetTables - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747

jdbc-v2/pom.xml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -225,43 +225,20 @@
225225
<artifactSet>
226226
<includes>
227227
<include>com.clickhouse:clickhouse-data</include>
228-
<include>com.google.code.gson:gson</include>
229-
<include>com.google.guava:failureaccess</include>
230-
<include>com.google.guava:guava</include>
231-
<include>com.google.protobuf:protobuf-java</include>
232-
<include>com.squareup.okio:okio</include>
233-
<include>io.opencensus:opencensus-api</include>
234-
<include>io.opencensus:opencensus-impl</include>
235-
<include>io.opencensus:opencensus-impl-core</include>
236-
<include>io.perfmark:perfmark-api</include>
228+
<include>com.clickhouse:clickhouse-client</include>
229+
<include>com.clickhouse:client-v2</include>
237230
<include>org.apache.commons:commons-compress</include>
238231
<include>org.apache.httpcomponents.client5:httpclient5</include>
239232
<include>org.apache.httpcomponents.core5:httpcore5</include>
240233
<include>org.apache.httpcomponents.core5:httpcore5-h2</include>
234+
<include>com.fasterxml.jackson.core:jackson-core</include>
235+
<include>org.roaringbitmap:RoaringBitmap</include>
236+
<include>org.ow2.asm:asm</include>
237+
<include>org.slf4j:slf4j-api</include>
241238
<include>org.lz4:lz4-pure-java</include>
242239
</includes>
243240
</artifactSet>
244241
<relocations>
245-
<relocation>
246-
<pattern>com.google</pattern>
247-
<shadedPattern>${shade.base}.google</shadedPattern>
248-
</relocation>
249-
<relocation>
250-
<pattern>io.opencensus</pattern>
251-
<shadedPattern>${shade.base}.opencensus</shadedPattern>
252-
</relocation>
253-
<relocation>
254-
<pattern>io.perfmark</pattern>
255-
<shadedPattern>${shade.base}.perfmark</shadedPattern>
256-
</relocation>
257-
<relocation>
258-
<pattern>net.jpountz</pattern>
259-
<shadedPattern>${shade.base}.jpountz</shadedPattern>
260-
</relocation>
261-
<relocation>
262-
<pattern>okio</pattern>
263-
<shadedPattern>${shade.base}.okio</shadedPattern>
264-
</relocation>
265242
<relocation>
266243
<pattern>org.apache</pattern>
267244
<shadedPattern>${shade.base}.apache</shadedPattern>

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import java.sql.*;
13+
import java.util.Collection;
1314
import java.util.Map;
1415
import java.util.Properties;
1516
import java.util.concurrent.Executor;
@@ -67,33 +68,13 @@ public void setDefaultQuerySettings(QuerySettings settings) {
6768
this.defaultQuerySettings = settings;
6869
}
6970

70-
private String getServerVersion() throws SQLException {
71+
public String getServerVersion() throws SQLException {
7172
GenericRecord result = client.queryAll("SELECT version() as server_version").stream()
7273
.findFirst().orElseThrow(() -> new SQLException("Failed to retrieve server version."));
7374

7475
return result.getString("server_version");
7576
}
7677

77-
public int getMajorVersion() throws SQLException {
78-
String version = getServerVersion();
79-
try {
80-
return Integer.parseInt(version.split("\\.")[0]);
81-
} catch (NumberFormatException e) {
82-
log.error("Failed to parse major version from server version: " + version, e);
83-
throw new SQLException("Failed to parse major version from server version: " + version);
84-
}
85-
}
86-
87-
public int getMinorVersion() throws SQLException {
88-
String version = getServerVersion();
89-
try {
90-
return Integer.parseInt(version.split("\\.")[1]);
91-
} catch (NumberFormatException e) {
92-
log.error("Failed to parse minor version from server version: " + version, e);
93-
throw new SQLException("Failed to parse minor version from server version: " + version);
94-
}
95-
}
96-
9778
@Override
9879
public Statement createStatement() throws SQLException {
9980
checkOpen();
@@ -115,7 +96,8 @@ public CallableStatement prepareCall(String sql) throws SQLException {
11596
@Override
11697
public String nativeSQL(String sql) throws SQLException {
11798
checkOpen();
118-
return sql;
99+
/// TODO: this is not implemented according to JDBC spec and may not be used.
100+
throw new RuntimeException("Not implemented");
119101
}
120102

121103
@Override

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ private static Map<ClickHouseDataType, Integer> generateTypeMap() {
2121
map.put(ClickHouseDataType.UInt64, Types.BIGINT);
2222
map.put(ClickHouseDataType.Float32, Types.FLOAT);
2323
map.put(ClickHouseDataType.Float64, Types.DOUBLE);
24+
map.put(ClickHouseDataType.Decimal, Types.DECIMAL);
2425
map.put(ClickHouseDataType.Decimal32, Types.DECIMAL);
2526
map.put(ClickHouseDataType.Decimal64, Types.DECIMAL);
2627
map.put(ClickHouseDataType.Decimal128, Types.DECIMAL);
27-
map.put(ClickHouseDataType.String, Types.CHAR);
28+
map.put(ClickHouseDataType.String, Types.VARCHAR);
2829
map.put(ClickHouseDataType.FixedString, Types.CHAR);
2930
map.put(ClickHouseDataType.Enum8, Types.VARCHAR);
3031
map.put(ClickHouseDataType.Enum16, Types.VARCHAR);
@@ -47,9 +48,22 @@ public static int convertToSqlType(ClickHouseDataType clickhouseType) {
4748
public static String generateSqlTypeEnum(String columnName) {
4849
StringBuilder sql = new StringBuilder("multiIf(");
4950
for (ClickHouseDataType type : CLICKHOUSE_TO_SQL_TYPE_MAP.keySet()) {
50-
sql.append(columnName).append(" == '").append(type.name()).append("', ").append(CLICKHOUSE_TO_SQL_TYPE_MAP.get(type)).append(", ");
51+
sql.append("position(").append(columnName).append(", '").append(type.name()).append("') > 0, ").append(CLICKHOUSE_TO_SQL_TYPE_MAP.get(type)).append(", ");
5152
}
5253
sql.append(Types.OTHER + ")");
5354
return sql.toString();
5455
}
56+
57+
public static String generateSqlTypeSizes(String columnName) {
58+
StringBuilder sql = new StringBuilder("multiIf(");
59+
sql.append("character_octet_length IS NOT NULL, character_octet_length, ");
60+
for (ClickHouseDataType type : ClickHouseDataType.values()) {
61+
if (type.getByteLength() > 0) {
62+
sql.append(columnName).append(" == '").append(type.name()).append("', ").append(type.getByteLength()).append(", ");
63+
}
64+
}
65+
sql.append("numeric_precision IS NOT NULL, numeric_precision, ");
66+
sql.append("0)");
67+
return sql.toString();
68+
}
5569
}

0 commit comments

Comments
 (0)