vvivekiyer commented on code in PR #8518:
URL: https://github.com/apache/pinot/pull/8518#discussion_r863226578


##########
pinot-common/src/main/java/org/apache/pinot/sql/parsers/rewriter/PredicateComparisonRewriter.java:
##########
@@ -33,75 +36,126 @@ public class PredicateComparisonRewriter implements 
QueryRewriter {
   public PinotQuery rewrite(PinotQuery pinotQuery) {
     Expression filterExpression = pinotQuery.getFilterExpression();
     if (filterExpression != null) {
-      
pinotQuery.setFilterExpression(updateComparisonPredicate(filterExpression));
+      pinotQuery.setFilterExpression(updatePredicate(filterExpression, null));
     }
     Expression havingExpression = pinotQuery.getHavingExpression();
     if (havingExpression != null) {
-      
pinotQuery.setHavingExpression(updateComparisonPredicate(havingExpression));
+      pinotQuery.setHavingExpression(updatePredicate(havingExpression, null));
     }
     return pinotQuery;
   }
 
-  // This method converts a predicate expression to the what Pinot could 
evaluate.
-  // For comparison expression, left operand could be any expression, but 
right operand only
-  // supports literal.
-  // E.g. 'WHERE a > b' will be updated to 'WHERE a - b > 0'
-  private static Expression updateComparisonPredicate(Expression expression) {
-    Function function = expression.getFunctionCall();
-    if (function != null) {
-      FilterKind filterKind;
-      try {
-        filterKind = FilterKind.valueOf(function.getOperator());
-      } catch (Exception e) {
-        throw new SqlCompilationException("Unsupported filter kind: " + 
function.getOperator());
-      }
-      List<Expression> operands = function.getOperands();
-      switch (filterKind) {
-        case AND:
-        case OR:
-        case NOT:
-          
operands.replaceAll(PredicateComparisonRewriter::updateComparisonPredicate);
-          break;
-        case EQUALS:
-        case NOT_EQUALS:
-        case GREATER_THAN:
-        case GREATER_THAN_OR_EQUAL:
-        case LESS_THAN:
-        case LESS_THAN_OR_EQUAL:
-          Expression firstOperand = operands.get(0);
-          Expression secondOperand = operands.get(1);
+  /**
+   * This method converts a predicate expression to the what Pinot could 
evaluate.
+   * 1. For comparison expression, left operand could be any expression, but 
right operand only
+   *    supports literal. E.g. 'WHERE a > b' will be converted to 'WHERE a - b 
> 0'
+   * 2. Updates boolean predicates (literals and scalar functions) that are 
missing an EQUALS filter.
+   *    E.g. 1:  'WHERE a' will be updated to 'WHERE a = true'
+   *    E.g. 2: "WHERE startsWith(col, 'str')" will be updated to "WHERE 
startsWith(col, 'str') = true"
+   *
+   * @param expression current expression in the expression tree
+   * @param parentFunction parentFunction parent expression
+   * @return re-written expression.
+   */
+  private static Expression updatePredicate(Expression expression, Function 
parentFunction) {
+    ExpressionType type = expression.getType();
+    if (type == ExpressionType.FUNCTION) {
+      Function function = expression.getFunctionCall();
+      String functionOperator = function.getOperator();
 
-          // Handle predicate like '10 = a' -> 'a = 10'
-          if (firstOperand.isSetLiteral()) {
-            if (!secondOperand.isSetLiteral()) {
-              function.setOperator(getOppositeOperator(filterKind).name());
-              operands.set(0, secondOperand);
-              operands.set(1, firstOperand);
+      if (!EnumUtils.isValidEnum(FilterKind.class, functionOperator)) {
+        // If the function is not of FilterKind, we might have to rewrite the 
function if parentFunction is not a
+        // predicate.
+        // Example: A query like "select col1 from table where 
startsWith(col1, 'myStr') AND col2 > 10;" should be
+        //          rewritten to "select col1 from table where 
startsWith(col1, 'myStr') = true AND col2 > 10;".
+        expression = convertPredicateToBooleanExpression(expression, 
parentFunction);

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