You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2020/05/18 07:51:29 UTC

[GitHub] [incubator-pinot] kishoreg commented on a change in pull request #5406: Adding support to execute functions during query compilation

kishoreg commented on a change in pull request #5406:
URL: https://github.com/apache/incubator-pinot/pull/5406#discussion_r426431490



##########
File path: pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -615,7 +618,47 @@ private static Expression toExpression(SqlNode node) {
             funcExpr.getFunctionCall().addToOperands(toExpression(child));
           }
         }
+
+        if (FunctionDefinitionRegistry.isScalarFunc(funcName) && isCompileTimeEvaluationPossible(funcExpr)) {
+
+          ScalarFunctionType scalarFunctionType = ScalarFunctionType.getScalarFunctionType(funcName);
+          switch (scalarFunctionType) {
+            case NOW:
+              funcExpr = RequestUtils.getLiteralExpression(System.currentTimeMillis());
+              break;
+            case FORMAT_DATETIME:
+              //DATETIME_FORMAT ('2020-01-01', 'yyyy-MM-dd')
+              String input = funcExpr.getFunctionCall().getOperands().get(0).getLiteral().getStringValue();
+              String format = funcExpr.getFunctionCall().getOperands().get(1).getLiteral().getStringValue();
+              long output = DateTimeFormat.forPattern(format).parseMillis(input);
+              funcExpr = RequestUtils.getLiteralExpression(output);
+              break;
+            default:
+              //no change, let the expression be handled during execution phase
+          }
+        }
+
         return funcExpr;
     }
   }
+
+  /**
+   * Utility method to check if the function can be evaluated during the query compilation phae
+   * @param funcExpr
+   * @return true if all arguments are literals
+   */
+  private static boolean isCompileTimeEvaluationPossible(Expression funcExpr) {
+
+    boolean compileTimeEvaluationPossible = true;
+    Function functionCall = funcExpr.getFunctionCall();
+    if(functionCall.getOperandsSize() > 0) {
+      for (Expression expression : functionCall.getOperands()) {
+        if (expression.getType() != ExpressionType.LITERAL) {

Review comment:
       since logic is depth-first, it should work.
   `format_time(now(), 'yyyy-MM-dd')` will first get changed to 
   `format_time(123123123123, 'yyyy-MM-dd')` which will be evaluated to
   `2020-01-01`
   




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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