icepick 0.4.1

Experimental Rust client for Apache Iceberg with WASM support for AWS S3 Tables and Cloudflare R2
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Client constructor methods for IcebergRestCatalog
use super::commit_types::{CommitTableRequest, CommitTableResponse};
use super::credentials::RestCredentialProvider;
use super::types;
use super::IcebergRestCatalog;
use crate::catalog::{
    AuthProvider, CatalogError, CatalogOptions, HttpClientConfig, R2Config, Result,
};
use crate::io::FileIO;
use crate::spec::TableIdent;
use reqwest::Client;
use std::sync::Arc;

#[cfg(not(target_family = "wasm"))]
use super::arn::{parse_s3tables_arn, ARN_ENCODE_SET};

#[cfg(not(target_family = "wasm"))]
use aws_credential_types::provider::ProvideCredentials;

#[cfg(not(target_family = "wasm"))]
use percent_encoding::utf8_percent_encode;

/// Fetch catalog configuration from /v1/config endpoint
async fn fetch_config_response(
    http_client: &Client,
    auth: &dyn AuthProvider,
    endpoint: &str,
    warehouse: &str,
) -> Result<types::ConfigResponse> {
    let config_url = format!(
        "{}/v1/config?warehouse={}",
        endpoint.trim_end_matches('/'),
        urlencoding::encode(warehouse)
    );

    let req = http_client
        .get(&config_url)
        .build()
        .map_err(|e| CatalogError::HttpError(format!("Failed to build config request: {}", e)))?;

    let signed_req = auth.sign_request(req).await?;

    let response = http_client
        .execute(signed_req)
        .await
        .map_err(|e| CatalogError::HttpError(format!("Config request failed: {}", e)))?;

    let status = response.status();
    let body_text = response
        .text()
        .await
        .map_err(|e| CatalogError::HttpError(format!(
            "Failed to read response body from {}: {}. This may indicate a network interruption or invalid response encoding.",
            config_url, e
        )))?;

    if !status.is_success() {
        return Err(CatalogError::HttpError(format!(
            "Config request failed with status {}: {}",
            status, body_text
        )));
    }

    serde_json::from_str(&body_text)
        .map_err(|e| CatalogError::HttpError(format!("Failed to parse config response: {}", e)))
}

impl IcebergRestCatalog {
    /// Create a generic Iceberg REST catalog from preconfigured components.
    pub(crate) fn from_components(
        name: String,
        endpoint: impl Into<String>,
        prefix: impl Into<String>,
        auth_provider: Box<dyn AuthProvider>,
        file_io: FileIO,
        options: CatalogOptions,
    ) -> Result<Self> {
        let http_client = build_http_client(options.http())?;

        Ok(Self {
            endpoint: endpoint.into(),
            prefix: prefix.into(),
            http_client,
            auth_provider,
            file_io,
            name,
            options,
        })
    }

    /// Create catalog for Cloudflare R2 Data Catalog (shortcut)
    pub async fn from_r2(
        name: String,
        account_id: impl Into<String>,
        bucket_name: impl Into<String>,
        api_token: impl Into<String>,
    ) -> Result<Self> {
        Self::from_r2_with_options(
            name,
            account_id,
            bucket_name,
            api_token,
            CatalogOptions::default(),
        )
        .await
    }

    /// Create catalog for Cloudflare R2 Data Catalog with explicit options
    pub async fn from_r2_with_options(
        name: String,
        account_id: impl Into<String>,
        bucket_name: impl Into<String>,
        api_token: impl Into<String>,
        options: CatalogOptions,
    ) -> Result<Self> {
        let config = R2Config {
            account_id: account_id.into(),
            bucket_name: bucket_name.into(),
            api_token: api_token.into(),
            endpoint_override: None,
        };
        Self::from_r2_config_with_options(name, config, options).await
    }

    pub(crate) async fn from_r2_config_with_options(
        name: String,
        config: R2Config,
        options: CatalogOptions,
    ) -> Result<Self> {
        let endpoint = config.endpoint_override.unwrap_or_else(|| {
            format!(
                "https://catalog.cloudflarestorage.com/{}/{}",
                config.account_id, config.bucket_name
            )
        });

        let auth = Box::new(crate::catalog::BearerTokenAuthProvider::new(
            config.api_token,
        ));
        let http_client = build_http_client(options.http())?;

        // Construct warehouse name from account_id and bucket_name
        let warehouse = format!("{}_{}", config.account_id, config.bucket_name);

        // Fetch catalog configuration
        let config_response =
            fetch_config_response(&http_client, auth.as_ref(), &endpoint, &warehouse).await?;

        // Merge configuration: defaults < client properties < overrides
        let mut properties = config_response.defaults;
        properties.insert("warehouse".to_string(), warehouse.clone());
        properties.extend(config_response.overrides);

        // Extract prefix from server configuration (defaults to empty string)
        let prefix = properties.get("prefix").cloned().unwrap_or_default();

        // Configure FileIO for R2 S3-compatible storage using opendal
        let r2_endpoint = format!("https://{}.r2.cloudflarestorage.com", config.account_id);

        let mut s3_config_vec = vec![
            ("endpoint".to_string(), r2_endpoint),
            ("bucket".to_string(), config.bucket_name.clone()),
            ("region".to_string(), "auto".to_string()), // R2 always uses "auto" region
        ];

        // Apply properties from config response
        for (key, value) in &properties {
            if key.starts_with("s3.") {
                let opendal_key = key.strip_prefix("s3.").unwrap_or(key).to_string();
                s3_config_vec.push((opendal_key, value.clone()));
            }
        }

        let operator =
            opendal::Operator::via_iter(opendal::Scheme::S3, s3_config_vec).map_err(|e| {
                CatalogError::Unexpected(format!("Failed to create S3 operator: {}", e))
            })?;
        let file_io = FileIO::new(operator);

        Ok(Self {
            endpoint,
            prefix,
            http_client,
            auth_provider: auth,
            file_io,
            name,
            options,
        })
    }

    /// Create catalog for Cloudflare R2 with a pre-configured FileIO (for explicit credentials)
    pub(crate) async fn from_r2_with_file_io(
        name: String,
        config: R2Config,
        file_io: FileIO,
        options: CatalogOptions,
    ) -> Result<Self> {
        let endpoint = config.endpoint_override.unwrap_or_else(|| {
            format!(
                "https://catalog.cloudflarestorage.com/{}/{}",
                config.account_id, config.bucket_name
            )
        });

        let auth = Box::new(crate::catalog::BearerTokenAuthProvider::new(
            config.api_token,
        ));
        let http_client = build_http_client(options.http())?;

        // Construct warehouse name from account_id and bucket_name
        let warehouse = format!("{}_{}", config.account_id, config.bucket_name);

        // Fetch catalog configuration
        let config_response =
            fetch_config_response(&http_client, auth.as_ref(), &endpoint, &warehouse).await?;

        // Merge configuration: defaults < client properties < overrides
        let mut properties = config_response.defaults;
        properties.insert("warehouse".to_string(), warehouse.clone());
        properties.extend(config_response.overrides);

        // Extract prefix from server configuration (defaults to empty string)
        let prefix = properties.get("prefix").cloned().unwrap_or_default();

        // Use the provided FileIO instead of creating a new one
        Ok(Self {
            endpoint,
            prefix,
            http_client,
            auth_provider: auth,
            file_io,
            name,
            options,
        })
    }

    /// Create catalog from a catalog URL and bearer token (calls /v1/config, sets up vended credentials)
    pub async fn from_url(
        name: impl Into<String>,
        catalog_url: impl Into<String>,
        token: impl Into<String>,
        warehouse: Option<String>,
    ) -> Result<Self> {
        Self::from_url_with_options(
            name,
            catalog_url,
            token,
            warehouse,
            CatalogOptions::default(),
        )
        .await
    }

    /// Create catalog from a catalog URL and bearer token with custom options.
    pub async fn from_url_with_options(
        name: impl Into<String>,
        catalog_url: impl Into<String>,
        token: impl Into<String>,
        warehouse: Option<String>,
        options: CatalogOptions,
    ) -> Result<Self> {
        let name = name.into();
        let endpoint = catalog_url.into();
        let token = token.into();

        // Derive warehouse from URL if not provided
        // URL format: https://catalog.example.com/account/bucket -> account_bucket
        let warehouse = warehouse.unwrap_or_else(|| derive_warehouse_from_url(&endpoint));

        let auth = Box::new(crate::catalog::BearerTokenAuthProvider::new(token.clone()));
        let http_client = build_http_client(options.http())?;

        // Fetch catalog configuration
        let config_response =
            fetch_config_response(&http_client, auth.as_ref(), &endpoint, &warehouse).await?;

        // Merge configuration: defaults < overrides
        let mut properties = config_response.defaults;
        properties.extend(config_response.overrides);

        // Extract prefix from server configuration
        let prefix = properties.get("prefix").cloned().unwrap_or_default();

        // Extract S3 endpoint from config if available (for R2, this comes from properties)
        // If not in config, derive from catalog URL for R2 (https://catalog.cloudflarestorage.com/{account_id}/...)
        let s3_endpoint = properties.get("s3.endpoint").cloned().or_else(|| {
            if endpoint.contains("cloudflarestorage.com") {
                // Parse account_id from R2 catalog URL: https://catalog.cloudflarestorage.com/{account_id}/{bucket}
                endpoint
                    .strip_prefix("https://catalog.cloudflarestorage.com/")
                    .and_then(|rest| rest.split('/').next())
                    .map(|account_id| format!("https://{}.r2.cloudflarestorage.com", account_id))
            } else {
                None
            }
        });

        // Create credential provider for vended credentials
        let credential_provider = Arc::new(RestCredentialProvider {
            endpoint: endpoint.clone(),
            prefix: prefix.clone(),
            token: token.clone(),
            http_client: http_client.clone(),
            s3_endpoint,
            credential_cache: Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())),
            table_registry: Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())),
        });

        // Create FileIO with vended credential support
        let file_io = FileIO::with_vended_credentials(credential_provider);

        Ok(Self {
            endpoint,
            prefix,
            http_client,
            auth_provider: auth,
            file_io,
            name,
            options,
        })
    }

    /// Load credentials for a table from the catalog's /credentials endpoint
    pub async fn load_table_credentials(
        &self,
        identifier: &TableIdent,
    ) -> Result<types::LoadTableCredentialsResponse> {
        let namespace = identifier.namespace().as_ref().join("/");
        let table_name = identifier.name();

        let url = format!(
            "{}/v1/{}/namespaces/{}/tables/{}/credentials",
            self.endpoint.trim_end_matches('/'),
            self.prefix,
            namespace,
            table_name
        );

        let req = self.http_client.get(&url).build().map_err(|e| {
            CatalogError::HttpError(format!("Failed to build credentials request: {}", e))
        })?;

        let response = self.send_request(req).await?;
        let json_value = self.handle_response(response).await?;

        serde_json::from_value(json_value).map_err(|e| {
            CatalogError::HttpError(format!("Failed to parse credentials response: {}", e))
        })
    }

    /// Create catalog for AWS S3 Tables
    #[cfg(not(target_family = "wasm"))]
    pub async fn from_s3_tables_arn(name: String, arn: &str) -> Result<Self> {
        Self::from_s3_tables_arn_with_options(name, arn, CatalogOptions::default()).await
    }

    #[cfg(not(target_family = "wasm"))]
    pub async fn from_s3_tables_arn_with_options(
        name: String,
        arn: &str,
        options: CatalogOptions,
    ) -> Result<Self> {
        let (region, _bucket_name) = parse_s3tables_arn(arn)?;
        let endpoint = format!("https://s3tables.{}.amazonaws.com/iceberg", region);

        // URL-encode the ARN for use in path
        let warehouse_prefix = utf8_percent_encode(arn, ARN_ENCODE_SET).to_string();

        // Load AWS credentials
        let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
        let credentials = config
            .credentials_provider()
            .ok_or_else(|| CatalogError::AuthError("No credentials provider found".to_string()))?
            .provide_credentials()
            .await
            .map_err(|e| CatalogError::AuthError(format!("Failed to load credentials: {}", e)))?;

        let auth = Box::new(crate::catalog::SigV4AuthProvider::new(
            region.clone(),
            "s3tables".to_string(),
            credentials.clone(),
        ));

        let http_client = build_http_client(options.http())?;

        // Create FileIO with AWS credentials for multi-bucket support
        // S3 Tables stores data in AWS-managed buckets that may be in different regions
        let file_io_credentials = crate::io::AwsCredentials {
            access_key_id: credentials.access_key_id().to_string(),
            secret_access_key: credentials.secret_access_key().to_string(),
            session_token: credentials.session_token().map(|s| s.to_string()),
        };
        let file_io = FileIO::from_aws_credentials(file_io_credentials, region.clone());

        Ok(Self {
            endpoint,
            prefix: warehouse_prefix, // url() method already adds /v1/
            http_client,
            auth_provider: auth,
            file_io,
            name,
            options,
        })
    }

    /// Commit table changes
    pub async fn commit_table(
        &self,
        identifier: &TableIdent,
        request: CommitTableRequest,
    ) -> Result<CommitTableResponse> {
        let namespace = identifier.namespace().as_ref().join("/");
        let table_name = identifier.name();

        let url = self.table_url(&namespace, table_name, true);

        // Diagnostic logging for debugging
        let req = self
            .http_client
            .post(&url)
            .json(&request)
            .build()
            .map_err(|e| CatalogError::HttpError(format!("Failed to build request: {}", e)))?;

        let response = self.send_request(req).await?;

        if response.status().as_u16() == 409 {
            return Err(CatalogError::Conflict(
                "Concurrent modification detected".to_string(),
            ));
        }

        // Handle response using common handler (supports empty responses)
        let json_value = self.handle_response(response).await?;

        let commit_response: CommitTableResponse = serde_json::from_value(json_value)
            .map_err(|e| CatalogError::HttpError(format!("Failed to parse response: {}", e)))?;

        Ok(commit_response)
    }
}

#[cfg(not(target_family = "wasm"))]
fn build_http_client(config: &HttpClientConfig) -> Result<Client> {
    let mut builder = Client::builder();
    if let Some(timeout) = config.timeout() {
        builder = builder.timeout(timeout);
    }
    if let Some(connect_timeout) = config.connect_timeout() {
        builder = builder.connect_timeout(connect_timeout);
    }
    if let Some(user_agent) = config.user_agent() {
        builder = builder.user_agent(user_agent.to_string());
    }
    builder
        .build()
        .map_err(|e| CatalogError::HttpError(format!("Failed to build HTTP client: {}", e)))
}

#[cfg(target_family = "wasm")]
fn build_http_client(_config: &HttpClientConfig) -> Result<Client> {
    Client::builder()
        .build()
        .map_err(|e| CatalogError::HttpError(format!("Failed to build HTTP client: {}", e)))
}

/// Derive warehouse from URL (last two path segments joined with underscore)
fn derive_warehouse_from_url(url: &str) -> String {
    // Parse URL and extract path segments
    if let Ok(parsed) = url::Url::parse(url) {
        let segments: Vec<&str> = parsed
            .path_segments()
            .map(|s| s.collect())
            .unwrap_or_default();

        // Take last two non-empty segments
        let non_empty: Vec<&str> = segments.into_iter().filter(|s| !s.is_empty()).collect();
        if non_empty.len() >= 2 {
            return format!(
                "{}_{}",
                non_empty[non_empty.len() - 2],
                non_empty[non_empty.len() - 1]
            );
        } else if non_empty.len() == 1 {
            return non_empty[0].to_string();
        }
    }

    // Fallback: use the full URL as warehouse (will likely fail, but provides context)
    url.to_string()
}

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

    #[test]
    fn test_derive_warehouse_from_url() {
        assert_eq!(
            derive_warehouse_from_url("https://catalog.example.com/account/bucket"),
            "account_bucket"
        );
        assert_eq!(
            derive_warehouse_from_url("https://catalog.cloudflarestorage.com/abc123/my-bucket"),
            "abc123_my-bucket"
        );
        assert_eq!(
            derive_warehouse_from_url("https://example.com/single"),
            "single"
        );
    }
}