hanke580 opened a new issue, #71182:
URL: https://github.com/apache/doris/issues/71182

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   - master `b4140d0b0538` (2026-11-30), built from source: `select 
@@version_comment` = `doris version doris-0.0.0-b4140d0b05`
   - doris-4.1.5-release-917b20be838 (`apache/doris:fe-4.1.5` and 
`apache/doris:be-4.1.5`, one FE and one BE)
   
   
   ### What's Wrong?
   
   
   A BITMAP holds unsigned 64-bit values: `bitmap_from_string` and `to_bitmap` 
accept 0 .. 18446744073709551615, and
   `bitmap_to_string` prints them back as stored. Every function that returns 
the elements as numbers returns a BIGINT
   with the same bits instead, so 9223372036854775808 (2^63) becomes 
-9223372036854775808 and 18446744073709551615
   becomes -1. Sorting, comparisons and sums over the elements are then wrong, 
and `bitmap_max` can be smaller than
   `bitmap_min`. No error or warning.
   
   ```
   bitmap_to_string(bitmap_from_string('9223372036854775808,20,30'))   -> 
20,30,9223372036854775808
   UNNEST(...) AS uid ORDER BY uid                                     -> 
-9223372036854775808 | 20 | 30
   LATERAL VIEW explode_bitmap(...) ... ORDER BY e                     -> 
-9223372036854775808 | 20 | 30
   bitmap_to_array(...)                                                -> [20, 
30, -9223372036854775808]
   bitmap_max(...)                                                     -> 
-9223372036854775808
   count(*) ... WHERE uid > 25                                         -> 1
   sum(uid)                                                            -> 
-9223372036854775758
   bitmap_max / bitmap_min of '18446744073709551615,20,30'             -> -1 / 
20
   BITMAP column holding to_bitmap(18446744073709551615): bitmap_to_array / 
bitmap_max / explode_bitmap -> [-1] / -1 / -1
   ```
   
   The values are read as signed in the BE:
   
   - `BitmapValue::minimum()` and `maximum()` return `int64_t` although the 
members are `uint64_t`
     (`be/src/core/value/bitmap_value.h`,
     [master, lines 2559 and 
2627](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/core/value/bitmap_value.h#L2559-L2635));
     `bitmap_min` / `bitmap_max` return them as `Int64`
     ([`function_bitmap_min_or_max.h`, lines 
29-54](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/exprs/function/function_bitmap_min_or_max.h#L29-L54)).
   - `explode_bitmap` (and `UNNEST` of a bitmap) writes the elements into a 
`ColumnInt64`
     ([`vexplode_bitmap.cpp`, lines 
75-123](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/exprs/table_function/vexplode_bitmap.cpp#L75-L123)).
   - `bitmap_to_array` builds an `ARRAY<BIGINT>` (`DataTypeInt64`, 
`ColumnInt64`) from them
     ([`function_bitmap.cpp`, lines 
1157-1190](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/exprs/function/function_bitmap.cpp#L1157-L1190)).
   
   ### What You Expected?
   
   
   The elements as stored, as `bitmap_to_string` prints them: `20, 30, 
9223372036854775808` in that order, `[20, 30,
   9223372036854775808]`, `bitmap_max` = 9223372036854775808 (and 
18446744073709551615 >= `bitmap_min` for the second
   bitmap), 2 rows with `uid > 25`, and a sum of 9223372036854775858. The 
documented range of a bitmap element is 0 ..
   18446744073709551615
   
(https://doris.apache.org/docs/4.x/sql-manual/sql-functions/scalar-functions/bitmap-functions/bitmap-from-string/,
   .../to-bitmap/). Returning LARGEINT from these functions, or raising an 
error for an element above
   9223372036854775807, would keep the value; returning a different number 
should not happen.
   
   
   ### How to Reproduce?
   
   
   One FE and one BE with default settings.
   
   ```sql
   SELECT bitmap_to_string(bitmap_from_string('9223372036854775808,20,30'));
   SELECT UNNEST(bitmap_from_string('9223372036854775808,20,30')) AS uid ORDER 
BY uid;
   SELECT e FROM (SELECT 1 AS k) x LATERAL VIEW 
explode_bitmap(bitmap_from_string('9223372036854775808,20,30')) t AS e ORDER BY 
e;
   SELECT bitmap_to_array(bitmap_from_string('9223372036854775808,20,30'));
   SELECT bitmap_max(bitmap_from_string('9223372036854775808,20,30'));
   SELECT bitmap_max(bitmap_from_string('18446744073709551615,20,30')), 
bitmap_min(bitmap_from_string('18446744073709551615,20,30'));
   SELECT count(*) FROM (SELECT 
UNNEST(bitmap_from_string('9223372036854775808,20,30')) AS uid) t WHERE uid > 
25;
   SELECT sum(uid) FROM (SELECT 
UNNEST(bitmap_from_string('9223372036854775808,20,30')) AS uid) t;
   
   -- the same from a table
   CREATE DATABASE bitmap_repro;
   USE bitmap_repro;
   CREATE TABLE bm (id INT, b BITMAP BITMAP_UNION) AGGREGATE KEY(id) 
DISTRIBUTED BY HASH(id) BUCKETS 1
     PROPERTIES ("replication_num" = "1");
   INSERT INTO bm VALUES (1, bitmap_from_string('9223372036854775808,20,30')), 
(2, to_bitmap(18446744073709551615));
   SELECT id, bitmap_to_string(b), bitmap_to_array(b), bitmap_max(b) FROM bm 
ORDER BY id;
   SELECT id, e FROM bm LATERAL VIEW explode_bitmap(b) t AS e ORDER BY id, e;
   DROP DATABASE bitmap_repro;
   ```
   
   
   ### Anything Else?
   
   #68219 and #70844 handle negative integers on the way into a bitmap; this is 
the way out, for values the bitmap
   accepts.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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