Skip to content

feat(c-abi): statement parameter binding + executed-statement query id - #165

Merged
mani-mathur-arch merged 5 commits into
mainfrom
mani/c-abi-pupr-statement-surface
Jul 17, 2026
Merged

mani-mathur-arch merged 5 commits into
mainfrom
mani/c-abi-pupr-statement-surface

Conversation

@mani-mathur-arch

@mani-mathur-arch mani-mathur-arch commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Adds the two C-ABI surfaces the Go driver's PuPr milestone needs, as reviewable feature commits (originally stacked on #163; that has since landed on main, so this now targets main directly).

Commits

  • kernel_statement_bind_parameter — pre-marshalled generic parameter bind (host-supplied Databricks type name + stringified value, positional or named). Maps 1:1 to a C-ABI host's already-stringified params (e.g. the Go driver's backend.Param{Name, Type, Value}) and covers every Databricks type + named params, unlike the typed positional binders. StatementSpec.raw_params + param_raw; build_execute_params emits them with raw-vs-typed and positional-vs-named-raw mutual exclusion. set_sql clears raw_params so statement reuse doesn't accumulate stale binds.
  • kernel_executed_statement_query_id — success-path accessor for the server statement/query id (the counterpart to KernelError.query_id, which crosses only on failure). Returns a borrowed C string valid for the executed handle's lifetime; NULL on a null/invalidated handle. A host uses it for EXECUTE_STATEMENT telemetry and query-history correlation.

Testing

make check clean (fmt + clippy -D warnings + full suite + C smoke). Coverage: raw-param build/exclusion/reset unit tests; wiremock success test for query_id (returns the server id, NULL after re-execute invalidation); a positive-flow bind→execute→query_id wiremock reference; named-raw-param wire assertion (name key, no ordinal); non-UTF-8 rejection at bind for each of sql_type / name / value; and C-smoke null-handle checks for both new entry points.

Review history

First pass (Isaac Review): 1 MAJOR — raw params accumulated across statement re-execute (param_raw appends, no ordinal to overwrite by, no reset), so a reused statement handle would duplicate positional markers. Fixed by clearing all bind collections in set_sql (documented reuse contract + regression test). Re-ran make check clean.

Second pass (/full-review, score 86/100 — no critical/major/minor): 6 quality findings, all addressed in e91b1ed:

  • raw_params → pub(crate) — it holds the raw SEA wire struct StatementParameter (not #[non_exhaustive]), so a public field would make evolving the wire a breaking crate change; param_raw (strings only) stays the public surface.
  • statement_id_c now built via to_cstring_lossy (promoted to pub(crate)), so the success-path query id and the error-path KernelError.query_id share identical interior-NUL semantics; corrected the contradictory field doc.
  • param_raw docstring now states the append-only / one-shot-per-query contract. Duplicate-named-raw dedup is deferred to PR5 in lockstep with param_named (kept accumulate-by-design; push-time rejection intentionally not added — the consumer binds once per execute on a fresh handle).
  • sql() docstring narrowed to "bound parameters" with a scope note that statement_conf / row_limit survive a sql() call.
  • New wiremock tests for the named-raw wire shape and non-UTF-8 rejection (see Testing).

Isaac Review re-run on the second-pass fixes: 0 findings.

This pull request and its description were written by Isaac.

@mani-mathur-arch mani-mathur-arch changed the title feat(c-abi): PuPr statement surface — param binding + query-id accessor feat(c-abi): statement parameter binding + executed-statement query id Jul 12, 2026
@mani-mathur-arch
mani-mathur-arch force-pushed the mani/c-abi-pupr-statement-surface branch from 53171cd to 518bd02 Compare July 14, 2026 12:29
… bind)

Matches the shape a C-ABI host driver already produces (stringified value +
Databricks type name + optional name), building a SEA StatementParameter
directly instead of round-tripping through TypedValue — the binding path for a
host like the Go driver, whose backend.Param{Name, Type, Value} maps 1:1 to it
and covers every Databricks type (DECIMAL/TIMESTAMP/INTERVAL/VARIANT/GEOMETRY/
BINARY) and named params, which the typed positional binders cannot.

StatementSpec gains raw_params + param_raw; build_execute_params emits them
(positional ordinals assigned 1-based in push order, named passed through), with
raw-vs-typed and positional-vs-named-raw mutual exclusion enforced at build time
so a mixed markers array is rejected before the wire. Unit tests cover positional
ordinal assignment, named + NULL (VOID), and both exclusion rejections.

Co-authored-by: Isaac
Signed-off-by: Mani Kaustubh Mathur <mani.mathur@databricks.com>
The server statement (query) id crossed the C ABI only on the error path
(KernelError.query_id). A host driver needs it on success too — for
EXECUTE_STATEMENT telemetry and query-history correlation — so add
kernel_executed_statement_query_id, returning the id as a borrowed
NUL-terminated C string valid for the executed handle's lifetime (no per-call
allocation, nothing for the caller to free; copy out to outlive the handle).
NULL on a null or re-execute/close-invalidated handle, with a stored last
error, matching the num_modified_rows staleness rule.

The id is snapshotted as a CString on the C-ABI executed handle at construction
(a server UUID, interior NULs stripped defensively). Adds an inherent
ExecutedStatement::statement_id() so a concrete-handle consumer need not import
the ExecutedStatementHandle trait. Covered by a wiremock success test (returns
the server id, NULL after close) and C-smoke null-handle checks.

Co-authored-by: Isaac
Signed-off-by: Mani Kaustubh Mathur <mani.mathur@databricks.com>
Review-hardening pass on the PuPr statement surface (bind_parameter +
query_id), addressing the code-review findings:

- set_sql now clears ALL bound params (raw + typed positional/named), not
  just raw_params, so a reused statement handle can never leak stale binds
  into the next query. Previously a typed-then-raw reuse tripped the new
  raw-vs-typed exclusion on leftover typed slots, and a shrinking-arity
  typed reuse emitted stale high-ordinal markers — the "fresh parameter
  set" contract now holds for every binding style. (Isaac MAJOR.)
- bind_parameter rejects an empty sql_type at bind with InvalidArgument,
  symmetric with the NULL guard and param_named's empty-name reject,
  rather than emitting "type":"" and failing opaquely at the server.
- query_id: drop the dead .replace('\0') so the CString fallback is live;
  clarify the fallback contract in the comment.
- header: document the raw-bind lifecycle (accumulate; set_sql is the sole
  reset, clears all binds; exclusion enforced at execute, bind returns
  Success), define "typed params" by cross-referencing the bind_* family.
- fix the misleading query_id success-test comment (close(exec) frees the
  handle; staleness comes from re-execute / parent close).

Tests: real re-execute invalidation test (query_id -> NULL +
InvalidStatementHandle); bind_parameter through the real C entry point
(empty-name -> positional, null -> NULL, null/empty type -> InvalidArgument)
+ positive bind->execute->query_id flow asserting normalization on the wire;
raw-vs-named-typed exclusion; empty-string-value-vs-NULL; set_sql clears
typed params on reuse. make check green (687 lib + 11 wiremock + C smoke).

Co-authored-by: Isaac
Signed-off-by: Mani Kaustubh Mathur <mani.mathur@databricks.com>
@mani-mathur-arch
mani-mathur-arch force-pushed the mani/c-abi-pupr-statement-surface branch from 518bd02 to d22a8b6 Compare July 14, 2026 18:25
@mani-mathur-arch
mani-mathur-arch changed the base branch from mani/c-abi-statement-canceller to main July 14, 2026 18:25
Empty commit to fire a fresh synchronize event so the pull_request:[main]
gated workflows (Unit + doc tests) evaluate now that the PR base was
retargeted from mani/c-abi-statement-canceller (landed as #163) to main.
No content change; squashed away at merge.

Co-authored-by: Isaac
Signed-off-by: Mani Kaustubh Mathur <mani.mathur@databricks.com>
Comment thread src/statement/spec.rs Resolved
Comment thread tests/v0_c_abi_canceller_wiremock.rs Resolved
Comment thread src/c_abi/statement.rs Resolved
Comment thread src/statement/spec.rs Resolved
Comment thread src/c_abi/statement.rs Resolved
Comment thread src/statement/spec.rs Resolved
@mani-mathur-arch

Copy link
Copy Markdown
Contributor Author

Code Review Squad — Review

Score: 86/100 — MODERATE RISK

Two new C-ABI surfaces for the Go driver's PuPr milestone: kernel_statement_bind_parameter (pre-marshalled raw param bind) and kernel_executed_statement_query_id (success-path query-id accessor). No security, memory-safety, or FFI-soundness defects — the injection surface, null-guards, panic-safety, and the borrowed query-id lifetime were all verified sound. The findings are design-boundary, test-coverage, and consistency quality issues, not correctness bugs; none block merge.


Second /full-review pass on PR #165 raised 6 quality findings (0 critical,
score 86/100). All addressed here:

- raw_params -> pub(crate): it holds the raw SEA wire struct StatementParameter
  (Serialize, not #[non_exhaustive]), so a public field would make evolving the
  wire a breaking change to the kernel crate. param_raw (strings only) stays the
  public surface.
- statement_id_c now built via to_cstring_lossy (promoted to pub(crate)), so the
  success-path query id and the error-path KernelError.query_id share identical
  interior-NUL semantics (strip -> '?', never discard the whole id); corrected the
  contradictory field doc.
- param_raw docstring now states the append-only / one-shot-per-query contract and
  notes duplicate-named-raw dedup is deferred to PR5 in lockstep with param_named
  (kept accumulate-by-design; push-time rejection intentionally not added).
- sql() docstring narrowed to "bound parameters" with a scope note that
  statement_conf / row_limit survive a sql() call.
- New wiremock tests: named raw param reaches the wire with a name key and no
  ordinal; non-UTF-8 rejected at bind for each of sql_type / name / value.

make check clean (fmt + clippy -D warnings + full suite + C smoke); the two new
wiremock tests pass. Isaac Review re-run on the changes: 0 findings.

Co-authored-by: Isaac
Signed-off-by: Mani Kaustubh Mathur <mani.mathur@databricks.com>
@mani-mathur-arch
mani-mathur-arch added this pull request to the merge queue Jul 17, 2026
Merged via the queue into main with commit c37548c Jul 17, 2026
15 of 16 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — e91b1eda Deployed Jul 15, 2026 by mani-mathur-arch via v0 TLS (mitmproxy) #208
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants