HappenLee commented on code in PR #69456:
URL: https://github.com/apache/doris/pull/69456#discussion_r4347997245


##########
be/src/exprs/function/function_topk_merge.cpp:
##########
@@ -0,0 +1,459 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <glog/logging.h>
+
+#include <cstddef>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "common/check.h"
+#include "common/compare.h"
+#include "common/exception.h"
+#include "common/status.h"
+#include "core/assert_cast.h"
+#include "core/block/block.h"
+#include "core/block/column_numbers.h"
+#include "core/block/column_with_type_and_name.h"
+#include "core/column/column.h"
+#include "core/column/column_array.h"
+#include "core/column/column_nullable.h"
+#include "core/column/column_struct.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type.h"
+#include "core/data_type/data_type_array.h"
+#include "core/data_type/data_type_nullable.h"
+#include "core/data_type/data_type_number.h"
+#include "core/data_type/data_type_struct.h"
+#include "core/data_type/primitive_type.h"
+#include "core/types.h"
+#include "exprs/function/function.h"
+#include "exprs/function/simple_function_factory.h"
+
+namespace doris {
+class FunctionContext;
+} // namespace doris
+
+namespace doris {
+
+namespace {
+
+/// Upper bound of k. Keep in sync with the aggregate's MAX_TOPK_K
+/// (aggregate_function_topk_count.h) and FE TopKCountFunction.MAX_TOPK_K.
+constexpr int64_t MAX_TOPK_K = 65536;
+
+/// One row of an ARRAY<STRUCT<value, count>> argument. A NULL array is an 
empty view.
+struct TopKArrayView {
+    const IColumn* values = nullptr;
+    const ColumnInt64* counts = nullptr;
+    /// NULL bitmap of the array element (the pair struct).
+    const NullMap* null_map = nullptr;
+    /// NULL bitmap of the `value` field; NULL values are not part of the 
contract.
+    const NullMap* value_null_map = nullptr;
+    /// NULL bitmap of the `count` field; NULL counts are not part of the 
contract.
+    const NullMap* count_null_map = nullptr;
+    size_t begin = 0;
+    size_t end = 0;
+};
+
+TopKArrayView make_array_view(const IColumn& column, size_t row) {
+    TopKArrayView view;
+    const IColumn* array_column = &column;
+    if (const auto* nullable = check_and_get_column<ColumnNullable>(&column)) {
+        if (nullable->is_null_at(row)) {
+            return view;
+        }
+        array_column = &nullable->get_nested_column();
+    }
+    const auto& array = assert_cast<const ColumnArray&>(*array_column);
+    // offsets[i] is the cumulative size after row i, so row's range is 
[offsets[row-1], offsets[row]).
+    // offset_at(0) reads offsets[-1], which PaddedPODArray keeps as 0.
+    view.begin = array.offset_at(static_cast<ssize_t>(row));
+    view.end = array.offset_at(static_cast<ssize_t>(row) + 1);
+
+    const IColumn* pair_column = &array.get_data();
+    if (const auto* element_nullable = 
check_and_get_column<ColumnNullable>(pair_column)) {
+        view.null_map = &element_nullable->get_null_map_data();
+        pair_column = &element_nullable->get_nested_column();
+    }
+    const auto& pairs = assert_cast<const ColumnStruct&>(*pair_column);
+    view.values = &pairs.get_column(0);
+    // A cast may widen the `value` field to a nullable column; the entries 
themselves are never
+    // NULL, so only the nested column is used.
+    if (const auto* value_nullable = 
check_and_get_column<ColumnNullable>(view.values)) {
+        view.value_null_map = &value_nullable->get_null_map_data();
+        view.values = &value_nullable->get_nested_column();
+    }
+    // The same holds for `count`: a nested cast may have made the field 
nullable.
+    const auto* count_column = &pairs.get_column(1);
+    if (const auto* count_nullable = 
check_and_get_column<ColumnNullable>(count_column)) {
+        view.count_null_map = &count_nullable->get_null_map_data();
+        count_column = &count_nullable->get_nested_column();
+    }
+    view.counts = &assert_cast<const ColumnInt64&>(*count_column);
+    return view;
+}
+
+/// One scalar bound argument: NULL means the corresponding side was not 
truncated.
+struct TopKBoundView {
+    const IColumn* value = nullptr;
+    bool is_null = true;
+};
+
+TopKBoundView make_bound_view(const IColumn& column, size_t row) {
+    TopKBoundView view;
+    const IColumn* value_column = &column;
+    if (const auto* nullable = check_and_get_column<ColumnNullable>(&column)) {
+        if (nullable->is_null_at(row)) {
+            return view;
+        }
+        value_column = &nullable->get_nested_column();
+    }
+    view.value = value_column;

Review Comment:
   Adding a concrete counterexample for the original implementation at 
`ddda17f849`: this can produce a wrong nonempty candidate and suppress a 
required fallback.
   
   Consider two rows in the same execution block, with `k = 1`:
   
   | Row | Original group data | Stored S / b | Inserts I / bi | Deletes D / bd 
|
   | --- | --- | --- | --- | --- |
   | 0 | `{10,20}` | `[(10,1)] / 10` | `[] / NULL` | `[] / NULL` |
   | 1 | `{1,3}` | `[(1,1)] / 1` | `[(5,1)] / NULL` | `[(1,1)] / NULL` |
   
   For row 1, `n = 2`, `dn = 0`, and the actual post-update data is `{3,5}`, so 
MIN is **3**. Its old state has discarded 3, and only values <= 1 remain within 
the trusted range.
   
   **Expected:** use row 1's bound, `bE = 1`. The count of 1 becomes `1 + 0 - 1 
= 0`; 5 is outside the trusted range. Return `(values=[], bound=1)`. Since `n' 
= 2`, the empty candidate list tells the caller to fall back to a complete 
refresh, which finds 3.
   
   **Original behavior:** the lists are taken from row 1, but the bound is read 
from row 0, giving `bE = 10`. After removing 1, the merge incorrectly admits 5 
and returns `(values=[(5,1)], bound=10)`. The candidate list is nonempty, so 
the documented exhaustion check does not trigger fallback; using the first 
candidate as MIN would return **5**, although 3 still exists.
   
   I checked the latest head, `e4acad346d`: `TopKBoundView` now carries the row 
index, and effective-bound selection, range filtering, and bound output all use 
it. This fixes the identified indexing cause by source inspection. The added 
multirow regression uses empty insert/delete lists; please also cover this 
delete-plus-insert case and its MAX counterpart, with both rows in one 
execution block, to guard against suppressing a required fallback.
   
   This example is derived from the code; I have not executed it against Doris.
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to