Skip to content

Commit 0b7a240

Browse files
authored
Merge pull request #2017 from ClickHouse/metabase-table-types
Updating code + test
2 parents f880b09 + 1353dd9 commit 0b7a240

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
public class DatabaseMetaData implements java.sql.DatabaseMetaData, JdbcV2Wrapper {
2222
private static final Logger log = LoggerFactory.getLogger(DatabaseMetaData.class);
23+
public static final String[] TABLE_TYPES = new String[] { "DICTIONARY", "LOG TABLE", "MEMORY TABLE",
24+
"REMOTE TABLE", "TABLE", "VIEW", "SYSTEM TABLE", "TEMPORARY TABLE" };
25+
2326
ConnectionImpl connection;
2427

2528
private boolean useCatalogs = false;
@@ -737,12 +740,22 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
737740
// TODO: when switch between catalog and schema is implemented, then TABLE_SCHEMA and TABLE_CAT should be populated accordingly
738741
// String commentColumn = connection.getServerVersion().check("[21.6,)") ? "t.comment" : "''";
739742
// TODO: handle useCatalogs == true and return schema catalog name
743+
if (types == null || types.length == 0) {
744+
types = TABLE_TYPES;
745+
}
740746

741747
String sql = "SELECT " +
742748
catalogPlaceholder + " AS TABLE_CAT, " +
743749
"t.database AS TABLE_SCHEM, " +
744750
"t.name AS TABLE_NAME, " +
745-
"t.engine AS TABLE_TYPE, " +
751+
"CASE WHEN t.engine LIKE '%Log' THEN 'LOG TABLE' " +
752+
"WHEN t.engine in ('Buffer', 'Memory', 'Set') THEN 'MEMORY TABLE' " +
753+
"WHEN t.is_temporary != 0 THEN 'TEMPORARY TABLE' " +
754+
"WHEN t.engine like '%View' THEN 'VIEW'" +
755+
"WHEN t.engine = 'Dictionary' THEN 'DICTIONARY' " +
756+
"WHEN t.engine LIKE 'Async%' OR t.engine LIKE 'System%' THEN 'SYSTEM TABLE' " +
757+
"WHEN empty(t.data_paths) THEN 'REMOTE TABLE' " +
758+
"ELSE 'TABLE' END AS TABLE_TYPE, " +
746759
"t.comment AS REMARKS, " +
747760
"null AS TYPE_CAT, " + // no types catalog
748761
"d.engine AS TYPE_SCHEM, " + // no types schema
@@ -752,14 +765,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
752765
" FROM system.tables t" +
753766
" JOIN system.databases d ON system.tables.database = system.databases.name" +
754767
" WHERE t.database LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'" +
755-
" AND t.name LIKE '" + (tableNamePattern == null ? "%" : tableNamePattern) + "'";
756-
if (types != null && types.length > 0) {
757-
sql += "AND t.engine IN (";
758-
for (String type : types) {
759-
sql += "'" + type + "',";
760-
}
761-
sql = sql.substring(0, sql.length() - 1) + ") ";
762-
}
768+
" AND t.name LIKE '" + (tableNamePattern == null ? "%" : tableNamePattern) + "'" +
769+
" AND TABLE_TYPE IN ('" + String.join("','", types) + "')";
763770

764771
try {
765772
return connection.createStatement().executeQuery(sql);
@@ -802,14 +809,14 @@ public ResultSet getCatalogs() throws SQLException {
802809
}
803810

804811
/**
805-
* Returns name of the ClickHouse table types as they are used in create table statements.
812+
* Returns name of the ClickHouse table types as the broad category (rather than engine name).
806813
* @return - ResultSet with one column TABLE_TYPE
807814
* @throws SQLException - if an error occurs
808815
*/
809816
@Override
810817
public ResultSet getTableTypes() throws SQLException {
811818
try {
812-
return connection.createStatement().executeQuery("SELECT name AS TABLE_TYPE FROM system.table_engines");
819+
return connection.createStatement().executeQuery("SELECT arrayJoin(['" + String.join("','", TABLE_TYPES) + "']) AS TABLE_TYPE ORDER BY TABLE_TYPE");
813820
} catch (Exception e) {
814821
throw ExceptionUtils.toSqlState(e);
815822
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.sql.Types;
1414
import java.sql.DatabaseMetaData;
1515
import java.util.Arrays;
16+
import java.util.Collections;
1617
import java.util.HashSet;
1718
import java.util.List;
1819
import java.util.Properties;
@@ -96,8 +97,20 @@ public void testGetTables() throws Exception {
9697
ResultSet rs = dbmd.getTables("system", null, "numbers", null);
9798
assertTrue(rs.next());
9899
assertEquals(rs.getString("TABLE_NAME"), "numbers");
99-
assertEquals(rs.getString("TABLE_TYPE"), "SystemNumbers");
100+
assertEquals(rs.getString("TABLE_TYPE"), "SYSTEM TABLE");
100101
assertFalse(rs.next());
102+
rs.close();
103+
104+
rs = dbmd.getTables("system", null, "numbers", new String[] { "SYSTEM TABLE" });
105+
assertTrue(rs.next());
106+
assertEquals(rs.getString("TABLE_NAME"), "numbers");
107+
assertEquals(rs.getString("TABLE_TYPE"), "SYSTEM TABLE");
108+
assertFalse(rs.next());
109+
rs.close();
110+
111+
rs = dbmd.getTables("system", null, "numbers", new String[] { "TABLE" });
112+
assertFalse(rs.next());
113+
rs.close();
101114
}
102115
}
103116

@@ -168,15 +181,14 @@ public void testGetTableTypes() throws Exception {
168181
try (Connection conn = getJdbcConnection()) {
169182
DatabaseMetaData dbmd = conn.getMetaData();
170183
ResultSet rs = dbmd.getTableTypes();
171-
int count = 0;
172-
Set<String> tableTypes = new HashSet<>(Arrays.asList("MergeTree", "Log", "Memory"));
173-
while (rs.next()) {
174-
tableTypes.remove(rs.getString("TABLE_TYPE"));
175-
count++;
184+
List<String> sortedTypes = Arrays.asList(com.clickhouse.jdbc.metadata.DatabaseMetaData.TABLE_TYPES);
185+
Collections.sort(sortedTypes);
186+
for (String type: sortedTypes) {
187+
assertTrue(rs.next());
188+
assertEquals(rs.getString("TABLE_TYPE"), type);
176189
}
177190

178-
assertTrue(count > 10);
179-
assertTrue(tableTypes.isEmpty(), "Not all table types are found: " + tableTypes);
191+
assertFalse(rs.next());
180192
}
181193
}
182194

0 commit comments

Comments
 (0)