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/07 23:56:28 UTC

[GitHub] [pinot] agavra commented on a diff in pull request #9937: [multistage] Resolve case sensitivity issue on functions

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


##########
pinot-query-planner/src/main/java/org/apache/calcite/prepare/PinotCalciteCatalogReader.java:
##########
@@ -0,0 +1,463 @@
+/**
+ * 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.prepare;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import org.apache.calcite.config.CalciteConnectionConfig;
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.linq4j.function.Hints;
+import org.apache.calcite.model.ModelHandler;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.schema.AggregateFunction;
+import org.apache.calcite.schema.ScalarFunction;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.TableFunction;
+import org.apache.calcite.schema.TableMacro;
+import org.apache.calcite.schema.Wrapper;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.SqlSyntax;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.InferTypes;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandMetadata;
+import org.apache.calcite.sql.type.SqlOperandTypeInference;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.util.ListSqlOperatorTable;
+import org.apache.calcite.sql.validate.SqlMoniker;
+import org.apache.calcite.sql.validate.SqlMonikerImpl;
+import org.apache.calcite.sql.validate.SqlMonikerType;
+import org.apache.calcite.sql.validate.SqlNameMatcher;
+import org.apache.calcite.sql.validate.SqlNameMatchers;
+import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableMacro;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.calcite.util.Optionality;
+import org.apache.calcite.util.Util;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+
+/**
+ * ============================================================================
+ * THIS CLASS IS COPIED FROM Calcite's {@link org.apache.calcite.prepare.CalciteCatalogReader} and modified the
+ * case sensitivity of Function lookup. which is ALWAYS case-insensitive regardless of conventions on
+ * column/table identifier.

Review Comment:
   can we contribute a plugin back to Calcite for this?



##########
pinot-query-planner/src/main/java/org/apache/calcite/sql/util/PinotChainedSqlOperatorTable.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.calcite.sql.util;
+
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.SqlSyntax;
+import org.apache.calcite.sql.validate.SqlNameMatcher;
+
+import com.google.common.collect.ImmutableList;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ============================================================================
+ * THIS CLASS IS COPIED FROM Calcite's {@link org.apache.calcite.sql.util.ChainedSqlOperatorTable} and modified the
+ * function lookup to terminate early once found from ordered SqlOperatorTable list.
+ * ============================================================================
+ *
+ * PinotChainedSqlOperatorTable implements the {@link SqlOperatorTable} interface by
+ * chaining together any number of underlying operator table instances.
+ */
+public class PinotChainedSqlOperatorTable implements SqlOperatorTable {
+  //~ Instance fields --------------------------------------------------------
+
+  protected final List<SqlOperatorTable> _tableList;
+
+  //~ Constructors -----------------------------------------------------------
+
+  public PinotChainedSqlOperatorTable(List<SqlOperatorTable> tableList) {
+    this(ImmutableList.copyOf(tableList));
+  }
+
+  /** Internal constructor; call {@link SqlOperatorTables#chain}. */
+  protected PinotChainedSqlOperatorTable(ImmutableList<SqlOperatorTable> tableList) {
+    _tableList = ImmutableList.copyOf(tableList);
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  @Deprecated // to be removed before 2.0
+  public void add(SqlOperatorTable table) {
+    if (!_tableList.contains(table)) {
+      _tableList.add(table);
+    }
+  }
+
+  @Override public void lookupOperatorOverloads(SqlIdentifier opName,
+      @Nullable SqlFunctionCategory category, SqlSyntax syntax,
+      List<SqlOperator> operatorList, SqlNameMatcher nameMatcher) {
+    for (SqlOperatorTable table : _tableList) {
+      table.lookupOperatorOverloads(opName, category, syntax, operatorList,
+          nameMatcher);
+      // ======================================================================
+      // CHANGED LINES BELOW
+      // ======================================================================
+      if (!operatorList.isEmpty()) {

Review Comment:
   if I understand calcite correctly (i spent a lot of time debugging this code) it'll just return everything from the first operator table - which might still have multiple matches within it.
   
   @walterddr why is this necessary?



##########
pinot-query-planner/src/main/java/org/apache/calcite/prepare/PinotCalciteCatalogReader.java:
##########
@@ -0,0 +1,466 @@
+/**
+ * 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.prepare;
+
+import org.apache.calcite.config.CalciteConnectionConfig;
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.linq4j.function.Hints;
+import org.apache.calcite.model.ModelHandler;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.schema.AggregateFunction;
+import org.apache.calcite.schema.ScalarFunction;
+import org.apache.calcite.schema.Table;
+import org.apache.calcite.schema.TableFunction;
+import org.apache.calcite.schema.TableMacro;
+import org.apache.calcite.schema.Wrapper;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.SqlSyntax;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.InferTypes;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandMetadata;
+import org.apache.calcite.sql.type.SqlOperandTypeInference;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.util.ListSqlOperatorTable;
+import org.apache.calcite.sql.validate.SqlMoniker;
+import org.apache.calcite.sql.validate.SqlMonikerImpl;
+import org.apache.calcite.sql.validate.SqlMonikerType;
+import org.apache.calcite.sql.validate.SqlNameMatcher;
+import org.apache.calcite.sql.validate.SqlNameMatchers;
+import org.apache.calcite.sql.validate.SqlUserDefinedAggFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableFunction;
+import org.apache.calcite.sql.validate.SqlUserDefinedTableMacro;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.calcite.util.Optionality;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+
+/**
+ * ============================================================================
+ * THIS CLASS IS COPIED FROM Calcite's {@link org.apache.calcite.prepare.CalciteCatalogReader} and modified the
+ * case sensitivity of Function lookup. which is ALWAYS case-insensitive regardless of conventions on
+ * column/table identifier.
+ * ============================================================================
+ *
+ * Pinot's implementation of {@link org.apache.calcite.prepare.Prepare.CatalogReader}
+ * and also {@link org.apache.calcite.sql.SqlOperatorTable} based on tables and
+ * functions defined schemas.
+ */
+public class PinotCalciteCatalogReader implements Prepare.CatalogReader {
+  protected final CalciteSchema _rootSchema;
+  protected final RelDataTypeFactory _typeFactory;
+  private final List<List<String>> _schemaPaths;
+  protected final SqlNameMatcher _nameMatcher;
+  protected final CalciteConnectionConfig _config;
+
+  public PinotCalciteCatalogReader(CalciteSchema rootSchema,
+      List<String> defaultSchema, RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
+    this(rootSchema, SqlNameMatchers.withCaseSensitive(config != null && config.caseSensitive()),
+        ImmutableList.of(Objects.requireNonNull(defaultSchema, "defaultSchema"),
+            ImmutableList.of()),
+        typeFactory, config);
+  }
+
+  protected PinotCalciteCatalogReader(CalciteSchema rootSchema,
+      SqlNameMatcher nameMatcher, List<List<String>> schemaPaths,
+      RelDataTypeFactory typeFactory, CalciteConnectionConfig config) {
+    _rootSchema = Objects.requireNonNull(rootSchema, "rootSchema");
+    _nameMatcher = nameMatcher;
+    _schemaPaths =
+        Util.immutableCopy(Util.isDistinct(schemaPaths)
+            ? schemaPaths
+            : new LinkedHashSet<>(schemaPaths));
+    _typeFactory = typeFactory;
+    _config = config;
+  }
+
+  @Override public PinotCalciteCatalogReader withSchemaPath(List<String> schemaPath) {
+    return new PinotCalciteCatalogReader(_rootSchema, _nameMatcher,
+        ImmutableList.of(schemaPath, ImmutableList.of()), _typeFactory, _config);
+  }
+
+  @Override public Prepare.@Nullable PreparingTable getTable(final List<String> names) {
+    // First look in the default schema, if any.
+    // If not found, look in the root schema.
+    CalciteSchema.TableEntry entry = SqlValidatorUtil.getTableEntry(this, names);
+    if (entry != null) {
+      final Table table = entry.getTable();
+      if (table instanceof Wrapper) {
+        final Prepare.PreparingTable relOptTable =
+            ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
+        if (relOptTable != null) {
+          return relOptTable;
+        }
+      }
+      return RelOptTableImpl.create(this,
+          table.getRowType(_typeFactory), entry, null);
+    }
+    return null;
+  }
+
+  @Override public CalciteConnectionConfig getConfig() {
+    return _config;
+  }
+
+  private Collection<org.apache.calcite.schema.Function> getFunctionsFrom(
+      List<String> names) {
+    final List<org.apache.calcite.schema.Function> functions2 =
+        new ArrayList<>();
+    final List<List<String>> schemaNameList = new ArrayList<>();
+    if (names.size() > 1) {
+      // Name qualified: ignore path. But we do look in "/catalog" and "/",
+      // the last 2 items in the path.
+      if (_schemaPaths.size() > 1) {
+        schemaNameList.addAll(Util.skip(_schemaPaths));
+      } else {
+        schemaNameList.addAll(_schemaPaths);
+      }
+    } else {
+      for (List<String> schemaPath : _schemaPaths) {
+        CalciteSchema schema =
+            SqlValidatorUtil.getSchema(_rootSchema, schemaPath, _nameMatcher);
+        if (schema != null) {
+          schemaNameList.addAll(schema.getPath());
+        }
+      }
+    }
+    for (List<String> schemaNames : schemaNameList) {
+      CalciteSchema schema =
+          SqlValidatorUtil.getSchema(_rootSchema,
+              Iterables.concat(schemaNames, Util.skipLast(names)), _nameMatcher);
+      if (schema != null) {
+        final String name = Util.last(names);
+        // ====================================================================
+        // LINE CHANGED BELOW
+        // ====================================================================
+        functions2.addAll(schema.getFunctions(name, false));

Review Comment:
   I think the "proper" way to do that would be to create:
   ```
   private static final boolean NOT_CASE_SENSITIVE = false;
   ...
   functions2.addAll(schema.getFunctions(name, NOT_CASE_SENSITIVE));
   ```



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