The IPC reader accepts a column that is shorter than its RecordBatch, and fills the missing rows with nulls

未关闭 适合新手
#557 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Sealdot 已经在做这个了。

开始于 2026年12月2日。

  • #561 来自 @Sealdot —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
backend

调研方向

从 src/ipc/reader.ts 中的 _loadRecordBatch() 开始,追踪字段节点长度如何传到 src/recordbatch.ts 中的 RecordBatch 构造函数。issue 中的复现示例展示了批次大小为 4 时长度为 2 和 0 的不匹配情况;请分别运行这两种情况。完成标准是每个格式错误的流都抛出错误,而不是用 null 填充缺失的行。

由索引模型根据 Issue 内容生成。

描述

Describe the bug, including details regarding any error messages, version, and platform.

The IPC reader does not compare the length of each field node with the length of its RecordBatch. A column that declares fewer rows than its batch reads with no error. The table keeps the row count of the batch, the reader fills the rows past the length of the column with nulls, and the field of the column becomes nullable.

The repro changes one valid stream of x: [10, 20, 30, 40] and y: [1, 2, 3, 4] (Int32, 4 rows) in two ways:

  1. The field node of x declares 2 rows. x reads as [10, 20, null, null].
  2. The field node of x declares 0 rows. x reads as [null, null, null, null].

The writer did not write these nulls, and a reader cannot tell them from real nulls. In Arrow C++, RecordBatch::Validate() refuses a column whose length is not the row count of the batch.

The open PR #551 checks the buffer regions against the message body. The buffers of these streams do not change, so the two reads also give no error with the change of #551. This fault is not the same as the second case of #548: there, the field node declares more rows than the buffers hold. Here, the buffers are correct and the field node declares fewer rows than the batch.

Version and platform: apache-arrow 21.2.0, Node.js 24.21.0 on Windows 11. The code below is the same on main (f547423).

Repro. Save as repro.ts, install [email protected], and run node repro.ts (Node.js 24 strips the types). It exits 1 while the fault is present.

// apache-arrow 21.2.0. The IPC reader does not compare the length of each field node with the length of its
// RecordBatch. A column that declares fewer rows than its batch reads with no error: the table keeps the row count of
// the batch, and the reader fills the rows past the length of the column with nulls.
import * as A from "apache-arrow";

const table = new A.Table({
  x: A.vectorFromArray([10, 20, 30, 40], new A.Int32()),
  y: A.vectorFromArray([1, 2, 3, 4], new A.Int32()),
});
const valid = A.tableToIPC(table, "stream");

// The field nodes of the batch are [{ length: 4, nullCount: 0 }, { length: 4, nullCount: 0 }], as little-endian int64 values.
const nodes = new Uint8Array(new BigInt64Array([4n, 0n, 4n, 0n]).buffer);

/** This function gives the one place of `pattern` in `bytes`. */
function placeOf(bytes: Uint8Array, pattern: Uint8Array): number {
  const places: number[] = [];
  for (let at = 0; at + pattern.length <= bytes.length; at += 1) {
    if (pattern.every((byte, index) => bytes[at + index] === byte)) places.push(at);
  }
  if (places.length !== 1) throw new Error(`expected 1 place of the field nodes, found ${places.length}`);
  return places[0]!;
}

/** This function gives a copy of the valid stream in which the field node of x declares `length` rows. */
function withLengthOfX(length: number): Uint8Array {
  const copy = valid.slice();
  new DataView(copy.buffer, copy.byteOffset).setBigInt64(placeOf(copy, nodes), BigInt(length), true);
  return copy;
}

let faults = 0;
for (const length of [2, 0]) {
  try {
    const read = A.tableFromIPC(withLengthOfX(length));
    const x = read.getChild("x");
    console.log(`x declares ${length} rows in a batch of 4: no error, numRows ${read.numRows}, x.get(i) = [${Array.from({ length: read.numRows }, (_, i) => String(x?.get(i))).join(", ")}], x.length ${x?.length}`);
    faults += 1;
  } catch (error) {
    console.log(`x declares ${length} rows in a batch of 4: ${String(error)}`);
  }
}
process.exit(faults > 0 ? 1 : 0);

Output:

x declares 2 rows in a batch of 4: no error, numRows 4, x.get(i) = [10, 20, null, null], x.length 4
x declares 0 rows in a batch of 4: no error, numRows 4, x.get(i) = [null, null, null, null], x.length 4
exit code: 1

Expected: An error for each of the two streams. The field node of each column of a RecordBatch must declare the length of the batch.

Cause: _loadRecordBatch() in src/ipc/reader.ts (lines 378 to 382) loads the columns with the lengths of their field nodes and gives them to the RecordBatch constructor with header.length. The constructor calls ensureSameLengthData() in src/recordbatch.ts (line 318). For each column that is shorter than the batch, this function makes the field nullable and fills the missing rows with nulls (_changeLengthAndBackfillNullBitmap). This is correct when a program makes a table from columns of different lengths, but it hides the fault in IPC data.

https://github.com/apache/arrow-js/blob/f547423d9f9c1f061ab8155674e9639538fa7307/src/recordbatch.ts#L327-L332

Possible fix: In _loadRecordBatch(), throw an error when the length of a column is not header.length, before the RecordBatch constructor runs.

An AI coding assistant (Claude Code) found this fault, wrote the repro and wrote this text.

Component(s)

JavaScript

主要语言
TypeScript
星标
114
派生
35
平均合并
13 小时 35 分钟
30 天内合并 PR
5

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/arrow-js 的其他 Issue

查看 apache/arrow-js 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。