From 78cecbc5042ffd929c6bc857eeaae16aa58f510b Mon Sep 17 00:00:00 2001 From: Vikrant Puppala Date: Mon, 10 Aug 2026 05:54:03 +0000 Subject: [PATCH] refactor(c_abi): simplify table_types empty-CSV handling post-#201 The empty/whitespace/comma-only table_types CSV collapse in kernel_metadata_list_tables existed to dodge the pre-#187 kernel bug where Some(&[]) matched nothing. Since #201 merged, list_tables / reshape_tables treat Some(&[]) the same as None (match-all), so the collapse-to-None is no longer needed for correctness. - Rewrite the stale comment that claimed the kernel rejects every row on Some(&[]); it now describes post-#201 match-all behavior. - Extract the CSV split into a pure split_table_types_csv helper and drop the empty->None collapse: an empty parse now yields Some(vec![]), which is match-all. ODBC's ""/whitespace/comma-only -> all-types convention is preserved (the empties are still dropped). - Add a unit test locking in the empty/junk -> all-types normalization and non-empty CSV parsing. No SQLTables behavior change; comparator remains clean. Closes databricks/databricks-odbc#135 Signed-off-by: Vikrant Puppala --- src/c_abi/metadata.rs | 57 ++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/c_abi/metadata.rs b/src/c_abi/metadata.rs index 7030359e..920283da 100644 --- a/src/c_abi/metadata.rs +++ b/src/c_abi/metadata.rs @@ -173,6 +173,23 @@ pub unsafe extern "C" fn kernel_metadata_list_schemas( }) } +/// Split a comma-separated `table_types` CSV into a borrowed slice of +/// 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. +fn split_table_types_csv(csv: &str) -> Vec<&str> { + csv.split(',') + .map(str::trim) + .filter(|s| !s.is_empty()) + .collect() +} + /// List tables filtered by catalog (**pattern**), schema / table /// patterns, and an optional comma-separated `table_types` filter. /// @@ -209,24 +226,8 @@ pub unsafe extern "C" fn kernel_metadata_list_tables( let catalog = opt_pattern(catalog, "catalog")?; let schema_pattern = opt_pattern(schema_pattern, "schema_pattern")?; let table_pattern = opt_pattern(table_pattern, "table_pattern")?; - // Split the comma-separated table types into a borrowed slice. - // An empty / whitespace-only / comma-only CSV must mean - // "all types" (like a NULL arg), NOT "match nothing": the - // kernel treats Some(&[]) as a filter that rejects every row, - // so collapse an empty result back to None. let types_csv = opt_str(table_types_csv, "table_types")?; - let types_owned: Option> = types_csv.and_then(|csv| { - let v: Vec<&str> = csv - .split(',') - .map(str::trim) - .filter(|s| !s.is_empty()) - .collect(); - if v.is_empty() { - None - } else { - Some(v) - } - }); + let types_owned = types_csv.map(split_table_types_csv); session .metadata() .list_tables( @@ -410,4 +411,26 @@ mod tests { assert!(opt_ident(real.as_ptr(), "catalog").unwrap().is_some()); } } + + /// 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. + #[test] + fn split_table_types_csv_normalizes_empty_and_parses_values() { + // Empty / whitespace / comma-only junk → empty list (all types). + assert!(split_table_types_csv("").is_empty()); + assert!(split_table_types_csv(" ").is_empty()); + assert!(split_table_types_csv(",").is_empty()); + assert!(split_table_types_csv(",,").is_empty()); + assert!(split_table_types_csv(" , , ").is_empty()); + + // Real values are trimmed; interspersed empties are dropped. + assert_eq!(split_table_types_csv("TABLE"), vec!["TABLE"]); + assert_eq!(split_table_types_csv("TABLE, VIEW"), vec!["TABLE", "VIEW"]); + assert_eq!( + split_table_types_csv(" TABLE ,, VIEW ,"), + vec!["TABLE", "VIEW"] + ); + } }