taylor12805 commented on code in PR #7738:
URL: https://github.com/apache/gravitino/pull/7738#discussion_r2225164425


##########
catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/operations/StarRocksDatabaseOperations.java:
##########
@@ -19,34 +19,87 @@
 package org.apache.gravitino.catalog.starrocks.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.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import org.apache.commons.lang3.NotImplementedException;
 import org.apache.gravitino.catalog.jdbc.JdbcSchema;
 import org.apache.gravitino.catalog.jdbc.operation.JdbcDatabaseOperations;
+import org.apache.gravitino.catalog.starrocks.utils.StarRocksUtils;
 import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.meta.AuditInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Database operations for StarRocks. */
 public class StarRocksDatabaseOperations extends JdbcDatabaseOperations {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(StarRocksDatabaseOperations.class);
+
   @Override
   public String generateCreateDatabaseSql(
       String databaseName, String comment, Map<String, String> properties) {
-    throw new NotImplementedException("To be implemented in the future");
+    StringBuilder sqlBuilder = new StringBuilder();
+    sqlBuilder.append(String.format("CREATE DATABASE `%s`", databaseName));
+
+    // Append properties
+    sqlBuilder.append("\n");
+    sqlBuilder.append(StarRocksUtils.generatePropertiesSql(properties));
+
+    String ddl = sqlBuilder.toString();
+    LOG.info("Generated create database:{} sql: {}", databaseName, ddl);
+    return ddl;
   }
 
   @Override
   public String generateDropDatabaseSql(String databaseName, boolean cascade) {
-    throw new NotImplementedException("To be implemented in the future");
+    StringBuilder sqlBuilder = new StringBuilder();
+    sqlBuilder.append(String.format("DROP DATABASE `%s`", databaseName));
+    if (cascade) {
+      sqlBuilder.append(" FORCE");
+      return sqlBuilder.toString();
+    }
+    String query = String.format("SHOW TABLES IN `%s`", databaseName);
+    try (final Connection connection = this.dataSource.getConnection();
+        Statement statement = connection.createStatement();
+        ResultSet resultSet = statement.executeQuery(query)) {
+      if (resultSet.next()) {
+        throw new IllegalStateException(
+            String.format(
+                "Database %s is not empty, the value of cascade should be 
true.", databaseName));
+      }
+    } catch (SQLException sqlException) {
+      throw this.exceptionMapper.toGravitinoException(sqlException);
+    }
+    return sqlBuilder.toString();
   }
 
   @Override
   public JdbcSchema load(String databaseName) throws NoSuchSchemaException {
-    throw new NotImplementedException("To be implemented in the future");
+    List<String> allDatabases = listDatabases();
+    String dbName =
+        allDatabases.stream()
+            .filter(db -> db.equals(databaseName))
+            .findFirst()
+            .orElseThrow(
+                () -> new NoSuchSchemaException("Database %s could not be 
found", databaseName));
+    // StarRocks support set properties, but can't get them after setting
+    // 
https://docs.starrocks.io/docs/3.3/sql-reference/sql-statements/Database/SHOW_CREATE_DATABASE/
+    return JdbcSchema.builder()
+        .withName(dbName)
+        .withComment("")
+        .withProperties(Collections.emptyMap())
+        .withAuditInfo(AuditInfo.EMPTY)
+        .build();
   }
 
   @Override
   protected boolean supportSchemaComment() {
-    return true;
+    return false;

Review Comment:
   Why not support schema comment since it can be added to PROPERTIES as in 
Doris



##########
catalogs/catalog-jdbc-starrocks/src/main/java/org/apache/gravitino/catalog/starrocks/utils/StarRocksUtils.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.starrocks.utils;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import org.apache.gravitino.rel.expressions.NamedReference;
+import org.apache.gravitino.rel.expressions.distributions.Distribution;
+import org.apache.gravitino.rel.expressions.distributions.Distributions;
+import org.apache.gravitino.rel.expressions.distributions.Strategy;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.expressions.transforms.Transform;
+import org.apache.gravitino.rel.expressions.transforms.Transforms;
+import org.apache.gravitino.rel.partitions.ListPartition;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.gravitino.rel.partitions.Partitions;
+import org.apache.gravitino.rel.partitions.RangePartition;
+import org.apache.gravitino.rel.types.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StarRocksUtils {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(StarRocksUtils.class);
+
+  private static final Pattern PARTITION_INFO_PATTERN =
+      Pattern.compile("PARTITION BY \\b(LIST|RANGE)\\b\\((.+)\\)\\s*\\(?");
+
+  private static final Pattern DISTRIBUTION_INFO_PATTERN =
+      Pattern.compile(
+          "DISTRIBUTED 
BY\\s+(HASH|RANDOM)\\s*(\\(([^)]+)\\))?\\s*(BUCKETS\\s+(\\d+|AUTO))?");

Review Comment:
   StarRocks don't have auto keyword



-- 
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