Skip to content

[Bug]: BITMAP_COUNT reports malformed bitmap payloads as empty sets #28865

Closed

Description

@Ariznawlll

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, commit 07fdd4ae0f80f287b93fc4b525fd296d5617abc7
  • 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_COUNT and BITMAP_OR_AGG consistency;
  • 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

  1. added this to the MO-202609 milestone on Sep 14, 2026
  2. added
    kind/bug
    on Sep 14, 2026
  3. added theissue type on Sep 14, 2026
  4. self-assigned this
    on Sep 14, 2026
  5. slyunnn commented on Sep 24, 2026

    @slyunnn

    Regression and extended verification for #28865 (2026-09-24)

    Verification Environment

    • Validated source: GitHub origin/main head 95b1d2bfd9d756d08320569858080cb16f230fff — 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.
    • Runtime image: locally built matrixorigin/matrixone:issue-29161-main (sha256:e2c4991f7620728f4526d9417833dbf0cfcd9f4e0c67da602aff51fb21f3a5b2).
    • Topology: 1 Log service / 1 TN / 2 CN / Proxy.
    • Endpoint checks: Proxy 6001, CN1 16001, and CN2 16002 all accepted real SQL connections. Function validation ran through all three endpoints.
    • Client protocol: Go database/sql with go-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.
    • Related history: the issue timeline references aggregate PR #28885, merged as a3ede72bef53bcd72a10b7bba17002f19f8132cf. The current bitmap implementation and focused tests are traceable to a002ce95975e3aa5c1ebb93428f31d7fab5c1712 — fix: make aggregate states version compatible, delivered in merged PR #28915 as 8a4c84f4516d5098de9eef50b8afecc7524f37a2.

    What Was Covered

    • OK Happy path: a valid serialized roaring bitmap containing 0, 1, and 32767 returned BITMAP_COUNT = 3.
    • OK Boundary path: a valid serialized empty roaring bitmap returned BITMAP_COUNT = 0.
    • OK Unhappy path: empty bytes, not-a-bitmap, and truncated 0x01020304 each returned an invalid bitmap error instead of 0.
    • OK Aggregate consistency: a valid BITMAP_OR_AGG state produced count 3; adding malformed input made the mixed aggregate fail with a bitmap decode error.
    • OK Endpoint coverage: the complete matrix passed through Proxy 6001, CN1 16001, and CN2 16002 in parallel with isolated temporary databases.
    • OK Existing BVT: test/distributed/cases/function/issue_28864_28865_bitmap_contract.test passed twice through Proxy 6001, each run completing 9/9 statements successfully.
    • Not covered Direct BINARY/BLOB overload behavior was not separately asserted; the public function overload under test is VARBINARY.

    Findings

    BITMAP_COUNT now propagates roaring deserialization failure as invalid input: invalid bitmap: ..., so corrupted payloads can no longer be mistaken for a valid empty bitmap. This is consistent with the failure behavior of BITMAP_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.go and the self-contained BVT issue_28864_28865_bitmap_contract.test; the BVT was replayed twice successfully.

    Final Conclusion

    Fixed. BITMAP_COUNT rejects malformed roaring bitmap payloads instead of silently returning 0, while valid non-empty and valid empty serialized bitmaps retain their correct cardinalities.

  6. Ariznawlll commented on Sep 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d7d940e93bd037e9aacdc0f1256b91e32df84642
    • 环境:本地 MatrixOne 单机

    原问题验证

    对合法 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。
    • 重复输入值不影响集合基数。
    • NULL 输入返回 NULL。

    异常与边界覆盖

    • 空 VARBINARY:报告 cookie EOF。
    • 非 bitmap 文本:报告 header/cookie 非法。
    • 4 字节短二进制:报告 header/cookie 非法。
    • 三次错误后继续执行表查询和 SELECT 1,session 保持可用。

    白盒验证

    错误覆盖 roaring 反序列化的初始 cookie EOF 和 serialCookie 不匹配两条路径,确认 malformed payload 不再被解释为空集合。

    回归测试

    测试证据

    • MOTR 结果:3/3 success,stmts=15 ok=15 fail=0(每轮)。
    • 合法 payload 返回 3;三类非法 payload 均返回 20301。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions