yuqi1129 commented on code in PR #9761:
URL: https://github.com/apache/gravitino/pull/9761#discussion_r2741126945


##########
catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/operations/ClickHouseDatabaseOperations.java:
##########
@@ -18,19 +18,106 @@
  */
 package org.apache.gravitino.catalog.clickhouse.operations;
 
-import com.google.common.collect.Sets;
+import static 
org.apache.gravitino.catalog.clickhouse.ClickHouseConfig.DEFAULT_CK_ON_CLUSTER;
+
+import com.google.common.collect.ImmutableSet;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import javax.sql.DataSource;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.StringIdentifier;
+import org.apache.gravitino.catalog.clickhouse.ClickHouseConfig;
+import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
 import org.apache.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations;
 
 public class ClickHouseDatabaseOperations extends JdbcDatabaseOperations {
 
+  // TODO: handle ClickHouse cluster properly when creating/dropping 
databases/tables
+  //  use https://github.com/apache/gravitino/issues/9820 to track it.
+  @SuppressWarnings("unused")
+  private boolean onCluster = false;
+
+  @SuppressWarnings("unused")
+  private String clusterName = null;
+
+  @Override
+  public void initialize(
+      DataSource dataSource, JdbcExceptionConverter exceptionMapper, 
Map<String, String> conf) {
+    super.initialize(dataSource, exceptionMapper, conf);
+
+    final String cn = conf.get(ClickHouseConfig.CK_CLUSTER_NAME.getKey());
+    if (StringUtils.isNotBlank(cn)) {
+      clusterName = cn;
+    }
+
+    final String oc =
+        conf.getOrDefault(
+            ClickHouseConfig.CK_ON_CLUSTER.getKey(), 
String.valueOf(DEFAULT_CK_ON_CLUSTER));
+    onCluster = Boolean.parseBoolean(oc);
+
+    if (onCluster && StringUtils.isBlank(clusterName)) {
+      throw new IllegalArgumentException(
+          "ClickHouse 'ON CLUSTER' is enabled, but cluster name is not 
provided.");
+    }
+  }
+
   @Override
   protected boolean supportSchemaComment() {
-    return false;
+    return true;
   }
 
   @Override
   protected Set<String> createSysDatabaseNameSet() {
-    return Sets.newHashSet();
+    return ImmutableSet.of("information_schema", "default", "system");
+  }
+
+  @Override
+  public List<String> listDatabases() {
+    List<String> databaseNames = new ArrayList<>();
+    try (final Connection connection = getConnection()) {
+      // It is possible that other catalogs have been deleted,
+      // causing the following statement to error,
+      // so here we manually set a system catalog
+      connection.setCatalog(createSysDatabaseNameSet().iterator().next());
+      try (Statement statement = connection.createStatement();
+          ResultSet resultSet = statement.executeQuery("SHOW DATABASES")) {
+        while (resultSet.next()) {
+          String databaseName = resultSet.getString(1);
+          if (!isSystemDatabase(databaseName)) {
+            databaseNames.add(databaseName);
+          }
+        }
+      }
+      return databaseNames;
+    } catch (final SQLException se) {
+      throw this.exceptionMapper.toGravitinoException(se);
+    }
+  }
+
+  @Override
+  protected String generateCreateDatabaseSql(
+      String databaseName, String comment, Map<String, String> properties) {
+
+    String originComment = StringIdentifier.removeIdFromComment(comment);
+    if (!supportSchemaComment() && StringUtils.isNotEmpty(originComment)) {

Review Comment:
   This is a standard interface.  
   ```
     @Override
     protected boolean supportSchemaComment() {
       return true;
     }
   ```
   
   I would be reluctant to remove it. 
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to