Skip to content

[Bug]: three batch-insert engines disagree on missing/extra row columns #31391

Closed

Description

@Ariznawlll

DRAFT 14 (SDK, 2026-10-27) — to be filed

Title: [Bug]: two batch-insert implementations with conflicting missing-column semantics (KeyError crash vs NULL-fill vs silently-drop)

Description

The SDK contains three different batch-insert engines that handle heterogeneous row dicts differently, so the same input produces three different outcomes depending on which API entry point is used:

Entry point Engine Missing column in later row Extra column in later row
client.batch_insert / session.batch_insert base_client._build_batch_insert_sql KeyError crash (no .get) — filed in #31376 silently dropped — filed in #31106
query.insert(...); query.bulk_insert([...]) → orm._build_insert_sql → build_insert_query MatrixOneSQLBuilder.values() + _build_insert fills None → SQL NULL (union of keys) included in union (inserted)
client.load_data.read_csv_inline (row ingestion) separate again — —

Exact evidence (all verified statically + pure-python repro):

Engine A (base_client.py:~121):

columns = list(data_list[0].keys())          # first row's keys rule
for col in columns:
    value = data[col]                         # KeyError on missing key

Engine B (sql_builder.py _build_insert dict-format branch):

all_columns = set()
for values in self._insert_values:
    all_columns.update(values.keys())        # union of ALL rows' keys
for col in columns:
    if col in values: placeholders.append("?"); params.append(values[col])
    else: placeholders.append("NULL")        # missing → NULL

So:

  • client.batch_insert("t", [{"a":1,"b":2},{"a":3}]) → KeyError 'b'
  • query.bulk_insert("t", [{"a":1,"b":2},{"a":3}]) → executes INSERT INTO t (a, b) VALUES (1, 2), (3, NULL) — silently NULLs the b column (which may be NOT NULL declared! server rejects — differing behavior again) or inserts NULL where the user expected a default value to apply.
  • And inside Engine B, MatrixOneSQLBuilder.values(**kwargs) aligns to the first row's key order via self._insert_columns and pads None for missing keys — but _build_insert's dict-branch rebuilds the union itself, so the two halves of Engine B even disagree about the column set.

Same conceptual row-set, three incompatible contracts — any ORM/ETL code switching from one API to another silently changes data outcomes: crash vs NULL vs drop.

Reproduction (pure python, no server)

from matrixone.base_client import BaseMatrixOneClient
from matrixone.sql_builder import build_insert_query

BaseMatrixOneClient()._build_batch_insert_sql("t", [{"a":1,"b":2},{"a":3}])
# KeyError: 'b'

build_insert_query("t", [{"a":1,"b":2},{"a":3}])
# ('INSERT INTO t (b, a) VALUES (?, ?), (NULL, ?)', [1, 2, 3]) - union + NULL-fill

(notice also the union loses ordering: (b, a) — column order depends on set iteration)

Impact

  • Silent NULL-filling is the worst member: with a NOT NULL column the server errors late (with no pointer to which row), and with a nullable/defaulted column the user's implicit "use DEFAULT" expectation is replaced by explicit NULL — data semantics differ from both other engines and from SQLAlchemy's own bulk semantics (which align by key sets and error clearly).
  • Column order is derived from a Python set — nondeterministic across runs for >1 missing-ordering cases; harmless to the server but breaks byte-comparison tests and generated-SQL snapshots.
  • Developers cannot write one batch helper and reuse it across client/session/ORM layers without tripping a different failure mode in each.

Expected behavior

Pick ONE semantics (recommend: SQLAlchemy-like — build the union with a deterministic order (sorted or first-seen), fill missing keys with DEFAULT (or raise on rows missing NOT NULL columns — but at minimum be explicit), never KeyError, never silently drop) and route all three entry points through the same engine — the same refactor #31376's expected-behavior section already suggests. Tests: heterogeneous rows through client.batch_insert and query.bulk_insert assert identical SQL.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. added a commit that references this issue on Oct 30, 2026
    bbfe556
  5. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31774 (main bbfe55669a1440424914d45a283a1d0e36f722fa): please verify the reported public SDK contract and its failure/recovery controls for three batch-insert engines disagree on missing/extra row columns. This isolated QA run inspected the issue and merged PR but did not run tests or a live MatrixOne service at this SHA because the existing resource guard left insufficient headroom. The PR's author-reported validation is separate evidence, not a result of this QA run.

  6. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。当前三个批量 INSERT 入口对缺失字段和后续新增字段采用一致策略:缺失字段写入 NULL,未声明的新增字段明确拒绝,不再静默丢弃。

    关键证据

    • 官方 main@d39d8a40343ccd40bf665036388df7a40ee867f8;修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 合并提交 bbfe55669a1440424914d45a283a1d0e36f722fa 位于祖先链;运行二进制和 SDK 同完整 SHA。本地隔离 1 CN / 1 TN / 1 LogService,Python 3.12、SQLAlchemy 2.0.43。
    • 三个独立库中分别使用 Client.batch_insert、Session.batch_insert 和 legacy Query.bulk_insert;两行输入中后行缺少 label 时,三者均实际写入 (1,'first'),(2,NULL),即便列有 DEFAULT 也不使用 DEFAULT。
    • 后续行引入 extra 字段时三个入口均抛 ValueError;原两行逐值不变,后续 SELECT 1 正常。每轮测试库已清理。
    • 回归已加入 MOTR #295,定向 3/3 通过、0 skipped。

    待解决点

    • 本次未覆盖异步及所有 ORM 映射类型;缺失字段是明确的 NULL 策略,不表示支持逐行 DEFAULT。未运行完整 MOTR suite;Add build constraint to assembly source files #295 尚未合入,SDK 回归需显式启用和匹配源码,默认 SKIP 不计通过。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions