rohangarg commented on code in PR #15095:
URL: https://github.com/apache/druid/pull/15095#discussion_r1347256583


##########
sql/src/main/java/org/apache/druid/sql/calcite/aggregation/builtin/EarliestLatestAnySqlAggregator.java:
##########
@@ -340,5 +345,47 @@ private static class EarliestLatestSqlAggFunction extends 
SqlAggFunction
           Optionality.FORBIDDEN
       );
     }
+
+    @Override
+    public SqlNode rewriteCall(
+        SqlValidator validator,
+        SqlCall call
+    )
+    {
+      // Rewrite EARLIEST to EARLIEST_BY and LATEST to LATEST_BY to make
+      // reference to __time column explicit so that Calcite tracks it
+
+      List<SqlNode> operands = call.getOperandList();
+
+      SqlParserPos pos = call.getParserPosition();
+
+      SqlAggFunction aggFunction;
+
+      switch (getName()) {
+        case "EARLIEST":
+          aggFunction = 
EarliestLatestBySqlAggregator.EARLIEST_BY.calciteFunction();
+          break;
+        case "LATEST":
+          aggFunction = 
EarliestLatestBySqlAggregator.LATEST_BY.calciteFunction();
+          break;
+        default:
+          return call;
+      }
+
+      switch (operands.size()) {
+        case 1:
+          return aggFunction.createCall(pos, operands.get(0), new 
SqlIdentifier(
+              "__time", pos));
+        case 2:
+          return aggFunction.createCall(pos, operands.get(0), new 
SqlIdentifier(

Review Comment:
   I wonder if we should validate the new call created as well - since we're 
hard coding the `__time` now in the function call. It may or may not be present 
in the aggregation scope - which is actually a bad thing that the 
LATEST/EARLIEST invocations have.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to