PDGGK opened a new pull request, #18267:
URL: https://github.com/apache/iotdb/pull/18267

   ## Description
   
   Per @Wei-hao-Li's clarification in #16985: an aggregate function should 
always return NULL when there is no input data (except `count`), consistent 
with SQL-standard engines such as Trino. So `select last_by(s0, time) from t 
where deviceId = 'never'` — where the device was never written — must return 
exactly one row whose value is NULL, not zero rows.
   
   Root cause: for the last-cache-optimized `last` / `last_by` path, a device 
set that resolves to zero devices leaves `LastQueryAggTableScanOperator` with 
`allDeviceCount == 0`, so the device loop never runs and the operator returns 
no rows.
   
   ## How
   
   `LastQueryAggTableScanOperator` now emits one all-NULL row when there are 
zero devices, no GROUP BY, and the aggregation is at its final/complete stage 
(each aggregator's no-input `evaluate()` writes NULL for `last` / `last_by`). 
Guards:
   
   - GROUP BY over an empty group still returns no rows (the one-NULL-row rule 
is for no-GROUP-BY global aggregation only).
   - A partial stage emits nothing (only the final stage produces the row).
   - The deleted-device case is untouched (the device still exists, so the 
operator already iterates and emits the NULL row).
   
   ## Tests
   
   `IoTDBDeletionTableIT`: a never-written device queried with 2-arg and 3-arg 
`last_by` returns one NULL row; multiple `last_by` aggregates return one 
all-NULL row; GROUP BY over a never-written device returns no rows; and a 
regression check that a deleted device still returns one NULL row (both the 
last-value-cache and recomputed paths).
   
   ## Scope
   
   Scoped to `last` / `last_by`. The general aggregation operator (`max` / 
`min` / `sum` / `avg`) likely returns empty for a never-written device as well; 
that is left as a follow-up rather than expanding this PR.
   
   Relates to #16985.
   


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

Reply via email to