This is an automated email from the ASF dual-hosted git repository.

xiangfu pushed a commit to branch handle_predicate_right_side_expression
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git

commit 0852fc8fdcd0a9c05c326a15ac2f4b636e389483
Author: Xiang Fu <fx19880...@gmail.com>
AuthorDate: Fri Feb 14 01:47:44 2020 -0800

    Support non-literal expression in right side of comparison operators
---
 .../apache/pinot/sql/parsers/CalciteSqlParser.java | 11 +++++-
 .../pinot/sql/parsers/CalciteSqlCompilerTest.java  | 41 ++++++++++++++++++----
 2 files changed, 44 insertions(+), 8 deletions(-)

diff --git 
a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java 
b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
index be32173..522cad8 100644
--- 
a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
+++ 
b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
@@ -292,13 +292,22 @@ public class CalciteSqlParser {
   private static Expression rewritePredicate(Expression expression) {
     Function functionCall = expression.getFunctionCall();
     if (functionCall != null) {
-      switch (SqlKind.valueOf(functionCall.getOperator().toUpperCase())) {
+      SqlKind sqlKind = SqlKind.OTHER_FUNCTION;
+      try {
+        sqlKind = SqlKind.valueOf(functionCall.getOperator().toUpperCase());
+      } catch (Exception e) {
+        // Do nothing
+      }
+      switch (sqlKind) {
         case EQUALS:
         case NOT_EQUALS:
         case GREATER_THAN:
         case GREATER_THAN_OR_EQUAL:
         case LESS_THAN:
         case LESS_THAN_OR_EQUAL:
+          if (functionCall.getOperands().get(1).getLiteral() != null) {
+            return expression;
+          }
           Expression comparisonFunction = 
RequestUtils.getFunctionExpression(functionCall.getOperator());
           List<Expression> exprList = new ArrayList<>();
           exprList.add(getLeftOperand(functionCall));
diff --git 
a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java
 
b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java
index ffdefc4..c327d0f 100644
--- 
a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java
+++ 
b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java
@@ -60,14 +60,14 @@ public class CalciteSqlCompilerTest {
 
     pinotQuery =
         CalciteSqlParser.compileToPinotQuery("select * from vegetables where 
origin = \"Martha\"\"s Vineyard\"");
-    Assert
-        
.assertEquals(pinotQuery.getFilterExpression().getFunctionCall().getOperands().get(1).getIdentifier().getName(),
-            "Martha\"s Vineyard");
+    Assert.assertEquals(
+        
pinotQuery.getFilterExpression().getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(1)
+            .getIdentifier().getName(), "Martha\"s Vineyard");
 
     pinotQuery = CalciteSqlParser.compileToPinotQuery("select * from 
vegetables where origin = \"Martha''s Vineyard\"");
-    Assert
-        
.assertEquals(pinotQuery.getFilterExpression().getFunctionCall().getOperands().get(1).getIdentifier().getName(),
-            "Martha''s Vineyard");
+    Assert.assertEquals(
+        
pinotQuery.getFilterExpression().getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(1)
+            .getIdentifier().getName(), "Martha''s Vineyard");
   }
 
   @Test
@@ -119,6 +119,33 @@ public class CalciteSqlCompilerTest {
   }
 
   @Test
+  public void testFilterCalusesWithRightExpression() {
+    PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery("select * 
from vegetables where a > b");
+    Function func = pinotQuery.getFilterExpression().getFunctionCall();
+    Assert.assertEquals(func.getOperator(), SqlKind.GREATER_THAN.name());
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperator(), 
"MINUS");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(0).getIdentifier().getName(),
 "a");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(1).getIdentifier().getName(),
 "b");
+    Assert.assertEquals(func.getOperands().get(1).getLiteral().getLongValue(), 
0L);
+    pinotQuery = CalciteSqlParser.compileToPinotQuery("select * from 
vegetables where b < 100 + c");
+    func = pinotQuery.getFilterExpression().getFunctionCall();
+    Assert.assertEquals(func.getOperator(), SqlKind.LESS_THAN.name());
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperator(), 
"MINUS");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(0).getIdentifier().getName(),
 "b");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(1).getFunctionCall().getOperator(),
 "PLUS");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(1).getFunctionCall().getOperands().get(0).getLiteral().getLongValue(),
 100L);
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(1).getFunctionCall().getOperands().get(1).getIdentifier().getName(),
 "c");
+    Assert.assertEquals(func.getOperands().get(1).getLiteral().getLongValue(), 
0L);
+    pinotQuery = CalciteSqlParser.compileToPinotQuery("select * from 
vegetables where 10 >= c");
+    func = pinotQuery.getFilterExpression().getFunctionCall();
+    Assert.assertEquals(func.getOperator(), 
SqlKind.GREATER_THAN_OR_EQUAL.name());
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperator(), 
"MINUS");
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(0).getLiteral().getLongValue(),
 10L);
+    
Assert.assertEquals(func.getOperands().get(0).getFunctionCall().getOperands().get(1).getIdentifier().getName(),
 "c");
+    Assert.assertEquals(func.getOperands().get(1).getLiteral().getLongValue(), 
0L);
+  }
+
+  @Test
   public void testBrokerConverter() {
     PinotQuery pinotQuery =
         CalciteSqlParser.compileToPinotQuery("select * from vegetables where g 
IN (12, 13, 15.2, 17)");
@@ -1216,7 +1243,7 @@ public class CalciteSqlCompilerTest {
             .getFunctionCall().getOperands().get(0).getIdentifier().getName(), 
"time");
     
Assert.assertEquals(pinotQuery.getGroupByList().get(0).getIdentifier().getName(),
 "group");
   }
-  
+
   @Test
   public void testCastTransformation() {
     PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery("select 
CAST(25.65 AS int) from myTable");


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to