apache-spark-connect-core 4.2.0

Spark Connect client transport: gRPC channel, retries, reattach, artifacts, config, errors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Spark Connect connection-string parsing and channel configuration.
//!
//! Mirrors `pyspark.sql.connect.client.core.ChannelBuilder` /
//! `DefaultChannelBuilder`. Parsing follows the Spark Connect URL spec and
//! reproduces CPython's `urllib.parse.urlparse` semantics for the `params`
//! component (the segment after the first `;` following the last `/`).

#![allow(clippy::result_large_err)] // SparkError is large; changing the type would break the API

use std::collections::BTreeMap;

use crate::error::{Result, SparkError};

/// Client version reported in the user-agent (mirrors `pyspark.__version__`).
pub const SPARK_VERSION: &str = "4.2.0";

/// Default Spark Connect server port.
pub const DEFAULT_PORT: u16 = 15002;

/// Max gRPC message length default (128 MiB).
pub const GRPC_MAX_MESSAGE_LENGTH_DEFAULT: usize = 128 * 1024 * 1024;

pub const PARAM_USE_SSL: &str = "use_ssl";
pub const PARAM_TOKEN: &str = "token";
pub const PARAM_USER_ID: &str = "user_id";
pub const PARAM_USER_AGENT: &str = "user_agent";
pub const PARAM_SESSION_ID: &str = "session_id";
pub const PARAM_GRPC_KEEPALIVE_ENABLED: &str = "grpc_keepalive_enabled";
pub const PARAM_GRPC_KEEPALIVE_TIME_MS: &str = "grpc_keepalive_time_ms";
pub const PARAM_GRPC_KEEPALIVE_TIMEOUT_MS: &str = "grpc_keepalive_timeout_ms";
pub const PARAM_GRPC_KEEPALIVE_WITHOUT_CALLS: &str = "grpc_keepalive_without_calls";

const GRPC_DEFAULT_KEEPALIVE_ENABLED: bool = true;
const GRPC_DEFAULT_KEEPALIVE_TIME_MS: i64 = 60 * 1000;
const GRPC_DEFAULT_KEEPALIVE_TIMEOUT_MS: i64 = 20 * 1000;
const GRPC_DEFAULT_KEEPALIVE_WITHOUT_CALLS: bool = true;

/// Parsed Spark Connect connection string plus channel parameters.
#[derive(Debug, Clone)]
pub struct ChannelBuilder {
    params: BTreeMap<String, String>,
    host: String, // display host; IPv6 wrapped in [ ]
    port: u16,
}

impl ChannelBuilder {
    /// Parse a `sc://host[:port][/;k=v;...]` connection string.
    ///
    /// Mirrors `DefaultChannelBuilder.__init__` + `_extract_attributes`.
    pub fn parse(url: &str) -> Result<Self> {
        if !url.starts_with("sc://") {
            return Err(SparkError::value(
                "INVALID_CONNECT_URL",
                &[(
                    "detail",
                    "The URL must start with 'sc://'. Please update the URL to \
                     follow the correct format, e.g., 'sc://hostname:port'.",
                )],
            ));
        }
        let rest = &url["sc://".len()..];

        // Split authority from the remainder at the first of '/', '?', '#'.
        let auth_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
        let authority = &rest[..auth_end];
        let tail = &rest[auth_end..];

        // Path is the tail up to any query/fragment.
        let path_end = tail.find(['?', '#']).unwrap_or(tail.len());
        let path = &tail[..path_end];

        // CPython `_splitparams`: params begin at the first ';' after the last '/'.
        let (path_only, params_str) = split_params(path);

        if !path_only.is_empty() && path_only != "/" {
            return Err(SparkError::value(
                "INVALID_CONNECT_URL",
                &[(
                    "detail",
                    &format!(
                        "The path component '{path_only}' must be empty. Please update \
                         the URL to follow the correct format, e.g., 'sc://hostname:port'."
                    ),
                )],
            ));
        }

        let mut params: BTreeMap<String, String> = BTreeMap::new();
        if !params_str.is_empty() {
            for p in params_str.split(';') {
                let kv: Vec<&str> = p.split('=').collect();
                if kv.len() != 2 {
                    return Err(SparkError::value(
                        "INVALID_CONNECT_URL",
                        &[(
                            "detail",
                            &format!(
                                "Parameter '{p}' should be provided as a key-value pair \
                                 separated by an equal sign (=). Please update the parameter \
                                 to follow the correct format, e.g., 'key=value'."
                            ),
                        )],
                    ));
                }
                params.insert(kv[0].to_string(), unquote(kv[1]));
            }
        }

        let (host_raw, port) = parse_authority(authority, url)?;
        // urllib lowercases the hostname.
        let hostname = host_raw.to_ascii_lowercase();
        let host = if hostname.contains(':') {
            format!("[{hostname}]")
        } else {
            hostname
        };

        Ok(Self { params, host, port })
    }

    pub fn get(&self, key: &str) -> Option<&str> {
        self.params.get(key).map(String::as_str)
    }

    pub fn set(&mut self, key: &str, value: &str) {
        self.params.insert(key.to_string(), value.to_string());
    }

    /// `host:port` endpoint used to dial the gRPC channel.
    pub fn endpoint(&self) -> String {
        format!("{}:{}", self.host, self.port)
    }

    pub fn host(&self) -> &str {
        &self.host
    }

    pub fn port(&self) -> u16 {
        self.port
    }

    pub fn use_ssl(&self) -> bool {
        self.params
            .get(PARAM_USE_SSL)
            .map(|v| v.eq_ignore_ascii_case("true"))
            .unwrap_or(false)
    }

    /// Token from the connection string, falling back to the env var.
    pub fn token(&self) -> Option<String> {
        self.params
            .get(PARAM_TOKEN)
            .cloned()
            .or_else(|| std::env::var("SPARK_CONNECT_AUTHENTICATE_TOKEN").ok())
    }

    /// Mirrors the reference client's `secure` property (`use_ssl` OR a token is
    /// present). NOTE: this does NOT gate the transport. It reads like a security
    /// check but grants none: the token half was the source of the cleartext-token
    /// bug, since a token alone reports "secure" while the scheme stays plaintext.
    /// The real gate on sending the token is `use_ssl() && !is_loopback()` in
    /// `SparkConnectClient::connect`; this method is kept only for parity/inspection.
    pub fn secure(&self) -> bool {
        self.use_ssl() || self.token().is_some()
    }

    /// Whether the endpoint is a loopback address.
    ///
    /// The reference client (`core.py` `toChannel`) permits a token over an
    /// unencrypted channel only for `host == "localhost"` (via
    /// `grpc.local_channel_credentials()`); every other host is forced onto TLS.
    /// We treat the IPv4/IPv6 loopback literals as equivalent to `localhost` so
    /// local development keeps working without `use_ssl=true`.
    pub fn is_loopback(&self) -> bool {
        matches!(self.host.as_str(), "localhost" | "127.0.0.1" | "[::1]")
    }

    pub fn user_id(&self) -> Option<&str> {
        self.get(PARAM_USER_ID)
    }

    /// Validates the `session_id` param is a v4 UUID (mirrors the Python check).
    pub fn session_id(&self) -> Result<Option<String>> {
        match self.params.get(PARAM_SESSION_ID) {
            None => Ok(None),
            Some(s) => match uuid::Uuid::parse_str(s) {
                Ok(u) if u.get_version_num() == 4 => Ok(Some(s.clone())),
                Ok(_) | Err(_) => Err(SparkError::value(
                    "INVALID_SESSION_UUID_ID",
                    &[("arg_name", "session_id"), ("origin", "invalid UUID")],
                )),
            },
        }
    }

    pub fn keepalive_enabled(&self) -> bool {
        self.bool_param(PARAM_GRPC_KEEPALIVE_ENABLED, GRPC_DEFAULT_KEEPALIVE_ENABLED)
    }

    pub fn keepalive_time_ms(&self) -> i64 {
        self.int_param(PARAM_GRPC_KEEPALIVE_TIME_MS, GRPC_DEFAULT_KEEPALIVE_TIME_MS)
    }

    pub fn keepalive_timeout_ms(&self) -> i64 {
        self.int_param(
            PARAM_GRPC_KEEPALIVE_TIMEOUT_MS,
            GRPC_DEFAULT_KEEPALIVE_TIMEOUT_MS,
        )
    }

    pub fn keepalive_without_calls(&self) -> bool {
        self.bool_param(
            PARAM_GRPC_KEEPALIVE_WITHOUT_CALLS,
            GRPC_DEFAULT_KEEPALIVE_WITHOUT_CALLS,
        )
    }

    /// The user-agent string, matching Python's format and 2048-char cap.
    pub fn user_agent(&self) -> Result<String> {
        let ua = self
            .params
            .get(PARAM_USER_AGENT)
            .cloned()
            .or_else(|| std::env::var("SPARK_CONNECT_USER_AGENT").ok())
            .unwrap_or_else(|| "_SPARK_CONNECT_PYTHON".to_string());
        let ua_len = quote_len(&ua);
        if ua_len > 2048 {
            return Err(SparkError::connect_msg(format!(
                "'user_agent' parameter should not exceed 2048 characters after URL \
                 escaping, found {ua_len} characters."
            )));
        }
        let os = std::env::consts::OS.to_lowercase();
        Ok(format!("{ua} spark/{SPARK_VERSION} os/{os} python/rust"))
    }

    /// gRPC metadata: every param except the reserved channel-config keys.
    pub fn metadata(&self) -> Vec<(String, String)> {
        const RESERVED: &[&str] = &[
            PARAM_TOKEN,
            PARAM_USE_SSL,
            PARAM_USER_ID,
            PARAM_USER_AGENT,
            PARAM_SESSION_ID,
            PARAM_GRPC_KEEPALIVE_ENABLED,
            PARAM_GRPC_KEEPALIVE_TIME_MS,
            PARAM_GRPC_KEEPALIVE_TIMEOUT_MS,
            PARAM_GRPC_KEEPALIVE_WITHOUT_CALLS,
        ];
        self.params
            .iter()
            .filter(|(k, _)| !RESERVED.contains(&k.as_str()))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }

    fn bool_param(&self, key: &str, default: bool) -> bool {
        match self.params.get(key) {
            Some(v) => v.eq_ignore_ascii_case("true"),
            None => default,
        }
    }

    fn int_param(&self, key: &str, default: i64) -> i64 {
        self.params
            .get(key)
            .and_then(|v| v.parse().ok())
            .unwrap_or(default)
    }
}

/// CPython `urllib.parse._splitparams`: params begin at the first `;` at or
/// after the last `/`; if there is no `/`, at the first `;` anywhere.
fn split_params(path: &str) -> (&str, &str) {
    let search_from = path.rfind('/');
    let semi = match search_from {
        Some(last_slash) => path[last_slash..].find(';').map(|i| last_slash + i),
        None => path.find(';'),
    };
    match semi {
        Some(i) => (&path[..i], &path[i + 1..]),
        None => (path, ""),
    }
}

/// Parse `host`, `host:port`, `[ipv6]`, or `[ipv6]:port` into (host, port).
fn parse_authority(authority: &str, full_url: &str) -> Result<(String, u16)> {
    let missing_host = || {
        SparkError::value(
            "INVALID_CONNECT_URL",
            &[(
                "detail",
                &format!(
                    "Hostname is missing in the URL: '{full_url}'. Please update the URL \
                     to follow the correct format, e.g., 'sc://hostname:port'."
                ),
            )],
        )
    };

    let (host, port_str) = if let Some(bracket_end) = authority.strip_prefix('[') {
        // IPv6: [addr] or [addr]:port
        let close = bracket_end.find(']').ok_or_else(missing_host)?;
        let addr = &bracket_end[..close];
        let after = &bracket_end[close + 1..];
        let port = after.strip_prefix(':');
        (addr.to_string(), port)
    } else if let Some((h, p)) = authority.rsplit_once(':') {
        (h.to_string(), Some(p))
    } else {
        (authority.to_string(), None)
    };

    if host.is_empty() {
        return Err(missing_host());
    }

    let port = match port_str {
        None | Some("") => DEFAULT_PORT,
        Some(p) => p.parse::<u16>().map_err(|_| {
            SparkError::value(
                "INVALID_CONNECT_URL",
                &[(
                    "detail",
                    &format!("Port '{p}' in URL '{full_url}' is not a valid integer."),
                )],
            )
        })?,
    };

    Ok((host, port))
}

/// Decode `%XX` percent-escapes (CPython `urllib.parse.unquote`).
fn unquote(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' && i + 2 < bytes.len() {
            let hi = (bytes[i + 1] as char).to_digit(16);
            let lo = (bytes[i + 2] as char).to_digit(16);
            if let (Some(h), Some(l)) = (hi, lo) {
                out.push((h * 16 + l) as u8);
                i += 3;
                continue;
            }
        }
        out.push(bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// Length of `urllib.parse.quote(s)` with the default `safe='/'`.
fn quote_len(s: &str) -> usize {
    fn unreserved(b: u8) -> bool {
        b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b'-' | b'~' | b'/')
    }
    s.bytes().map(|b| if unreserved(b) { 1 } else { 3 }).sum()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_host_default_port() {
        let c = ChannelBuilder::parse("sc://localhost").unwrap();
        assert_eq!(c.endpoint(), "localhost:15002");
        assert!(!c.secure());
        assert!(!c.use_ssl());
    }

    #[test]
    fn host_with_port() {
        let c = ChannelBuilder::parse("sc://example.com:1234").unwrap();
        assert_eq!(c.endpoint(), "example.com:1234");
    }

    #[test]
    fn params_ssl_and_token() {
        let c = ChannelBuilder::parse("sc://localhost/;use_ssl=true;token=aaa").unwrap();
        assert!(c.use_ssl());
        assert!(c.secure());
        assert_eq!(c.token().as_deref(), Some("aaa"));
        assert_eq!(c.endpoint(), "localhost:15002");
    }

    #[test]
    fn keepalive_override() {
        let c = ChannelBuilder::parse("sc://localhost/;grpc_keepalive_time_ms=30000").unwrap();
        assert_eq!(c.keepalive_time_ms(), 30000);
        assert_eq!(c.keepalive_timeout_ms(), 20000); // default
        assert!(c.keepalive_enabled());
    }

    #[test]
    fn token_implies_secure() {
        let c = ChannelBuilder::parse("sc://localhost/;token=xyz").unwrap();
        assert!(c.secure());
        assert!(!c.use_ssl());
    }

    #[test]
    fn ipv6_host_is_bracketed() {
        let c = ChannelBuilder::parse("sc://[::1]:15003").unwrap();
        assert_eq!(c.host(), "[::1]");
        assert_eq!(c.endpoint(), "[::1]:15003");
    }

    #[test]
    fn percent_decoded_param_value() {
        let c = ChannelBuilder::parse("sc://localhost/;user_agent=my%20agent").unwrap();
        assert_eq!(c.get("user_agent"), Some("my agent"));
    }

    #[test]
    fn metadata_excludes_reserved() {
        let c = ChannelBuilder::parse("sc://localhost/;token=t;user_id=u;x-custom=v").unwrap();
        let md = c.metadata();
        assert_eq!(md, vec![("x-custom".to_string(), "v".to_string())]);
    }

    #[test]
    fn rejects_missing_scheme() {
        let e = ChannelBuilder::parse("localhost:15002").unwrap_err();
        assert_eq!(e.error_class, "INVALID_CONNECT_URL");
    }

    #[test]
    fn rejects_nonempty_path() {
        let e = ChannelBuilder::parse("sc://localhost/foo").unwrap_err();
        assert_eq!(e.error_class, "INVALID_CONNECT_URL");
    }

    #[test]
    fn rejects_bad_param_pair() {
        let e = ChannelBuilder::parse("sc://localhost/;use_ssl").unwrap_err();
        assert_eq!(e.error_class, "INVALID_CONNECT_URL");
    }

    #[test]
    fn rejects_missing_host() {
        let e = ChannelBuilder::parse("sc://:15002").unwrap_err();
        assert_eq!(e.error_class, "INVALID_CONNECT_URL");
    }

    #[test]
    fn session_id_must_be_uuid4() {
        let c = ChannelBuilder::parse(
            "sc://localhost/;session_id=550e8400-e29b-41d4-a716-446655440000",
        )
        .unwrap();
        assert!(c.session_id().is_ok());
        let bad = ChannelBuilder::parse("sc://localhost/;session_id=not-a-uuid").unwrap();
        assert!(bad.session_id().is_err());
    }
}