From 00ec994bc912643177b9ea1317fa880eece2b53d Mon Sep 17 00:00:00 2001 From: cat23123 <58714163+cat23123@users.noreply.github.com> Date: Wed, 26 Aug 2026 00:18:12 +0000 Subject: [PATCH 1/2] fix(napi): preserve qualified interval parameters Signed-off-by: cat23123 <58714163+cat23123@users.noreply.github.com> --- napi/Cargo.lock | 129 +++++++++++++++++++++++++++++++++++ napi/Cargo.toml | 3 + napi/NEXT_CHANGELOG.md | 4 ++ napi/index.d.ts | 19 ++++++ napi/src/connection.rs | 151 ++++++++++++++++++++++++++++++++++++++++- napi/src/params.rs | 16 +++++ 6 files changed, 321 insertions(+), 1 deletion(-) diff --git a/napi/Cargo.lock b/napi/Cargo.lock index 10e93ddc..676c2717 100644 --- a/napi/Cargo.lock +++ b/napi/Cargo.lock @@ -177,6 +177,16 @@ dependencies = [ "num-traits", ] +[[package]] +name = "assert-json-diff" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "async-compression" version = "0.4.42" @@ -596,8 +606,27 @@ dependencies = [ "tokio", "tracing", "tracing-subscriber", + "wiremock", +] + +[[package]] +name = "deadpool" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0be2b1d1d6ec8d846f05e137292d0b89133caf95ef33695424c09568bdd39b1b" +dependencies = [ + "deadpool-runtime", + "lazy_static", + "num_cpus", + "tokio", ] +[[package]] +name = "deadpool-runtime" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "092966b41edc516079bdf31ec78a2e0588d1d0c08f78b91d8307215928642b2b" + [[package]] name = "der" version = "0.7.10" @@ -670,6 +699,12 @@ dependencies = [ "syn", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "errno" version = "0.3.14" @@ -706,6 +741,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -852,6 +893,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "h2" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb093c84e8bd9b188d4c4a8cb6579fc016968d14c99882163cd3ff402a4f155" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "2.7.1" @@ -876,6 +936,12 @@ version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + [[package]] name = "hex" version = "0.4.3" @@ -939,6 +1005,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "hybrid-array" version = "0.4.13" @@ -959,9 +1031,11 @@ dependencies = [ "bytes", "futures-channel", "futures-core", + "h2", "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -1135,6 +1209,16 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", +] + [[package]] name = "inout" version = "0.1.4" @@ -1495,6 +1579,16 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "oauth2" version = "5.0.0" @@ -1824,6 +1918,18 @@ dependencies = [ "thiserror 2.0.18", ] +[[package]] +name = "regex" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0e75113e14dc5acb068cd0786884f214f1312650a3d36d269f5c4f3cdee8a2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + [[package]] name = "regex-automata" version = "0.4.15" @@ -2801,6 +2907,29 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wiremock" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08db1edfb05d9b3c1542e521aea074442088292f00b5f28e435c714a98f85031" +dependencies = [ + "assert-json-diff", + "base64", + "deadpool", + "futures", + "http", + "http-body-util", + "hyper", + "hyper-util", + "log", + "once_cell", + "regex", + "serde", + "serde_json", + "tokio", + "url", +] + [[package]] name = "wit-bindgen" version = "0.57.1" diff --git a/napi/Cargo.toml b/napi/Cargo.toml index 267f1543..e728598a 100644 --- a/napi/Cargo.toml +++ b/napi/Cargo.toml @@ -128,6 +128,9 @@ serde_json = "1" # Version kept in lock-step with the kernel's `chrono` dep. chrono = { version = "0.4", default-features = false, features = ["clock"] } +[dev-dependencies] +wiremock = "0.6" + [build-dependencies] napi-build = "2" # napi-build stays on 2.x; napi-rs 3 build script is compatible diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index 38fdc438..bb9c60e9 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -21,6 +21,10 @@ layout, or npm publishing. Pure-kernel changes go in the root ### Fixed +- Added `ExecuteOptions.rawParams`, allowing Node.js callers to preserve + pre-marshalled SQL type names such as `INTERVAL MONTH` and `INTERVAL DAY` + instead of collapsing them through `TypedValue::Interval`. + --- *Note: When making changes, please add your change under the appropriate section with a brief description. At release time, move the entries here diff --git a/napi/index.d.ts b/napi/index.d.ts index 21cf939e..4c29b203 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -20,6 +20,7 @@ * `rowLimit` (SEA `row_limit`) is exposed here and threaded onto the kernel * `StatementSpec`. `positionalParams` (`?`) and `namedParams` (`:name`) * carry bound query parameters, decoded via `params::parse_typed_value`. + * `rawParams` forwards pre-marshalled SEA parameters unchanged. * (There is no `queryTimeoutSecs`: it abused the SEA `wait_timeout` inline-hold * window and was removed — a real per-statement timeout is `STATEMENT_TIMEOUT`.) * @@ -85,6 +86,12 @@ export interface ExecuteOptions { * mutually exclusive at the SQL level (`?` vs `:name`). */ namedParams?: Array + /** + * Pre-marshalled parameters passed through `StatementSpec::param_raw`. + * Omit `name` for positional values. Raw parameters cannot be mixed with + * typed parameters or combine named and positional markers. + */ + rawParams?: Array } /** * A named bound parameter — a [`TypedValueInput`] plus its `:name`. Kept a @@ -675,6 +682,18 @@ export interface TypedValueInput { */ value?: string } +/** + * A pre-marshalled SEA parameter that preserves `sql_type` verbatim. + * Omit `name` for positional markers; the kernel assigns their ordinals. + */ +export interface RawParameterInput { + /** Named marker name. Omit for a positional marker. */ + name?: string + /** Databricks SQL type name sent verbatim to SEA. */ + sqlType: string + /** String-encoded value. `None` represents SQL NULL. */ + value?: string +} /** * A single Arrow IPC stream payload encoding one record batch (plus * the schema header so the JS-side reader is stateless). diff --git a/napi/src/connection.rs b/napi/src/connection.rs index 338ff0d6..5333e1ec 100644 --- a/napi/src/connection.rs +++ b/napi/src/connection.rs @@ -35,7 +35,7 @@ use databricks_sql_kernel::{Error as KernelError, Session, TypedValue}; use crate::async_statement::AsyncStatement; use crate::error::napi_err_from_kernel; -use crate::params::{parse_typed_value, TypedValueInput}; +use crate::params::{parse_typed_value, RawParameterInput, TypedValueInput}; use crate::runtime; use crate::statement::Statement; use crate::util::guarded; @@ -56,6 +56,7 @@ use crate::util::guarded; /// `rowLimit` (SEA `row_limit`) is exposed here and threaded onto the kernel /// `StatementSpec`. `positionalParams` (`?`) and `namedParams` (`:name`) /// carry bound query parameters, decoded via `params::parse_typed_value`. +/// `rawParams` forwards pre-marshalled SEA parameters unchanged. /// (There is no `queryTimeoutSecs`: it abused the SEA `wait_timeout` inline-hold /// window and was removed — a real per-statement timeout is `STATEMENT_TIMEOUT`.) /// @@ -115,6 +116,10 @@ pub struct ExecuteOptions { /// positional is the documented-undocumented variant. The two are /// mutually exclusive at the SQL level (`?` vs `:name`). pub named_params: Option>, + /// Pre-marshalled parameters passed through `StatementSpec::param_raw`. + /// Omit `name` for positional values. Raw parameters cannot be mixed with + /// typed parameters or combine named and positional markers. + pub raw_params: Option>, } /// A named bound parameter — a [`TypedValueInput`] plus its `:name`. Kept a @@ -187,6 +192,7 @@ fn build_statement( row_limit: Option, positional_params: Vec, named_params: Vec<(String, TypedValue)>, + raw_params: Vec, ) -> napi::Result { // Positional/named mutual-exclusivity is enforced in the kernel's // `build_execute_params` (StatementSpec dispatch), so every binding @@ -221,6 +227,10 @@ fn build_statement( .param_named(name, value) .map_err(napi_err_from_kernel)?; } + // Preserve exact SQL types through the raw binder. + for raw in raw_params { + stmt.spec().param_raw(raw.name, raw.sql_type, raw.value); + } Ok(stmt) } @@ -300,6 +310,10 @@ impl Connection { .map(decode_named_params) .transpose()? .unwrap_or_default(); + let raw_params = options + .as_mut() + .and_then(|o| o.raw_params.take()) + .unwrap_or_default(); // Pull the explicit `statement_conf` map and the `query_tags` // convenience map out of `options`. The kernel `StatementSpec` // owns the merge + serialisation: set the explicit conf first, @@ -330,6 +344,7 @@ impl Connection { row_limit, positional_params, named_params, + raw_params, )? // session lock released here }; @@ -384,6 +399,10 @@ impl Connection { .map(decode_named_params) .transpose()? .unwrap_or_default(); + let raw_params = options + .as_mut() + .and_then(|o| o.raw_params.take()) + .unwrap_or_default(); let statement_conf = options.as_mut().and_then(|o| o.statement_conf.take()); let query_tags = options.as_mut().and_then(|o| o.query_tags.take()); guarded(async move { @@ -398,6 +417,7 @@ impl Connection { row_limit, positional_params, named_params, + raw_params, )? }; @@ -452,6 +472,10 @@ impl Connection { .map(decode_named_params) .transpose()? .unwrap_or_default(); + let raw_params = options + .as_mut() + .and_then(|o| o.raw_params.take()) + .unwrap_or_default(); let statement_conf = options.as_mut().and_then(|o| o.statement_conf.take()); let query_tags = options.as_mut().and_then(|o| o.query_tags.take()); guarded(async move { @@ -473,6 +497,7 @@ impl Connection { row_limit, positional_params, named_params, + raw_params, )? // session lock released here }; @@ -520,6 +545,10 @@ impl Connection { .map(decode_named_params) .transpose()? .unwrap_or_default(); + let raw_params = options + .as_mut() + .and_then(|o| o.raw_params.take()) + .unwrap_or_default(); // Same conf + query_tags plumbing as `execute_statement`: the // kernel `StatementSpec` owns the merge + serialisation. let statement_conf = options.as_mut().and_then(|o| o.statement_conf.take()); @@ -546,6 +575,7 @@ impl Connection { row_limit, positional_params, named_params, + raw_params, )? // session lock released here }; @@ -621,6 +651,10 @@ impl Drop for Connection { #[cfg(test)] mod tests { use super::*; + use databricks_sql_kernel::{AuthConfig, ConnectionConfig, ErrorCode}; + use serde_json::json; + use wiremock::matchers::{method, path}; + use wiremock::{Mock, MockServer, ResponseTemplate}; // The closed-state error shape pins the TS adapter's reviver // contract — a future maintainer who switches back to raw @@ -667,4 +701,119 @@ mod tests { }; assert_eq!(conn.session_id(), "session-survives-close"); } + + #[tokio::test] + async fn build_statement_forwards_raw_params_and_rejects_mixed_binding() { + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/api/2.0/sql/sessions")) + .respond_with( + ResponseTemplate::new(200).set_body_json(json!({ "session_id": "sess-napi-raw" })), + ) + .mount(&server) + .await; + Mock::given(method("POST")) + .and(path("/api/2.0/sql/statements")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "status": { + "state": "FAILED", + "sql_state": "22023", + "error": { + "error_code": "INVALID_PARAMETER_VALUE", + "message": "expected test failure. SQLSTATE: 22023" + } + } + }))) + .mount(&server) + .await; + Mock::given(method("DELETE")) + .and(path("/api/2.0/sql/sessions/sess-napi-raw")) + .respond_with(ResponseTemplate::new(200)) + .mount(&server) + .await; + + let session = Session::builder() + .connection( + ConnectionConfig::from_http_path(server.uri(), "/sql/1.0/warehouses/test_wh") + .expect("valid HTTP path"), + ) + .auth(AuthConfig::Pat { + token: "dapi_test_token".into(), + identity_federation_client_id: None, + }) + .open() + .await + .expect("session opens against mock server"); + + let raw = || RawParameterInput { + name: None, + sql_type: "INTERVAL MONTH".into(), + value: Some("13".into()), + }; + let mut raw_stmt = build_statement( + &session, + "SELECT ?".into(), + None, + None, + None, + vec![], + vec![], + vec![raw()], + ) + .expect("raw statement builds"); + let dispatched_err = raw_stmt + .execute() + .await + .err() + .expect("mock returns a terminal SQL error"); + assert_eq!(dispatched_err.code, ErrorCode::SqlError); + + let requests = server.received_requests().await.unwrap_or_default(); + let statement_requests: Vec<_> = requests + .iter() + .filter(|request| { + request.method.as_str() == "POST" && request.url.path() == "/api/2.0/sql/statements" + }) + .collect(); + assert_eq!(statement_requests.len(), 1); + let body: serde_json::Value = + serde_json::from_slice(&statement_requests[0].body).expect("statement request is JSON"); + assert_eq!( + body["parameters"], + json!([{ "ordinal": 1, "type": "INTERVAL MONTH", "value": "13" }]) + ); + + let mut mixed_stmt = build_statement( + &session, + "SELECT ?".into(), + None, + None, + None, + vec![TypedValue::Int32(1)], + vec![], + vec![raw()], + ) + .expect("mixed statement builds before dispatch validation"); + let mixed_err = mixed_stmt + .execute() + .await + .err() + .expect("raw and typed params must be rejected"); + assert_eq!(mixed_err.code, ErrorCode::InvalidArgument); + + let requests = server.received_requests().await.unwrap_or_default(); + assert_eq!( + requests + .iter() + .filter(|request| { + request.method.as_str() == "POST" + && request.url.path() == "/api/2.0/sql/statements" + }) + .count(), + 1, + "mixed binding must fail before a second request" + ); + + session.close().await.expect("session closes"); + } } diff --git a/napi/src/params.rs b/napi/src/params.rs index 733a0588..74344279 100644 --- a/napi/src/params.rs +++ b/napi/src/params.rs @@ -66,3 +66,19 @@ pub struct TypedValueInput { /// `VoidParameter` shape and the pyo3 binding's contract. pub value: Option, } + +/// A pre-marshalled SEA parameter that preserves `sql_type` verbatim. +/// Omit `name` for positional markers; the kernel assigns their ordinals. +#[allow( + dead_code, + reason = "exported via napi macro codegen; not called from Rust" +)] +#[napi(object)] +pub struct RawParameterInput { + /// Named marker name. Omit for a positional marker. + pub name: Option, + /// Databricks SQL type name sent verbatim to SEA. + pub sql_type: String, + /// String-encoded value. `None` represents SQL NULL. + pub value: Option, +} From 2cb440610a791fd5f7a0a6bc5d3bacfd64e91967 Mon Sep 17 00:00:00 2001 From: cat23123 <58714163+cat23123@users.noreply.github.com> Date: Wed, 26 Aug 2026 17:58:02 +0000 Subject: [PATCH 2/2] chore(napi): address review feedback Signed-off-by: cat23123 <58714163+cat23123@users.noreply.github.com> --- napi/NEXT_CHANGELOG.md | 8 ++++---- napi/src/params.rs | 4 ---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/napi/NEXT_CHANGELOG.md b/napi/NEXT_CHANGELOG.md index bb9c60e9..7d477b95 100644 --- a/napi/NEXT_CHANGELOG.md +++ b/napi/NEXT_CHANGELOG.md @@ -15,16 +15,16 @@ layout, or npm publishing. Pure-kernel changes go in the root ### Added +- Added `ExecuteOptions.rawParams`, allowing Node.js callers to preserve + pre-marshalled SQL type names such as `INTERVAL MONTH` and `INTERVAL DAY` + instead of collapsing them through `TypedValue::Interval`. + ### Changed ### Updated ### Fixed -- Added `ExecuteOptions.rawParams`, allowing Node.js callers to preserve - pre-marshalled SQL type names such as `INTERVAL MONTH` and `INTERVAL DAY` - instead of collapsing them through `TypedValue::Interval`. - --- *Note: When making changes, please add your change under the appropriate section with a brief description. At release time, move the entries here diff --git a/napi/src/params.rs b/napi/src/params.rs index 74344279..e10bba1f 100644 --- a/napi/src/params.rs +++ b/napi/src/params.rs @@ -69,10 +69,6 @@ pub struct TypedValueInput { /// A pre-marshalled SEA parameter that preserves `sql_type` verbatim. /// Omit `name` for positional markers; the kernel assigns their ordinals. -#[allow( - dead_code, - reason = "exported via napi macro codegen; not called from Rust" -)] #[napi(object)] pub struct RawParameterInput { /// Named marker name. Omit for a positional marker.