Skip to content

[Bug]: HLL, Bitmap, and approximate aggregate states fail cleanup during multi-level spill #28853

Closed

Description

@Ariznawlll

Description

Accounted aggregate states return an internal allocation-account error when a grouped query enters multi-level aggregate spill. Confirmed affected functions are APPROX_COUNT_DISTINCT, APPROX_PERCENTILE, HLL_ADD_AGG, HLL_MERGE_AGG, BITMAP_CONSTRUCT_AGG, and BITMAP_OR_AGG. The same queries succeed when kept resident, and exact/scalar aggregate controls succeed under the same spill threshold.

Environment

  • Branch: main
  • Commit: 07fdd4ae0f80f287b93fc4b525fd296d5617abc7
  • Deployment: local standalone, 1 CN / 1 TN / 1 LogService, dedicated data directory and ports

Steps to reproduce

drop database if exists approx_hll_spill_repro;
create database approx_hll_spill_repro;
use approx_hll_spill_repro;

create table t as
select result as id, result as g
from generate_series(1, 7400) s;

set @@max_dop = 1;
set @@agg_spill_mem = 65536;

select count(*) as groups_out, sum(ad) as sum_ad
from (
    select g, approx_count_distinct(id) as ad
    from t
    group by g
) q;

Actual behavior

The supported aggregate fails before returning its 7,400 groups:

ERROR 1105 (HY000): allocation account still owns memory

The CN diagnostic summary confirms recursive spill/reload on the failing operator, including GroupSpillMaxLevel=3, GroupSpillRespills=1056, and GroupSpillReloadRows=22200.

Replacing the aggregate in the reproducer with any of the following produces the same error:

hll_cardinality(hll_add_agg(id))
hll_cardinality(hll_merge_agg(sketch))
bitmap_count(bitmap_construct_agg(bitmap_bit_position(id)))
bitmap_count(bitmap_or_agg(bmp))
approx_percentile(value, 0.5)

Expected behavior

Each one-row-per-group query should return:

groups_out  sum_ad
7400        7400

Lowering agg_spill_mem may change the execution path and latency, but it should not make a supported aggregate fail with an internal memory-ownership error.

Stability and controls

  • Reproducer: 3/3 for each of APPROX_COUNT_DISTINCT, APPROX_PERCENTILE, HLL add/merge, and Bitmap construct/or.
  • Resident controls (agg_spill_mem=1073741824): every affected aggregate returns its expected group count and compact result invariant (7400 / 7400 for one-value-per-group HLL/Bitmap cases; 20000 / 1000010000 for the percentile case).
  • Exact control (COUNT(DISTINCT id), agg_spill_mem=65536): returns 7400 / 7400.
  • Scalar-state controls (COUNT, SUM, VAR_POP, MEDIAN, BIT_AND/OR/XOR) and JSON aggregates complete the same forced-spill path.
  • Scale control: 7,200 one-row groups succeeds; 7,400 one-row groups fails.
  • Failure atomicity/data check: the service remains available; the table still contains 7,400 rows with SUM(id)=27383700.
  • EXPLAIN without execution succeeds; both plain execution and EXPLAIN ANALYZE fail once this HLL spill path is exercised.

Evidence

  • Failing SQL and three independent outputs were saved during validation.
  • Server log records the exact statement and allocation account still owns memory from the Group operator.
  • Tested commit: 07fdd4a

Code analysis

pkg/sql/colexec/aggexec/approx_count2.go stores a dense HLL sketch of 16,384 registers per active group and allocates its register storage through the aggregate allocation account. Bitmap aggregation uses the same bounded opaque-state ownership mechanism, while APPROX_PERCENTILE owns a bounded quantile sketch. The failure appears after multi-level Group spill reload/reuse and during terminal account cleanup. Because these state-owning families fail while scalar states and JSON aggregation succeed, this points to retained or incorrectly transferred MPool ownership in the generic spill recovery path; the exact missing release/transfer site still needs developer confirmation.

Regression coverage

After the fix, add:

  1. Group spill UTs covering HLL, Bitmap, and approximate-percentile aggregate state across recursive spill/reload and terminal allocation-account cleanup.
  2. A big-data SQL case that forces those aggregate states to spill and compares compact result invariants against resident execution.

This is not suitable as a passing MOTR baseline before the product path is fixed.

Related

Discovered while expanding aggregate/statistical-function spill coverage; duplicate searches included the exact error, APPROX_COUNT_DISTINCT, HLL, aggregate spill, allocation account, and open/closed issues.

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. changed the title [-][Bug]: APPROX_COUNT_DISTINCT fails with allocation-account error during multi-level aggregate spill[/-] [+][Bug]: HLL and Bitmap aggregate states fail allocation cleanup during multi-level spill[/+] on Sep 14, 2026
  5. self-assigned this
    on Sep 14, 2026
  6. changed the title [-][Bug]: HLL and Bitmap aggregate states fail allocation cleanup during multi-level spill[/-] [+][Bug]: HLL, Bitmap, and approximate aggregate states fail cleanup during multi-level spill[/+] on Sep 14, 2026
  7. Ariznawlll commented on Sep 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充当前 main 的 APPROX_PERCENTILE 规模证据:

    • 版本:0c3a04f390adaf6281fd592a49778ea5b1155e67
    • 数据:400,000 行、20,000 组;每组 20 个值
    • 常驻内存:agg_spill_mem=1073741824
      • max_dop=1 与 max_dop=8 均连续 3 轮返回完整 20,000 组;每组的 p0/p25/p50/p75/p1 分别为 -2/-1/0/1/2
      • 全表近似 p50/p95 的相对误差均低于 0.003
    • 强制 spill:agg_spill_mem=65536
      • max_dop=1 连续 3 轮均返回 ERROR 1105 (HY000): allocation account still owns memory
      • max_dop=8 连续 3 轮均返回相同错误
      • CN 日志确认进入多级 spill/re-spill
    • 查询结束后服务仍可连接,源表可查询;全局 max_dop 和 agg_spill_mem 已恢复默认值

    该结果说明问题在当前 main 仍存在,且同时覆盖串行、并行两种 recursive spill 路径;常驻路径的数值与分组结果正常。

  8. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:8e8e1998ef02b1f6233ddb1c2d3208f3b70d2d83
    • 部署:隔离的单 CN / 单 TN / 单 LogService 本地集群,通过 MySQL 协议执行。
    • 日期:2026-09-17

    原问题验证

    按原始触发条件构造 7,400 个一行分组,设置 max_dop=1、agg_spill_mem=64KiB 后执行 approx_count_distinct、approx_percentile、HLL 和 bitmap 聚合序列。所有分组数和汇总值均匹配精确预期:7400/7400/7400/27383700.0/7400/7400,未再出现 spill 后错误。

    Happy Path 覆盖

    覆盖原始 7,400 分组规模、64KiB spill 阈值以及四类 opaque aggregate。

    Unhappy Path 覆盖

    不适用:该问题是合法低阈值 spill 下的结果正确性。测试会恢复 agg_spill_mem、max_dop 并删除 fixture。

    白盒验证

    不适用:以低 spill 阈值下的全量聚合结果与 golden 比对作为端到端验收。

    回归测试

    已加入 big-data PR #1698:38_issue_28853_28871_grouped_agg_spill.sql,并接入 1Y big-data workflow。本地连续执行与 golden 一致。

    测试证据

    run.py 使用该 case 执行:7,400 分组的汇总为 7400/7400/7400/27383700.0/7400/7400,连续 golden 比对一致。

    测试结论

    测试通过。

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