Skip to content

feat(pyo3): statement query tags + custom HTTP headers (match Thrift behavior) - #119

Merged
vikrantpuppala merged 1 commit into
mainfrom
feat/kernel-query-tags-and-headers
Jun 3, 2026
Merged

vikrantpuppala merged 1 commit into
mainfrom
feat/kernel-query-tags-and-headers

Conversation

@vikrantpuppala

@vikrantpuppala vikrantpuppala commented Jun 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

Two more use_kernel-parity features on the kernel surface, designed to mirror the Thrift connector's behavior:

  1. Statement-level query tags — kernel core already had StatementSpec::query_tags; this closes the last parity gap and exposes it through pyo3.
  2. Custom HTTP headers — net-new; carries user_agent_entry.

Follow-up connector PR wires both + bumps KERNEL_REV.

Query tags

  • spec.rs: query_tags now takes HashMap<String, Option<String>>. A None value emits a bare key (no colon) — matching the Python connector's Dict[str, Optional[str]] serializer (the only parity gap; escaping already matched byte-for-byte). Serializes into the SEA query_tags statement-conf key.
  • pyo3: Statement.set_query_tags(dict[str, str | None]).

Custom HTTP headers (Thrift precedence + UA append)

  • config.rs: custom_headers: Vec<(String,String)> on public HttpConfig.
  • http.rs: caller headers applied first, then Authorization / x-databricks-org-id last so they win (matching Thrift). Those two caller names are skipped (warn).
  • User-Agent is special-cased — appended to the kernel base UA, never replacing it. The base UA carries the DatabricksJDBCDriverOSS token the SEA server keys on to enable the INLINE_OR_EXTERNAL_LINKS result disposition; a caller UA that replaced it breaks result fetching (HTTP 400 "not a supported disposition"). This was caught by the live e2e — appending preserves the gating and still surfaces the caller's identity.
  • pyo3 Session(http_headers=...).

Tests

  • wiremock unit test: custom header + appended User-Agent reach the wire; bogus caller Authorization overridden by the real token.
  • live e2e (tests/v0_query_tags_headers_e2e.rs, wired into rust-e2e.yml): tagged queries (valued + None/bare-key) and a custom-header session (appended UA) all round-trip green against a dogfood warehouse. Smoke-level — query.history ingestion lag (minutes) makes a sync tag-readback infeasible; exact wire shapes pinned by the unit/wiremock tests.

Verification

  • cargo fmt + clippy -D warnings clean under tls-native and tls-rustls.
  • 612 kernel lib tests, 11 pyo3 Rust tests, 3 e2e (live) all pass.

This pull request and its description were written by Isaac.

@vikrantpuppala
vikrantpuppala force-pushed the feat/kernel-query-tags-and-headers branch from a239658 to c2053f6 Compare June 3, 2026 08:47
@vikrantpuppala
vikrantpuppala requested a review from a team as a code owner June 3, 2026 08:47
@gopalldb

gopalldb commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

P1 — Should fix

  1. StatementSpec::query_tags reshape breaks the napi binding (src/statement/spec.rs:308 + napi/src/connection.rs:94, 202, 224)
    - Signature changed from HashMap<String, String> → HashMap<String, Option>. napi/src/connection.rs:94 still declares Option<HashMap<String, String>> and passes it straight in. cargo build in napi/
    fails with a type mismatch.
    - Invisible to this PR's CI: napi/ is a separate Cargo workspace excluded from the kernel root, and napi-checks.yml is path-gated to napi/** and root Cargo.toml — neither touched here.
    - Impact: databricks-sql-kernel-napi is unbuildable from main after this lands; the first subsequent napi-touching PR fails with a kernel-incompatibility error that has nothing to do with that PR's
    changes.
    - Fix: in this PR, update napi/src/connection.rs (3-line type adjustment + value wrapping) AND consider revising napi-checks.yml path filter so future kernel-API breaks surface immediately.
  2. custom_headers rustdoc contradicts the implementation (src/config.rs:425-429, 440-442)
    - Doc claims caller-supplied Authorization / x-databricks-org-id are overwritten by the kernel's value, and that "later entries win on names that collapse to a single header". The implementation actually
    silently drops (with a warn) those names entirely. And reqwest's RequestBuilder::header calls HeaderMap::append, not insert — so for non-special-cased duplicate names, the wire carries all entries, in
    insertion order, not just the last.
    - Impact: a future maintainer who trusts the doc may relax the skip to support a debug-tool-supplied auth override, triggering duplicate Authorization headers (undefined receiver behaviour, credential-leak
    vector if a logging proxy is on the path).
    - Fix: rewrite the doc to match actual semantics — "caller entries named Authorization / x-databricks-org-id are silently dropped with a warn; the kernel adds its own values; other duplicate names are
    emitted on the wire as-is in insertion order".
  3. E2E test doesn't validate the auth-skip claim on a live server (tests/v0_query_tags_headers_e2e.rs:140-160)
    - Only sends X-Custom-E2E-Header + User-Agent. The wiremock test covers the auth-skip path, but the wire test against a real server doesn't.
    - Fix: add a third e2e variant that sets ("Authorization", "Bearer obviously-bogus") alongside the real PAT and asserts SELECT 1 still succeeds → proves the bogus token was skipped and the real PAT won.

P2 — Minor

  1. UA append is unbounded (src/client/http.rs:343) — caller can pass many-KB UA on every request. Add a length cap or warn! above e.g. 1 KB.
  2. Header validation deferred to first request build for custom headers (other than UA) — src/client/http.rs:343-351, 694. Validate (name, value) once in new() via HeaderName::try_from +
    HeaderValue::try_from so malformed headers fail at session-open rather than every request.
  3. Caller UA also rides OAuth token-endpoint requests (src/client/http.rs:347-351 + execute_without_auth path) — verify Thrift connector parity. If Thrift doesn't apply http_headers to IdP requests, document
    this divergence or factor the per-request loop to gate by with_auth.
  4. Broken rustdoc cross-link to execute_impl (private fn) at src/client/http.rs:211-212.
  5. Empty-string vs None semantic asymmetry undocumented on StatementSpec::query_tags (src/statement/spec.rs:294-296) — Some("") produces key:; None produces bare key. Add a one-line note.
  6. field_reassign_with_default clippy hit in tests/v0_query_tags_headers_e2e.rs:75-77 — switch to struct-literal form to avoid an -D warnings failure.

Wire two more connection features onto the kernel surface so the
databricks-sql-python use_kernel path can reach Thrift parity:
statement-level query tags, and caller-supplied HTTP headers (which
also carries user_agent_entry).

Query tags (statement-level):
- spec.rs: `StatementSpec::query_tags` now takes
  `HashMap<String, Option<String>>` — a `None` value emits a BARE KEY
  (no colon), matching the Python connector's `Dict[str, Optional[str]]`
  serializer. The escaping already matched byte-for-byte; this closes
  the last parity gap (key-only tags). Serializes into the SEA
  `query_tags` statement_conf key.
- pyo3 Statement gains `set_query_tags(dict[str, str | None])`.

Custom HTTP headers (carries user_agent_entry):
- config.rs: add `custom_headers: Vec<(String,String)>` to public
  HttpConfig.
- http.rs: apply caller headers FIRST, then the kernel's own
  Authorization / x-databricks-org-id LAST so they win — matching the
  Thrift connector's precedence. A caller header named Authorization /
  x-databricks-org-id is skipped (warn).
- **User-Agent is special-cased: APPENDED to the kernel base UA, never
  replacing it.** The base UA carries the `DatabricksJDBCDriverOSS`
  token the SEA server keys on to enable the INLINE_OR_EXTERNAL_LINKS
  result disposition; a caller UA that *replaced* it breaks result
  fetching (HTTP 400 "not a supported disposition") — caught by the
  live e2e below. Appending preserves the gating and still surfaces the
  caller's identity (the connector's PyDatabricksSqlConnector/x UA).
- session.rs: thread HttpConfig.custom_headers into the HTTP client.
- pyo3 Session gains an `http_headers` kwarg.

Tests:
- wiremock unit test: custom header + appended User-Agent reach the
  wire; a bogus caller Authorization is overridden by the real token.
- live e2e (tests/v0_query_tags_headers_e2e.rs, wired into rust-e2e.yml):
  tagged queries (valued + None/bare-key) and a custom-header session
  (with appended UA) all round-trip green against a dogfood warehouse.
  These are smoke tests — query.history ingestion lag (minutes) makes a
  sync tag-readback assertion infeasible; exact wire shapes are pinned
  by the unit/wiremock tests.

cargo fmt + clippy clean under tls-native and tls-rustls; 612 kernel
lib tests pass; 11 pyo3 Rust tests pass; 3 e2e pass live.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

Thanks @gopalldb — all addressed in the latest push (81da32e):

P1

  1. napi break — fixed. Per a side discussion, changed the napi query_tags declaration itself (the #[napi(object)] field + build_statement param) to Option<HashMap<String, Option<String>>> rather than wrapping at the call site — so JS gets full parity (a null value → bare key, like the Python connector). cargo build + clippy in napi/ are green. (Re: the path-filter gap — agree the napi-vs-kernel break should surface in CI; noting it as a follow-up to widen napi-checks.yml rather than expand this PR further.)
  2. custom_headers rustdoc — rewritten to match the implementation: Authorization / x-databricks-org-id are silently dropped with a warn (not "overwritten"); other duplicate names are emitted via HeaderMap::append in insertion order (not "later wins"); User-Agent is folded/appended at construction.
  3. auth-skip e2e — added custom_authorization_header_is_dropped_and_real_auth_wins: sets a bogus Authorization alongside the real PAT and asserts SELECT 1 still succeeds. Passes live against dogfood.

P2
4. UA cap — caller UA suffix truncated (with warn) above 1 KiB.
5. Header validation in new() — custom headers validated via HeaderName/HeaderValue::try_from at session-open; malformed name/value now fails there, not per-request. New unit test.
6. OAuth-UA parity — confirmed custom headers + merged UA do ride token-endpoint requests (shared execute_impl), which matches Thrift (setCustomHeaders applies to all transport requests, IdP included). Documented inline rather than diverging.
7. rustdoc link — reworded the execute_impl reference to plain prose (no private-item link).
8. Some("") vs None — documented on query_tags: None → bare key, Some("") → key: (present-but-empty).
9. clippy field_reassign_with_default — the e2e helper can't use a struct literal (HttpConfig is #[non_exhaustive], external test crate), so #[allow]'d with a rationale comment.

fmt + clippy clean under tls-native, tls-rustls, and napi; 613 lib tests pass; 4 e2e pass live.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jun 3, 2026
Merged via the queue into main with commit df8302f Jun 3, 2026
22 of 23 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — 81da32e2 Deployed Jun 3, 2026 by vikrantpuppala via PyO3 build + test (Python 3.12) #53
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