Skip to content

Commit

Permalink
Merge pull request #2017 from ClickHouse/metabase-table-types
Browse files Browse the repository at this point in the history
Updating code + test
  • Loading branch information
Paultagoras authored Dec 13, 2024
2 parents f880b09 + 1353dd9 commit 0b7a240
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

public class DatabaseMetaData implements java.sql.DatabaseMetaData, JdbcV2Wrapper {
private static final Logger log = LoggerFactory.getLogger(DatabaseMetaData.class);
public static final String[] TABLE_TYPES = new String[] { "DICTIONARY", "LOG TABLE", "MEMORY TABLE",
"REMOTE TABLE", "TABLE", "VIEW", "SYSTEM TABLE", "TEMPORARY TABLE" };

ConnectionImpl connection;

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

String sql = "SELECT " +
catalogPlaceholder + " AS TABLE_CAT, " +
"t.database AS TABLE_SCHEM, " +
"t.name AS TABLE_NAME, " +
"t.engine AS TABLE_TYPE, " +
"CASE WHEN t.engine LIKE '%Log' THEN 'LOG TABLE' " +
"WHEN t.engine in ('Buffer', 'Memory', 'Set') THEN 'MEMORY TABLE' " +
"WHEN t.is_temporary != 0 THEN 'TEMPORARY TABLE' " +
"WHEN t.engine like '%View' THEN 'VIEW'" +
"WHEN t.engine = 'Dictionary' THEN 'DICTIONARY' " +
"WHEN t.engine LIKE 'Async%' OR t.engine LIKE 'System%' THEN 'SYSTEM TABLE' " +
"WHEN empty(t.data_paths) THEN 'REMOTE TABLE' " +
"ELSE 'TABLE' END AS TABLE_TYPE, " +
"t.comment AS REMARKS, " +
"null AS TYPE_CAT, " + // no types catalog
"d.engine AS TYPE_SCHEM, " + // no types schema
Expand All @@ -752,14 +765,8 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam
" FROM system.tables t" +
" JOIN system.databases d ON system.tables.database = system.databases.name" +
" WHERE t.database LIKE '" + (schemaPattern == null ? "%" : schemaPattern) + "'" +
" AND t.name LIKE '" + (tableNamePattern == null ? "%" : tableNamePattern) + "'";
if (types != null && types.length > 0) {
sql += "AND t.engine IN (";
for (String type : types) {
sql += "'" + type + "',";
}
sql = sql.substring(0, sql.length() - 1) + ") ";
}
" AND t.name LIKE '" + (tableNamePattern == null ? "%" : tableNamePattern) + "'" +
" AND TABLE_TYPE IN ('" + String.join("','", types) + "')";

try {
return connection.createStatement().executeQuery(sql);
Expand Down Expand Up @@ -802,14 +809,14 @@ public ResultSet getCatalogs() throws SQLException {
}

/**
* Returns name of the ClickHouse table types as they are used in create table statements.
* Returns name of the ClickHouse table types as the broad category (rather than engine name).
* @return - ResultSet with one column TABLE_TYPE
* @throws SQLException - if an error occurs
*/
@Override
public ResultSet getTableTypes() throws SQLException {
try {
return connection.createStatement().executeQuery("SELECT name AS TABLE_TYPE FROM system.table_engines");
return connection.createStatement().executeQuery("SELECT arrayJoin(['" + String.join("','", TABLE_TYPES) + "']) AS TABLE_TYPE ORDER BY TABLE_TYPE");
} catch (Exception e) {
throw ExceptionUtils.toSqlState(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.Types;
import java.sql.DatabaseMetaData;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -96,8 +97,20 @@ public void testGetTables() throws Exception {
ResultSet rs = dbmd.getTables("system", null, "numbers", null);
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_NAME"), "numbers");
assertEquals(rs.getString("TABLE_TYPE"), "SystemNumbers");
assertEquals(rs.getString("TABLE_TYPE"), "SYSTEM TABLE");
assertFalse(rs.next());
rs.close();

rs = dbmd.getTables("system", null, "numbers", new String[] { "SYSTEM TABLE" });
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_NAME"), "numbers");
assertEquals(rs.getString("TABLE_TYPE"), "SYSTEM TABLE");
assertFalse(rs.next());
rs.close();

rs = dbmd.getTables("system", null, "numbers", new String[] { "TABLE" });
assertFalse(rs.next());
rs.close();
}
}

Expand Down Expand Up @@ -168,15 +181,14 @@ public void testGetTableTypes() throws Exception {
try (Connection conn = getJdbcConnection()) {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getTableTypes();
int count = 0;
Set<String> tableTypes = new HashSet<>(Arrays.asList("MergeTree", "Log", "Memory"));
while (rs.next()) {
tableTypes.remove(rs.getString("TABLE_TYPE"));
count++;
List<String> sortedTypes = Arrays.asList(com.clickhouse.jdbc.metadata.DatabaseMetaData.TABLE_TYPES);
Collections.sort(sortedTypes);
for (String type: sortedTypes) {
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_TYPE"), type);
}

assertTrue(count > 10);
assertTrue(tableTypes.isEmpty(), "Not all table types are found: " + tableTypes);
assertFalse(rs.next());
}
}

Expand Down

0 comments on commit 0b7a240

Please sign in to comment.