amrishlal commented on a change in pull request #7394:
URL: https://github.com/apache/pinot/pull/7394#discussion_r706501866



##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();
+    if (filter != null && schema != null) {
+      optimizeExpression(filter, schema);
+    }
+
+    Expression expression = query.getHavingExpression();
+    if (expression != null && schema != null) {
+      optimizeExpression(expression, schema);
+    }
+  }
+
+  /** Traverse an expression tree to replace MINUS function with COMPARE if 
function operands are STRING. */

Review comment:
       Fixed.

##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();
+    if (filter != null && schema != null) {
+      optimizeExpression(filter, schema);
+    }
+
+    Expression expression = query.getHavingExpression();
+    if (expression != null && schema != null) {
+      optimizeExpression(expression, schema);
+    }
+  }
+
+  /** Traverse an expression tree to replace MINUS function with COMPARE if 
function operands are STRING. */
+  private static void optimizeExpression(Expression expression, Schema schema) 
{
+    ExpressionType type = expression.getType();
+    if (type != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    FilterKind kind = FilterKind.valueOf(operator);
+    switch (kind) {
+      case AND:
+      case OR: {
+        optimizeExpression(operands.get(0), schema);

Review comment:
       Fixed.

##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();
+    if (filter != null && schema != null) {
+      optimizeExpression(filter, schema);
+    }
+
+    Expression expression = query.getHavingExpression();
+    if (expression != null && schema != null) {
+      optimizeExpression(expression, schema);
+    }
+  }
+
+  /** Traverse an expression tree to replace MINUS function with COMPARE if 
function operands are STRING. */
+  private static void optimizeExpression(Expression expression, Schema schema) 
{
+    ExpressionType type = expression.getType();
+    if (type != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    FilterKind kind = FilterKind.valueOf(operator);
+    switch (kind) {
+      case AND:
+      case OR: {
+        optimizeExpression(operands.get(0), schema);
+        break;
+      }
+      case EQUALS:
+      case NOT_EQUALS:
+      case GREATER_THAN:
+      case GREATER_THAN_OR_EQUAL:
+      case LESS_THAN:
+      case LESS_THAN_OR_EQUAL:
+      case IS_NULL:
+      case IS_NOT_NULL: {
+        replaceMinusWithCompareForStrings(operands.get(0), schema);

Review comment:
       Moved to "default. Let's leave it as "switch" though as switch tends to 
perform better with a set of options as compared to if and probably easier to 
modify in future as well.

##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";

Review comment:
       Fixed.

##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();
+    if (filter != null && schema != null) {
+      optimizeExpression(filter, schema);
+    }
+
+    Expression expression = query.getHavingExpression();
+    if (expression != null && schema != null) {
+      optimizeExpression(expression, schema);
+    }
+  }
+
+  /** Traverse an expression tree to replace MINUS function with COMPARE if 
function operands are STRING. */
+  private static void optimizeExpression(Expression expression, Schema schema) 
{
+    ExpressionType type = expression.getType();
+    if (type != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    FilterKind kind = FilterKind.valueOf(operator);
+    switch (kind) {
+      case AND:
+      case OR: {
+        optimizeExpression(operands.get(0), schema);
+        break;
+      }
+      case EQUALS:
+      case NOT_EQUALS:
+      case GREATER_THAN:
+      case GREATER_THAN_OR_EQUAL:
+      case LESS_THAN:
+      case LESS_THAN_OR_EQUAL:
+      case IS_NULL:
+      case IS_NOT_NULL: {
+        replaceMinusWithCompareForStrings(operands.get(0), schema);
+        break;
+      }
+      default:
+        break;
+    }
+  }
+
+  /** Replace the operator of a MINUS function with COMPARE if both operands 
are STRING. */
+  private static void replaceMinusWithCompareForStrings(Expression expression, 
Schema schema) {
+    if (expression.getType() != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    if (operator.equals(MINUS_OPERATOR_NAME) && operands.size() == 2 && 
isStringColumn(operands.get(0), schema)
+        && isStringColumn(operands.get(1), schema)) {
+      function.setOperator(COMPARE_OPERATOR_NAME);
+    }
+  }
+
+  /** @return true if expression is a column of numeric type. */

Review comment:
       Fixed.

##########
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
+ * "STRCMP(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 STRCMP(strColumn1, strColumn2). 
At 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 = "STRCMP";
+
+  @Override
+  public void optimize(PinotQuery query, @Nullable TableConfig tableConfig, 
@Nullable Schema schema) {
+    Expression filter = query.getFilterExpression();
+    if (filter != null && schema != null) {
+      optimizeExpression(filter, schema);
+    }
+
+    Expression expression = query.getHavingExpression();
+    if (expression != null && schema != null) {
+      optimizeExpression(expression, schema);
+    }
+  }
+
+  /** Traverse an expression tree to replace MINUS function with COMPARE if 
function operands are STRING. */
+  private static void optimizeExpression(Expression expression, Schema schema) 
{
+    ExpressionType type = expression.getType();
+    if (type != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    FilterKind kind = FilterKind.valueOf(operator);
+    switch (kind) {
+      case AND:
+      case OR: {
+        optimizeExpression(operands.get(0), schema);
+        break;
+      }
+      case EQUALS:
+      case NOT_EQUALS:
+      case GREATER_THAN:
+      case GREATER_THAN_OR_EQUAL:
+      case LESS_THAN:
+      case LESS_THAN_OR_EQUAL:
+      case IS_NULL:
+      case IS_NOT_NULL: {
+        replaceMinusWithCompareForStrings(operands.get(0), schema);
+        break;
+      }
+      default:
+        break;
+    }
+  }
+
+  /** Replace the operator of a MINUS function with COMPARE if both operands 
are STRING. */
+  private static void replaceMinusWithCompareForStrings(Expression expression, 
Schema schema) {
+    if (expression.getType() != ExpressionType.FUNCTION) {
+      // We have nothing to rewrite if expression is not a function.
+      return;
+    }
+
+    Function function = expression.getFunctionCall();
+    String operator = function.getOperator();
+    List<Expression> operands = function.getOperands();
+    if (operator.equals(MINUS_OPERATOR_NAME) && operands.size() == 2 && 
isStringColumn(operands.get(0), schema)
+        && isStringColumn(operands.get(1), schema)) {
+      function.setOperator(COMPARE_OPERATOR_NAME);
+    }
+  }
+
+  /** @return true if expression is a column of numeric type. */
+  private static boolean isStringColumn(Expression expression, Schema schema) {
+    if (expression.getType() != ExpressionType.IDENTIFIER) {
+      // Expression can not be a column.

Review comment:
       Done




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