Skip to content

[PECOBLR-2096] C ABI follow-up: lazy metadata streaming, empty-filter semantics, session-close delete initiation - #160

Merged
vikrantpuppala merged 2 commits into
mainfrom
pecoblr-2096-kernel-metadata-session-followup
Jul 6, 2026
Merged

vikrantpuppala merged 2 commits into
mainfrom
pecoblr-2096-kernel-metadata-session-followup

Conversation

@vikrantpuppala

Copy link
Copy Markdown
Collaborator

Follow-up to #158 (merged), addressing @gopalldb's three post-merge review findings on the v0 C ABI.

[MAJOR] Unbounded metadata buffering → lazy streaming

drain_metadata materialized the whole metadata result into a Vec<RecordBatch> at construction, justified as "bounded — a handful of batches". That's false for kernel_metadata_list_columns with a NULL catalog, which becomes SHOW COLUMNS IN ALL CATALOGS — a row per column of every table in every visible catalog. In a large workspace an ODBC getColumns(null, …) would materialize the entire cross-catalog column inventory at once.

The owned result-stream handle now holds the ResultStream and drives next_batch lazily on the runtime (one batch per kernel_result_stream_next_batch, via block_on_ffi — the same on-runtime discipline the executed path already uses to avoid driving a CloudFetch reader off a plain C thread). run_metadata now captures just the schema up front and hands the stream to the handle. Bounded memory, no behavior change for consumers.

[MAJOR] Empty-string identifier rejected instead of "unfiltered"

opt_ident mapped only a NULL pointer to None; a non-null empty string became Some("") → Identifier::new("") → InvalidArgument. ODBC/JDBC drivers commonly pass "" (not NULL) to mean "match all", so list_schemas / list_tables / list_columns rejected a legitimate call. New opt_filter_str treats NULL and empty/whitespace-only alike as None; opt_ident / opt_pattern route through it. Required identifiers (PK/FK table) still correctly reject empty.

[MAJOR] kernel_session_close didn't initiate DeleteSession when handles outlive the session

Close dropped one Arc<SessionInner>; DeleteSession only fires from SessionInner::Drop (last-Arc-drop). A live statement/executed handle holds its own clone, so the delete was deferred until those were released too — contradicting the header's "close initiates the delete" contract and risking the server session lingering to idle-timeout. Added SessionInner::initiate_delete (idempotent via a compare-and-swap on closed; Drop now delegates to it), exposed Session::initiate_delete, and call it from kernel_session_close so the delete is initiated immediately regardless of outstanding handles.

Docs + tests

  • databricks_kernel.h and the Rust docs updated to match (metadata NULL/empty semantics; close-initiation contract).
  • Metadata empty→unfiltered unit tests (opt_filter_str, opt_ident).
  • New C-ABI wiremock test asserting close initiates DELETE with a statement handle still open.
  • The existing owned-stream exhaustion test now builds a lazy ResultStream via MockReader.

cargo fmt / clippy --all-targets clean; 673 lib tests + all integration suites + C smoke pass.

The dependent ODBC PR databricks/databricks-odbc#101 will re-pin KERNEL_REPO_TAG to this PR's merge commit once it lands.

This pull request and its description were written by Isaac.

…unfiltered, initiate session delete on close

Follow-up to the merged C ABI PR (#158), addressing gopalldb's three
post-merge findings.

[MAJOR] Unbounded metadata buffering
`drain_metadata` materialised the entire metadata result into a
Vec<RecordBatch> up front, justified as "bounded". That is false for
`kernel_metadata_list_columns` with a NULL catalog (→ SHOW COLUMNS IN ALL
CATALOGS), which returns a row per column of every table in every visible
catalog. The owned result-stream handle now holds the `ResultStream` and
drives `next_batch` lazily on the runtime (one batch per C call, via
`block_on_ffi` — same on-runtime discipline the executed path uses), so a
large metadata result streams with bounded memory instead of spiking it
for the life of the handle. `run_metadata` now captures only the schema up
front and hands the stream to the handle.

[MAJOR] Empty-string identifier rejected instead of "unfiltered"
`opt_ident` mapped only a NULL pointer to None; a non-null empty string
became Some("") → Identifier::new("") → InvalidArgument. ODBC/JDBC drivers
routinely pass "" (not NULL) to mean "match all", so list_schemas /
list_tables / list_columns rejected a legitimate call. Added
`opt_filter_str`, which treats NULL and empty/whitespace-only alike as
None; `opt_ident` / `opt_pattern` route through it. (Required identifiers
— PK/FK table — still reject empty, correctly.)

[MAJOR] session_close didn't initiate DeleteSession with handles open
`kernel_session_close` dropped one Arc<SessionInner>; DeleteSession only
fires from SessionInner::Drop, i.e. when the LAST Arc drops. A live
statement / executed handle holds its own clone, so the delete was
deferred until those were released too — contradicting the header's "close
initiates the delete" contract and risking a server session lingering to
idle-timeout. Added SessionInner::initiate_delete (idempotent via a
compare-and-swap on `closed`; Drop now delegates to it), exposed
Session::initiate_delete, and call it from kernel_session_close so the
delete is initiated immediately regardless of outstanding handles.

Header + Rust docs updated to match. Tests: metadata empty→unfiltered unit
tests; a C-ABI wiremock test asserting close initiates DELETE with a
statement handle still open; the existing owned-stream exhaustion test now
builds a lazy ResultStream via MockReader.

cargo fmt / clippy --all-targets clean; 673 lib tests + all integration
suites + C smoke pass.

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

gopalldb commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

💡 [MINOR] · bug · src/c_abi/session.rs:555

Metadata stream driven after kernel_session_close hits a session whose DeleteSession was already initiated

Outstanding metadata streams become undefined after kernel_session_close now that close eagerly initiates DeleteSession. kernel_session_close (src/c_abi/session.rs:555) now calls initiate_delete(), which spawns the server-side DeleteSession immediately (src/session.rs:150-168). A metadata ResultStream is built via from_reader_with_close and holds its own Arc clone (src/metadata_v0.rs:302-307), so it outlives the session box and remains drivable. If a caller closes the session while such a stream is still open and then calls kernel_result_stream_next_batch (src/c_abi/result.rs:217), the reader issues chunk-fetch RPCs against a session whose delete has already been initiated, likely surfacing server errors. Before this PR the delete deferred until the last Arc dropped, so a metadata stream obtained before close stayed valid until its own close; the eager-delete change alters that. The header documents only that the session handle becomes invalid after close (src/c_abi/session.rs:530-534) — it says nothing about independently-held result-stream handles, so the required ordering is unspecified.

Addresses gopalldb's MINOR on #160. Now that kernel_session_close eagerly
initiates DeleteSession, an outstanding metadata result stream (which
holds its own Arc<SessionInner> and stays drivable) driven after close
would issue chunk-fetch RPCs against an already-deleting session and
surface server errors. The header only stated the session handle becomes
invalid; the required ordering for independently-held handles was
unspecified.

Document it in databricks_kernel.h and the kernel_session_close doc:
close the session LAST — drain/close result streams → executed handles →
statements → session. Docs only; no behavior change.

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

Copy link
Copy Markdown
Collaborator Author

Thanks @gopalldb — addressed in f742ed4. You're right that eager delete-on-close changes the lifetime of an outstanding metadata stream (it holds its own Arc<SessionInner> and stays drivable, so driving it after close now hits an already-deleting session). Rather than re-defer the delete, I documented the required ordering — databricks_kernel.h and the kernel_session_close doc now state to close the session last: drain/close result streams → executed handles → statements → session. This mirrors the statement-close ordering note already in the header. Docs only, no behavior change.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 6, 2026
Merged via the queue into main with commit b307fb4 Jul 6, 2026
19 of 21 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — f742ed47 Deployed Jul 6, 2026 by vikrantpuppala via v0 TLS (mitmproxy) #192
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