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


##########
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:
   The new call will be validated post the rewrite. But there was still the 
issue of validator treating rewritten query as originating from the user and 
flagging unrelatable `col __time not found in any table (row x, col y)` error. 
I've now addressed it by introducing a custom SQLIdentifier class.



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