wuchong commented on a change in pull request #12303:
URL: https://github.com/apache/flink/pull/12303#discussion_r435244412



##########
File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/operators/rank/AppendOnlyTopNFunction.java
##########
@@ -210,14 +210,19 @@ private void processElementWithoutRowNumber(RowData 
input, Collector<RowData> ou
                        RowData lastKey = lastEntry.getKey();
                        List<RowData> lastList = (List<RowData>) 
lastEntry.getValue();
                        // remove last one
-                       RowData lastElement = lastList.remove(lastList.size() - 
1);
-                       if (lastList.isEmpty()) {
+                       int size = lastList.size();
+                       RowData lastElement = null;
+                       if (size > 0) {
+                               lastElement = lastList.get(size - 1);

Review comment:
       Remove on the list use index will be more efficient than `buffer.remove`.

##########
File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/operators/rank/AppendOnlyTopNFunction.java
##########
@@ -210,14 +210,19 @@ private void processElementWithoutRowNumber(RowData 
input, Collector<RowData> ou
                        RowData lastKey = lastEntry.getKey();
                        List<RowData> lastList = (List<RowData>) 
lastEntry.getValue();
                        // remove last one
-                       RowData lastElement = lastList.remove(lastList.size() - 
1);
-                       if (lastList.isEmpty()) {
+                       int size = lastList.size();
+                       RowData lastElement = null;
+                       if (size > 0) {
+                               lastElement = lastList.get(size - 1);
+                       }
+                       if (size <= 1) {
                                buffer.removeAll(lastKey);
                                dataState.remove(lastKey);
                        } else {
+                               buffer.remove(lastKey, lastElement);
                                dataState.put(lastKey, lastList);

Review comment:
       ```suggestion
                                dataState.put(lastKey, new 
ArrayList<>(lastList));
   ```
   
   We should do a shallow copy for the `lastList`, see FLINK-17918.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to