You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2022/12/08 17:31:03 UTC

[GitHub] [pinot] walterddr commented on a diff in pull request #9912: support case-insensitive query options in SET syntax

walterddr commented on code in PR #9912:
URL: https://github.com/apache/pinot/pull/9912#discussion_r1043633303


##########
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java:
##########
@@ -171,7 +172,7 @@ static SqlNodeAndOptions extractSqlNodeAndOptions(String sql, SqlNodeList sqlNod
     if (sqlType == null) {
       throw new SqlCompilationException("SqlNode with executable statement not found!");
     }
-    return new SqlNodeAndOptions(statementNode, sqlType, options);
+    return new SqlNodeAndOptions(statementNode, sqlType, QueryOptionsUtils.resolveCaseInsensitiveOptions(options));

Review Comment:
   this has several issues
   1. using legacy OPTION keyword will still be case-sensitive; and legacy OPTIONS keyword is written into the map after resolving SqlNodeAndOptions; how do we resolve conflicts in this situation? for example:
   ```
   SET useMultistageengine = true;
   SELECT ...
   OPTION(useMultistageEngine, false)
   ```
   2. SqlNodeAndOption only parses options in query, how does it handle json payload option keys?



##########
pinot-spi/src/test/java/org/apache/pinot/spi/utils/QueryOptionsUtilsTest.java:
##########
@@ -0,0 +1,45 @@
+/**
+ * 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.spi.utils;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class QueryOptionsUtilsTest {
+
+  @Test
+  public void shouldConvertCaseInsensitiveMapToUseCorrectValues() {
+    // Given:
+    Map<String, String> configs = ImmutableMap.of(
+        "ENABLENullHandling", "true",
+        "useMULTISTAGEEngine", "false"
+    );

Review Comment:
   let's also add a test for option key-value pairs that are not in the QueryOptionKey set
   1. with underscores
   2. with partial match
   3. completely irrelevant. 
   4. dot separated and some escaped characters. 
   
   the goal is to show that only those in QueryOptionKey are case insensitive. others are not



##########
pinot-spi/src/main/java/org/apache/pinot/spi/utils/QueryOptionsUtils.java:
##########
@@ -32,6 +36,59 @@ public class QueryOptionsUtils {
   private QueryOptionsUtils() {
   }
 
+
+  private static final Map<String, String> CONFIG_RESOLVER;
+  private static final RuntimeException CLASS_LOAD_ERROR;
+
+  static {
+    // this is a bit hacky, but lots of the code depends directly on usage of
+    // Map<String, String> (JSON serialization/GRPC code) so we cannot just
+    // refactor all code to use a case-insensitive abstraction like PinotConfiguration
+    // without a lot of work - additionally, the config constants are string constants
+    // instead of enums so there's no good way to iterate over them, but they are
+    // public API so we cannot just change them to be an enum
+    Map<String, String> configResolver = new HashMap<>();
+    Throwable classLoadError = null;
+
+    try {
+      for (Field declaredField : QueryOptionKey.class.getDeclaredFields()) {
+        if (declaredField.getType().equals(String.class)) {
+          int mods = declaredField.getModifiers();
+          if (Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
+            String config = (String) declaredField.get(null);
+            configResolver.put(config.toLowerCase(), config);
+          }
+        }
+      }
+    } catch (IllegalAccessException e) {
+      // prefer rethrowing this during runtime instead of a ClassNotFoundException
+      configResolver = null;
+      classLoadError = e;
+    }
+
+    CONFIG_RESOLVER = configResolver == null ? null : ImmutableMap.copyOf(configResolver);
+    CLASS_LOAD_ERROR = classLoadError == null ? null
+        : new RuntimeException("Failure to build case insensitive mapping.", classLoadError);
+  }
+
+  public static Map<String, String> resolveCaseInsensitiveOptions(Map<String, String> queryOptions) {
+    if (CLASS_LOAD_ERROR != null) {
+      throw CLASS_LOAD_ERROR;
+    }
+
+    Map<String, String> resolved = new HashMap<>();
+    for (Map.Entry<String, String> configEntry : queryOptions.entrySet()) {
+      String config = CONFIG_RESOLVER.get(configEntry.getKey().toLowerCase());
+      if (config != null) {
+        resolved.put(config, configEntry.getValue());
+      } else {
+        resolved.put(configEntry.getKey(), configEntry.getValue());
+      }
+    }

Review Comment:
   seems like the reason why this has to be so complex is b/c `QueryOptionKey` is a inner class not a enum
   it looks like thing will get much simpler if we can do the same as `DataTable.MetadataKey` which is an ENUM. 
   
   @siddharthteotia any concerns on backward compatibility?



-- 
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-unsubscribe@pinot.apache.org

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


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