Yaoxuan Wu created FLINK-41759:
----------------------------------

             Summary: Streaming Top-N with an offset over an updating input 
loses retractions: the emitted row kind is stored with the rows kept in state
                 Key: FLINK-41759
                 URL: https://issues.apache.org/jira/browse/FLINK-41759
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Runtime
    Affects Versions: 2.3.0
            Reporter: Yaoxuan Wu


A streaming Top-N with an offset over an updating input produces a wrong 
changelog: the retraction of an updated row is lost, and later the operator 
retracts a row it never emitted. The final result keeps stale rows. No error is 
raised.

{code:sql}
-- streaming mode, parallelism 1
SELECT k, c FROM (
  SELECT k, COUNT(*) AS c FROM (VALUES (7), (-2), (2), (-9), (2)) AS v(k) GROUP 
BY k
) ORDER BY k LIMIT 2 OFFSET 1;
{code}

Changelog:
{noformat}
+I(7,1) -U(7,1) +U(2,1) +I(7,1) -U(2,1) +U(-2,1) -U(7,1) +U(2,1) -U(7,1) +U(2,2)
{noformat}

Folded: (-2,1), (2,1), (2,2), and (7,1) with count -1. Expected: (-2,1), (2,2). 
When group 2 changes from (2,1) to (2,2), nothing is emitted for -U(2,1) 
(expected -U(2,1) +U(7,1)). Then (2,2) is inserted as if (7,1) were still in 
the result.

The same happens with {{ROW_NUMBER() OVER (ORDER BY k) AS rn ... WHERE rn 
BETWEEN 2 AND 3}} when rn is not selected. The result is correct when rn is 
selected, and without an offset ({{LIMIT 3}}). Both the hashmap and the RocksDB 
state backend are affected.

Reproduced on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT).

Cause: with a rank start > 1, RetractableTopNFunction uses the with-row-number 
algorithm also when the row number is not output. Its collect helpers call 
AbstractTopNFunction#createOutputRow, which without a row number calls 
{{inputRow.setRowKind(kind)}} on the emitted row itself. Those rows are the 
ones kept in state:
* emitRecordsWithRowNumber emits the new input row as UPDATE_AFTER, and 
processElement then stores the same object in dataState, now with row kind 
UPDATE_AFTER (any backend);
* on the heap backend, rows read from dataState and emitted as -U/+U are also 
changed in place.

On retraction, processElement erases the input's row kind to INSERT and looks 
the row up with the RecordEqualiser, which compares row kinds. The stored row 
does not match, so retractRecordWithRowNumber emits nothing and the row stays 
in dataState although treeMap no longer counts it. When the row number is 
output, createOutputRow sets the kind on the JoinedRowData wrapper instead, so 
that case is correct.

FLINK-27418 fixed the same equaliser problem for the input row by erasing its 
row kind before the lookup, but rows already stored in state can still carry a 
changed kind. A possible fix is to never change the row kind of a row that is 
or will be kept in state: emit a copy or a reused wrapper, as the row-number 
case does, or reset the kind before storing the row.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to