Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions src/c_abi/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,41 @@ 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)
.filter(|s| !s.is_empty())
.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<Vec<&str>> {
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.
///
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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).
Expand All @@ -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"])
);
}
}
36 changes: 20 additions & 16 deletions src/metadata/thrift_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading