Doris-Breakwater commented on issue #71182:
URL: https://github.com/apache/doris/issues/71182#issuecomment-7297571656

   Thanks for the boundary-value repro. This is a bitmap result correctness 
bug. Doris accepts the full unsigned 64-bit range as bitmap members, but the 
numeric outputs expose those members as signed `BIGINT`s. That accounts for 
`2^63` turning into `-2^63` and `2^64 - 1` turning into `-1`. The sign change 
occurs before SQL sorts, filters, or sums the values. 
[BITMAP_FROM_STRING](https://doris.apache.org/docs/4.x/sql-manual/sql-functions/scalar-functions/bitmap-functions/bitmap-from-string/)
 · 
[TO_BITMAP](https://doris.apache.org/docs/4.x/sql-manual/sql-functions/scalar-functions/bitmap-functions/to-bitmap/)
   
   I checked the source at both reported revisions, `b4140d0b0538` and 
`917b20be838` (4.1.5). Both have the same result-type mismatch.
   
   - Nereids lowers `UNNEST(BITMAP)` to `explode_bitmap`. The BE iterator 
yields `uint64_t` members and the generator writes them to `ColumnInt64`. The 
outer variant shares the same generator. [UNNEST 
rewrite](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java#L1740-L1763)
 · [BE 
generator](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/exprs/table_function/vexplode_bitmap.cpp#L106-L132)
   - `bitmap_to_array` writes into an `ARRAY<BIGINT>`. `bitmap_min` and 
`bitmap_max` select extrema in unsigned order, then return signed `Int64`. This 
is why `{20, 30, 2^64 - 1}` can yield min `20` and max `-1`. A bitmap 
containing only upper-half values also exposes the issue in `bitmap_min`. 
`bitmap_to_string` formats the unsigned members directly. [Array 
output](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/exprs/function/function_bitmap.cpp#L1157-L1198)
 · [Extrema and string 
formatting](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/core/value/bitmap_value.h#L2559-L2638)
   
   The supplied versions and SQL are sufficient for triage. I verified the code 
paths and used the issue’s SQL output as the runtime evidence. If a maintainer 
sees a different result when running the repro, the exact output and FE/BE 
build IDs would help identify the difference.
   
   For a fix, consider returning `LARGEINT` and `ARRAY<LARGEINT>` so every 
stored member is representable. That requires coordinating the FE signatures 
with the BE output columns and reviewing compatibility for existing views and 
client result metadata. If the existing signatures must stay `BIGINT`, return 
an explicit out-of-range error when a member being returned exceeds 
`BIGINT_MAX`. Add regressions for `2^63 - 1`, `2^63`, and `2^64 - 1` with 
constant and stored bitmaps, including mixed and upper-half-only values, empty 
and NULL inputs, and the outer generator. The existing `UNNEST` boundary test 
stops at `2^63 - 1`. [UNNEST boundary 
test](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/regression-test/suites/nereids_function_p0/gen_function/unnest_bitmap_border_test.groovy#L98-L110)
   
   For exact text output today, use `bitmap_to_string`. For numeric queries on 
the affected builds, cast the emitted element to `LARGEINT` and add `2^64` only 
when it is negative, before applying filters, sorting, or aggregation.
   
   Breakwater-GitHub-Analysis-Slot: slot_3ed2431e8b58
   


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