Skip to content

Commit be910c9

Browse files
fix(metadata): empty table_types slice matches NONE, not all (hiveserver2 #83)
`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 <e.wang@databricks.com>
1 parent 3ea1c3b commit be910c9

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)