Repository navigation
Description
DRAFT 2 (SDK, 2026-10-27) — candidate issue, not yet filed
Title: [Bug]: single-row _build_insert_sql does not escape quotes (batch version does) and batch silently drops unknown columns / crashes on missing ones
Description
(Scope note: the "silently drops extra columns in later batch rows" half of this report was already filed as #31106; this issue covers the remaining, distinct defects: the unescaped single-row builder, the KeyError crash on missing keys, and numeric-value quoting.)
BaseMatrixOneClient._build_insert_sql (single-row) and _build_batch_insert_sql sit in the same file with opposite escaping behavior — evidence that a past injection fix patched only the batch path: (single-row) and _build_batch_insert_sql sit in the same file with opposite escaping behavior — evidence that a past injection fix patched only the batch path:
# base_client.py:88 (single) — NO escaping:
formatted_values.append(f"'{str(value)}'")
# base_client.py:136 (batch) — escaped:
escaped_value = str(value).replace("'", "''")Verified side by side:
b._build_insert_sql("t", {"name": "o'rien"}) # → INSERT INTO t (name) VALUES ('o'rien') ← broken/injectable
b._build_batch_insert_sql("t", [{"name": "o'rien"}]) # → INSERT INTO t (name) VALUES ('o''rien') ← correctsession.insert() / client.insert() route through the single-row builder, so the common path is the injectable one.
Additional defects in the same pair:
- All values are string-quoted, including numerics (
{"count": 5}→VALUES ('5')). MySQL tolerance hides this for INT columns, but the quote-wrapping selects string semantics — e.g. width checks, comparison coercion and strict-mode writes to numeric columns behave differently from batch inserts of unquoted numbers, and it also re-invokes the numeric-string comparison bugs ([Bug]: Default collation is binary/case-sensitive; explicit _ci collations are ignored #25096 family) for predicate columns written this way. - Batch insert with heterogeneous dicts: columns are taken from the first row only.
- A later row missing a column → raw KeyError crash (
KeyError: 'b') mid-payload generation. - A later row with an extra column → silently dropped (already covered: [Bug]: Python INSERT builder silently drops unexpected fields in later rows #31106).
- A later row missing a column → raw KeyError crash (
- Vector
listvalues are joined without quoting elements — an element containing]or,corrupts the vector literal.
Reproduction
from matrixone import Client
c = Client(); c.connect(...)
c.execute("CREATE TABLE t (id INT, name VARCHAR(20))")
c.insert("t", {"id": 1, "name": "o'rien"}) # SQL syntax broken by literal
c.batch_insert("t", [{"id":1,"name":"a"}, {"id":2}]) # KeyError('name')
c.batch_insert("t", [{"id":1}, {"id":2,"extra":99}]) # extra column silently discardedImpact
- The single-row path is the everyday insert API (
session.insert,client.insert, ORM flush paths building through it); any string with an apostrophe — names (O'Brien), free-text fields, JSON payloads with quotes — breaks the statement or becomes an injection vector, exactly what [Bug]: Python SDK builders interpolate unsafe SQL and misrender typed parameter values #31186 was filed against at the builder layer; this is the same vulnerability one layer down, still unpatched. - Heterogeneous batch payloads are the norm in ETL (rows from loosely-typed sources); today you either crash on the first missing key or silently drop keys that exist in later rows only — silent divergence between input and stored data.
- The numeric-quotes issue makes single and batch semantics differ (batch quotes too, so behavior matches at least; but both diverge from normal unquoted numeric literal SQL and interact badly with strict compare semantics).
Expected behavior
- Single-row
_build_insert_sqlmust escape quotes exactly like batch (ideally: share one formatting function so they can't diverge again). - Batch should either (a) union the keys of all rows (fill missing with NULL/DEFAULT) or (b) validate and raise a precise error ("row 3 misses column 'name'"); silently dropping is the worst option.
- Emit numerics unquoted (int/float/bool → str(value)), date/datetime via proper literal, bytes via hex; only strings get quoted+escaped.
- Vector elements need escaping or a strict numeric-element assertion.
Activity
Ariznawlll commentedon Oct 27, 2026
Verified against current main (2026-10-27): the single-row
_build_insert_sqlin base_client.py now routes through_sql_literals.sql_literal/sql_identifierwith proper '' doubling; the escaping gap (first half of this report) is fixed. Closing as fixed on main. NOTE: if the KeyError-on-missing-column batch behavior is still of interest, it is tracked separately in the batch-semantics consolidation (please see the unified batch-insert semantics issue) — flagging so this half doesn't get lost.