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/10/26 18:34:47 UTC

[GitHub] [pinot] agavra commented on a diff in pull request #9643: [multistage] create pinot override SqlOpTable

agavra commented on code in PR #9643:
URL: https://github.com/apache/pinot/pull/9643#discussion_r1006036963


##########
pinot-query-planner/src/main/java/org/apache/calcite/sql/fun/PinotOperatorTable.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.calcite.sql.fun;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.validate.SqlNameMatchers;
+import org.apache.calcite.util.Util;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+
+
+/**
+ * {@link PinotOperatorTable} defines the {@link SqlOperator} overrides on top of the {@link SqlStdOperatorTable}.
+ *
+ * <p>The main purpose of this Pinot specific SQL operator table is to
+ * <ul>
+ *   <li>Ensure that any specific SQL validation rules can apply with Pinot override entirely over Calcite's.</li>
+ *   <li>Ability to create customer operators that are not function and cannot use
+ *     {@link org.apache.calcite.prepare.Prepare.CatalogReader} to override</li>
+ *   <li>Still maintain minimum customization and benefit from Calcite's original operator table setting.</li>
+ * </ul>
+ */
+public class PinotOperatorTable extends SqlStdOperatorTable {
+
+  private static @MonotonicNonNull PinotOperatorTable _instance;
+
+  public static final SqlFunction COALESCE = new PinotSqlCoalesceFunction();
+
+  public static synchronized PinotOperatorTable instance() {
+    if (_instance == null) {

Review Comment:
   nit: we can use Guava's `Suppliers#memoize` here if we want which has a safe implementation of DCL all wrapped into a convenient `Supplier` interface



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java:
##########
@@ -91,7 +91,10 @@ public QueryEnvironment(TypeFactory typeFactory, CalciteSchema rootSchema, Worke
         new CalciteConnectionConfigImpl(catalogReaderConfigProperties));
 
     _config = Frameworks.newConfigBuilder().traitDefs()
-        .operatorTable(new ChainedSqlOperatorTable(Arrays.asList(SqlStdOperatorTable.instance(), _catalogReader)))
+        .operatorTable(new ChainedSqlOperatorTable(Arrays.asList(
+            PinotOperatorTable.instance(),
+//            SqlStdOperatorTable.instance(),

Review Comment:
   nit: commented out code



##########
pinot-query-planner/src/main/java/org/apache/calcite/sql/fun/PinotOperatorTable.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.calcite.sql.fun;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.validate.SqlNameMatchers;
+import org.apache.calcite.util.Util;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+
+
+/**
+ * {@link PinotOperatorTable} defines the {@link SqlOperator} overrides on top of the {@link SqlStdOperatorTable}.
+ *
+ * <p>The main purpose of this Pinot specific SQL operator table is to
+ * <ul>
+ *   <li>Ensure that any specific SQL validation rules can apply with Pinot override entirely over Calcite's.</li>
+ *   <li>Ability to create customer operators that are not function and cannot use
+ *     {@link org.apache.calcite.prepare.Prepare.CatalogReader} to override</li>
+ *   <li>Still maintain minimum customization and benefit from Calcite's original operator table setting.</li>
+ * </ul>
+ */
+public class PinotOperatorTable extends SqlStdOperatorTable {
+
+  private static @MonotonicNonNull PinotOperatorTable _instance;
+
+  public static final SqlFunction COALESCE = new PinotSqlCoalesceFunction();
+
+  public static synchronized PinotOperatorTable instance() {
+    if (_instance == null) {
+      // Creates and initializes the standard operator table.
+      // Uses two-phase construction, because we can't initialize the
+      // table until the constructor of the sub-class has completed.
+      _instance = new PinotOperatorTable();
+      _instance.initNoDuplicate();
+    }
+    return _instance;
+  }
+
+  /**
+   * Initialize without duplicate, e.g. when 2 duplicate operator is linked with the same op
+   * {@link org.apache.calcite.sql.SqlKind} it causes problem.
+   */
+  public final void initNoDuplicate() {
+    // Use reflection to register the expressions stored in public fields.
+    for (Field field : getClass().getFields()) {

Review Comment:
   😬 instead of using reflection can we just have them all in an enum?
   ```
   enum Functions {
     COALESCE(new PinotSqlCoalesceFunction());
   
     final SqlOperator _op;
   
     Functions(SqlOperator op) {...}
   
     void register(PinotOperatorTable table) {...}
   }
   ```
   
   and then just call:
   ```
   for (Functions fun : Functions.values()) {
     fun.register(this);
   }
   ```
   
   Also is there any reason we differentiate between SqlFunction and SqlOperator? it looks like the code handling them is the same.



##########
pinot-query-planner/src/main/java/org/apache/calcite/sql/fun/PinotOperatorTable.java:
##########
@@ -0,0 +1,98 @@
+/**
+ * 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.calcite.sql.fun;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.validate.SqlNameMatchers;
+import org.apache.calcite.util.Util;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+
+
+/**
+ * {@link PinotOperatorTable} defines the {@link SqlOperator} overrides on top of the {@link SqlStdOperatorTable}.
+ *
+ * <p>The main purpose of this Pinot specific SQL operator table is to
+ * <ul>
+ *   <li>Ensure that any specific SQL validation rules can apply with Pinot override entirely over Calcite's.</li>
+ *   <li>Ability to create customer operators that are not function and cannot use
+ *     {@link org.apache.calcite.prepare.Prepare.CatalogReader} to override</li>
+ *   <li>Still maintain minimum customization and benefit from Calcite's original operator table setting.</li>
+ * </ul>
+ */
+public class PinotOperatorTable extends SqlStdOperatorTable {
+
+  private static @MonotonicNonNull PinotOperatorTable _instance;
+
+  public static final SqlFunction COALESCE = new PinotSqlCoalesceFunction();

Review Comment:
   nice, I think this is cleaner than my approach in https://github.com/apache/pinot/pull/9480 and would work for `AT TIME ZONE` as well



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