You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by xi...@apache.org on 2020/02/14 09:48:06 UTC

[incubator-pinot] branch handle_predicate_right_side_expression created (now 0852fc8)

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

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


      at 0852fc8  Support non-literal expression in right side of comparison operators

This branch includes the following new commits:

     new e406b59  Update predicate expression
     new 0852fc8  Support non-literal expression in right side of comparison operators

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



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


[incubator-pinot] 02/02: Support non-literal expression in right side of comparison operators

Posted by xi...@apache.org.
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 <fx...@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-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[incubator-pinot] 01/02: Update predicate expression

Posted by xi...@apache.org.
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 e406b59b360f4ad7143d9e2c34d084f6f33bcc7b
Author: Xiang Fu <fx...@gmail.com>
AuthorDate: Fri Feb 14 01:13:43 2020 -0800

    Update predicate expression
---
 .../pinot/common/utils/request/RequestUtils.java   | 29 ++++++++++++-
 .../apache/pinot/sql/parsers/CalciteSqlParser.java | 50 +++++++++++++++++++++-
 2 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/pinot-common/src/main/java/org/apache/pinot/common/utils/request/RequestUtils.java b/pinot-common/src/main/java/org/apache/pinot/common/utils/request/RequestUtils.java
index a82eb6a..d1e3eda 100644
--- a/pinot-common/src/main/java/org/apache/pinot/common/utils/request/RequestUtils.java
+++ b/pinot-common/src/main/java/org/apache/pinot/common/utils/request/RequestUtils.java
@@ -126,14 +126,39 @@ public class RequestUtils {
     return expression;
   }
 
-  public static Expression getLiteralExpression(String value) {
+  public static Expression createNewLiteralExpression() {
     Expression expression = new Expression(ExpressionType.LITERAL);
     Literal literal = new Literal();
-    literal.setStringValue(value);
     expression.setLiteral(literal);
     return expression;
   }
 
+  public static Expression getLiteralExpression(String value) {
+    Expression expression = createNewLiteralExpression();
+    expression.getLiteral().setStringValue(value);
+    return expression;
+  }
+
+  public static Expression getLiteralExpression(Integer value) {
+    return getLiteralExpression(value.longValue());
+  }
+
+  public static Expression getLiteralExpression(Long value) {
+    Expression expression = createNewLiteralExpression();
+    expression.getLiteral().setLongValue(value);
+    return expression;
+  }
+
+  public static Expression getLiteralExpression(Float value) {
+    return getLiteralExpression(value.doubleValue());
+  }
+
+  public static Expression getLiteralExpression(Double value) {
+    Expression expression = createNewLiteralExpression();
+    expression.getLiteral().setDoubleValue(value);
+    return expression;
+  }
+
   public static Expression getFunctionExpression(String operator) {
     Expression expression = new Expression(ExpressionType.FUNCTION);
     Function function = new Function(operator);
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 5a85b38..be32173 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
@@ -273,7 +273,7 @@ public class CalciteSqlParser {
         }
 
         if (selectSqlNode.getWhere() != null) {
-          pinotQuery.setFilterExpression(toExpression(selectSqlNode.getWhere()));
+          pinotQuery.setFilterExpression(rewritePredicate(toExpression(selectSqlNode.getWhere())));
         }
         if (selectSqlNode.getGroup() != null) {
           pinotQuery.setGroupByList(convertSelectList(selectSqlNode.getGroup()));
@@ -289,6 +289,54 @@ public class CalciteSqlParser {
     return pinotQuery;
   }
 
+  private static Expression rewritePredicate(Expression expression) {
+    Function functionCall = expression.getFunctionCall();
+    if (functionCall != null) {
+      switch (SqlKind.valueOf(functionCall.getOperator().toUpperCase())) {
+        case EQUALS:
+        case NOT_EQUALS:
+        case GREATER_THAN:
+        case GREATER_THAN_OR_EQUAL:
+        case LESS_THAN:
+        case LESS_THAN_OR_EQUAL:
+          Expression comparisonFunction = RequestUtils.getFunctionExpression(functionCall.getOperator());
+          List<Expression> exprList = new ArrayList<>();
+          exprList.add(getLeftOperand(functionCall));
+          exprList.add(RequestUtils.getLiteralExpression(0));
+          comparisonFunction.getFunctionCall().setOperands(exprList);
+          return comparisonFunction;
+        default:
+          List<Expression> operands = functionCall.getOperands();
+          List<Expression> newOperands = new ArrayList<>();
+          for (int i = 0; i < operands.size(); i++) {
+            newOperands.add(rewritePredicate(operands.get(i)));
+          }
+          functionCall.setOperands(newOperands);
+      }
+    }
+    return expression;
+  }
+
+  private static Expression getLeftOperand(Function functionCall) {
+    Expression minusFunction = RequestUtils.getFunctionExpression(SqlKind.MINUS.toString());
+    List<Expression> updatedOperands = new ArrayList<>();
+    for (Expression operand : functionCall.getOperands()) {
+      updatedOperands.add(rewritePredicate(operand));
+    }
+    minusFunction.getFunctionCall().setOperands(updatedOperands);
+    return minusFunction;
+  }
+
+  private static Expression getRightOperand(Function functionCall) {
+    Expression minusFunction = RequestUtils.getFunctionExpression(SqlKind.MINUS.toString());
+    List<Expression> updatedOperands = new ArrayList<>();
+    for (Expression operand : functionCall.getOperands()) {
+      updatedOperands.add(rewritePredicate(operand));
+    }
+    minusFunction.getFunctionCall().setOperands(updatedOperands);
+    return minusFunction;
+  }
+
   private static void applyAlias(Map<Identifier, Expression> aliasMap, PinotQuery pinotQuery) {
     if (pinotQuery.isSetFilterExpression()) {
       applyAlias(aliasMap, pinotQuery.getFilterExpression());


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