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


##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -320,6 +321,11 @@ protected BrokerResponse handleRequest(long requestId, 
String query, @Nullable S
         // Compile the request into PinotQuery
         compilationStartTimeNs = System.nanoTime();
         pinotQuery = CalciteSqlParser.compileToPinotQuery(sqlNodeAndOptions);
+        if (pinotQuery.getDataSource() != null && 
pinotQuery.getDataSource().getTableName() != null) {
+          String tableName = 
getActualTableName(DatabaseUtils.translateTableName(
+              pinotQuery.getDataSource().getTableName(), httpHeaders), 
_tableCache);
+        pinotQuery.getDataSource().setTableName(tableName);
+        }

Review Comment:
   Revert this? Table name parsing is handled below



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -1652,6 +1648,7 @@ private static void fixColumnName(String rawTableName, 
Expression expression, Ma
    * Returns the actual column name for the given column name for:
    * - Case-insensitive cluster
    * - Column name in the format of [table_name].[column_name]
+   * - Column name in the format of [database_name].[table_name].[column_name]

Review Comment:
   I don't think we need to support such complicated column name (at least for 
database isolation purpose)



##########
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:
   Are the changes in this class required?



##########
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)) {

Review Comment:
   Why skipping "default"? There shouldn't be default concept



##########
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);

Review Comment:
   (minor) Simple concatenation should be faster: `databaseName + "." + 
tableName`



##########
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");
+    }

Review Comment:
   (minor) 
   ```suggestion
       Preconditions.checkArgument(tableName != null, "'tableName' cannot be 
null");
   ```



##########
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:
   (minor) reformat



##########
pinot-common/src/main/java/org/apache/pinot/common/utils/DatabaseUtils.java:
##########
@@ -0,0 +1,91 @@
+/**
+ * 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.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Objects;
+import javax.annotation.Nullable;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.core.MultivaluedMap;
+import org.apache.pinot.common.config.provider.TableCache;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DatabaseUtils {
+  private DatabaseUtils() {
+  }
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DatabaseUtils.class);
+
+  private static final List<String> TABLE_NAME_KEYS = List.of("tableName", 
"tableNameWithType", "schemaName");
+
+  public static void translateTableNameQueryParam(ContainerRequestContext 
requestContext, TableCache tableCache) {
+    MultivaluedMap<String, String> queryParams = 
requestContext.getUriInfo().getQueryParameters();
+    String uri = requestContext.getUriInfo().getRequestUri().toString();
+    String databaseName = null;
+    if (requestContext.getHeaders().containsKey(CommonConstants.DATABASE)) {
+      databaseName = requestContext.getHeaderString(CommonConstants.DATABASE);
+    }
+    for (String key : TABLE_NAME_KEYS) {
+      if (queryParams.containsKey(key)) {
+        String tableName = queryParams.getFirst(key);
+        String actualTableName = translateTableName(tableName, databaseName, 
tableCache);
+        // table is not part of default database
+        if (!actualTableName.equals(tableName)) {
+          uri = uri.replaceAll(String.format("%s=%s", key, tableName),
+              String.format("%s=%s", key, actualTableName));
+          try {
+            requestContext.setRequestUri(new URI(uri));
+          } catch (URISyntaxException e) {
+            LOGGER.error("Unable to translate the table name from {} to {}", 
tableName, actualTableName);
+          }
+        }
+      }
+    }
+  }
+
+  public static String translateTableName(String tableName, String 
databaseName, @Nullable TableCache tableCache) {
+    if (tableName != null && databaseName != null) {
+      String[] tableSplit = tableName.split("\\.");
+      if (tableSplit.length > 2) {
+        throw new IllegalStateException("Table name: '" + tableName + "' 
containing more than one '.' is not allowed");
+      } else if (tableSplit.length == 2) {
+        databaseName = tableSplit[0];
+        tableName = tableSplit[1];
+      }
+      if (databaseName != null && !databaseName.isBlank()) {
+        tableName = String.format("%s.%s", databaseName, tableName);
+      }
+    }
+    String actualTableName = null;
+    if (tableCache != null) {
+      actualTableName = tableCache.getActualTableName(tableName);
+    }
+    return actualTableName != null ? actualTableName : tableName;
+  }
+
+  public static boolean isTableNameEquivalent(String name1, String name2) {

Review Comment:
   Is this still in use?



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