Skip to content

[Bug]: Iceberg append INSERT fails with "failed to encode rows" once a data file nears 128 MiB (DML memory budget too small, cause hidden) #32514

Todo

Description

@iamlinjunhong

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

Reproduced on 2f6fcc772175d0c0e80ad97aac6d44360c513347; all files referenced below are unchanged on 553e16f52b02d59e9ecc5e3c8862f2603e661f48 (main, 2026-11-11).

Other Environment Information

Actual Behavior

INSERT INTO <iceberg table> SELECT ... fails as soon as a single data file approaches the 128 MiB target file size:

ERROR 20400: ICEBERG_OBJECT_IO: Iceberg Parquet writer failed to encode rows

Nothing else appears in the client error or in the CN log, so the error looks like a Parquet encoding or data problem. It is neither: the real cause is the Iceberg DML memory budget (Iceberg DML memory limit exceeded), and that message is lost when it is wrapped.

Measured on an 18-column table (BIGINT, DOUBLE, DATE, DATETIME(6), INT, VARCHAR; identity partition on a DATE column; all rows in one or two partitions), source is a MO native table, about 210 bytes per row in the written Parquet:

rows in one INSERT default dml-max-memory (256 MiB) dml-max-memory = 2147483648
100,000 OK (3.2 s) OK (4.1 s)
500,000 OK (13.2 s) OK (17.3 s)
1,000,000 FAIL failed to encode rows (16.7 s) OK (27.2 s)
2,000,000 FAIL failed to encode rows (21.9 s) OK (49.9 s)

The only change between the two columns is dml-max-memory plus a CN restart. The same statement over the full 22M-row source also fails with the default setting.

Expected Behavior

  1. With default settings, an append INSERT of any size succeeds: the default memory budget must be able to hold at least one data file of the default target size, including the copy made at close.
  2. When the budget is exceeded, the error says so (for example Iceberg DML memory limit exceeded [used_bytes=..., requested_bytes=..., limit_bytes=...]) and names the setting to change, instead of failed to encode rows.

Steps to Reproduce

The table in our test is wide; the SQL below is a minimal equivalent that puts more than 128 MiB of Parquet into one unpartitioned data file.

-- 1. Create the Iceberg table through the REST catalog (MO maps existing tables only), e.g.
--    POST /iceberg/v1/main%7Cwarehouse/namespaces/demo/tables
--    { "name": "t", "stage-create": false,
--      "schema": {"type":"struct","schema-id":0,"fields":[
--         {"id":1,"name":"id","required":false,"type":"long"},
--         {"id":2,"name":"s","required":false,"type":"string"}]},
--      "partition-spec": {"spec-id":0,"fields":[]},
--      "properties": {"format-version":"2"} }

-- 2. Map and write from MO with the default [cn.frontend.iceberg] settings
CREATE EXTERNAL TABLE t (id BIGINT, s VARCHAR(256)) ENGINE = ICEBERG
  WITH ('catalog'='c','namespace'='demo','table'='t','ref'='main',
        'read_mode'='merge_on_read','write_mode'='merge_on_read');

INSERT INTO t SELECT result, REPEAT('x', 200) FROM generate_series(1, 100000) g;   -- ~20 MiB file: OK
INSERT INTO t SELECT result, REPEAT('x', 200) FROM generate_series(1, 1000000) g;  -- ~200 MiB: ICEBERG_OBJECT_IO: ... failed to encode rows

-- 3. Add `dml-max-memory = 2147483648` under [cn.frontend.iceberg], restart the CN: the second INSERT succeeds.

Additional information

Why a 128 MiB file cannot fit in a 256 MiB budget. The append path buffers every data file fully in memory and charges it to one budget:

  • The budget is created with DMLMaxMemory, 256 MiB by default:
    • appendWriteBudget := newDMLMemoryBudget(
      effectiveCfg.Write.DMLMaxMemory,
      metadata.ManifestListMemoryWeight(0, preserved),
      )
      writer, err := icebergwritecore.NewFanoutParquetDataWriter(ctx, icebergwritecore.FanoutWriterConfig{
      Schema: schema,
      PartitionSpec: spec,
      TableLocation: strings.TrimRight(strings.TrimSpace(tableMeta.Location), "/"),
      DataDir: appendDataDir(api.FirstNonBlankPreserveWhitespace(req.StatementID, req.IdempotencyKey), snapshotID),
      WriterID: writerID,
      TargetFileSizeBytes: f.opts.TargetFileSizeBytes,
      TimeZone: req.TimeZone,
      }, bufferedDataFileOutputFactory{
      Writer: objectWriter,
      MemoryBudget: appendWriteBudget,
      RetainedMetadataBytes: retainedDMLDataFileMetadataBytesForSchema(schema, spec),
      })
    • ip.DMLMaxMemory = 256 << 20
  • The fanout writer closes a file only after ApproxSize() >= TargetFileSizeBytes. That value is 128 MiB by default; the append path does not set it, and it does not read the table property write.target-file-size-bytes:
  • bufferedDataFileObject.Write grows its buffer geometrically (64 KiB · 2ⁿ). It reserves the new capacity before releasing the old one. When the buffer is full at 128 MiB, it asks for 256 MiB while still holding 128 MiB, which exceeds the default budget:
    • if oldCapacity <= math.MaxInt/2 && oldCapacity*2 > nextCapacity {
      nextCapacity = oldCapacity * 2
      }
      if nextCapacity < 64<<10 {
      nextCapacity = 64 << 10
      }
      // make+copy keeps the old backing array live until the copy completes.
      // Reserve the full new capacity first, then release the old capacity;
      // accounting only the delta would understate the real growth peak by
      // almost 2x for a geometrically growing buffer.
      if err := o.budget.reserve(o.ctx, int64(nextCapacity)); err != nil {
      return 0, err
      }
      next := make([]byte, len(o.data), nextCapacity)
      copy(next, o.data)
      o.data = next
      o.budget.release(int64(oldCapacity))
  • Even with no growth step, Close() reserves a second copy of the whole buffer (copyBytes) before WriteObject. A 128 MiB file therefore needs at least 256 MiB plus the per-file metadata reservation, which is already more than the default budget:

So, with defaults, every data file that reaches the target size fails. Small statements succeed only because their files stay well below 128 MiB. Uncompressed Parquet (#32466) makes this more likely, because files are about 14x larger than they need to be.

Why the cause is hidden. The budget error is returned from Write on the output stream, so parquet-go returns it from GenericWriter.Write. ParquetDataWriter.WriteRows then wraps it as ErrObjectIO "failed to encode rows":

IcebergError.Error() prints only the code, message and fields, not Cause:

  • func (e *IcebergError) Error() string {
    if e == nil {
    return ""
    }
    var b strings.Builder
    b.WriteString(string(e.Code))
    if e.Message != "" {
    b.WriteString(": ")
    b.WriteString(e.Message)
    }
    if len(e.Fields) > 0 {
    keys := make([]string, 0, len(e.Fields))
    for k := range e.Fields {
    keys = append(keys, k)
    }
    sort.Strings(keys)
    b.WriteString(" [")
    for i, k := range keys {
    if i > 0 {
    b.WriteString(", ")
    }
    b.WriteString(k)
    b.WriteString("=")
    b.WriteString(e.Fields[k])
    }
    b.WriteString("]")
    }
    return b.String()

ToMOErr returns the outer error unchanged because the inner cause is already a moerr:

The user therefore sees only the outer message.

Possible directions (for the owners to decide):

  • Make the default budget at least 2x the target file size plus headroom, or derive the target file size from the budget.
  • Stream data files to object storage with multipart upload instead of buffering whole files. This also removes the extra copy at close.
  • Honour write.target-file-size-bytes.
  • Include Cause in IcebergError.Error(), or do not wrap errors that come from the output sink.

Workaround: set [cn.frontend.iceberg] dml-max-memory high enough. The peak per open data file is about 3x the target size: old buffer + doubled buffer while growing, or doubled buffer + copy at close. That is ~384 MiB per partition file open at the same time; we used 2 GiB. Alternatively, split large INSERTs so that no single data file reaches about 128 MiB.

Activity

  1. added
    severity/s0
    on Nov 11, 2026
  2. XuPeng-SH commented on Nov 23, 2026

    @XuPeng-SH
    Contributor

    转测:#32523 已合入 main(42d1c888f6c301d2d974daa17e9d79370818d9de)。改动在共享写内存吃紧时提前轮转分区文件,并保留硬预算限制;真正超限时应给出可定位的 Iceberg DML 内存错误,而不是只报 Parquet 编码失败。

    历史候选 8074dfd0 的单 CN、MinIO/Nessie 4 MiB 测试验证了多文件读回、真正超限时快照与原数据不变和清理。该结果未覆盖合入版本、原 256 MiB 默认预算下的 100 万/200 万行、原 2200 万行规模或生产并发。最终提交 ffe916a8 的 Actions 记录 中 adapter module tidy 通过、Iceberg E2E Local 跳过,不能当作完整 E2E 通过;#33077 的 Coverage UT runner 内存事件另行跟踪。

    请用合入的 main 在默认 256 MiB 下先跑原单/少分区宽表的 100 万和 200 万行,核实际读回、文件数及提交快照;再用确实超限的负向对照核错误里有预算上下文,快照/数据不变及清理。记录规模、耗时和峰值,未跑的原 2200 万行、取消和远程存储边界留作未验证。本次为交接,非验收结论。

  3. Ariznawlll commented on Dec 1, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。 原先“预算错误被隐藏成 Parquet 编码失败”的问题已修复,小规模及两列大数据写入通过;但默认 256 MiB 预算下,18 列、双 DATE 分区、原生表来源的 2,200 万行 INSERT 连续两次失败,仍未满足本 Issue 的大规模 append 要求。失败后未发现部分提交。

    关键证据

    精确版本与部署

    • 本轮官方 main:32ffdd4ac2722972be184af63906c8f23901e323,测试期间重新查询官方 main 仍为该 SHA。实际 SELECT git_version() 返回相同完整 SHA。
    • 修复 #32523 已合入 main,merge 42d1c888f6c301d2d974daa17e9d79370818d9de。本次只验收 main。
    • 128 上独立单节点 MO、Nessie、MinIO,独立目录和端口;MO 8 GiB memory+swap 上限,未使用 129。主复现未设置 dml-max-memory,采用默认 268435456 bytes。Parquet 显式设为 uncompressed,避免压缩后的较小数据掩盖文件轮转/预算路径。
    • 以下是按原问题类型和规模重建的合成 18 列表,不声称拿到了原始业务表全部 DDL/数据。

    通过的对照

    两列 id BIGINT, s VARCHAR(256),s 为每行派生的 200 字节文本,分四次 append:

    单次行数 INSERT 耗时 本次新增数据文件 精确数据核对
    100,000 0.892 s 1 PASS
    1,000,000 3.649 s 4 PASS
    2,000,000 7.216 s 8 PASS
    22,000,000 80.300 s 90 PASS

    每批验证行数、distinct id、ID 范围/总和、payload 总长度以及每行 payload 内容,错误行数均为 0。最终累计 25,100,000 行;独立读取对象存储中 103 个 Parquet 文件的 footer,物理行数也为 25,100,000,文件总字节 5,365,987,011,所有 column chunk 为 UNCOMPRESSED。

    18 列双分区表的 1,000,000 行和 2,000,000 行 INSERT 分别用时 21.045 s / 41.953 s,新增 10 / 21 个文件。累计 3,000,000 行,另外 17 列逐列按 ID 派生表达式全量核对,错误数均为 0。

    失败场景如何复现

    1. 创建 Iceberg v2 表,18 列为:id BIGINT、dt DATE、ts DATETIME(6)、tenant INT、4 个 BIGINT、3 个 DOUBLE、7 个 VARCHAR(32);以 dt 做 identity 分区。write.parquet.compression-codec=uncompressed。

    2. 创建相同结构的 MO 原生表 source。用 generate_series(1,22000000) 生成数据:id=result;dt 依奇偶交替为两个日期;ts 为固定微秒时间;tenant=result%100;4 个 BIGINT=result×1..4;3 个 DOUBLE=(result%1000)/2、/4、/8;7 个字符串分别为 left(md5(concat(cast(result as char),'-N')),20)。

    3. 保持默认预算,执行:

      INSERT INTO qa32514.wide SELECT * FROM qa32514.source;
    4. 第一次在已通过 1M/2M append 后执行,失败。确认 source 仍为 22,000,000 行、目标仍为 3,000,000 行,再原样执行第二次,同样失败。两次实际错误一致:

      ERROR 20400:
      invalid state ICEBERG_PLANNING_LIMIT_EXCEEDED:
      Iceberg memory limit exceeded
      [used_bytes=268008352, requested_bytes=740352,
       limit_bytes=268435456, phase=reservation,
       config=iceberg.write.dml_max_memory, toml=dml-max-memory]
      

    第二轮包含 INSERT 和随后状态检查总计 415.952 s,快照前后均为 1796141121852121090,目标表仍为 3,000,000 行。随后全量检查另外 17 列,错误数仍全为 0。

    1. 在同一服务上追加 source 中 1,000 行,小 INSERT 成功,目标变为 3,001,000 行且全部 17 列仍正确;正常重启任务自有 MO,核对完整版本和 3,001,000 行仍可读取。这说明本次失败没有把服务或后续小写入永久卡死。

    代码核对与边界

    retainedDMLDataFileMetadataBytesForSchema 按列数预留每个数据文件的常驻元数据,当前 18 列/一个分区字段计算为:

    2048 + 18 * (40 << 10) + 1024 = 740352 bytes/file
    

    这与两次失败中的 requested_bytes 完全一致。创建文件时保留该元数据预算;文件 Close 释放 payload buffer,但保留供 commit/abort 使用的 DataFile 元数据。文件轮转可以限制活动 payload,却不能消除随文件数量增长的保留元数据。此处是已有源码和错误数值相互印证的定位;未注入修改产品实现,也未把它描述为进程 OOM。

    两次失败后对象存储 wide 路径共看到 753 个 Parquet 对象,而成功快照只有此前 31 个文件。这里仅记录失败写入产生的未提交对象仍存在,不凭此另判“清理泄漏”;仍需研发核对 orphan 登记和回收策略。

    低预算和原生测试

    • 临时将任务自有 MO 配置为 4 MiB,官方 iceberg_e2e_local.go --low-budget-regression 完整执行三轮,每轮 setup、轮转、真实拒绝、cleanup 均通过。拒绝错误含 used_bytes=4173279, requested_bytes=65536, limit_bytes=4194304;快照和种子数据保持不变。之后已恢复默认配置再执行上述宽表复现。
    • api/write/sql-iceberg 的预算边界、错误传播、共享分区轮转、flush/cancellation、metadata publish 等 15 个根测试 ×3,45/45 race 执行通过。这些测试通过没有覆盖掉实际 22M 宽表失败。

    回归

    已追加现有 MOTR main PR #404,提交 73480029379d142dcf40eb966fa306d1d16aff24,没有主库测试 PR。

    • issue_32514_iceberg_budget.py:真实 SQL、逐列精确结果、快照和物理 Parquet 验证。
    • PR CI 默认 100k/1M/2M;独立 large 模式通过 QA32514_ROWS=100000,1000000,2000000,22000000 或 workflow 的 rows 输入启用,失败直接报错,不改为预期成功。
    • 首轮 CI 在 COUNT(DISTINCT) 读回触发 Group spill 限额,已改为独立客户端位图+逐行内容校验;保留完整性/唯一性断言。128 上最终流式版本的 100k/1M/2M 两种表型均通过。
    • 第二轮 CI 的百万行矩阵通过,随后低预算 Go runner 编译因缺少 CGo include 路径而失败(xxhash.h / roaring.h / usearch.h),未进入低预算产品断言。已于 bb847b173dbc0a4f0374a3c7777930ecd760632a 补齐 headers/libraries 环境,新 CI 已成功完成:默认百万行矩阵及低预算拒绝/清理三轮均通过。
    • 小矩阵即使通过也不能代替大规模门槛。该 workflow 为 PR/manual,未声称已每日执行。

    待解决点

    请继续处理默认预算下已完成文件元数据累计导致的宽表大规模写入失败,或明确产品支持范围并调整可执行的验收契约。错误信息已经正确传播,不应再按旧的“编码错误”方向定位。修复后需原样重跑 22M 宽表、失败原子性和对象回收。

    最终结论:测试不通过。未通过项:main 默认 256 MiB 预算下,18 列双分区的 22M 行 INSERT 连续两次超预算失败。已通过:错误详情传播、两列 22M、宽表 1M/2M、失败后数据/快照原子性、后续小写入及重启、三轮低预算套件和定向 race。MOTR 默认矩阵 CI 已通过,但不覆盖已失败的 22M 宽表专项;本 Issue 保持开放,转回原修复负责人继续处理。

  4. added
    ai-easy
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugneeds-triageseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions