Skip to content

Commit cf8bfd3

Browse files
refactor(c_abi): simplify table_types empty-CSV handling post-#201 (#223)
## Summary Follow-up cleanup for **databricks/databricks-odbc#135**, unblocked now that #201 (fix for #187) has merged. `kernel_metadata_list_tables` (`src/c_abi/metadata.rs`) collapsed an empty / whitespace-only / comma-only `table_types` CSV to `None` **specifically to dodge the pre-#187 kernel bug** where `Some(&[])` matched *nothing*. Its inline comment said so. Since #201, `list_tables` / `reshape_tables` treat `Some(&[])` the same as `None` (match-all), so: - The comment was **stale/misleading** — it still claimed the kernel rejects every row on `Some(&[])`. - The `if v.is_empty() { None } else { Some(v) }` collapse is no longer needed for correctness. ## Changes - **Rewrote the stale comment** to describe post-#201 match-all behavior. - **Extracted** the CSV split into a pure `split_table_types_csv` helper and **dropped 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 empty entries are still dropped, the result is just `Some(&[])` instead of `None`, and those are now equivalent. - **Added a unit test** locking in the empty/junk → all-types normalization and non-empty CSV parsing. ## Behavior No `SQLTables` behavior change; comparator remains clean (`Some(&[])` ≡ `None` post-#201). This is purely internal cleanup so the now-stale rationale doesn't mislead a future reader. ## Testing - `cargo clippy --all-targets` clean - Full suite green (856 passed, 0 failed), including the new `split_table_types_csv_normalizes_empty_and_parses_values` test ## Related - Addresses databricks/databricks-odbc#135. Note: that issue also needs the ODBC repo's kernel pin (`KERNEL_REPO_TAG`) bumped past this PR before the cleanup is live in the shipped driver — that pin bump is intentionally **out of scope here** and left for a routine kernel-pin refresh, so the ODBC issue stays open after this merges. - Follows databricks-sql-kernel#201 (fixes #187) Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
1 parent 309e4c5 commit cf8bfd3

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

‎src/c_abi/metadata.rs‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ pub unsafe extern "C" fn kernel_metadata_list_schemas(
173173
})
174174
}
175175

176+
/// Split a comma-separated `table_types` CSV into a borrowed slice of
177+
/// non-empty, trimmed type names.
178+
///
179+
/// 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.
186+
fn split_table_types_csv(csv: &str) -> Vec<&str> {
187+
csv.split(',')
188+
.map(str::trim)
189+
.filter(|s| !s.is_empty())
190+
.collect()
191+
}
192+
176193
/// List tables filtered by catalog (**pattern**), schema / table
177194
/// patterns, and an optional comma-separated `table_types` filter.
178195
///
@@ -209,24 +226,8 @@ pub unsafe extern "C" fn kernel_metadata_list_tables(
209226
let catalog = opt_pattern(catalog, "catalog")?;
210227
let schema_pattern = opt_pattern(schema_pattern, "schema_pattern")?;
211228
let table_pattern = opt_pattern(table_pattern, "table_pattern")?;
212-
// Split the comma-separated table types into a borrowed slice.
213-
// An empty / whitespace-only / comma-only CSV must mean
214-
// "all types" (like a NULL arg), NOT "match nothing": the
215-
// kernel treats Some(&[]) as a filter that rejects every row,
216-
// so collapse an empty result back to None.
217229
let types_csv = opt_str(table_types_csv, "table_types")?;
218-
let types_owned: Option<Vec<&str>> = types_csv.and_then(|csv| {
219-
let v: Vec<&str> = csv
220-
.split(',')
221-
.map(str::trim)
222-
.filter(|s| !s.is_empty())
223-
.collect();
224-
if v.is_empty() {
225-
None
226-
} else {
227-
Some(v)
228-
}
229-
});
230+
let types_owned = types_csv.map(split_table_types_csv);
230231
session
231232
.metadata()
232233
.list_tables(
@@ -410,4 +411,26 @@ mod tests {
410411
assert!(opt_ident(real.as_ptr(), "catalog").unwrap().is_some());
411412
}
412413
}
414+
415+
/// ODBC's "" / whitespace / comma-only `table_types` spellings all
416+
/// normalize to an empty type list, which `list_tables` treats as
417+
/// "all types" (match-all, like a NULL arg) since #187/#201. A
418+
/// non-empty CSV parses into its trimmed, non-empty entries.
419+
#[test]
420+
fn split_table_types_csv_normalizes_empty_and_parses_values() {
421+
// Empty / whitespace / comma-only junk → empty list (all types).
422+
assert!(split_table_types_csv("").is_empty());
423+
assert!(split_table_types_csv(" ").is_empty());
424+
assert!(split_table_types_csv(",").is_empty());
425+
assert!(split_table_types_csv(",,").is_empty());
426+
assert!(split_table_types_csv(" , , ").is_empty());
427+
428+
// Real values are trimmed; interspersed empties are dropped.
429+
assert_eq!(split_table_types_csv("TABLE"), vec!["TABLE"]);
430+
assert_eq!(split_table_types_csv("TABLE, VIEW"), vec!["TABLE", "VIEW"]);
431+
assert_eq!(
432+
split_table_types_csv(" TABLE ,, VIEW ,"),
433+
vec!["TABLE", "VIEW"]
434+
);
435+
}
413436
}

0 commit comments

Comments
 (0)