2020
2121public 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 }
0 commit comments