fx19880617 commented on a change in pull request #5406: URL: https://github.com/apache/incubator-pinot/pull/5406#discussion_r426428899
########## 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); Review comment: better to implement method `eval` for each function which take operands list as argument which could be either literal or functions. Then this logic will just be ```funcExpr = getScalarFunction(scalarFunctionType).eval(funcExpr.getFunctionCall().getOperands())``` ########## 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: this should be recursively evaluated, technically a function of function of literal should still be true here. E.g. `format_time(now(), 'yyyy-MM-dd')` ---------------------------------------------------------------- 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: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org