ColinLeeo opened a new issue, #18863:
URL: https://github.com/apache/iotdb/issues/18863

   ### Description
   
   CASE can clear a shared stateful DIFF result during evaluation. 
Materializing the same expression for projection then evaluates the same input 
block again, advancing DIFF state twice and returning incorrect values. PR 
#18826 fixes stale caches across blocks and several sibling-column problems, 
but does not fix this within-block state transition.
   
   ### Reproduction
   
   ```sql
   CREATE TABLE dt2 (dev STRING TAG, x DOUBLE FIELD);
   INSERT INTO dt2(time, dev, x) VALUES (1, 'd0', 1.0), (2, 'd0', 3.0), (3, 
'd0', 6.0);
   FLUSH;
   INSERT INTO dt2(time, dev, x) VALUES (4, 'd0', 10.0), (5, 'd0', 11.0);
   FLUSH;
   SELECT time, x, diff(x) AS d FROM dt2
   WHERE CASE WHEN diff(x) > 1.0 THEN 1.0 ELSE 0.0 END > 0.0;
   ```
   
   Expected `d`: 2.0, 3.0, 4.0 at times 2, 3, 4. The review reported a 
column-size error on merge base `fbd5fd22a6`, while the cross-block fix changes 
that case to a silent wrong value of -1.0 at time 4. See [the SQL review 
reproduction](https://github.com/apache/iotdb/pull/18826#discussion_r4258223689).
   
   A deterministic actual-operator reproduction has been confirmed on 
`d7239384fd`: feed `[1, 3, 6]` and `[10, 11]` as two blocks, share one 
`DiffFunctionColumnTransformer` between the CASE filter's first WHEN and the 
common-expression projection, and assert `[2, 3]` then `[4]`. The second 
assertion fails with `expected:<4.0> but was:<-1.0>`.
   
   ### Mechanism
   
   `DiffFunctionColumnTransformer.evaluateWithSelection()` evaluates every row 
because DIFF needs row continuity. The first evaluation of `[10, 11]` advances 
`lastValue` to 11. CASE cleanup invalidates that column. Re-evaluation begins 
with `10 - 11`, yielding -1 instead of 4. A filter mask cannot roll back 
internal state, and resetting column caches at block boundaries cannot repair 
repeated evaluation within a block.
   
   Selection-mode results currently enter the same ref-counted `ColumnCache` as 
full results, forcing evaluation-time invalidation and recomputation. Follow-up 
design work should separate selection-local results (for example through direct 
returned columns or a selection-local map) from reusable full columns, and 
explicitly ensure stateful expressions advance once per input block. Preserve 
CASE short-circuit guards and multi-consumer sharing while doing so.
   
   ### Acceptance criteria
   
   - The two-block reproduction returns `[2, 3]` then `[4]`.
   - An always-passing ELSE variant preserves the initial NULL and does not 
rewind DIFF state.
   - Tests cover shared consumers, consecutive blocks, and selection-mode 
evaluation.
   - A wrong numerical result is never treated as the expected behavior of a 
passing regression test.
   


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