Jackie-Jiang commented on a change in pull request #7394:
URL: https://github.com/apache/pinot/pull/7394#discussion_r703817541



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/StringFunctions.java
##########
@@ -389,4 +389,15 @@ public static int hammingDistance(String input1, String 
input2) {
   public static boolean contains(String input, String substring) {
     return input.contains(substring);
   }
+
+  /**
+   * Compare input strings lexicographically.
+   * @return the value 0 if the first string argument is equal to second 
string; a value less than 0 if first string
+   * argument is lexicographically less than the second string argument; and a 
value greater than 0 if the first string
+   * argument is lexicographically greater than the second string argument.
+   */
+  @ScalarFunction
+  public static int compare(String input1, String input2) {

Review comment:
       Let's rename it to `strcmp` which is a standard MySQL function. Since we 
don't have type matching yet, we want to differentiate the string comparison 
from comparison of other types

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/query/optimizer/statement/StringPredicateFilterOptimizer.java
##########
@@ -0,0 +1,127 @@
+/**
+ * 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.core.query.optimizer.statement;
+
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.ExpressionType;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+
+
+/**
+ * Given two column names 'strColumn1' and 'strColumn1', 
CalciteSqlParser.queryRewrite will turn WHERE and HAVING
+ * expressions of form "strColumn1 <operator> strColumn2" into 
"MINUS(strColumn1,strColumn2) <operator> 0" regardless
+ * of the column datatype. The resulting query will fail to evaluate since the 
MINUS operator does not work with the
+ * STRING column type. This class rewrites expressions of form 
"MINUS(strColumn1,strColumn2) <operator> 0" to
+ * "COMPARE(strColumn1, strColumn2) <operator> 0" to fix the issue.
+ *
+ * Currently, rewrite phase (see CalciteSqlParser.queryRewrite) does not have 
access to schema; hence, we need to again
+ * rewrite MINUS(strColumn1, strColumn2) into COMPARE(strColumnAt some point, 
we should merge query rewrite phase with
+ * optimizer phase to avoid such issues altogether.
+ */
+public class StringPredicateFilterOptimizer implements StatementOptimizer {
+  private static final String MINUS_OPERATOR_NAME = "MINUS";
+  private static final String COMPARE_OPERATOR_NAME = "COMPARE";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();

Review comment:
       Let's check if the schema is null here before calling 
`optimizeExpression` and remove the null checks there




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