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
16 changes: 10 additions & 6 deletions napi/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down Expand Up @@ -226,8 +227,8 @@ impl Connection {
.await
}

/// Functions visible to the session. `catalog` is exact;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: keep this comment

/// `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,
Expand All @@ -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
Comment thread
oss-reviewer-bot[bot] marked this conversation as resolved.
Resolved
.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
Expand Down
23 changes: 21 additions & 2 deletions src/metadata_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> = None;
Expand Down
30 changes: 26 additions & 4 deletions tests/v0_metadata_wiremock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!(
Expand All @@ -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::<i32>()
.value(0),
"answer"
);
let function_catalog = batches[0]
.column_by_name("FUNCTION_CAT")
.unwrap()
Expand Down
Loading