Fix last_by returning null row after deleting all table data (#16985)
Description
Fixes #16985
In table model, after deleting all data of a device, last_by queries still returned one row with null values instead of returning no rows:
-- After deleting all data:
select last_by(s0, time) from table where deviceId = 'd0'
-- Expected: 0 rows
-- Actual: 1 row with null
Root Cause
Three related issues:
-
LastQueryAggTableScanOperatorcould still emit a result row for non-groupedlast_byscans even when everyLastByAccumulatorhad no initialized result, causingevaluateFinal()to produce a null row. -
FileLoaderUtilsread aligned time-column modifications using the concrete measurement ID instead ofAlignedFullPath.VECTOR_PLACEHOLDER, inconsistent with aligned deletion semantics used in other paths. -
ModificationUtilsdid not setmodified = truewhen an aligned value chunk was fully deleted, allowing statistics-based shortcuts to use stale metadata.
Changes
| File | Change |
|---|---|
LastQueryAggTableScanOperator.java |
Skip emitting result row when all aggregators are LastByAccumulator and none has initialized result. Applied to both normal aggregation and cache-hit paths. |
FileLoaderUtils.java |
Use AlignedFullPath.VECTOR_PLACEHOLDER for time-column modification lookups |
ModificationUtils.java |
Set modified = true when value chunk is fully removed |
IoTDBDeletionTableIT.java |
Add regression tests for 2-arg and 3-arg last_by after delete |
Semantic Note
This changes non-grouped last_by on empty results from returning one null row to returning zero rows, consistent with grouped-query behavior (which already returns zero rows for empty devices). Other aggregations like count/max/last are not affected by this change. Please confirm this is the intended semantics.
Verification
Integration tests pass locally:
mvn -P with-integration-tests,TableSimpleIT -pl integration-test -am \
-DskipUTs -Dspotless.skip=true -Dcheckstyle.skip=true -Drat.skip=true \
-Dit.test=IoTDBDeletionTableIT#testLastByWithTimeShouldReturnNoRowsAfterDeletingAllData+testThreeArgLastByShouldReturnNoRowsAfterDeletingAllData \
verify
Result: 2 tests run, 0 failures, 0 errors.
Rebased onto the latest master. The only conflict was in LastQueryAggTableScanOperator, where the table last-row optimization was refactored since this PR was opened (the accumulator classes moved to the calc-commons module and the EMPTY_PRIMITIVE_TYPE placeholder was renamed to PLACEHOLDER_NO_VALUE); I reconciled the fix against those changes, and the last_by empty-result handling (shouldSkipEmptyLastByResult) is preserved. The two regression tests in IoTDBDeletionTableIT are unchanged.
@Caideyipi would you have a chance to take a look? You touched this operator most recently (#17677), so your eyes here would be very helpful. Thanks!
Hello, the correct performance of target SQL I have commented in https://github.com/apache/iotdb/issues/16985#issuecomment-5018814871. You can try to re-fix this issue.
Thanks @Wei-hao-Li — you're right, and I looked into this more carefully.
For the deleted-data case in this issue, master already returns the correct single NULL row: after a device's data is deleted the device still exists, so last_by over it yields one row with a NULL value. My change in this PR added a guard that suppressed that row — the wrong direction — so I'm closing this PR.
While tracing it I did notice a separate case that may be the empty result you're pointing at: querying a device that was never written (where deviceId = 'dX' with no such device) appears to return 0 rows rather than a single NULL row. If a no-GROUP-BY aggregate over a non-existent device should also return one NULL row per the "always return NULL when there's no input data" rule, I'd be glad to take that on as a separate, properly-scoped fix — could you confirm that's the intended behavior?
Thanks again for the correction.
Thanks @Wei-hao-Li — you're right, and I looked into this more carefully.
For the deleted-data case in this issue, master already returns the correct single NULL row: after a device's data is deleted the device still exists, so
last_byover it yields one row with a NULL value. My change in this PR added a guard that suppressed that row — the wrong direction — so I'm closing this PR.While tracing it I did notice a separate case that may be the empty result you're pointing at: querying a device that was never written (
where deviceId = 'dX'with no such device) appears to return 0 rows rather than a single NULL row. If a no-GROUP-BY aggregate over a non-existent device should also return one NULL row per the "always return NULL when there's no input data" rule, I'd be glad to take that on as a separate, properly-scoped fix — could you confirm that's the intended behavior?Thanks again for the correction.
Yes, we should keep performance consistent with other query engine which meets SQL standard, like Trino.