Copilot commented on code in PR #9761: URL: https://github.com/apache/gravitino/pull/9761#discussion_r2707435876
########## catalogs/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/operations/ClickHouseDatabaseOperations.java: ########## @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.catalog.clickhouse.operations; + +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; +import org.apache.gravitino.exceptions.SchemaAlreadyExistsException; + +public class ClickHouseDatabaseOperations extends JdbcDatabaseOperations { + + private boolean onCluster = false; + 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.isNoneBlank(cn)) { Review Comment: Incorrect use of StringUtils.isNoneBlank() with a single parameter. The method isNoneBlank() is designed for checking multiple strings (varargs), but here only a single string is being checked. Use StringUtils.isNotBlank(cn) instead, which is the standard pattern used throughout the codebase for single string null/blank checks. ```suggestion if (StringUtils.isNotBlank(cn)) { ``` ########## catalogs/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/operations/TestClickHouseDatabaseOperations.java: ########## @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.catalog.clickhouse.operations; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.gravitino.catalog.clickhouse.ClickHouseConfig; +import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestClickHouseDatabaseOperations { + + private static class TestableClickHouseDatabaseOperations extends ClickHouseDatabaseOperations { + String buildCreateSql(String databaseName, String comment, Map<String, String> properties) { + return generateCreateDatabaseSql(databaseName, comment, properties); + } + } + + private TestableClickHouseDatabaseOperations newOps(Map<String, String> conf) { + TestableClickHouseDatabaseOperations ops = new TestableClickHouseDatabaseOperations(); + ops.initialize(null, new JdbcExceptionConverter(), conf); + return ops; + } + + @Test + void testGenerateCreateDatabaseSqlWithoutCluster() { + Map<String, String> conf = new HashMap<>(); + String sql = newOps(conf).buildCreateSql("db_name", null, Collections.emptyMap()); + Assertions.assertEquals("CREATE DATABASE `db_name`", sql); + } + + @Test + void testGenerateCreateDatabaseSqlWithCluster() { + Map<String, String> conf = new HashMap<>(); + conf.put(ClickHouseConfig.CK_CLUSTER_NAME.getKey(), "ck_cluster"); + conf.put(ClickHouseConfig.CK_ON_CLUSTER.getKey(), "true"); + + String sql = newOps(conf).buildCreateSql("db_name", "comment", Collections.emptyMap()); + Assertions.assertEquals( + "CREATE DATABASE `db_name` ON CLUSTER ck_cluster COMMENT 'comment'", sql); + } + + @Test + void testGenerateCreateDatabaseSqlWithClusterNameButDisabled() { + Map<String, String> conf = new HashMap<>(); + conf.put(ClickHouseConfig.CK_CLUSTER_NAME.getKey(), "ck_cluster"); + + String sql = newOps(conf).buildCreateSql("db_name", "comment", Collections.emptyMap()); + Assertions.assertEquals("CREATE DATABASE `db_name` COMMENT 'comment'", sql); + } + + @Test + void testGenerateCreateDatabaseSqlWithPropertiesThrows() { + Map<String, String> conf = new HashMap<>(); + TestableClickHouseDatabaseOperations ops = newOps(conf); + + Assertions.assertEquals( + "CREATE DATABASE `db_name` COMMENT 'comment'", + ops.buildCreateSql("db_name", "comment", Collections.emptyMap())); + } Review Comment: The test method name testGenerateCreateDatabaseSqlWithPropertiesThrows suggests it should test an exception being thrown, but the test actually just asserts that a SQL statement is generated correctly. Either rename the test to reflect what it actually tests (e.g., testGenerateCreateDatabaseSqlWithComment), or implement the intended exception-throwing test if properties are not supported. ########## catalogs/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/ClickhouseConstants.java: ########## @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.catalog.clickhouse; + +public class ClickhouseConstants { Review Comment: Inconsistent naming convention: This class is named "ClickhouseConstants" with lowercase 'h', while all other classes in the package use "ClickHouse" with uppercase 'H' (e.g., ClickHouseCatalog, ClickHouseConfig, ClickHouseDatabaseOperations). Rename to "ClickHouseConstants" for consistency across the module. ```suggestion public class ClickHouseConstants { ``` ########## catalogs/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/operations/ClickHouseDatabaseOperations.java: ########## @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.catalog.clickhouse.operations; + +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; +import org.apache.gravitino.exceptions.SchemaAlreadyExistsException; + +public class ClickHouseDatabaseOperations extends JdbcDatabaseOperations { + + private boolean onCluster = false; + 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.isNoneBlank(cn)) { + clusterName = cn; + } + + final String oc = conf.getOrDefault(ClickHouseConfig.CK_ON_CLUSTER.getKey(), "false"); + onCluster = Boolean.parseBoolean(oc); + } + + @Override + protected boolean supportSchemaComment() { + return true; + } + + @Override + protected Set<String> createSysDatabaseNameSet() { + return ImmutableSet.of("information_schema", "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 + public void create(String databaseName, String comment, Map<String, String> properties) + throws SchemaAlreadyExistsException { + super.create(databaseName, comment, properties); + } + + @Override Review Comment: This override of create() method serves no purpose as it only calls super.create() with the same parameters. Remove this unnecessary override to reduce code complexity and improve maintainability. ```suggestion ``` -- 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]
