diff --git a/src/statement/params.rs b/src/statement/params.rs index a8fe0343..a9d36828 100644 --- a/src/statement/params.rs +++ b/src/statement/params.rs @@ -22,7 +22,7 @@ use crate::kernel_error::{Error, Result}; use crate::metadata::type_mapping::{arrow_type_to_databricks, databricks_type_to_arrow}; -use crate::statement::spec::{geo_wire, TypedValue}; +use crate::statement::spec::TypedValue; use crate::types::sea::StatementParameter; use arrow_array::cast::AsArray; use arrow_array::types::*; @@ -394,49 +394,62 @@ fn typed_value_to_sea_payload(value: &TypedValue) -> Result { Some(ts.format("%Y-%m-%dT%H:%M:%S%.fZ").to_string()), ), TypedValue::Interval(s) => ok("INTERVAL", Some(s.clone())), - // `VARIANT` — pass-through. The kernel doesn't validate the - // JSON; the server parses on receipt and rejects malformed - // input with its own error. The connector or caller is - // expected to have produced valid JSON. - TypedValue::Variant(s) => ok("VARIANT", Some(s.clone())), - // `GEOGRAPHY` and `GEOMETRY` — bind as EWKT today. The - // `param_type` carries the SRID decorator when present - // (e.g. `GEOGRAPHY(4326)`), matching the form the server - // emits in column metadata. The `value` is EWKT (`SRID=N; - // WKT`) when SRID is set; bare WKT when not. + // `VARIANT` — bind as `STRING` on the wire, carrying the + // JSON text as the value. Same pattern as geo: SEA's + // `parameters` array accepts `VARIANT` as a parameter type + // but treats the value as the **textual content of a JSON + // string** (i.e. the server stores a `String`-typed + // VARIANT, not a parsed `Object`/`Array`-typed VARIANT). + // Empirically `? AS c_variant` with `param_type=VARIANT` + // round-trips `{"k":1}` back as the JSON-encoded string + // `"\"{\\\"k\\\":1}\""` — not what callers want. // - // Binding via WKB is not yet wired — when SEA grows the - // binary geo input path, this arm gets a branch on - // `wkb.is_some()` to send the binary form instead. The - // variant's optional `wkb` field is already in place to - // make that change additive. - TypedValue::Geography { wkt, srid, .. } => ok( - geo_param_type(geo_wire::GEOGRAPHY, *srid), - Some(geo_param_value(wkt, *srid)), - ), - TypedValue::Geometry { wkt, srid, .. } => ok( - geo_param_type(geo_wire::GEOMETRY, *srid), - Some(geo_param_value(wkt, *srid)), - ), - } -} - -/// Format the SEA `param_type` for a geospatial value. Appends an -/// `(SRID)` decorator when the SRID is set, matching the form the -/// server emits in column metadata (e.g. `GEOGRAPHY(4326)`). -fn geo_param_type(family: &str, srid: Option) -> String { - match srid { - Some(s) => format!("{family}({s})"), - None => family.to_owned(), - } -} - -/// Format the SEA `value` field for a geospatial binding. EWKT -/// (`SRID=N;WKT`) when an SRID is present; bare WKT otherwise. -fn geo_param_value(wkt: &str, srid: Option) -> String { - match srid { - Some(s) => format!("{}{};{}", geo_wire::EWKT_SRID_PREFIX, s, wkt), - None => wkt.to_owned(), + // Callers therefore write SQL like + // `SELECT PARSE_JSON(?) AS v` + // and bind `TypedValue::Variant()`. The kernel + // sends the JSON text as a STRING; `PARSE_JSON` produces + // a proper Object/Array-typed VARIANT column. Matches the + // geo + JDBC pattern (typed-value bind desugars to the + // STRING wire form; SQL function handles parsing). + // + // The kernel does NOT validate the JSON before sending; + // the server parses on receipt and rejects malformed + // input with its own SQL error. + TypedValue::Variant(s) => ok("STRING", Some(s.clone())), + // `GEOGRAPHY` and `GEOMETRY` — bind as `STRING` on the + // wire, carrying **bare WKT** (e.g. `POINT(1 2)`). SEA's + // `parameters` array does not accept `GEOGRAPHY` / + // `GEOMETRY` as parameter types: + // - `param_type=GEOGRAPHY(SRID)` → server rejects + // `INVALID_PARAMETER_MARKER_VALUE.INVALID_VALUE_FOR_DATA_TYPE` + // - `param_type=GEOGRAPHY` (bare) → server rejects + // `INVALID_PARAMETER_MARKER_VALUE.INVALID_DATA_TYPE` + // + // The wire format that DOES work is the same pattern JDBC + // production users follow: bind as `STRING`, send bare WKT + // in `value`, and let the caller's SQL parse it server- + // side with `ST_GeogFromText(?)` / `ST_GeomFromText(?)`. + // Those functions accept bare WKT only — they reject the + // EWKT `SRID=N;WKT` prefix with `WKT_PARSE_ERROR: Invalid + // or unsupported type 'SRID=...'`. So the kernel does NOT + // serialise the SRID into the value; callers needing an + // explicit SRID write + // `SELECT ST_SetSRID(ST_GeogFromText(?), 4326) AS g` + // and the SRID lives in the SQL, not the bound value. + // + // The `srid` field on `TypedValue::Geography` / + // `TypedValue::Geometry` is therefore informational only + // on the bind path today — kept on the variant so a + // future SEA bind-path that natively accepts EWKT (or + // WKB) can pick it up without a breaking-variant change. + // The result-reader path is unchanged: it reads the SRID + // out of column metadata. + // + // Binding via WKB is not yet wired — when SEA grows a + // native geo input path, this arm gets a branch on + // `wkb.is_some()` to send the binary form. + TypedValue::Geography { wkt, .. } => ok("STRING", Some(wkt.clone())), + TypedValue::Geometry { wkt, .. } => ok("STRING", Some(wkt.clone())), } } @@ -1028,78 +1041,80 @@ mod tests { } #[test] - fn typed_value_variant_emits_json_pass_through() { - // Object form. + fn typed_value_variant_emits_json_text_as_string_type() { + // VARIANT binds go on the wire as STRING + raw JSON text; + // the caller's SQL wraps the marker with `PARSE_JSON(?)`. + // See the comment in `typed_value_to_sea_payload` for why + // — SEA's parameter type `VARIANT` stores the value as + // a String-typed VARIANT rather than parsing the JSON + // into an Object/Array-typed VARIANT, so the kernel + // desugars VARIANT binds to the STRING wire form + // (mirrors the geo + JDBC pattern). let json = r#"{"k":1,"nested":{"a":[true,null,"x"]}}"#; let p = typed_value_to_sea_parameter(1, &TypedValue::Variant(json.into())).unwrap(); - assert_eq!(p.param_type, "VARIANT"); + assert_eq!(p.param_type, "STRING"); assert_eq!(p.value.as_deref(), Some(json)); // Scalar JSON values are also valid VARIANT contents. for raw in [r#""hello""#, "42", "true", "null"] { let p = typed_value_to_sea_parameter(1, &TypedValue::Variant(raw.into())).unwrap(); - assert_eq!(p.param_type, "VARIANT"); + assert_eq!(p.param_type, "STRING"); assert_eq!(p.value.as_deref(), Some(raw)); } } #[test] - fn typed_value_geography_with_srid_emits_ewkt_and_decorated_type() { - let p = typed_value_to_sea_parameter( - 1, - &TypedValue::Geography { - wkt: "POINT(1 2)".into(), - srid: Some(4326), - wkb: None, - }, - ) - .unwrap(); - assert_eq!(p.param_type, "GEOGRAPHY(4326)"); - assert_eq!(p.value.as_deref(), Some("SRID=4326;POINT(1 2)")); - } - - #[test] - fn typed_value_geography_without_srid_emits_bare_wkt() { - let p = typed_value_to_sea_parameter( - 1, - &TypedValue::Geography { - wkt: "POINT(1 2)".into(), - srid: None, - wkb: None, - }, - ) - .unwrap(); - assert_eq!(p.param_type, "GEOGRAPHY"); - assert_eq!(p.value.as_deref(), Some("POINT(1 2)")); - } - - #[test] - fn typed_value_geometry_with_srid_emits_ewkt() { - let p = typed_value_to_sea_parameter( - 1, - &TypedValue::Geometry { - wkt: "LINESTRING(0 0,1 1)".into(), - srid: Some(0), - wkb: None, - }, - ) - .unwrap(); - assert_eq!(p.param_type, "GEOMETRY(0)"); - assert_eq!(p.value.as_deref(), Some("SRID=0;LINESTRING(0 0,1 1)")); + fn typed_value_geography_emits_bare_wkt_as_string_type() { + // Geo binds always go on the wire as STRING + bare WKT. + // The `srid` field on the variant is informational only + // on the bind path (callers carry SRID semantics in their + // SQL via `ST_SetSRID(ST_GeogFromText(?), )`). + // SRID values present on the variant are dropped before + // serialisation. See the comment in + // `typed_value_to_sea_payload`. + for srid in [None, Some(0), Some(4326)] { + let p = typed_value_to_sea_parameter( + 1, + &TypedValue::Geography { + wkt: "POINT(1 2)".into(), + srid, + wkb: None, + }, + ) + .unwrap(); + assert_eq!( + p.param_type, "STRING", + "geo binds always emit STRING param_type, srid={srid:?}" + ); + assert_eq!( + p.value.as_deref(), + Some("POINT(1 2)"), + "value is bare WKT regardless of srid, srid={srid:?}" + ); + } } #[test] - fn typed_value_geometry_without_srid_emits_bare_wkt() { - let p = typed_value_to_sea_parameter( - 1, - &TypedValue::Geometry { - wkt: "LINESTRING(0 0,1 1)".into(), - srid: None, - wkb: None, - }, - ) - .unwrap(); - assert_eq!(p.param_type, "GEOMETRY"); - assert_eq!(p.value.as_deref(), Some("LINESTRING(0 0,1 1)")); + fn typed_value_geometry_emits_bare_wkt_as_string_type() { + for srid in [None, Some(0), Some(3857)] { + let p = typed_value_to_sea_parameter( + 1, + &TypedValue::Geometry { + wkt: "LINESTRING(0 0,1 1)".into(), + srid, + wkb: None, + }, + ) + .unwrap(); + assert_eq!( + p.param_type, "STRING", + "geo binds always emit STRING param_type, srid={srid:?}" + ); + assert_eq!( + p.value.as_deref(), + Some("LINESTRING(0 0,1 1)"), + "value is bare WKT regardless of srid, srid={srid:?}" + ); + } } } diff --git a/tests/v0_execute_e2e.rs b/tests/v0_execute_e2e.rs index ff778f79..e5827805 100644 --- a/tests/v0_execute_e2e.rs +++ b/tests/v0_execute_e2e.rs @@ -396,13 +396,22 @@ async fn bind_all_typed_values_round_trip() { // metadata, not in the accessor). .param(10, TypedValue::TimestampLtz(ts_ltz)) .unwrap() - // VARIANT round-trip: bind the JSON text, wrap with - // `PARSE_JSON(?)` server-side so the resulting column is a - // proper VARIANT (not a STRING). The kernel reads VARIANT - // back as Arrow Utf8 with `databricks.type_name="VARIANT"` - // metadata on the schema field; `get_str` reads the JSON - // text. Server JSON canonicalisation removes whitespace, so - // we compare against a canonical form. + // VARIANT round-trip: bind the JSON text. The kernel binds + // `TypedValue::Variant` as SEA `param_type=STRING` (same + // pattern as geo — see `typed_value_to_sea_payload`), so + // the SQL must wrap the marker with `PARSE_JSON(?)` to get + // a proper Object/Array-typed VARIANT column. Without the + // wrapper, the server stores the value as a String-typed + // VARIANT (the JSON text gets JSON-encoded as a quoted + // string) and round-trip readback doesn't yield the + // structural shape the assertion expects. + // + // The kernel reads VARIANT back as Arrow Utf8 with + // `databricks.type_name="VARIANT"` metadata on the schema + // field; `get_str` reads the JSON text. Server JSON + // canonicalisation removes whitespace, so we compare + // structurally against a parsed `serde_json::Value` + // rather than byte-equality against the input string. .param(11, TypedValue::Variant(variant_json.into())) .unwrap() .param(12, TypedValue::Null) @@ -529,11 +538,12 @@ async fn bind_geography_and_geometry_round_trip() { ); assert_eq!(row.get_i32(1).unwrap(), Some(4326), "Geography SRID"); - // Geometry column: same shape. + // Geometry column: same shape. Server preserves the comma + // formatting from the bound WKT (no extra spaces inserted). assert_eq!( row.get_str(2).unwrap(), - Some("LINESTRING(0 0, 1 1, 2 2)"), - "Geometry WKT round-trip (note: server reformats with spaces after commas)", + Some("LINESTRING(0 0,1 1,2 2)"), + "Geometry WKT round-trip", ); assert_eq!(row.get_i32(3).unwrap(), Some(0), "Geometry SRID");