From be910c9cdcb9ff1d86d2d45e628a7fa0c654349e Mon Sep 17 00:00:00 2001 From: eric-wang-1990 Date: Thu, 13 Aug 2026 06:33:27 +0000 Subject: [PATCH] fix(metadata): empty table_types slice matches NONE, not all (hiveserver2 #83) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `reshape_tables` special-cased `Some(&[])` (explicit empty table_types slice) as match-ALL, equivalent to `None` — added in #201 (fix for #187) and relied on by #223's C-ABI cleanup. That premise was reversed by adbc-drivers/hiveserver2 #83: an empty type filter matches NOTHING (zero rows), matching databricks-jdbc's long-standing `listTables` short-circuit, and driver-test's METADATA-035 conformance test was flipped to expect match-none on 2026-08-06. The kernel driver landed the opposite behavior on 2026-08-10, so the live nightly (get_tables_empty_table_types_filter_matches_none, both normal and reyden legs) went red: the test demands 0 rows, the driver returned all rows. Realign the driver with the canonical (#83) contract: - 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. Only `None` now means match-all. Rewrote the inline + doc comments (they cited the reversed #72 reading). - thrift_shape.rs 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. ODBC passes "" / whitespace / comma-only junk for "no type filter" (match-all); now that `Some(&[])` means match-none again on the Rust surface, the C-ABI boundary must collapse an empty parse back to `None` to preserve ODBC's empty→"all types" convention. Keeps #223's `split_table_types_csv` helper; restores the pre-#223 guard via `.filter(|t| !t.is_empty())`. The napi/pyo3 typed surfaces intentionally pass `Some(&[])` through (match-none), consistent with the Rust API. Validated: 766 lib unit tests pass; fmt + clippy clean; the live E2E get_tables_empty_table_types_filter_matches_none passes against a real warehouse (was red in the nightly). Reopens/supersedes #187 (its match-all premise is overturned by #83). Co-authored-by: Isaac Signed-off-by: eric-wang-1990 --- src/c_abi/metadata.rs | 70 ++++++++++++++++++++++++++++++------ src/metadata/thrift_shape.rs | 36 ++++++++++--------- 2 files changed, 80 insertions(+), 26 deletions(-) diff --git a/src/c_abi/metadata.rs b/src/c_abi/metadata.rs index 75742a8a..64a017ae 100644 --- a/src/c_abi/metadata.rs +++ b/src/c_abi/metadata.rs @@ -177,12 +177,13 @@ pub unsafe extern "C" fn kernel_metadata_list_schemas( /// non-empty, trimmed type names. /// /// Empty entries are dropped, so ODBC's "" / whitespace / comma-only -/// junk (e.g. `""`, `" "`, `",,"`) normalizes to an empty `Vec`. An -/// empty `Vec` means "all types" (like a NULL arg): the kernel treats -/// `Some(&[])` the same as `None` (match-all) since #187/#201, so an -/// empty result needs no collapse to `None` to avoid a match-nothing -/// bug — the ODBC-specific empty→"all types" convention is preserved -/// either way. +/// junk (e.g. `""`, `" "`, `",,"`) normalizes to an empty `Vec`. The +/// caller MUST collapse an empty `Vec` to `None` before calling +/// `list_tables`: since hiveserver2 #83 the kernel treats an explicit +/// `Some(&[])` as match-NONE (zero rows), so passing the empty vec +/// straight through would turn ODBC's empty→"all types" convention +/// into a match-nothing bug. See the `.filter(|t| !t.is_empty())` at +/// the call site. fn split_table_types_csv(csv: &str) -> Vec<&str> { csv.split(',') .map(str::trim) @@ -190,6 +191,27 @@ fn split_table_types_csv(csv: &str) -> Vec<&str> { .collect() } +/// Map an optional ODBC `table_types` CSV to the kernel's typed filter: +/// `Some(non-empty types)` to restrict, or `None` to match all types. +/// +/// ODBC's `SQLTables` treats an empty `TableType` argument as "type not +/// used to restrict the search" — i.e. all types — and has no way to +/// express a non-null *empty* type list distinctly (which is why +/// driver-test's METADATA-035 skips ODBC). So a `None` CSV (null arg) OR +/// a CSV that splits to zero real types (`""` / `" "` / `",,"`) both +/// mean match-all → `None`. +/// +/// This collapse is load-bearing: since hiveserver2 #83 the kernel treats +/// an explicit `Some(&[])` as match-NONE (zero rows), so passing the empty +/// vec straight through would turn ODBC's empty→"all types" convention +/// into a match-nothing bug. (It restores the guard #223 removed — safe to +/// drop only while `Some(&[])` == `None`, which #83 reversed.) Only a CSV +/// with at least one real type reaches `list_tables` as a `Some`. +fn parse_table_types_filter(csv: Option<&str>) -> Option> { + csv.map(split_table_types_csv) + .filter(|types| !types.is_empty()) +} + /// List tables filtered by catalog (**pattern**), schema / table /// patterns, and an optional comma-separated `table_types` filter. /// @@ -227,7 +249,7 @@ pub unsafe extern "C" fn kernel_metadata_list_tables( let schema_pattern = opt_pattern(schema_pattern, "schema_pattern")?; let table_pattern = opt_pattern(table_pattern, "table_pattern")?; let types_csv = opt_str(table_types_csv, "table_types")?; - let types_owned = types_csv.map(split_table_types_csv); + let types_owned = parse_table_types_filter(types_csv); session .metadata() .list_tables( @@ -504,9 +526,10 @@ mod tests { } /// ODBC's "" / whitespace / comma-only `table_types` spellings all - /// normalize to an empty type list, which `list_tables` treats as - /// "all types" (match-all, like a NULL arg) since #187/#201. A - /// non-empty CSV parses into its trimmed, non-empty entries. + /// normalize to an empty type list. The `list_tables` call site then + /// collapses that empty list to `None` (match-all, like a NULL arg), + /// since after hiveserver2 #83 an explicit `Some(&[])` means match-NONE. + /// A non-empty CSV parses into its trimmed, non-empty entries. #[test] fn split_table_types_csv_normalizes_empty_and_parses_values() { // Empty / whitespace / comma-only junk → empty list (all types). @@ -524,4 +547,31 @@ mod tests { vec!["TABLE", "VIEW"] ); } + + /// The empty→`None` collapse at the C-ABI boundary: a null arg AND any + /// CSV that splits to zero real types both map to `None` (ODBC + /// empty→"all types"), while a CSV with real types maps to `Some`. + /// Locks the guard so it isn't dropped again — after hiveserver2 #83 a + /// `Some(&[])` reaching the kernel means match-NONE, so an empty ODBC + /// `TableType` must NOT reach `list_tables` as `Some(&[])`. + #[test] + fn parse_table_types_filter_collapses_empty_to_none() { + // Null arg (ODBC NULL TableType) → None (all types). + assert_eq!(parse_table_types_filter(None), None); + + // Empty / whitespace / comma-only junk → None (all types), NOT + // Some(&[]) (which the kernel would treat as match-none). + assert_eq!(parse_table_types_filter(Some("")), None); + assert_eq!(parse_table_types_filter(Some(" ")), None); + assert_eq!(parse_table_types_filter(Some(",")), None); + assert_eq!(parse_table_types_filter(Some(",,")), None); + assert_eq!(parse_table_types_filter(Some(" , , ")), None); + + // At least one real type → Some(trimmed, non-empty entries). + assert_eq!(parse_table_types_filter(Some("TABLE")), Some(vec!["TABLE"])); + assert_eq!( + parse_table_types_filter(Some(" TABLE ,, VIEW ,")), + Some(vec!["TABLE", "VIEW"]) + ); + } } diff --git a/src/metadata/thrift_shape.rs b/src/metadata/thrift_shape.rs index 50aab462..ed9c4532 100644 --- a/src/metadata/thrift_shape.rs +++ b/src/metadata/thrift_shape.rs @@ -543,9 +543,10 @@ pub(crate) fn reshape_schemas( /// that omit it (single-catalog form). `table_types`, when `Some` with /// at least one entry, filters rows to those whose `TABLE_TYPE` appears /// in the slice (matches JDBC `DatabaseMetaData.getTables` behaviour). -/// A `Some(&[])` (explicit empty slice) is treated like `None` and -/// matches all table types — see the inline note in the filter block -/// below (METADATA-035 conformance / C# ADBC parity). +/// A `Some(&[])` (explicit empty slice) matches NO table types and +/// yields an empty result — it is NOT treated like `None`. See the +/// inline note in the filter block below (METADATA-035 conformance / +/// hiveserver2 #83, which reversed the earlier empty==null reading). /// /// Catalog *matching* is not done here: a wildcard catalog is resolved /// in `list_tables` by enumerating catalogs and issuing one scoped @@ -599,13 +600,16 @@ pub(crate) fn reshape_tables( // getTables(...)` Javadoc specifies that `null` types returns all // types, but is silent on what an *explicit empty array* means. // Per the multi-language conformance spec (METADATA-035) and the C# - // ADBC driver, an explicit *empty* array is equivalent to `null`: - // it matches ALL table types, not none. Only a `Some` with at least - // one entry actually restricts the result set. This mirrors JDBC - // #1570 / hiveserver2 #72, which align the same way. + // ADBC / JDBC drivers, an explicit *empty* array matches NO table + // types — the result is empty, NOT equivalent to `null`. Only `None` + // means "all types". This mirrors hiveserver2 #83, which reversed the + // earlier #72 reading (empty == null) after establishing that + // databricks-jdbc's `listTables` has always short-circuited an empty + // array to "return nothing". An empty `Some(&[])` therefore flows + // through `filter_tables_by_type`, whose `types.iter().any(..)` is + // `false` for every row → an empty (correctly-typed) batch. let filtered = match table_types { None => reshaped, - Some([]) => reshaped, Some(types) => filter_tables_by_type(&reshaped, types)?, }; // Sort to match the JDBC driver's `getTables` row order @@ -1338,21 +1342,21 @@ mod tests { } #[test] - fn tables_empty_types_matches_all() { - // Per the JDBC/ADBC contract (spec METADATA-035), an explicit - // empty `table_types` slice is equivalent to `None`: it matches - // ALL table types, not none. This is the same result as the - // `None` path — see `tables_none_types_passes_all`. + fn tables_empty_types_matches_none() { + // Per the JDBC/ADBC contract (spec METADATA-035 / hiveserver2 #83), + // an explicit empty `table_types` slice matches NO table types — + // the result is empty, NOT equivalent to `None`. Contrast with + // `tables_none_types_passes_all`, where `None` returns all rows. let b = server_tables_full(); let out = reshape_tables(&b, Some("main"), Some(&[])).unwrap(); - assert_eq!(out.num_rows(), 3); + assert_eq!(out.num_rows(), 0); } #[test] fn tables_none_types_passes_all() { // The all-types path — distinct from the explicit-empty path - // above. Locks the contract so future refactors don't quietly - // re-merge the two. + // above (which now matches none). Locks the contract so future + // refactors don't quietly re-merge the two. let b = server_tables_full(); let out = reshape_tables(&b, Some("main"), None).unwrap(); assert_eq!(out.num_rows(), 3);