tsreaper commented on a change in pull request #17605:
URL: https://github.com/apache/flink/pull/17605#discussion_r742497844



##########
File path: 
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/rank/UpdatableTopNFunction.java
##########
@@ -312,6 +357,37 @@ private void processElementWithRowNumber(RowData inputRow, 
Collector<RowData> ou
                 "Failed to find the sortKey, rowkey in the buffer. This should 
never happen");
     }
 
+    private void emitRecordsWithRowNumberIgnoreStateError(
+            RowData newRow, int newRank, RankRow oldRow, int oldRank, 
Collector<RowData> out) {
+        Iterator<Map.Entry<RowData, Collection<RowData>>> iterator = 
buffer.entrySet().iterator();
+        int currentRank = 0;
+        RowData currentRow = null;
+        RowData prevRow = null;
+
+        while (iterator.hasNext() && currentRank <= newRank) {
+            Map.Entry<RowData, Collection<RowData>> entry = iterator.next();
+            Collection<RowData> rowKeys = entry.getValue();
+            Iterator<RowData> rowKeyIter = rowKeys.iterator();
+            while (rowKeyIter.hasNext()) {
+                RowData rowKey = rowKeyIter.next();
+                currentRank += 1;
+                currentRow = rowKeyMap.get(rowKey).row;
+                if (oldRank <= currentRank) {
+                    if (currentRank == oldRank) {
+                        collectUpdateBefore(out, oldRow.row, oldRank);
+                    } else {
+                        collectUpdateBefore(out, prevRow, currentRank);
+                        collectUpdateAfter(out, prevRow, currentRank - 1);
+                        if (currentRank == newRank) {
+                            collectUpdateAfter(out, newRow, currentRank);
+                        }
+                    }
+                }
+                prevRow = currentRow;
+            }
+        }
+    }

Review comment:
       This piece of algorithm seems awkward to me. Consider modifying it to:
   ```java
   while (iterator.hasNext() && currentRank < newRank) {
       // ...
       while (rowKeyIter.hasNext()) {
           // ...
           if (oldRank <= currentRank) {
               collectUpdateBefore(out, currentRow, currentRank + 1);
               collectUpdateAfter(out, currentRow, currentRank);
           }
       }
   }
   collectUpdateBefore(out, oldRow.row, oldRank);
   collectUpdateAfter(out, newRow, newRank);
   ```
   so that there is no `prevRow` thingy. It is misleading to see a `prevRow` 
and a `currentRank` sending within the same message.




-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to