Skip to content

fix(metadata): treat list_tables catalog as a LIKE pattern - #199

Merged
vikrantpuppala merged 1 commit into
mainfrom
fix/list-tables-catalog-wildcard
Jul 30, 2026
Merged

vikrantpuppala merged 1 commit into
mainfrom
fix/list-tables-catalog-wildcard

Conversation

@vikrantpuppala

@vikrantpuppala vikrantpuppala commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Metadata::list_tables typed the catalog argument as an exact Identifier, so ODBC SQLTables (and the other bindings) silently dropped _ / % wildcards in the catalog. A caller passing comparator_tests got only that catalog and not the sibling comparator-tests that _ should match — the divergence reported in databricks/databricks-odbc#129 (OSS 4 rows vs reference 8 rows).

This is a real bug: ODBC SQLTables treats CatalogName as a pattern-value argument under the default SQL_ATTR_METADATA_ID = SQL_FALSE, and the Thrift protocol backs it — TGetTablesReq.catalogName is a TPatternOrIdentifier, so the server wildcards the catalog. The reference (Simba/Thrift) driver gets the extra rows that way.

What the server supports, and how the fix resolves a wildcard

The Databricks SEA SHOW grammar (which this kernel uses) has no catalog-pattern position — the LIKE clause applies only to schema/table names; the catalog is always IN CATALOG <exact> or IN ALL CATALOGS. Rather than SHOW TABLES IN ALL CATALOGS + client-side row filtering (which scans every table in every catalog on the workspace and throws away the non-matches), list_tables resolves a wildcard catalog by enumerating catalogs and fanning out one scoped query per match:

  • Wildcard catalog (_ / % unescaped, or a */empty literal the SHOW builder would misread as "all catalogs") → SHOW CATALOGS, filter the catalog names client-side with a case-insensitive LikeMatcher, then SHOW TABLES IN CATALOG <name> per match (schema/table/type pushed down server-side) with bounded parallelism (cap 8 in flight). Results are concatenated and re-sorted into the JDBC row order.
  • Literal catalog (no active wildcards, incl. the ODBC SQL_ATTR_METADATA_ID = SQL_TRUE identifier case) → unescape and scope server-side via SHOW TABLES IN CATALOG <name> (fast exact path).
  • Empty catalog pattern (Some("")) → "match nothing" per LikePattern (mirrors the schema/table LIKE '' convention) → empty result, no wire call.
  • None → a single SHOW TABLES IN ALL CATALOGS — genuinely "every catalog", the cheapest correct query.

Why fan-out instead of a whole-workspace scan

The per-catalog fan-out is strictly cheaper than SHOW TABLES IN ALL CATALOGS for selective catalog patterns (raised in review by @msrathore-db), and the common single-catalog case never hits it at all — a BI tool targeting one catalog named e.g. my_catalog either sets SQL_ATTR_METADATA_ID = SQL_TRUE or escapes the _, both of which take the fast exact path. Because each fan-out query is catalog-scoped, it also sidesteps the pre-DBR-16 edge where IN ALL CATALOGS omits catalogName and rows can't be attributed to a catalog. An object-not-found on a per-catalog query (catalog dropped mid-enumeration) contributes zero rows, per the JDBC empty-result contract.

Scope: list_tables only

The catalog contract is per-call, grounded in the Thrift IDL:

Method Thrift request field Catalog type This PR
list_tables TGetTablesReq.catalogName TPatternOrIdentifier catalog → LikePattern ✅
list_columns TGetColumnsReq.catalogName TIdentifier unchanged (exact)
list_schemas TGetSchemasReq.catalogName TIdentifier unchanged (exact)

The issue's suspicion that SQLColumns is affected identically does not hold — the Thrift TGetColumnsReq.catalogName is a TIdentifier that "must not contain a search pattern", so Simba doesn't wildcard the columns catalog either. Keeping columns/schemas exact preserves JDBC-spec semantics for Python / Node / ADBC on the shared kernel.

Changes

  • sql.rs: pattern_has_wildcards, strip_jdbc_escapes, is_wildcard_or_empty_scope helpers, and a compiled, reusable LikeMatcher (case-insensitive ODBC/JDBC LIKE) used to filter catalog names (+ unit tests).
  • metadata_v0.rs: list_tables catalog is now Option<LikePattern>, with routing across the exact / fan-out / empty / all-catalogs paths above; adds the enumerate-then-fan-out with bounded parallelism.
  • thrift_shape.rs: reshape_tables is back to its 3-arg form (catalog matching moved to the name level, so the client-side row filter was removed); exposes sort_tables_batch so the fan-out can re-sort the merged cross-catalog result.
  • c_abi / pyo3 / napi: call sites updated for the Identifier → LikePattern signature change.
  • Tests: unit (matcher / escapes / cross-catalog merge-sort), wiremock asserting the emitted SQL per routing path (literal IN CATALOG, wildcard fan-out via SHOW CATALOGS + per-catalog IN CATALOG, empty match-nothing), and a self-adapting live-workspace e2e regression test.

Testing

  • cargo fmt --all + cargo clippy --all-targets -- -D warnings: clean.
  • 715 lib tests + 16 wiremock integration tests pass.
  • 10/10 e2e tests pass against a live warehouse, including the catalog-wildcard regression test (an s_mples pattern drives SHOW CATALOGS + per-catalog SHOW TABLES IN CATALOG samples and returns rows, all matching TABLE_CAT).

Note: run with --no-default-features --features tls-rustls — the default tls-native fails to link in this environment due to a pre-existing system OpenSSL 1.1-vs-3.x symbol mismatch, unrelated to this change.

Fixes databricks/databricks-odbc#129

This pull request and its description were written by Isaac.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Nit

Looks good — a well-scoped, correct fix that types list_tables's catalog as a LikePattern and reproduces Thrift's catalog-wildcarding client-side (IN ALL CATALOGS + TABLE_CAT filter) while keeping the literal fast path. The wildcard/literal routing, the requested_catalog vs catalog_filter split, and all binding call sites (napi/pyo3/c_abi/e2e/lib.rs) are consistent, and the helpers + reshape path are unit-tested including the issue-#129 underscore repro. Only a doc nit on the LIKE matcher; no correctness or contract concerns.

Other findings

  • ⚪ Nit — The % arm comment says it will "Collapse consecutive %", but the code doesn't collapse runs of % — it recurses into like_match_inner(rest, ...) at every suffix, and if rest begins with another % that inner call repeats the same fan-out. Matching is still correct, but the claimed optimization isn't present, so a pattern with many consecutive/interleaved % against a long non-matching candidate can backtrack super-linearly. In practice catalog names and patterns are capped at 255 bytes and this only runs on catalog values, so the cost is bounded and negligible — this is purely a comment/behavior mismatch worth tidying (either actually collapse consecutive %, or drop the 'Collapse' wording).

@msrathore-db msrathore-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid there would be a high usage of _ in catalog name where the latency would account for SHOW TABLES IN ALL CATALOGS which would be extremely slow
One alternative I can suggest is to do a SHOW CATALOGS and filter and call show tables for each of them. I think that would be a faster alternative

@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

Good call, @msrathore-db — adopted. The wildcard-catalog path no longer does SHOW TABLES IN ALL CATALOGS + client-side row filtering. It now:

  1. SHOW CATALOGS, filter the catalog names client-side (case-insensitive ODBC LIKE), then
  2. run SHOW TABLES IN CATALOG <name> for each match (schema/table/type pushed down server-side), with bounded parallelism (cap 8 in flight) so a loose pattern matching many catalogs doesn't serialize into a long N+1 chain or burst the warehouse, then
  3. concatenate + re-sort into JDBC row order.

This avoids the whole-workspace table scan for selective patterns, exactly as you suggested. Two related notes:

  • The common single-catalog case never hits this path at all: a BI tool targeting one catalog named e.g. my_catalog either sets SQL_ATTR_METADATA_ID = SQL_TRUE (identifier) or escapes the _, and both route to the fast exact SHOW TABLES IN CATALOG — so the fan-out only runs when the client genuinely intends a catalog pattern.
  • Because each query is catalog-scoped, this also sidesteps the pre-DBR-16 edge where IN ALL CATALOGS omits catalogName and rows can't be attributed to a catalog.

Also addressed the peco-review-bot nit (dropped the inaccurate "Collapse consecutive %" comment on the matcher). Updated tests: unit (matcher/escapes/merge-sort), wiremock asserting the emitted SQL per routing path (literal IN CATALOG, wildcard fan-out, empty match-nothing), and a live-workspace e2e regression test. PTAL.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium · 1 Low

Solid, well-tested fix for treating the list_tables catalog as a LIKE pattern — good wiremock + unit + e2e coverage, and the Identifier→LikePattern signature change is propagated consistently across bindings. One medium issue: the fan-out path leaks server-side statements on read/reshape error paths (bypasses close_statement). One low doc/code mismatch on the wildcard-catalog strategy.

Comment thread src/metadata_v0.rs Resolved
Comment thread src/metadata_v0.rs Resolved
ODBC `SQLTables` treats `CatalogName` as a pattern-value argument under
the default `SQL_ATTR_METADATA_ID = SQL_FALSE`, and the Thrift protocol
backs this: `TGetTablesReq.catalogName` is a `TPatternOrIdentifier`, so
the server wildcards `_` / `%` in the catalog. The kernel's SEA `SHOW`
path typed the catalog as an exact `Identifier`, silently dropping the
wildcard — a caller passing `comparator_tests` got only that catalog,
not the sibling `comparator-tests` a `_` should match.

The Databricks `SHOW` grammar has no catalog-pattern position (the
`LIKE` clause applies only to schema/table names). Rather than
`SHOW TABLES IN ALL CATALOGS` + client-side row filtering — which scans
every table in every catalog on the workspace and discards the
non-matches — `list_tables` now resolves a wildcard catalog by
enumerating catalogs and fanning out one scoped query per match:

- Wildcard catalog (`_` / `%`, or a `*`/empty literal the `SHOW`
  builder would misread as "all catalogs"): `SHOW CATALOGS`, filter the
  names with a case-insensitive `LikeMatcher`, then run
  `SHOW TABLES IN CATALOG <name>` for each match with bounded
  parallelism (schema/table/type pushed down server-side). Results are
  concatenated and re-sorted into the JDBC row order.
- Literal catalog (no active wildcards, incl. the ODBC
  `SQL_ATTR_METADATA_ID = SQL_TRUE` identifier case): unescape and scope
  server-side via `SHOW TABLES IN CATALOG <name>` — the fast exact path.
- Empty catalog pattern: "match nothing" per `LikePattern` — an empty
  result with no wire call (matches the schema/table `LIKE ''`
  convention rather than silently broadening to all catalogs).
- `None`: a single `SHOW TABLES IN ALL CATALOGS` — genuinely "every
  catalog", the cheapest correct query.

The per-catalog fan-out is strictly cheaper than a whole-workspace scan
for selective patterns (per PR review feedback), and because each query
is catalog-scoped it also sidesteps the pre-DBR-16 case where
`SHOW TABLES IN ALL CATALOGS` omits `catalogName` and rows can't be
attributed to a catalog. An object-not-found on a per-catalog query
(catalog dropped mid-enumeration) contributes zero rows, per the JDBC
empty-result contract.

Scope is `list_tables` only. `list_columns` and `list_schemas` keep an
exact catalog identifier, matching their Thrift request fields
(`TGetColumnsReq.catalogName` / `TGetSchemasReq.catalogName` are both
`TIdentifier`, which "must not contain a search pattern").

Adds `pattern_has_wildcards` / `strip_jdbc_escapes` /
`is_wildcard_or_empty_scope` helpers and a compiled, reusable
`LikeMatcher` to the SQL builder; unit tests (matcher, escapes,
merge-sort across catalogs); wiremock tests asserting the emitted SQL
per routing path (literal `IN CATALOG`, wildcard fan-out, empty
match-nothing); and a live-workspace e2e regression test. Wrapper call
sites (pyo3 / napi / C ABI) updated for the `Identifier` -> `LikePattern`
signature change.

Fixes databricks/databricks-odbc#129

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

Copy link
Copy Markdown
Collaborator Author

Addressed the review-round findings (pushed 35c0e07):

Behavior

  • % / match-all catalog now takes the single SHOW TABLES IN ALL CATALOGS path, not the fan-out. % is semantically identical to "no catalog filter", so fanning it out was both N round trips for "everything" and a way for one inaccessible catalog to fail the whole call. New pattern_matches_all helper routes runs of % to the single query.
  • Statement leak on the fan-out error path fixed: fetch_tables_for_catalog now closes the per-catalog statement on every exit (success or a mid-stream read error), not just after the loop completes.

Docs / cross-binding

  • napi list_tables docstring corrected (was still "catalog (exact)") to describe the LIKE-pattern semantics — matches pyo3 / C-ABI now.
  • Documented the empty-catalog divergence explicitly: Rust/pyo3/napi treat "" as "match nothing" (per LikePattern), while the C ABI normalizes ""→NULL = "all catalogs" (the ODBC convention its consumers expect). Noted at each boundary + pattern-validation behavior.

Observability

  • Added a debug! line recording catalogs enumerated vs matched, so fan-out amplification (a loose pattern hitting many catalogs) is visible in one correlatable log line.

Cleanup

  • Removed list_tables_all_catalogs (it was list_tables_single_catalog(None, …) verbatim); None and match-all now share the single-query path. Fixed stale LikeMatcher doc that still referenced the removed row-filter design.

Tests (all green: 716 lib + 21 wiremock + 10 live e2e)

  • New wiremock coverage: %→single all-catalogs query; a catalog dropped mid-enumeration (42704) skipped; a non-not-found per-catalog error (42501) propagates; zero-catalogs-match → empty, no per-catalog wire call; >8 matching catalogs exercising the bounded-parallel refill.

PTAL.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low · 1 Nit

Looks good — a solid, well-tested fix that correctly retypes the list_tables catalog as a LIKE pattern and resolves wildcards via enumerate-then-fan-out. Routing, escaping, and match-arm ordering are consistent, and wiremock coverage (fan-out, dropped-catalog tolerance, error propagation, >8 in-flight cap, empty/percent) is thorough. One low-severity resource-lifecycle note on statement cleanup when a per-catalog error aborts the fan-out, plus a doc-duplication nit.

Other findings

  • ⚪ Nit — sort_tables_batch (new pub(crate) wrapper) carries a doc comment identical to the private sort_tables it delegates to, so the two blocks are verbatim duplicates. Consider making sort_tables itself pub(crate) (or having the wrapper's doc just say 'see [sort_tables]') to avoid the copy-paste drift risk.

Comment thread src/metadata_v0.rs
)
.await
}
// Literal catalog: scope server-side (fast path). Unescape

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — When a per-catalog fetch returns a non-not-found error, result? aborts the whole call and the FuturesUnordered (inflight) is dropped along with any still-pending sibling futures. fetch_tables_for_catalog creates a server-side statement (via transport().list_tables) and only closes it at the end of its own body; a future cancelled after its create request completed but before reaching close_statement leaks that statement server-side. This is the exact edge the repo's resource-lifecycle convention targets ('server-side handles/sessions/streams closed on every path (incl. error/early-return)'). The window is narrow and error-abort is exceptional, so severity is low, but it's worth noting since the happy/in-future error paths were deliberately hardened here while the cross-future abort path was not. A structured cleanup (e.g. draining inflight to completion on error before returning, discarding rows) would close them deterministically.

@msrathore-db msrathore-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do a quick benchmarking on arca US West 2 if possible and mention the perf diff in the PR descripion for future use

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 29, 2026
@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 29, 2026
@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

Merge-queue update: the driver-test compile break is resolved (databricks-driver-test#962, merged) and the refreshed replay tapes pass for 30/31 metadata tests. The one remaining failure, get_objects_aggregated_metadata, is a replay-harness limitation, not a kernel bug: list_tables's wildcard fan-out issues per-catalog SHOW TABLES with bounded parallelism, so the SEA statement order is non-deterministic, while the SEA replay matcher is strictly positional (the Thrift/CloudFetch matchers are already order-independent for exactly this reason).

Keeping the kernel fan-out parallel is the right call — serializing it just to satisfy the harness would slow real metadata calls. So the test is replay-skipped in databricks-driver-test#967 with a follow-up to extend the harness's order-independent matching to SEA statements. Once #967 lands, this merge queue should go green.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 30, 2026
@vikrantpuppala
vikrantpuppala removed this pull request from the merge queue due to a manual request Jul 30, 2026
@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 30, 2026
@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Jul 30, 2026
Merged via the queue into main with commit f43434d Jul 30, 2026
29 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — 35c0e074 Deployed Jul 29, 2026 by oss-reviewer-bot[bot] via followup #179
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