Skip to content

Commit a79b8cf

Browse files
fix(metadata): empty table_types slice matches NONE, not all (hiveserver2 #83) (#246)
## What Fixes the kernel nightly (red on **both** the normal and reyden legs): the live E2E test `get_tables_empty_table_types_filter_matches_none` demanded zero rows for an empty `table_types` filter, but the driver returned all rows. Realigns `Metadata::list_tables` / `reshape_tables` with the canonical cross-driver contract from **adbc-drivers/hiveserver2 #83**: an **explicit empty** `table_types` slice (`Some(&[])`) matches **NONE** (zero rows), not all. Only `None` means "all types". ## Why this regressed Two changes landed with opposite readings of the same spec: | Date | Change | Empty `table_types` | |------|--------|--------------------| | 2026-08-06 | driver-test #1121 (per hiveserver2 #83) flipped METADATA-035 | → match **NONE** | | 2026-08-10 | kernel #201 (fix for #187) | → match **ALL** | #201 implemented #187's "empty should match all like null" premise — but #83 had **already reversed** that premise 4 days earlier (establishing that databricks-jdbc's `listTables` has always short-circuited an empty array to "return nothing", making the earlier #72 reading wrong). Driver and conformance test ended up on opposite sides. Replay masked it (recording predates the split); only the live nightly caught it. ## The two surfaces (why both files change) The typed API and the ODBC C-ABI have **different** empty-type contracts, and they're coupled: - **Typed Rust API** (`Option<&[&str]>`, also napi/pyo3) follows JDBC/ADBC: `None` = all, `Some(&[])` = **none**. - **ODBC C-ABI** (`SQLTables` CSV) follows the ODBC spec: an empty `TableType` means "type not used to restrict the search" = **all types**. ODBC has no way to express a non-null empty list (which is why driver-test's METADATA-035 is `skipped` for ODBC). Source: [Microsoft ODBC `SQLTables`](https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqltables-function) — *"If TableType is not an empty string, it must contain a list of comma-separated values…"*. Because the C-ABI previously relied on `Some(&[]) == None == match-all` (true post-#201), flipping the typed API to match-none would make an ODBC empty `TableType` return **zero tables** — a regression. So the C-ABI must collapse an empty CSV back to `None`. **Both changes are required together**; shipping only the kernel-typed fix would trade the nightly failure for an ODBC `SQLTables` regression. ## Changes - **`thrift_shape.rs`:** drop the `Some([]) => reshaped` arm so an empty slice flows through `filter_tables_by_type` (whose `types.iter().any(..)` is `false` for every row → a correctly-typed empty batch). Rewrote inline + doc comments (they cited the reversed #72 reading). Flipped unit test `tables_empty_types_matches_all` → `tables_empty_types_matches_none` (asserts 0 rows). - **`c_abi/metadata.rs`:** re-add the empty-CSV → `None` collapse that **#223** removed, extracted into a testable `parse_table_types_filter` helper (keeps #223's `split_table_types_csv` split helper). Added unit test `parse_table_types_filter_collapses_empty_to_none` locking: null arg AND `""`/`" "`/`",,"` → `None` (match-all); a CSV with real types → `Some`. The napi/pyo3 typed surfaces intentionally pass `Some(&[])` through (match-none) — no change there. ## Testing - `cargo test --lib`: **767 passed, 0 failed** (incl. flipped `tables_empty_types_matches_none` and new `parse_table_types_filter_collapses_empty_to_none`). - `cargo fmt --check` + `cargo clippy --lib`: clean. - **Live E2E**: `get_tables_empty_table_types_filter_matches_none` (the nightly-red test) **passes** against a real warehouse via `DATABRICKS_KERNEL_PATH` → this branch, `--proxy-mode passthrough`. ## Related - Reverses the driver behavior from #201 and the C-ABI cleanup from #223 — both built on the match-all premise hiveserver2 #83 overturned. - Supersedes #187 (its "empty == match-all" premise is overturned by #83); should be reopened/closed-as-superseded. - Downstream: databricks-odbc's `KERNEL_REPO_TAG` pin picks this up on the next routine bump; ODBC `SQLTables` behavior is unchanged (empty CSV still → match-all via the restored collapse). This pull request and its description were written by Isaac. Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
1 parent 3e56482 commit a79b8cf

2 files changed

Lines changed: 80 additions & 26 deletions

File tree

‎src/c_abi/metadata.rs‎

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,41 @@ pub unsafe extern "C" fn kernel_metadata_list_schemas(
177177
/// non-empty, trimmed type names.
178178
///
179179
/// Empty entries are dropped, so ODBC's "" / whitespace / comma-only
180-
/// junk (e.g. `""`, `" "`, `",,"`) normalizes to an empty `Vec`. An
181-
/// empty `Vec` means "all types" (like a NULL arg): the kernel treats
182-
/// `Some(&[])` the same as `None` (match-all) since #187/#201, so an
183-
/// empty result needs no collapse to `None` to avoid a match-nothing
184-
/// bug — the ODBC-specific empty→"all types" convention is preserved
185-
/// either way.
180+
/// junk (e.g. `""`, `" "`, `",,"`) normalizes to an empty `Vec`. The
181+
/// caller MUST collapse an empty `Vec` to `None` before calling
182+
/// `list_tables`: since hiveserver2 #83 the kernel treats an explicit
183+
/// `Some(&[])` as match-NONE (zero rows), so passing the empty vec
184+
/// straight through would turn ODBC's empty→"all types" convention
185+
/// into a match-nothing bug. See the `.filter(|t| !t.is_empty())` at
186+
/// the call site.
186187
fn split_table_types_csv(csv: &str) -> Vec<&str> {
187188
csv.split(',')
188189
.map(str::trim)
189190
.filter(|s| !s.is_empty())
190191
.collect()
191192
}
192193

194+
/// Map an optional ODBC `table_types` CSV to the kernel's typed filter:
195+
/// `Some(non-empty types)` to restrict, or `None` to match all types.
196+
///
197+
/// ODBC's `SQLTables` treats an empty `TableType` argument as "type not
198+
/// used to restrict the search" — i.e. all types — and has no way to
199+
/// express a non-null *empty* type list distinctly (which is why
200+
/// driver-test's METADATA-035 skips ODBC). So a `None` CSV (null arg) OR
201+
/// a CSV that splits to zero real types (`""` / `" "` / `",,"`) both
202+
/// mean match-all → `None`.
203+
///
204+
/// This collapse is load-bearing: since hiveserver2 #83 the kernel treats
205+
/// an explicit `Some(&[])` as match-NONE (zero rows), so passing the empty
206+
/// vec straight through would turn ODBC's empty→"all types" convention
207+
/// into a match-nothing bug. (It restores the guard #223 removed — safe to
208+
/// drop only while `Some(&[])` == `None`, which #83 reversed.) Only a CSV
209+
/// with at least one real type reaches `list_tables` as a `Some`.
210+
fn parse_table_types_filter(csv: Option<&str>) -> Option<Vec<&str>> {
211+
csv.map(split_table_types_csv)
212+
.filter(|types| !types.is_empty())
213+
}
214+
193215
/// List tables filtered by catalog (**pattern**), schema / table
194216
/// patterns, and an optional comma-separated `table_types` filter.
195217
///
@@ -227,7 +249,7 @@ pub unsafe extern "C" fn kernel_metadata_list_tables(
227249
let schema_pattern = opt_pattern(schema_pattern, "schema_pattern")?;
228250
let table_pattern = opt_pattern(table_pattern, "table_pattern")?;
229251
let types_csv = opt_str(table_types_csv, "table_types")?;
230-
let types_owned = types_csv.map(split_table_types_csv);
252+
let types_owned = parse_table_types_filter(types_csv);
231253
session
232254
.metadata()
233255
.list_tables(
@@ -504,9 +526,10 @@ mod tests {
504526
}
505527

506528
/// ODBC's "" / whitespace / comma-only `table_types` spellings all
507-
/// normalize to an empty type list, which `list_tables` treats as
508-
/// "all types" (match-all, like a NULL arg) since #187/#201. A
509-
/// non-empty CSV parses into its trimmed, non-empty entries.
529+
/// normalize to an empty type list. The `list_tables` call site then
530+
/// collapses that empty list to `None` (match-all, like a NULL arg),
531+
/// since after hiveserver2 #83 an explicit `Some(&[])` means match-NONE.
532+
/// A non-empty CSV parses into its trimmed, non-empty entries.
510533
#[test]
511534
fn split_table_types_csv_normalizes_empty_and_parses_values() {
512535
// Empty / whitespace / comma-only junk → empty list (all types).
@@ -524,4 +547,31 @@ mod tests {
524547
vec!["TABLE", "VIEW"]
525548
);
526549
}
550+
551+
/// The empty→`None` collapse at the C-ABI boundary: a null arg AND any
552+
/// CSV that splits to zero real types both map to `None` (ODBC
553+
/// empty→"all types"), while a CSV with real types maps to `Some`.
554+
/// Locks the guard so it isn't dropped again — after hiveserver2 #83 a
555+
/// `Some(&[])` reaching the kernel means match-NONE, so an empty ODBC
556+
/// `TableType` must NOT reach `list_tables` as `Some(&[])`.
557+
#[test]
558+
fn parse_table_types_filter_collapses_empty_to_none() {
559+
// Null arg (ODBC NULL TableType) → None (all types).
560+
assert_eq!(parse_table_types_filter(None), None);
561+
562+
// Empty / whitespace / comma-only junk → None (all types), NOT
563+
// Some(&[]) (which the kernel would treat as match-none).
564+
assert_eq!(parse_table_types_filter(Some("")), None);
565+
assert_eq!(parse_table_types_filter(Some(" ")), None);
566+
assert_eq!(parse_table_types_filter(Some(",")), None);
567+
assert_eq!(parse_table_types_filter(Some(",,")), None);
568+
assert_eq!(parse_table_types_filter(Some(" , , ")), None);
569+
570+
// At least one real type → Some(trimmed, non-empty entries).
571+
assert_eq!(parse_table_types_filter(Some("TABLE")), Some(vec!["TABLE"]));
572+
assert_eq!(
573+
parse_table_types_filter(Some(" TABLE ,, VIEW ,")),
574+
Some(vec!["TABLE", "VIEW"])
575+
);
576+
}
527577
}

‎src/metadata/thrift_shape.rs‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,10 @@ pub(crate) fn reshape_schemas(
543543
/// that omit it (single-catalog form). `table_types`, when `Some` with
544544
/// at least one entry, filters rows to those whose `TABLE_TYPE` appears
545545
/// in the slice (matches JDBC `DatabaseMetaData.getTables` behaviour).
546-
/// A `Some(&[])` (explicit empty slice) is treated like `None` and
547-
/// matches all table types — see the inline note in the filter block
548-
/// below (METADATA-035 conformance / C# ADBC parity).
546+
/// A `Some(&[])` (explicit empty slice) matches NO table types and
547+
/// yields an empty result — it is NOT treated like `None`. See the
548+
/// inline note in the filter block below (METADATA-035 conformance /
549+
/// hiveserver2 #83, which reversed the earlier empty==null reading).
549550
///
550551
/// Catalog *matching* is not done here: a wildcard catalog is resolved
551552
/// in `list_tables` by enumerating catalogs and issuing one scoped
@@ -599,13 +600,16 @@ pub(crate) fn reshape_tables(
599600
// getTables(...)` Javadoc specifies that `null` types returns all
600601
// types, but is silent on what an *explicit empty array* means.
601602
// Per the multi-language conformance spec (METADATA-035) and the C#
602-
// ADBC driver, an explicit *empty* array is equivalent to `null`:
603-
// it matches ALL table types, not none. Only a `Some` with at least
604-
// one entry actually restricts the result set. This mirrors JDBC
605-
// #1570 / hiveserver2 #72, which align the same way.
603+
// ADBC / JDBC drivers, an explicit *empty* array matches NO table
604+
// types — the result is empty, NOT equivalent to `null`. Only `None`
605+
// means "all types". This mirrors hiveserver2 #83, which reversed the
606+
// earlier #72 reading (empty == null) after establishing that
607+
// databricks-jdbc's `listTables` has always short-circuited an empty
608+
// array to "return nothing". An empty `Some(&[])` therefore flows
609+
// through `filter_tables_by_type`, whose `types.iter().any(..)` is
610+
// `false` for every row → an empty (correctly-typed) batch.
606611
let filtered = match table_types {
607612
None => reshaped,
608-
Some([]) => reshaped,
609613
Some(types) => filter_tables_by_type(&reshaped, types)?,
610614
};
611615
// Sort to match the JDBC driver's `getTables` row order
@@ -1338,21 +1342,21 @@ mod tests {
13381342
}
13391343

13401344
#[test]
1341-
fn tables_empty_types_matches_all() {
1342-
// Per the JDBC/ADBC contract (spec METADATA-035), an explicit
1343-
// empty `table_types` slice is equivalent to `None`: it matches
1344-
// ALL table types, not none. This is the same result as the
1345-
// `None` path — see `tables_none_types_passes_all`.
1345+
fn tables_empty_types_matches_none() {
1346+
// Per the JDBC/ADBC contract (spec METADATA-035 / hiveserver2 #83),
1347+
// an explicit empty `table_types` slice matches NO table types —
1348+
// the result is empty, NOT equivalent to `None`. Contrast with
1349+
// `tables_none_types_passes_all`, where `None` returns all rows.
13461350
let b = server_tables_full();
13471351
let out = reshape_tables(&b, Some("main"), Some(&[])).unwrap();
1348-
assert_eq!(out.num_rows(), 3);
1352+
assert_eq!(out.num_rows(), 0);
13491353
}
13501354

13511355
#[test]
13521356
fn tables_none_types_passes_all() {
13531357
// The all-types path — distinct from the explicit-empty path
1354-
// above. Locks the contract so future refactors don't quietly
1355-
// re-merge the two.
1358+
// above (which now matches none). Locks the contract so future
1359+
// refactors don't quietly re-merge the two.
13561360
let b = server_tables_full();
13571361
let out = reshape_tables(&b, Some("main"), None).unwrap();
13581362
assert_eq!(out.num_rows(), 3);

0 commit comments

Comments
 (0)