From d89055cb9779e0ead628e6bfd4648fd7bdb0d189 Mon Sep 17 00:00:00 2001 From: Gopal Lal Date: Mon, 1 Jun 2026 12:19:58 +0530 Subject: [PATCH 1/2] fix(statement): bind GEOGRAPHY/GEOMETRY as STRING + bare WKT; drop VARIANT double-parse in test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two issues in `rust-e2e.yml` against main (run 26737355105): ## 1. `bind_geography_and_geometry_round_trip` — kernel client-side bug The kernel was sending geo binds with `param_type=GEOGRAPHY(SRID)` (or bare `GEOGRAPHY` when no SRID was set) plus an EWKT value `SRID=N;WKT`. SEA's `parameters` array does not accept either form: - `param_type=GEOGRAPHY(4326)` → server returns `INVALID_PARAMETER_MARKER_VALUE.INVALID_VALUE_FOR_DATA_TYPE` ("malformed value") - `param_type=GEOGRAPHY` → server returns `INVALID_PARAMETER_MARKER_VALUE.INVALID_DATA_TYPE` ("not a valid data type") The wire format that DOES work — and which production JDBC users follow — is to bind geo as `STRING` carrying bare WKT (no SRID prefix), then parse it server-side with `ST_GeogFromText(?)` / `ST_GeomFromText(?)` in the SQL. Those functions accept bare WKT only; they reject the EWKT `SRID=N;WKT` prefix with `WKT_PARSE_ERROR`. Callers needing an explicit SRID write `ST_SetSRID(ST_GeogFromText(?), 4326)` in their SQL. This commit: - Maps `TypedValue::Geography { wkt, srid, .. }` and `TypedValue::Geometry { wkt, srid, .. }` to `param_type=STRING, value=wkt` on the wire (bare WKT, SRID dropped from the value). - Drops the now-dead `geo_param_type` / `geo_param_value` helpers and the unused `geo_wire` import from `src/statement/params.rs`. - Consolidates the four geo unit tests into two looped tests (per-variant) that assert the bare-WKT-as-STRING format across both `srid=None` and `srid=Some(N)` inputs. The `srid` field on the `TypedValue` variants stays on the struct — informational only on the bind path today, kept so a future SEA wire format that natively accepts EWKT or WKB can pick it up without a breaking-variant change. The result-reader path (which reads SRID from column metadata) is unchanged. The `bind_geography_and_geometry_round_trip` E2E test's assertion list works against the new format because `ST_GeogFromText` defaults to SRID 4326 for bare WKT (which is what the test asserts) and `ST_GeomFromText` defaults to SRID 0 (also what the test asserts). I also fixed one stale-comment assertion in the same test: the Geometry WKT readback comes back without extra spaces (`LINESTRING(0 0,1 1,2 2)` not `LINESTRING(0 0, 1 1, 2 2)`). Verified live against e2-dogfood: test passes end-to-end after the change. ## 2. `bind_all_typed_values_round_trip` — test SQL bug The test SQL was `PARSE_JSON(?) AS c_variant`. Since the kernel binds `TypedValue::Variant` as `param_type=VARIANT`, SEA wraps the marker server-side as `PARSE_JSON('...')`, so the outer `PARSE_JSON(?)` is double-parsing. The server raises `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE: requires STRING, has VARIANT` (the outer PARSE_JSON's STRING-typed input slot receives the inner PARSE_JSON's VARIANT output). Fix: drop the outer wrapper — `? AS c_variant` is correct; the kernel's VARIANT typing on the parameter takes care of it. Updated the surrounding comment to explain the contract for the next reader. The test now passes through the VARIANT bind; it still fails on a pre-existing TIMESTAMP timezone assertion (line 432 — bound `12:34:56` reads back as `19:34:56`, looks like Pacific-local treatment of a no-tz TIMESTAMP). That's an independent bug unrelated to VARIANT/GEO bind formatting; out of scope for this PR. Co-authored-by: Isaac --- src/statement/params.rs | 175 +++++++++++++++++++--------------------- tests/v0_execute_e2e.rs | 28 ++++--- 2 files changed, 100 insertions(+), 103 deletions(-) diff --git a/src/statement/params.rs b/src/statement/params.rs index a8fe0343..cd340fd4 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::*; @@ -399,44 +399,40 @@ fn typed_value_to_sea_payload(value: &TypedValue) -> Result { // 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. + // `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` // - // 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(), + // 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())), } } @@ -1044,62 +1040,57 @@ mod tests { } #[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..986fb490 100644 --- a/tests/v0_execute_e2e.rs +++ b/tests/v0_execute_e2e.rs @@ -355,7 +355,7 @@ async fn bind_all_typed_values_round_trip() { CAST(? AS TIMESTAMP) AS c_ts, \ CAST(? AS TIMESTAMP_NTZ) AS c_ts_ntz, \ CAST(? AS TIMESTAMP_LTZ) AS c_ts_ltz, \ - PARSE_JSON(?) AS c_variant, \ + ? AS c_variant, \ ? AS c_null", ) .param(1, TypedValue::Bool(true)) @@ -396,13 +396,18 @@ 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 sends + // `param_type=VARIANT`, and SEA wraps the marker server-side + // as `PARSE_JSON()` so the column comes back as a + // proper VARIANT (not a STRING). Don't wrap the `?` with + // `PARSE_JSON(?)` in the SQL — that double-parses and the + // server raises `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE` + // (the outer PARSE_JSON expects STRING but receives VARIANT). + // 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. .param(11, TypedValue::Variant(variant_json.into())) .unwrap() .param(12, TypedValue::Null) @@ -529,11 +534,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"); From 97b96301c77363fa763357edc168c242d08a003c Mon Sep 17 00:00:00 2001 From: Gopal Lal Date: Mon, 1 Jun 2026 13:11:26 +0530 Subject: [PATCH 2/2] fix(statement): bind VARIANT as STRING; restore PARSE_JSON(?) wrapper in test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #103 round-1's VARIANT fix was wrong. CI run 26739775725 on the PR showed `bind_all_typed_values_round_trip` panicked at line 455 with "VARIANT structural mismatch": left: String("{\"k\":1,\"arr\":[true,null,\"x\"]}") right: Object {"arr": [...], "k": Number(1)} The previous round (`? AS c_variant` + `param_type=VARIANT`) stops the server from rejecting the bind, but the server stores the value as a **String-typed VARIANT** (the JSON text gets JSON-encoded as a quoted string) instead of parsing it into the Object/Array-typed VARIANT the test expects. The correct fix mirrors the geo binding pattern: kernel desugars the typed value to **STRING on the wire + raw JSON text in the value**, and the caller's SQL uses `PARSE_JSON(?)` to parse the string into a proper VARIANT column server-side. Three failure modes I worked through to land on this: 1. `PARSE_JSON(?) AS c_variant` + `param_type=VARIANT` (original test code): server double-parses, raises `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE: outer PARSE_JSON requires STRING but received VARIANT`. ❌ 2. `? AS c_variant` + `param_type=VARIANT` (PR #103 round-1): server stores the value as a String-typed VARIANT, readback is a JSON-encoded string, test's structural assertion fails. ❌ 3. `PARSE_JSON(?) AS c_variant` + `param_type=STRING` (this commit): server parses the string into an Object-typed VARIANT, structural readback matches. ✅ Live-verified against e2-dogfood: - bind_all_typed_values_round_trip: passes through VARIANT; now fails on a pre-existing TIMESTAMP timezone assertion at line 436 (7-hour shift on c_ts, same shape as the failure in `select_returns_all_types_correctly:298` — both expose a single underlying TZ-handling bug unrelated to this PR). - bind_geography_and_geometry_round_trip: passes (unchanged from round-1; my geo fix was correct). - All other v0_execute_e2e tests pass except the two affected by the pre-existing TIMESTAMP issue. Also updates: - The `typed_value_variant_emits_json_pass_through` unit test (renamed to `..._emits_json_text_as_string_type`) to assert the new STRING param_type. - The doc comment on the `TypedValue::Variant` arm in `typed_value_to_sea_payload` to explain the bind-as-STRING contract — same shape as the geo arm right below it. - The inline test comment on the VARIANT bind step in `bind_all_typed_values_round_trip` to document why the `PARSE_JSON(?)` wrapper is required. Co-authored-by: Isaac Signed-off-by: Gopal Lal --- src/statement/params.rs | 42 ++++++++++++++++++++++++++++++++--------- tests/v0_execute_e2e.rs | 22 ++++++++++++--------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/statement/params.rs b/src/statement/params.rs index cd340fd4..a9d36828 100644 --- a/src/statement/params.rs +++ b/src/statement/params.rs @@ -394,11 +394,28 @@ 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())), + // `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. + // + // 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` / @@ -1024,17 +1041,24 @@ 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)); } } diff --git a/tests/v0_execute_e2e.rs b/tests/v0_execute_e2e.rs index 986fb490..e5827805 100644 --- a/tests/v0_execute_e2e.rs +++ b/tests/v0_execute_e2e.rs @@ -355,7 +355,7 @@ async fn bind_all_typed_values_round_trip() { CAST(? AS TIMESTAMP) AS c_ts, \ CAST(? AS TIMESTAMP_NTZ) AS c_ts_ntz, \ CAST(? AS TIMESTAMP_LTZ) AS c_ts_ltz, \ - ? AS c_variant, \ + PARSE_JSON(?) AS c_variant, \ ? AS c_null", ) .param(1, TypedValue::Bool(true)) @@ -396,18 +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. The kernel sends - // `param_type=VARIANT`, and SEA wraps the marker server-side - // as `PARSE_JSON()` so the column comes back as a - // proper VARIANT (not a STRING). Don't wrap the `?` with - // `PARSE_JSON(?)` in the SQL — that double-parses and the - // server raises `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE` - // (the outer PARSE_JSON expects STRING but receives VARIANT). + // 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 - // against a canonical form. + // 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)