Skip to content

Commit e3700a7

Browse files
committed
feat(bindings): expose geospatial result representation
Signed-off-by: cat23123 <58714163+cat23123@users.noreply.github.com>
1 parent 7ccf066 commit e3700a7

11 files changed

Lines changed: 248 additions & 14 deletions

‎CONNECTION_PARAMETERS.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ binary). When unset, the kernel omits the key and lets the server choose its
194194
default, currently string. A caller may still provide the raw server key through
195195
`session_conf`; the typed setting takes precedence when both are supplied.
196196

197+
Bindings expose the same optional boolean as Python `geospatial_as_string`,
198+
Node.js `geospatialAsString`, and C
199+
`kernel_session_config_set_geospatial_as_string`.
200+
197201
For non-empty results, the Arrow schema returned by the server is authoritative
198202
and the kernel preserves it unless another explicit `ResultConfig`
199203
post-processing option is enabled. For empty results with no Arrow payload, the

‎NEXT_CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ changes go in their respective binding's NEXT_CHANGELOG.
1414
- Added typed `GEOMETRY` / `GEOGRAPHY` result representation configuration:
1515
Arrow EWKT strings or native `struct<srid,wkb>` binary values, including
1616
correct schema reconstruction for empty results.
17+
- Added `kernel_session_config_set_geospatial_as_string` to expose the same
18+
representation choice through the C ABI.
1719

1820
### Changed
1921

‎include/databricks_kernel.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ KernelStatusCode kernel_session_config_set_session_conf(KernelSessionConfig* con
343343
const char* key,
344344
const char* value);
345345

346+
/* Select the physical Arrow representation for GEOMETRY / GEOGRAPHY results.
347+
* `as_string = true` requests EWKT in Arrow UTF-8 values; `false` requests
348+
* Arrow `struct<srid: int32, wkb: binary>` values. Omitting this setter leaves
349+
* the server default in control. This typed setting takes precedence over a raw
350+
* `geospatial_as_string` entry set through kernel_session_config_set_session_conf. */
351+
KernelStatusCode kernel_session_config_set_geospatial_as_string(
352+
KernelSessionConfig* config, bool as_string);
353+
346354
/* Append one HTTP header sent on every request. Call once per header
347355
* (order preserved); `name` and `value` are both required. */
348356
KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* config,

‎napi/NEXT_CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ layout, or npm publishing. Pure-kernel changes go in the root
1414

1515
### Added
1616

17+
- Added `ConnectionOptions.geospatialAsString` for choosing EWKT string or
18+
SRID+WKB binary Arrow results.
19+
1720
### Changed
1821

1922
### Updated

‎napi/index.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ export interface ConnectionOptions {
318318
* `session_confs`. Unknown keys are rejected server-side.
319319
*/
320320
sessionConf?: Record<string, string>
321+
/**
322+
* Select the physical Arrow representation of `GEOMETRY` /
323+
* `GEOGRAPHY` results. `true` requests EWKT in Arrow UTF-8 values;
324+
* `false` requests Arrow `struct<srid: int32, wkb: binary>` values.
325+
* Omitted leaves the server default in control.
326+
*/
327+
geospatialAsString?: boolean
321328
/**
322329
* Driver name reported in telemetry system configuration. Omitted ⇒
323330
* kernel default.

‎napi/src/database.rs‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ use napi::bindgen_prelude::Buffer;
3434
type Buffer = Vec<u8>;
3535

3636
use databricks_sql_kernel::{
37-
AuthConfig, DefaultOpts, DriverSystemConfiguration, Error as KernelError, HttpConfig,
38-
ProxyConfig, ResultConfig, Session, TelemetryConfig, TlsConfig, TokenCacheConfig,
37+
AuthConfig, DefaultOpts, DriverSystemConfiguration, Error as KernelError,
38+
GeospatialResultRepresentation, HttpConfig, ProxyConfig, ResultConfig, Session,
39+
TelemetryConfig, TlsConfig, TokenCacheConfig,
3940
};
4041

4142
/// NodeJS-driver-appropriate default for `pool_max_idle_per_host`.
@@ -231,6 +232,11 @@ pub struct ConnectionOptions {
231232
/// query-tag presets, …). Forwarded verbatim to SEA
232233
/// `session_confs`. Unknown keys are rejected server-side.
233234
pub session_conf: Option<HashMap<String, String>>,
235+
/// Select the physical Arrow representation of `GEOMETRY` /
236+
/// `GEOGRAPHY` results. `true` requests EWKT in Arrow UTF-8 values;
237+
/// `false` requests Arrow `struct<srid: int32, wkb: binary>` values.
238+
/// Omitted leaves the server default in control.
239+
pub geospatial_as_string: Option<bool>,
234240
/// Driver name reported in telemetry system configuration. Omitted ⇒
235241
/// kernel default.
236242
pub driver_name: Option<String>,
@@ -763,6 +769,18 @@ fn build_driver_system_configuration(
763769
})
764770
}
765771

772+
fn geospatial_result_representation(
773+
as_string: Option<bool>,
774+
) -> Option<GeospatialResultRepresentation> {
775+
as_string.map(|as_string| {
776+
if as_string {
777+
GeospatialResultRepresentation::String
778+
} else {
779+
GeospatialResultRepresentation::Binary
780+
}
781+
})
782+
}
783+
766784
/// Open a Databricks SQL session and return an opaque `Connection`
767785
/// wrapping the kernel `Session`. Authentication is selected by
768786
/// `options.auth_mode` (PAT / OAuth M2M / OAuth U2M) — see
@@ -823,6 +841,10 @@ pub async fn open_session(mut options: ConnectionOptions) -> napi::Result<Connec
823841
if let Some(conf) = options.session_conf {
824842
builder = builder.session_conf(conf);
825843
}
844+
if let Some(representation) = geospatial_result_representation(options.geospatial_as_string)
845+
{
846+
builder = builder.geospatial_result_representation(representation);
847+
}
826848
// Result-config knobs (interval / complex-type rendering). Merge
827849
// into one `ResultConfig` so the two toggles don't clobber each
828850
// other when both are set. Only wire it when at least one is
@@ -901,6 +923,7 @@ mod tests {
901923
catalog: None,
902924
schema: None,
903925
session_conf: None,
926+
geospatial_as_string: None,
904927
driver_name: None,
905928
driver_version: None,
906929
runtime_name: None,
@@ -940,6 +963,19 @@ mod tests {
940963
}
941964
}
942965

966+
#[test]
967+
fn geospatial_as_string_maps_to_kernel_representation() {
968+
assert_eq!(geospatial_result_representation(None), None);
969+
assert_eq!(
970+
geospatial_result_representation(Some(true)),
971+
Some(GeospatialResultRepresentation::String)
972+
);
973+
assert_eq!(
974+
geospatial_result_representation(Some(false)),
975+
Some(GeospatialResultRepresentation::Binary)
976+
);
977+
}
978+
943979
/// Assert a `build_auth_config` failure surfaces as a kernel
944980
/// `InvalidArgument` (napi `Status::InvalidArg` + `"code":
945981
/// "InvalidArgument"` in the error envelope) and that the kernel

‎pyo3/NEXT_CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ changes go in the root `NEXT_CHANGELOG.md`.
1313

1414
### Added
1515

16+
- Added the optional `geospatial_as_string` session argument for choosing
17+
EWKT string or SRID+WKB binary Arrow results.
18+
1619
### Changed
1720

1821
### Updated

‎pyo3/src/session.rs‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::auth::{build_auth_config, OAuthParams};
2424
use crate::executed_async::ExecutedAsyncStatement;
2525
use crate::metadata::Metadata;
2626
use crate::statement::Statement;
27-
use databricks_sql_kernel::Session as KSession;
27+
use databricks_sql_kernel::{GeospatialResultRepresentation, Session as KSession};
2828
use pyo3::prelude::*;
2929
use std::collections::HashMap;
3030
use std::sync::Arc;
@@ -140,6 +140,18 @@ fn build_telemetry_config(
140140
Ok(telemetry)
141141
}
142142

143+
fn geospatial_result_representation(
144+
as_string: Option<bool>,
145+
) -> Option<GeospatialResultRepresentation> {
146+
as_string.map(|as_string| {
147+
if as_string {
148+
GeospatialResultRepresentation::String
149+
} else {
150+
GeospatialResultRepresentation::Binary
151+
}
152+
})
153+
}
154+
143155
#[allow(clippy::too_many_arguments)]
144156
fn build_http_config(
145157
request_timeout_secs: Option<f64>,
@@ -325,6 +337,11 @@ impl Session {
325337
/// presets, etc. The kernel does not validate keys; the server
326338
/// rejects unknown ones with a `KernelError(code='SqlError')`
327339
/// when it picks them up.
340+
///
341+
/// `geospatial_as_string` selects the physical Arrow representation of
342+
/// `GEOMETRY` / `GEOGRAPHY` results. `True` requests EWKT in Arrow UTF-8
343+
/// values; `False` requests Arrow `struct<srid: int32, wkb: binary>`
344+
/// values. `None` leaves the server default in control.
328345
#[new]
329346
#[pyo3(signature = (
330347
host,
@@ -364,6 +381,7 @@ impl Session {
364381
catalog=None,
365382
schema=None,
366383
session_conf=None,
384+
geospatial_as_string=None,
367385
driver_name=None,
368386
driver_version=None,
369387
runtime_name=None,
@@ -429,6 +447,7 @@ impl Session {
429447
catalog: Option<String>,
430448
schema: Option<String>,
431449
session_conf: Option<HashMap<String, String>>,
450+
geospatial_as_string: Option<bool>,
432451
driver_name: Option<String>,
433452
driver_version: Option<String>,
434453
runtime_name: Option<String>,
@@ -625,6 +644,9 @@ impl Session {
625644
if let Some(conf) = session_conf {
626645
builder = builder.session_conf(conf);
627646
}
647+
if let Some(representation) = geospatial_result_representation(geospatial_as_string) {
648+
builder = builder.geospatial_result_representation(representation);
649+
}
628650
// Merge result-config knobs into one `ResultConfig` so two
629651
// toggles don't clobber each other when both are set.
630652
if complex_types_as_json || intervals_as_string {
@@ -736,6 +758,19 @@ mod send_sync_tests {
736758
assert_send_sync::<SessionInner>();
737759
}
738760

761+
#[test]
762+
fn geospatial_as_string_maps_to_kernel_representation() {
763+
assert_eq!(geospatial_result_representation(None), None);
764+
assert_eq!(
765+
geospatial_result_representation(Some(true)),
766+
Some(GeospatialResultRepresentation::String)
767+
);
768+
assert_eq!(
769+
geospatial_result_representation(Some(false)),
770+
Some(GeospatialResultRepresentation::Binary)
771+
);
772+
}
773+
739774
#[test]
740775
fn telemetry_kwargs_map_to_kernel_config() {
741776
let telemetry = build_telemetry_config(TelemetryKwargs {
@@ -819,9 +854,11 @@ mod send_sync_tests {
819854
assert!(build_http_config(None, None, None, None, None, None, None)
820855
.unwrap()
821856
.is_none());
822-
assert!(build_http_config(Some(0.0), None, None, None, None, None, None)
823-
.unwrap()
824-
.is_none());
857+
assert!(
858+
build_http_config(Some(0.0), None, None, None, None, None, None)
859+
.unwrap()
860+
.is_none()
861+
);
825862

826863
let http = build_http_config(Some(0.5), None, None, None, None, None, None)
827864
.unwrap()
@@ -832,8 +869,7 @@ mod send_sync_tests {
832869

833870
let one_ns = std::time::Duration::from_nanos(1).as_secs_f64();
834871
assert!(
835-
build_http_config(Some(one_ns / 1_000.0), None, None, None, None, None, None)
836-
.is_err()
872+
build_http_config(Some(one_ns / 1_000.0), None, None, None, None, None, None).is_err()
837873
);
838874
let http = build_http_config(Some(one_ns), None, None, None, None, None, None)
839875
.unwrap()

‎src/c_abi/session.rs‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
2828
use super::{block_on_ffi, KernelStatusCode};
2929
use crate::config::{
30-
AuthConfig, ConnectionConfig, HttpConfig, ProxyConfig, ResultConfig, TelemetryConfig,
31-
TlsConfig, TokenCacheConfig,
30+
AuthConfig, ConnectionConfig, GeospatialResultRepresentation, HttpConfig, ProxyConfig,
31+
ResultConfig, TelemetryConfig, TlsConfig, TokenCacheConfig,
3232
};
3333
use crate::kernel_error::Error;
3434
use crate::logging::{LoggingOutcome, LoggingSink};
@@ -122,6 +122,9 @@ pub struct KernelSessionConfig {
122122
connection: Option<ConnectionSpec>,
123123
auth: Option<AuthSpec>,
124124
session_conf: HashMap<String, String>,
125+
/// Optional server-side representation for GEOMETRY / GEOGRAPHY results.
126+
/// `None` leaves the SEA server default in control.
127+
geospatial_result_representation: Option<GeospatialResultRepresentation>,
125128
/// Programmatic HTTP proxy (populated by `..._set_proxy`). `None` ⇒
126129
/// reqwest's env-var proxy behaviour.
127130
proxy: Option<ProxySpec>,
@@ -262,6 +265,7 @@ pub unsafe extern "C" fn kernel_session_config_new(
262265
connection: None,
263266
auth: None,
264267
session_conf: HashMap::new(),
268+
geospatial_result_representation: None,
265269
proxy: None,
266270
tls: TlsSpec::default(),
267271
custom_headers: Vec::new(),
@@ -1070,6 +1074,31 @@ pub unsafe extern "C" fn kernel_session_config_set_session_conf(
10701074
})
10711075
}
10721076

1077+
/// Select the physical Arrow representation of `GEOMETRY` and `GEOGRAPHY`
1078+
/// result columns. `as_string = true` requests EWKT in Arrow `Utf8` values;
1079+
/// `false` requests Arrow `struct<srid: int32, wkb: binary>` values.
1080+
///
1081+
/// Omitting this setter leaves the server default in control. Calling it takes
1082+
/// precedence over a raw `geospatial_as_string` entry supplied through
1083+
/// [`kernel_session_config_set_session_conf`].
1084+
///
1085+
/// # Safety
1086+
/// `config` must be a live config handle.
1087+
#[no_mangle]
1088+
pub unsafe extern "C" fn kernel_session_config_set_geospatial_as_string(
1089+
config: *mut KernelSessionConfig,
1090+
as_string: bool,
1091+
) -> KernelStatusCode {
1092+
set_on_config(config, |c| {
1093+
c.geospatial_result_representation = Some(if as_string {
1094+
GeospatialResultRepresentation::String
1095+
} else {
1096+
GeospatialResultRepresentation::Binary
1097+
});
1098+
Ok(())
1099+
})
1100+
}
1101+
10731102
/// Read an optional C string: null → `None`, else an owned `String`
10741103
/// (errors on non-UTF-8).
10751104
///
@@ -1551,6 +1580,7 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod
15511580
connection,
15521581
auth,
15531582
session_conf,
1583+
geospatial_result_representation,
15541584
proxy,
15551585
tls,
15561586
custom_headers,
@@ -1612,6 +1642,9 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod
16121642
if let Some(system_configuration) = driver_system_configuration {
16131643
builder = builder.driver_system_configuration(system_configuration);
16141644
}
1645+
if let Some(representation) = geospatial_result_representation {
1646+
builder = builder.geospatial_result_representation(representation);
1647+
}
16151648

16161649
// Skip `.http()` when no HTTP override was set, so the kernel's
16171650
// full `HttpConfig::default()` applies untouched.
@@ -2369,6 +2402,45 @@ mod tests {
23692402
}
23702403
}
23712404

2405+
#[test]
2406+
fn geospatial_as_string_setter_records_both_representations() {
2407+
unsafe {
2408+
let mut config: *mut KernelSessionConfig = std::ptr::null_mut();
2409+
assert_eq!(
2410+
kernel_session_config_new(&mut config),
2411+
KernelStatusCode::Success
2412+
);
2413+
assert_eq!(
2414+
config.as_ref().unwrap().geospatial_result_representation,
2415+
None
2416+
);
2417+
2418+
assert_eq!(
2419+
kernel_session_config_set_geospatial_as_string(config, true),
2420+
KernelStatusCode::Success
2421+
);
2422+
assert_eq!(
2423+
config.as_ref().unwrap().geospatial_result_representation,
2424+
Some(GeospatialResultRepresentation::String)
2425+
);
2426+
2427+
assert_eq!(
2428+
kernel_session_config_set_geospatial_as_string(config, false),
2429+
KernelStatusCode::Success
2430+
);
2431+
assert_eq!(
2432+
config.as_ref().unwrap().geospatial_result_representation,
2433+
Some(GeospatialResultRepresentation::Binary)
2434+
);
2435+
2436+
assert_eq!(
2437+
kernel_session_config_set_geospatial_as_string(std::ptr::null_mut(), true),
2438+
KernelStatusCode::InvalidArgument
2439+
);
2440+
kernel_session_config_free(config);
2441+
}
2442+
}
2443+
23722444
/// The JWT private-key M2M setter captures the required and optional
23732445
/// fields verbatim; NULL optionals record as `None`. Guards the C-side
23742446
/// wiring into `AuthSpec::OAuthM2MJwt` (which then maps to

0 commit comments

Comments
 (0)