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:07 UTC
[incubator-pinot] 01/02: Update predicate expression
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