Jackie-Jiang commented on code in PR #12417:
URL: https://github.com/apache/pinot/pull/12417#discussion_r1516957220


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/PinotHelixResourceManager.java:
##########
@@ -709,68 +709,131 @@ public List<String> getAllResources() {
   }
 
   /**
-   * Get all table names (with type suffix).
+   * Get all table names (with type suffix) in default database.
    *
-   * @return List of table names
+   * @return List of table names in default database
    */
   public List<String> getAllTables() {
+    return getAllTables(null);
+  }
+
+  /**
+   * Get all table names (with type suffix) from provided database.
+   *
+   * @param databaseName database name
+   * @return List of table names in provided database name
+   */
+  public List<String> getAllTables(String databaseName) {
     List<String> tableNames = new ArrayList<>();
     for (String resourceName : getAllResources()) {
-      if (TableNameBuilder.isTableResource(resourceName)) {
+      if (TableNameBuilder.isTableResource(resourceName) && 
isPartOfDatabase(resourceName, databaseName)) {
         tableNames.add(resourceName);
       }
     }
     return tableNames;
   }
 
+  private boolean isPartOfDatabase(String tableName, String databaseName) {
+    if (databaseName == null) {

Review Comment:
   Consider allowing empty database name



##########
pinot-common/src/main/java/org/apache/pinot/common/config/provider/TableCache.java:
##########
@@ -71,6 +73,8 @@ public class TableCache implements PinotConfigProvider {
   private static final String LOWER_CASE_OFFLINE_TABLE_SUFFIX = 
OFFLINE_TABLE_SUFFIX.toLowerCase();
   private static final String LOWER_CASE_REALTIME_TABLE_SUFFIX = 
REALTIME_TABLE_SUFFIX.toLowerCase();
 
+  private static final String DEFAULT_DATABASE_PREFIX = 
CommonConstants.DEFAULT_DATABASE + ".";

Review Comment:
   I don't fully follow the logic here. Ideally `DatabaseUtils` should be able 
to trim off the `default.` prefix, and we should never see it within this class



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/PinotHelixResourceManager.java:
##########
@@ -709,68 +709,131 @@ public List<String> getAllResources() {
   }
 
   /**
-   * Get all table names (with type suffix).
+   * Get all table names (with type suffix) in default database.
    *
-   * @return List of table names
+   * @return List of table names in default database
    */
   public List<String> getAllTables() {
+    return getAllTables(null);
+  }
+
+  /**
+   * Get all table names (with type suffix) from provided database.
+   *
+   * @param databaseName database name
+   * @return List of table names in provided database name
+   */
+  public List<String> getAllTables(String databaseName) {

Review Comment:
   Same for other places
   ```suggestion
     public List<String> getAllTables(@Nullable String databaseName) {
   ```



##########
pinot-common/src/main/java/org/apache/pinot/common/utils/DatabaseUtils.java:
##########
@@ -0,0 +1,72 @@
+/**
+ * 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.pinot.common.utils;
+
+import com.google.common.base.Preconditions;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.HttpHeaders;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+public class DatabaseUtils {
+  private DatabaseUtils() {
+  }
+
+  /**
+   * Construct the fully qualified table name i.e. {databaseName}.{tableName} 
from given table name and database name
+   * @param tableName table/schema name
+   * @param databaseName database name
+   * @return translated table name. Throws {@link IllegalArgumentException} if 
{@code tableName} contains
+   * more than 1 dot or if {@code tableName} has database prefix, and it does 
not match with {@code databaseName}
+   */
+  public static String translateTableName(String tableName, @Nullable String 
databaseName) {
+    Preconditions.checkArgument(tableName != null, "'tableName' cannot be 
null");
+    String[] tableSplit = StringUtils.split(tableName, '.');
+    switch (tableSplit.length) {
+      case 1:
+        // do not concat the database name prefix if it's a 'default' database
+        if (StringUtils.isNotEmpty(databaseName) && 
!databaseName.equalsIgnoreCase(CommonConstants.DEFAULT_DATABASE)) {
+          return databaseName + "." + tableName;
+        }
+        return tableName;
+      case 2:
+        String databasePrefix = tableSplit[0];
+        if (StringUtils.isNotEmpty(databaseName) && 
!databaseName.equals(databasePrefix)) {
+          throw new IllegalArgumentException("Database name '" + databasePrefix

Review Comment:
   (minor) Use `Preconditions.checkArgument()`



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/PinotHelixResourceManager.java:
##########
@@ -709,68 +709,131 @@ public List<String> getAllResources() {
   }
 
   /**
-   * Get all table names (with type suffix).
+   * Get all table names (with type suffix) in default database.
    *
-   * @return List of table names
+   * @return List of table names in default database
    */
   public List<String> getAllTables() {
+    return getAllTables(null);
+  }
+
+  /**
+   * Get all table names (with type suffix) from provided database.
+   *
+   * @param databaseName database name
+   * @return List of table names in provided database name
+   */
+  public List<String> getAllTables(String databaseName) {
     List<String> tableNames = new ArrayList<>();
     for (String resourceName : getAllResources()) {
-      if (TableNameBuilder.isTableResource(resourceName)) {
+      if (TableNameBuilder.isTableResource(resourceName) && 
isPartOfDatabase(resourceName, databaseName)) {
         tableNames.add(resourceName);
       }
     }
     return tableNames;
   }
 
+  private boolean isPartOfDatabase(String tableName, String databaseName) {
+    if (databaseName == null) {
+      return StringUtils.split(tableName, '.').length == 1;
+    } else if 
(databaseName.equalsIgnoreCase(CommonConstants.DEFAULT_DATABASE)) {
+      String[] split = StringUtils.split(tableName, '.');
+      return split.length == 1 || 
split[0].equalsIgnoreCase(CommonConstants.DEFAULT_DATABASE);

Review Comment:
   To simplify the handling, we should prevent table with `default.` prefix. 
Table should either have non-default database prefix, or no prefix



##########
pinot-common/src/main/java/org/apache/pinot/common/utils/DatabaseUtils.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * 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.pinot.common.utils;
+
+import java.util.Objects;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.HttpHeaders;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.utils.CommonConstants;
+
+
+public class DatabaseUtils {
+  private DatabaseUtils() {
+  }
+
+  /**
+   * Construct the fully qualified table name i.e. {databaseName}.{tableName} 
from given table name and database name
+   * @param tableName table/schema name
+   * @param databaseName database name
+   * @return translated table name. Throws {@link IllegalStateException} if 
{@code tableName} contains more than 1 dot
+   * or if {@code tableName} has database prefix, and it does not match with 
{@code databaseName}
+   */
+  public static String translateTableName(String tableName, @Nullable String 
databaseName) {
+    if (tableName == null) {
+      throw new IllegalArgumentException("'tableName' cannot be null");
+    }
+    String[] tableSplit = StringUtils.split(tableName, '.');
+    switch (tableSplit.length) {
+      case 1:
+        // do not concat the database name prefix if it's a 'default' database
+        if (StringUtils.isNotEmpty(databaseName) && 
!databaseName.equalsIgnoreCase(CommonConstants.DEFAULT_DATABASE)) {
+          return String.format("%s.%s", databaseName, tableName);
+        }
+        return tableName;
+      case 2:
+        String databasePrefix = tableSplit[0];
+        if (StringUtils.isNotEmpty(databaseName) && 
!databaseName.equals(databasePrefix)) {
+          throw new IllegalArgumentException("Database name '" + databasePrefix
+              + "' from table prefix does not match database name '" + 
databaseName + "' from header");
+        }
+        // skip database name prefix if it's a 'default' database
+        return 
databasePrefix.equalsIgnoreCase(CommonConstants.DEFAULT_DATABASE) ? 
tableSplit[1] : tableName;
+      default:
+      throw new IllegalArgumentException("Table name: '" + tableName + "' 
containing more than one '.' is not allowed");

Review Comment:
   ^^



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to