Repository navigation
Description
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 keyEngine 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 → NULLSo:
client.batch_insert("t", [{"a":1,"b":2},{"a":3}])→ KeyError 'b'query.bulk_insert("t", [{"a":1,"b":2},{"a":3}])→ executesINSERT 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 viaself._insert_columnsand padsNonefor 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
fix(python): unify SDK query and analytics lifecycle contracts (#31774)
XuPeng-SH commentedon Oct 30, 2026
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.Ariznawlll commentedon Oct 30, 2026
结论
测试通过。当前三个批量 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。(1,'first'),(2,NULL),即便列有 DEFAULT 也不使用 DEFAULT。extra字段时三个入口均抛ValueError;原两行逐值不变,后续SELECT 1正常。每轮测试库已清理。待解决点