Repository navigation
Description
Description
BITMAP_COUNT silently returns 0 for malformed binary input instead of reporting that the value is not a valid roaring bitmap. The same payload passed through BITMAP_OR_AGG produces an explicit decoding error.
This makes corrupted bitmap state indistinguishable from a valid empty set.
Environment
- MatrixOne: latest official
main, commit07fdd4ae0f80f287b93fc4b525fd296d5617abc7 - Deployment: local standalone launch (isolated Log/TN/CN ports and data directory)
Reproduction
select bitmap_count(cast('' as varbinary)) empty_payload,
bitmap_count(cast('not-a-bitmap' as varbinary)) text_payload,
bitmap_count(unhex('01020304')) short_binary_payload;
create table payloads(b varbinary);
insert into payloads
select bitmap_construct_agg(v) from
(select cast(1 as bigint unsigned) v) s;
insert into payloads values (cast('not-a-bitmap' as varbinary));
select bitmap_count(bitmap_or_agg(b)) from payloads;Actual behavior
BITMAP_COUNT('') 0
BITMAP_COUNT('not-a-bitmap') 0
BITMAP_COUNT(0x01020304) 0
BITMAP_OR_AGG mixed payload:
internal error: invalid roaring bitmap cookie 762605422
The behavior reproduced identically in 3/3 fresh runs.
Expected behavior
BITMAP_COUNT should return an invalid-input error for malformed bitmap payloads, consistent with BITMAP_OR_AGG and with HLL_CARDINALITY's validation of malformed HLL states.
Code-path analysis
pkg/sql/plan/function/func_unary.go:BitmapCount creates a roaring bitmap and calls UnmarshalBinary, but its callback discards the error and returns zero. HllCardinality immediately below uses an error-returning wrapper and propagates invalid-state errors, showing the missing validation pattern.
Impact
- corrupted persisted/intermediate bitmap state can be reported as a legitimate zero cardinality;
- monitoring and analytical queries cannot distinguish data corruption from an empty set;
- scalar count and aggregate merge disagree on whether the same payload is valid.
Suggested regression coverage
- empty, truncated, random, wrong-cookie, and valid-empty serialized payloads;
- VARBINARY/BINARY/BLOB inputs;
BITMAP_COUNTandBITMAP_OR_AGGconsistency;- valid construct/serialize/count round trips.
Duplicate search
Open and closed issues were searched for BITMAP_COUNT, invalid payload, roaring cookie, deserialization, and corrupted bitmap; no matching report was found.
Activity
fix: make aggregate states version compatible (#28885)
slyunnn commentedon Sep 24, 2026
Regression and extended verification for #28865 (2026-09-24)
Verification Environment
origin/mainhead95b1d2bfd9d756d08320569858080cb16f230fff—fix(external): keep parquet scalar siblings vectorized (#29285). The runtime was built from GitHub's exact source archive for that commit because the local Git remote ref was stale.matrixorigin/matrixone:issue-29161-main(sha256:e2c4991f7620728f4526d9417833dbf0cfcd9f4e0c67da602aff51fb21f3a5b2).1 Log service / 1 TN / 2 CN / Proxy.6001, CN116001, and CN216002all accepted real SQL connections. Function validation ran through all three endpoints.database/sqlwithgo-sql-driver/mysql; each endpoint used isolated temporary database objects. The three endpoint cases ran in parallel because they shared no session, global setting, table, or execution-order dependency.a3ede72bef53bcd72a10b7bba17002f19f8132cf. The current bitmap implementation and focused tests are traceable toa002ce95975e3aa5c1ebb93428f31d7fab5c1712—fix: make aggregate states version compatible, delivered in merged PR #28915 as8a4c84f4516d5098de9eef50b8afecc7524f37a2.What Was Covered
0,1, and32767returnedBITMAP_COUNT = 3.BITMAP_COUNT = 0.not-a-bitmap, and truncated0x01020304each returned aninvalid bitmaperror instead of0.BITMAP_OR_AGGstate produced count3; adding malformed input made the mixed aggregate fail with a bitmap decode error.6001, CN116001, and CN216002in parallel with isolated temporary databases.test/distributed/cases/function/issue_28864_28865_bitmap_contract.testpassed twice through Proxy6001, each run completing9/9statements successfully.BINARY/BLOBoverload behavior was not separately asserted; the public function overload under test isVARBINARY.Findings
BITMAP_COUNTnow propagates roaring deserialization failure asinvalid input: invalid bitmap: ..., so corrupted payloads can no longer be mistaken for a valid empty bitmap. This is consistent with the failure behavior ofBITMAP_OR_AGG.The endpoint validations were parallel by design: each used a separate temporary database and no global/session mutation. The temporary databases were removed after execution.
Large-data regression is not required: decoding validity depends on bitmap bytes, not row count, block/batch boundaries, memory, spill, concurrency, skew, network transport, or resource scale.
MOTR / BVT Coverage
No new MOTR case was added. Current MatrixOne already contains focused unit coverage in
pkg/sql/plan/function/func_unary_test.goand the self-contained BVTissue_28864_28865_bitmap_contract.test; the BVT was replayed twice successfully.Final Conclusion
Fixed.BITMAP_COUNTrejects malformed roaring bitmap payloads instead of silently returning0, while valid non-empty and valid empty serialized bitmaps retain their correct cardinalities.Ariznawlll commentedon Sep 24, 2026
测试版本
maind7d940e93bd037e9aacdc0f1256b91e32df84642原问题验证
对合法 roaring bitmap 和三类 malformed payload 复测
BITMAP_COUNT。合法聚合 payload 返回 cardinality 3;空 payload、普通文本和短二进制 payload 均不再静默返回 0,而是明确返回ERROR 20301 invalid input。正常场景覆盖
BITMAP_CONSTRUCT_AGG生成包含 1、2、3 的合法 payload,BITMAP_COUNT返回 3。异常与边界覆盖
SELECT 1,session 保持可用。白盒验证
错误覆盖 roaring 反序列化的初始 cookie EOF 和 serialCookie 不匹配两条路径,确认 malformed payload 不再被解释为空集合。
回归测试
issue_28865_bitmap_count_validation.test。测试证据
stmts=15 ok=15 fail=0(每轮)。测试结论
测试通过。