diff --git a/napi/src/metadata.rs b/napi/src/metadata.rs index 749f2d54..cd9d32d5 100644 --- a/napi/src/metadata.rs +++ b/napi/src/metadata.rs @@ -23,9 +23,10 @@ //! involved (metadata calls don't use the mutable `Statement` path). //! //! Parameter mapping from JS strings to kernel newtypes follows the -//! pyo3 binding pattern. Empty exact catalog filters become empty -//! patterns (match nothing); other validation remains kernel-owned and -//! surfaces as `InvalidArgument` through the standard error envelope. +//! pyo3 binding pattern. Empty catalog filters for schemas and columns +//! become empty patterns (match nothing); functions pass an empty catalog +//! through to native `GetFunctions`. Other validation remains kernel-owned +//! and surfaces as `InvalidArgument` through the standard error envelope. //! //! ## `!Send` future workaround //! @@ -226,8 +227,8 @@ impl Connection { .await } - /// Functions visible to the session. `catalog` is exact; - /// `schemaPattern` and `functionPattern` are SQL LIKE. + /// Functions visible to the session. An empty `catalog` is forwarded to + /// SEA for native `GetFunctions`; `schemaPattern` and `functionPattern` are SQL LIKE. #[napi] pub async fn list_functions( &self, @@ -238,7 +239,10 @@ impl Connection { let meta = take_metadata(&self.inner).await?; guarded(async move { let stream = run_on_blocking(move || async move { - let cat = Identifier::from_opt(&catalog)?; + let cat = catalog + .as_deref() + .map(Identifier::new_allow_empty) + .transpose()?; let sch = LikePattern::from_opt(&schema_pattern)?; let nam = LikePattern::from_opt(&function_pattern)?; meta.functions(cat, sch, nam).await diff --git a/src/metadata_v0.rs b/src/metadata_v0.rs index ecb66650..56ff5928 100644 --- a/src/metadata_v0.rs +++ b/src/metadata_v0.rs @@ -347,6 +347,17 @@ impl<'a> Identifier<'a> { Ok(Self(s)) } + /// Allow `""` for metadata calls with protocol-specific empty catalog + /// semantics, such as Thrift `GetFunctions`. Nonempty names use the + /// standard identifier validation. + pub fn new_allow_empty(s: &'a str) -> Result { + if s.is_empty() { + Ok(Self(s)) + } else { + Self::new(s) + } + } + /// Construct from an optional borrowed string. /// /// Maps `None` to `Ok(None)` ("not supplied" — e.g. ODBC-style @@ -1344,8 +1355,8 @@ impl Metadata { Self::finish_metadata_call(telemetry, result) } - /// Functions visible to the session. Catalog is exact; schema / - /// name are patterns. + /// Functions visible to the session. Catalog is exact, except that + /// native `GetFunctions` accepts an empty catalog; schema / name are patterns. /// /// SQL fallback shape (`SHOW FUNCTIONS IN CATALOG ...`): /// `functionName, namespace, catalogName, remarks, functionType, @@ -1838,6 +1849,14 @@ mod tests { assert_eq!(err.code, ErrorCode::InvalidArgument); } + #[test] + fn identifier_empty_opt_out_preserves_default_validation() { + let input = Some(String::new()); + assert_eq!(Identifier::new_allow_empty("").unwrap().as_str(), ""); + assert!(Identifier::from_opt(&input).is_err()); + assert!(Identifier::new_allow_empty(" ").is_err()); + } + #[test] fn like_pattern_from_opt_none_is_ok_none() { let input: Option = None; diff --git a/tests/v0_metadata_wiremock.rs b/tests/v0_metadata_wiremock.rs index e8a8c19f..24cc2f43 100644 --- a/tests/v0_metadata_wiremock.rs +++ b/tests/v0_metadata_wiremock.rs @@ -469,13 +469,25 @@ async fn native_columns_zero_based_ordinal_is_one_based() { #[tokio::test] async fn native_functions_header_and_result_match_jdbc() { - for (catalog, expected_sql, expected_catalog) in [ - (None, "SHOW FUNCTIONS IN CATALOG `main` LIKE '*'", None), + for (catalog, name_pattern, expected_sql, expected_catalog) in [ + ( + None, + None, + "SHOW FUNCTIONS IN CATALOG `main` LIKE '*'", + None, + ), ( Some("main"), + None, "SHOW FUNCTIONS IN CATALOG `main` LIKE '*'", Some("main"), ), + ( + Some(""), + Some("answer"), + "SHOW FUNCTIONS IN CATALOG `` LIKE 'answer'", + Some(""), + ), ] { let server = MockServer::start().await; mount_create_session(&server).await; @@ -522,10 +534,11 @@ async fn native_functions_header_and_result_match_jdbc() { .await; let session = open_session(&server).await; - let catalog = catalog.map(|catalog| Identifier::new(catalog).unwrap()); + let catalog = catalog.map(|catalog| Identifier::new_allow_empty(catalog).unwrap()); + let name_pattern = name_pattern.map(|pattern| LikePattern::new(pattern).unwrap()); let mut stream = session .metadata() - .functions(catalog, None, None) + .functions(catalog, None, name_pattern) .await .expect("native list_functions"); assert_eq!( @@ -541,6 +554,15 @@ async fn native_functions_header_and_result_match_jdbc() { ); let batches = drain_stream(&mut stream).await; + assert_eq!(batches[0].num_rows(), 1); + assert_eq!( + batches[0] + .column_by_name("FUNCTION_NAME") + .unwrap() + .as_string::() + .value(0), + "answer" + ); let function_catalog = batches[0] .column_by_name("FUNCTION_CAT") .unwrap()