Skip to content

Commit 2a0b19e

Browse files
committed
added check for empty result set. fixed places where empty RS should be returned
1 parent f156664 commit 2a0b19e

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataImpl.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import com.clickhouse.client.api.sql.SQLUtils;
44
import com.clickhouse.data.ClickHouseColumn;
55
import com.clickhouse.data.ClickHouseDataType;
6+
import com.clickhouse.jdbc.ClientInfoProperties;
67
import com.clickhouse.jdbc.ConnectionImpl;
78
import com.clickhouse.jdbc.Driver;
9+
import com.clickhouse.jdbc.DriverProperties;
810
import com.clickhouse.jdbc.JdbcV2Wrapper;
911
import com.clickhouse.jdbc.ResultSetImpl;
10-
import com.clickhouse.jdbc.ClientInfoProperties;
11-
import com.clickhouse.jdbc.DriverProperties;
1212
import com.clickhouse.jdbc.internal.ExceptionUtils;
1313
import com.clickhouse.jdbc.internal.JdbcUtils;
1414
import com.clickhouse.jdbc.internal.MetadataResultSet;
@@ -912,7 +912,8 @@ public ResultSet getColumnPrivileges(String catalog, String schema, String table
912912
"NULL::Nullable(String) AS GRANTOR, " +
913913
"NULL::Nullable(String) AS GRANTEE, " +
914914
"NULL::Nullable(String) AS PRIVILEGE, " +
915-
"NULL::Nullable(String) AS IS_GRANTABLE");
915+
"NULL::Nullable(String) AS IS_GRANTABLE" +
916+
" LIMIT 0");
916917
} catch (Exception e) {
917918
throw ExceptionUtils.toSqlState(e);
918919
}
@@ -929,7 +930,8 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern, String
929930
"NULL::Nullable(String) AS GRANTOR, " +
930931
"NULL::Nullable(String) AS GRANTEE, " +
931932
"NULL::Nullable(String) AS PRIVILEGE, " +
932-
"NULL::Nullable(String) AS IS_GRANTABLE");
933+
"NULL::Nullable(String) AS IS_GRANTABLE" +
934+
" LIMIT 0");
933935
} catch (Exception e) {
934936
throw ExceptionUtils.toSqlState(e);
935937
}
@@ -947,7 +949,8 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
947949
"NULL::Nullable(Int32) AS COLUMN_SIZE, " +
948950
"NULL::Nullable(Int32) AS BUFFER_LENGTH, " +
949951
"NULL::Nullable(Int16) AS DECIMAL_DIGITS, " +
950-
"NULL::Nullable(Int16) AS PSEUDO_COLUMN");
952+
"NULL::Nullable(Int16) AS PSEUDO_COLUMN" +
953+
" LIMIT 0");
951954
} catch (Exception e) {
952955
throw ExceptionUtils.toSqlState(e);
953956
}
@@ -965,7 +968,8 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
965968
"NULL::Nullable(Int32) AS COLUMN_SIZE, " +
966969
"NULL::Nullable(Int32) AS BUFFER_LENGTH, " +
967970
"NULL::Nullable(Int16) AS DECIMAL_DIGITS, " +
968-
"NULL::Nullable(Int16) AS PSEUDO_COLUMN");
971+
"NULL::Nullable(Int16) AS PSEUDO_COLUMN" +
972+
" LIMIT 0");
969973
} catch (Exception e) {
970974
throw ExceptionUtils.toSqlState(e);
971975
}
@@ -995,8 +999,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr
995999

9961000
@Override
9971001
public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException {
998-
//Return an empty result set with the required columns
999-
log.warn("getImportedKeys is not supported and may return invalid results");
1002+
// ClickHouse has no notion of foreign key. This method should return empty resultset
10001003
try {
10011004
String sql = "SELECT NULL::Nullable(String) AS PKTABLE_CAT, " +
10021005
"NULL::Nullable(String) AS PKTABLE_SCHEM, " +
@@ -1011,7 +1014,8 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
10111014
"NULL::Nullable(Int16) AS DELETE_RULE, " +
10121015
"NULL::Nullable(String) AS FK_NAME, " +
10131016
"NULL::Nullable(String) AS PK_NAME, " +
1014-
"NULL::Nullable(Int16) AS DEFERRABILITY";
1017+
"NULL::Nullable(Int16) AS DEFERRABILITY" +
1018+
" LIMIT 0";
10151019
return connection.createStatement().executeQuery(sql);
10161020
} catch (Exception e) {
10171021
throw ExceptionUtils.toSqlState(e);
@@ -1020,7 +1024,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) th
10201024

10211025
@Override
10221026
public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException {
1023-
//Return an empty result set with the required columns
1027+
// ClickHouse has no notion of foreign key. This method should return empty resultset
10241028
log.warn("getExportedKeys is not supported and may return invalid results");
10251029
try {
10261030
return connection.createStatement().executeQuery("SELECT NULL::Nullable(String) AS PKTABLE_CAT, " +
@@ -1036,7 +1040,8 @@ public ResultSet getExportedKeys(String catalog, String schema, String table) th
10361040
"NULL::Nullable(Int16) AS DELETE_RULE, " +
10371041
"NULL::Nullable(String) AS FK_NAME, " +
10381042
"NULL::Nullable(String) AS PK_NAME, " +
1039-
"NULL::Nullable(Int16) AS DEFERRABILITY");
1043+
"NULL::Nullable(Int16) AS DEFERRABILITY" +
1044+
" LIMIT 0");
10401045
} catch (Exception e) {
10411046
throw ExceptionUtils.toSqlState(e);
10421047
}
@@ -1060,7 +1065,8 @@ public ResultSet getCrossReference(String parentCatalog, String parentSchema, St
10601065
"NULL::Nullable(Int16) AS DELETE_RULE, " +
10611066
"NULL::Nullable(String) AS FK_NAME, " +
10621067
"NULL::Nullable(String) AS PK_NAME, " +
1063-
"NULL::Nullable(Int16) AS DEFERRABILITY";
1068+
"NULL::Nullable(Int16) AS DEFERRABILITY" +
1069+
" LIMIT 0";
10641070
return connection.createStatement().executeQuery("SELECT " + columns);
10651071
} catch (Exception e) {
10661072
throw ExceptionUtils.toSqlState(e);
@@ -1150,7 +1156,8 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole
11501156
"null::Nullable(String) AS ASC_OR_DESC, " +
11511157
"null::Nullable(Int64) AS CARDINALITY, " +
11521158
"null::Nullable(Int64) AS PAGES, " +
1153-
"null::Nullable(String) AS FILTER_CONDITION ";
1159+
"null::Nullable(String) AS FILTER_CONDITION " +
1160+
" LIMIT 0";
11541161
return connection.createStatement().executeQuery(sql);
11551162
} catch (Exception e) {
11561163
throw ExceptionUtils.toSqlState(e);
@@ -1229,7 +1236,8 @@ public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePa
12291236
"NULL::Nullable(String) AS CLASS_NAME, " +
12301237
"NULL::Nullable(Int32) AS DATA_TYPE, " +
12311238
"NULL::Nullable(String) AS REMARKS, " +
1232-
"NULL::Nullable(Int16) AS BASE_TYPE");
1239+
"NULL::Nullable(Int16) AS BASE_TYPE" +
1240+
" LIMIT 0");
12331241
} catch (Exception e) {
12341242
throw ExceptionUtils.toSqlState(e);
12351243
}
@@ -1274,7 +1282,8 @@ public ResultSet getSuperTypes(String catalog, String schemaPattern, String type
12741282
+ "NULL::Nullable(String) AS TYPE_NAME, "
12751283
+ "NULL::Nullable(String) AS SUPERTYPE_CAT, "
12761284
+ "NULL::Nullable(String) AS SUPERTYPE_SCHEM, "
1277-
+ "NULL::Nullable(String) AS SUPERTYPE_NAME");
1285+
+ "NULL::Nullable(String) AS SUPERTYPE_NAME" +
1286+
" LIMIT 0");
12781287
} catch (Exception e) {
12791288
throw ExceptionUtils.toSqlState(e);
12801289
}
@@ -1290,7 +1299,8 @@ public ResultSet getSuperTables(String catalog, String schemaPattern, String tab
12901299
+ "NULL::Nullable(String) AS TABLE_CAT, "
12911300
+ "NULL::Nullable(String) AS TABLE_SCHEM, "
12921301
+ "NULL::Nullable(String) AS TABLE_NAME, "
1293-
+ "NULL::Nullable(String) AS SUPERTABLE_NAME");
1302+
+ "NULL::Nullable(String) AS SUPERTABLE_NAME" +
1303+
" LIMIT 0");
12941304
} catch (Exception e) {
12951305
throw ExceptionUtils.toSqlState(e);
12961306
}
@@ -1323,7 +1333,8 @@ public ResultSet getAttributes(String catalog, String schemaPattern, String type
13231333
+ "NULL::Nullable(String) AS SCOPE_CATALOG, "
13241334
+ "NULL::Nullable(String) AS SCOPE_SCHEMA, "
13251335
+ "NULL::Nullable(String) AS SCOPE_TABLE, "
1326-
+ "NULL::Nullable(Int16) AS SOURCE_DATA_TYPE");
1336+
+ "NULL::Nullable(Int16) AS SOURCE_DATA_TYPE" +
1337+
" LIMIT 0");
13271338
} catch (Exception e) {
13281339
throw ExceptionUtils.toSqlState(e);
13291340
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataTest.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.clickhouse.client.ClickHouseServerForTest;
44
import com.clickhouse.data.ClickHouseDataType;
55
import com.clickhouse.data.ClickHouseVersion;
6-
import com.clickhouse.jdbc.JdbcIntegrationTest;
76
import com.clickhouse.jdbc.ClientInfoProperties;
87
import com.clickhouse.jdbc.DriverProperties;
8+
import com.clickhouse.jdbc.JdbcIntegrationTest;
99
import com.clickhouse.jdbc.internal.JdbcUtils;
1010
import org.testng.Assert;
1111
import org.testng.annotations.Test;
@@ -467,7 +467,7 @@ public void testGetFunctionColumns() throws Exception {
467467
try (Connection conn = getJdbcConnection()) {
468468
DatabaseMetaData dbmd = conn.getMetaData();
469469
try (ResultSet rs = dbmd.getFunctionColumns(null, null, "mapContains", null)) {
470-
470+
assertFalse(rs.next());
471471
List<String> expectedColumnNames = Arrays.asList(
472472
"FUNCTION_CAT",
473473
"FUNCTION_SCHEM",
@@ -577,6 +577,7 @@ public void testGetIndexInfoColumnType() throws Exception {
577577
);
578578

579579
ResultSet rs = dbmd.getIndexInfo(null, null, null, false, false);
580+
assertFalse(rs.next());
580581
ResultSetMetaData rsmd = rs.getMetaData();
581582
assertProcedureColumns(rsmd, expectedColumnNames, expectedColumnTypes);
582583
}
@@ -592,6 +593,7 @@ public void testGetProcedures() throws Exception {
592593
Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, Types.VARCHAR);
593594

594595
ResultSet rs = dbmd.getProcedures(null, null, null);
596+
assertFalse(rs.next());
595597
ResultSetMetaData rsmd = rs.getMetaData();
596598
assertProcedureColumns(rsmd, columnNames, columnTypes);
597599
}
@@ -645,6 +647,7 @@ public void testGetProceduresColumnType() throws Exception {
645647
Types.VARCHAR,
646648
Types.VARCHAR);
647649
ResultSetMetaData rsmd = dbmd.getProcedureColumns(null, null, null, null).getMetaData();
650+
648651
assertProcedureColumns(rsmd, expectedColumnNames, columnTypes);
649652
}
650653
}
@@ -655,7 +658,7 @@ public void testGetColumnPrivileges() throws Exception {
655658
try (Connection conn = getJdbcConnection()) {
656659
DatabaseMetaData dbmd = conn.getMetaData();
657660
ResultSet rs = dbmd.getColumnPrivileges(null, null, null, null);
658-
661+
assertFalse(rs.next());
659662
List<String> expectedColumnNames = Arrays.asList("TABLE_CAT",
660663
"TABLE_SCHEM",
661664
"TABLE_NAME",
@@ -686,7 +689,7 @@ public void testGetTablePrivileges() throws Exception {
686689
try (Connection conn = getJdbcConnection()) {
687690
DatabaseMetaData dbmd = conn.getMetaData();
688691
ResultSet rs = dbmd.getTablePrivileges(null, null, null);
689-
692+
assertFalse(rs.next());
690693
List<String> expectedColumnNames = Arrays.asList("TABLE_CAT",
691694
"TABLE_SCHEM",
692695
"TABLE_NAME",
@@ -714,7 +717,7 @@ public void testGetVersionColumnsColumns() throws Exception {
714717
try (Connection conn = getJdbcConnection()) {
715718
DatabaseMetaData dbmd = conn.getMetaData();
716719
ResultSet rs = dbmd.getVersionColumns(null, null, null);
717-
720+
assertFalse(rs.next());
718721
List<String> expectedColumnNames = Arrays.asList("SCOPE",
719722
"COLUMN_NAME",
720723
"DATA_TYPE",
@@ -771,8 +774,10 @@ public void testGetImportedKeys() throws Exception {
771774
try (Connection conn = getJdbcConnection()) {
772775
DatabaseMetaData dbmd = conn.getMetaData();
773776
ResultSet rs = dbmd.getImportedKeys(null, null, null);
777+
assertFalse(rs.next());
774778

775-
List<String> expectedColumnNames = Arrays.asList("PKTABLE_CAT",
779+
List<String> expectedColumnNames = Arrays.asList(
780+
"PKTABLE_CAT",
776781
"PKTABLE_SCHEM",
777782
"PKTABLE_NAME",
778783
"PKCOLUMN_NAME",
@@ -813,7 +818,7 @@ public void testGetExportedKeys() throws Exception {
813818
try (Connection conn = getJdbcConnection()) {
814819
DatabaseMetaData dbmd = conn.getMetaData();
815820
ResultSet rs = dbmd.getExportedKeys(null, null, null);
816-
821+
assertFalse(rs.next());
817822
List<String> expectedColumnNames = Arrays.asList("PKTABLE_CAT",
818823
"PKTABLE_SCHEM",
819824
"PKTABLE_NAME",
@@ -856,7 +861,7 @@ public void testGetCrossReference() throws Exception {
856861
try (Connection conn = getJdbcConnection()) {
857862
DatabaseMetaData dbmd = conn.getMetaData();
858863
ResultSet rs = dbmd.getCrossReference(null, null, null, null, null, null);
859-
864+
assertFalse(rs.next());
860865
List<String> expectedColumnNames = Arrays.asList("PKTABLE_CAT",
861866
"PKTABLE_SCHEM",
862867
"PKTABLE_NAME",
@@ -898,7 +903,7 @@ public void testGetUDTs() throws Exception {
898903
try (Connection conn = getJdbcConnection()) {
899904
DatabaseMetaData dbmd = conn.getMetaData();
900905
ResultSet rs = dbmd.getUDTs(null, null, null, null);
901-
906+
assertFalse(rs.next());
902907
List<String> expectedColumnNames = Arrays.asList("TYPE_CAT",
903908
"TYPE_SCHEM",
904909
"TYPE_NAME",
@@ -927,7 +932,7 @@ public void testGetSuperTypes() throws Exception {
927932
try (Connection conn = getJdbcConnection()) {
928933
DatabaseMetaData dbmd = conn.getMetaData();
929934
ResultSet rs = dbmd.getSuperTypes(null, null, null);
930-
935+
assertFalse(rs.next());
931936
List<String> expectedColumnNames = Arrays.asList("TYPE_CAT",
932937
"TYPE_SCHEM",
933938
"TYPE_NAME",
@@ -953,7 +958,7 @@ public void testGetSuperTables() throws Exception {
953958
try (Connection conn = getJdbcConnection()) {
954959
DatabaseMetaData dbmd = conn.getMetaData();
955960
ResultSet rs = dbmd.getSuperTables(null, null, null);
956-
961+
assertFalse(rs.next());
957962
List<String> expectedColumnNames = Arrays.asList("TABLE_CAT",
958963
"TABLE_SCHEM",
959964
"TABLE_NAME",
@@ -976,7 +981,7 @@ public void testGetBestRowIdentifier() throws Exception {
976981
try (Connection conn = getJdbcConnection()) {
977982
DatabaseMetaData dbmd = conn.getMetaData();
978983
ResultSet rs = dbmd.getBestRowIdentifier(null, null, null, 0, true);
979-
984+
assertFalse(rs.next());
980985
List<String> expectedColumnNames = Arrays.asList("SCOPE",
981986
"COLUMN_NAME",
982987
"DATA_TYPE",
@@ -1007,7 +1012,7 @@ public void testGetAttributes() throws Exception {
10071012
try (Connection conn = getJdbcConnection()) {
10081013
DatabaseMetaData dbmd = conn.getMetaData();
10091014
ResultSet rs = dbmd.getAttributes(null, null, null, null);
1010-
1015+
assertFalse(rs.next());
10111016
List<String> expectedColumnNames = Arrays.asList("TYPE_CAT",
10121017
"TYPE_SCHEM",
10131018
"TYPE_NAME",
@@ -1064,7 +1069,7 @@ public void testGetPseudoColumns() throws Exception {
10641069
try (Connection conn = getJdbcConnection()) {
10651070
DatabaseMetaData dbmd = conn.getMetaData();
10661071
ResultSet rs = dbmd.getPseudoColumns(null, null, null, null);
1067-
1072+
assertFalse(rs.next());
10681073
List<String> expectedColumnNames = Arrays.asList("TABLE_CAT",
10691074
"TABLE_SCHEM",
10701075
"TABLE_NAME",

0 commit comments

Comments
 (0)