1use serde::{Deserialize, Serialize};
2use std::{collections::HashMap, fmt::Display};
3use utoipa::ToSchema;
4
5#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
17pub enum IcebergIngestMode {
18 #[serde(rename = "snapshot")]
20 Snapshot,
21
22 #[serde(rename = "follow")]
24 Follow,
25
26 #[serde(rename = "snapshot_and_follow")]
28 SnapshotAndFollow,
29}
30
31impl Display for IcebergIngestMode {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 IcebergIngestMode::Snapshot => f.write_str("snapshot"),
35 IcebergIngestMode::Follow => f.write_str("follow"),
36 IcebergIngestMode::SnapshotAndFollow => f.write_str("snapshot_and_follow"),
37 }
38 }
39}
40
41#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
42pub enum IcebergCatalogType {
43 #[serde(rename = "rest")]
44 Rest,
45 #[serde(rename = "glue")]
46 Glue,
47 #[serde(rename = "s3tables")]
48 S3Tables,
49}
50
51#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
53pub struct GlueCatalogConfig {
54 #[serde(rename = "glue.warehouse")]
58 pub warehouse: Option<String>,
59
60 #[serde(rename = "glue.endpoint")]
64 pub endpoint: Option<String>,
65
66 #[serde(rename = "glue.access-key-id")]
68 pub access_key_id: Option<String>,
69
70 #[serde(rename = "glue.secret-access-key")]
72 pub secret_access_key: Option<String>,
73
74 #[serde(rename = "glue.profile-name")]
76 pub profile_name: Option<String>,
77
78 #[serde(rename = "glue.region")]
80 pub region: Option<String>,
81
82 #[serde(rename = "glue.session-token")]
84 pub session_token: Option<String>,
85
86 #[serde(rename = "glue.id")]
88 pub id: Option<String>,
89}
90
91#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
93pub struct RestCatalogConfig {
94 #[serde(rename = "rest.uri")]
96 pub uri: Option<String>,
97
98 #[serde(rename = "rest.warehouse")]
100 pub warehouse: Option<String>,
101
102 #[serde(rename = "rest.oauth2-server-uri")]
104 pub oauth2_server_uri: Option<String>,
105
106 #[serde(rename = "rest.credential")]
110 pub credential: Option<String>,
111
112 #[serde(rename = "rest.token")]
114 pub token: Option<String>,
115
116 #[serde(rename = "rest.scope")]
118 pub scope: Option<String>,
119
120 #[serde(rename = "rest.prefix")]
125 pub prefix: Option<String>,
126
127 #[serde(default)]
129 #[serde(rename = "rest.headers")]
130 pub headers: Option<Vec<(String, String)>>,
131
132 #[serde(rename = "rest.audience")]
134 pub audience: Option<String>,
135
136 #[serde(rename = "rest.resource")]
138 pub resource: Option<String>,
139}
140
141#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
143pub struct S3TablesCatalogConfig {
144 #[serde(rename = "s3tables.table-bucket-arn")]
149 pub table_bucket_arn: Option<String>,
150
151 #[serde(rename = "s3tables.endpoint")]
156 pub endpoint: Option<String>,
157
158 #[serde(rename = "s3tables.access-key-id")]
160 pub access_key_id: Option<String>,
161
162 #[serde(rename = "s3tables.secret-access-key")]
164 pub secret_access_key: Option<String>,
165
166 #[serde(rename = "s3tables.session-token")]
168 pub session_token: Option<String>,
169
170 #[serde(rename = "s3tables.profile-name")]
172 pub profile_name: Option<String>,
173
174 #[serde(rename = "s3tables.region")]
176 pub region: Option<String>,
177}
178
179#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
181pub struct IcebergReaderConfig {
182 pub mode: IcebergIngestMode,
184
185 pub timestamp_column: Option<String>,
210
211 pub snapshot_filter: Option<String>,
222
223 pub snapshot_id: Option<i64>,
234
235 pub datetime: Option<String>,
248
249 pub metadata_location: Option<String>,
254
255 pub table_name: Option<String>,
259
260 pub catalog_type: Option<IcebergCatalogType>,
265
266 #[serde(flatten)]
267 pub glue_catalog_config: GlueCatalogConfig,
268
269 #[serde(flatten)]
270 pub rest_catalog_config: RestCatalogConfig,
271
272 #[serde(flatten)]
273 pub s3tables_catalog_config: S3TablesCatalogConfig,
274
275 #[serde(flatten)]
279 pub fileio_config: HashMap<String, String>,
280}
281
282impl IcebergReaderConfig {
283 pub fn validate_catalog_config(&self) -> Result<(), String> {
284 self.validate_metadata_location()?;
285 self.validate_table_name()?;
286 self.validate_glue_catalog_config()?;
287 self.validate_rest_catalog_config()?;
288 self.validate_s3tables_catalog_config()?;
289
290 Ok(())
291 }
292
293 pub fn validate_glue_catalog_config(&self) -> Result<(), String> {
295 if self.catalog_type == Some(IcebergCatalogType::Glue) {
296 if self.glue_catalog_config.warehouse.is_none() {
297 return Err(r#"missing Iceberg warehouse location—set the 'glue.warehouse' property to the location of the Iceberg tables managed by the catalog (e.g., 's3://my-data-warehouse/tables/') when using "catalog_type" = "glue""#.to_string());
298 }
299 } else {
300 ensure_glue_property_not_set(&self.glue_catalog_config.warehouse, "warehouse")?;
301 ensure_glue_property_not_set(&self.glue_catalog_config.endpoint, "uri")?;
302 ensure_glue_property_not_set(&self.glue_catalog_config.access_key_id, "access-key-id")?;
303 ensure_glue_property_not_set(
304 &self.glue_catalog_config.secret_access_key,
305 "secret-access-key",
306 )?;
307 ensure_glue_property_not_set(&self.glue_catalog_config.profile_name, "profile-name")?;
308 ensure_glue_property_not_set(&self.glue_catalog_config.region, "region")?;
309 ensure_glue_property_not_set(&self.glue_catalog_config.session_token, "session-token")?;
310 ensure_glue_property_not_set(&self.glue_catalog_config.id, "id")?;
311 }
312
313 Ok(())
314 }
315
316 pub fn validate_rest_catalog_config(&self) -> Result<(), String> {
318 if self.catalog_type == Some(IcebergCatalogType::Rest) {
319 if self.rest_catalog_config.uri.is_none() {
320 return Err(r#"missing Iceberg Rest catalog URI—set the 'rest.uri' property when using "catalog_type" = "rest""#.to_string());
321 }
322 } else {
323 ensure_rest_property_not_set(&self.rest_catalog_config.uri, "uri")?;
324 ensure_rest_property_not_set(&self.rest_catalog_config.warehouse, "warehouse")?;
325 ensure_rest_property_not_set(
326 &self.rest_catalog_config.oauth2_server_uri,
327 "oauth2_server_uri",
328 )?;
329 ensure_rest_property_not_set(&self.rest_catalog_config.credential, "credential")?;
330 ensure_rest_property_not_set(&self.rest_catalog_config.token, "token")?;
331 ensure_rest_property_not_set(&self.rest_catalog_config.scope, "scope")?;
332 ensure_rest_property_not_set(&self.rest_catalog_config.prefix, "prefix")?;
333 ensure_rest_property_not_set(&self.rest_catalog_config.headers, "headers")?;
334 ensure_rest_property_not_set(&self.rest_catalog_config.audience, "audience")?;
335 ensure_rest_property_not_set(&self.rest_catalog_config.resource, "resource")?;
336 }
337
338 Ok(())
339 }
340
341 pub fn validate_s3tables_catalog_config(&self) -> Result<(), String> {
343 if self.catalog_type == Some(IcebergCatalogType::S3Tables) {
344 if self.s3tables_catalog_config.table_bucket_arn.is_none() {
345 return Err(r#"missing S3 table bucket ARN; set the 's3tables.table-bucket-arn' property to the ARN of the S3 table bucket (e.g., 'arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket') when using "catalog_type" = "s3tables""#.to_string());
346 }
347 } else {
348 ensure_s3tables_property_not_set(
349 &self.s3tables_catalog_config.table_bucket_arn,
350 "table-bucket-arn",
351 )?;
352 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.endpoint, "endpoint")?;
353 ensure_s3tables_property_not_set(
354 &self.s3tables_catalog_config.access_key_id,
355 "access-key-id",
356 )?;
357 ensure_s3tables_property_not_set(
358 &self.s3tables_catalog_config.secret_access_key,
359 "secret-access-key",
360 )?;
361 ensure_s3tables_property_not_set(
362 &self.s3tables_catalog_config.session_token,
363 "session-token",
364 )?;
365 ensure_s3tables_property_not_set(
366 &self.s3tables_catalog_config.profile_name,
367 "profile-name",
368 )?;
369 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.region, "region")?;
370 }
371
372 Ok(())
373 }
374
375 pub fn validate_table_name(&self) -> Result<(), String> {
377 if self.catalog_type.is_none() && self.table_name.is_some() {
378 Err("unexpected 'table_name' property: the 'table_name' property is valid only when an Iceberg catalog is configured using 'catalog_type'".to_string())
379 } else if self.catalog_type.is_some() && self.table_name.is_none() {
380 Err("missing 'table_name' property—'table_name' must be specified when Iceberg catalog is configured using 'catalog_type'".to_string())
381 } else {
382 Ok(())
383 }
384 }
385
386 pub fn validate_metadata_location(&self) -> Result<(), String> {
388 if self.catalog_type.is_none() && self.metadata_location.is_none() {
389 Err("missing metadata location: you must either specify an Iceberg catalog configuration by setting the 'catalog_type' property or provide a table metadata location directly via the 'metadata_location' property".to_string())
390 } else if self.catalog_type.is_some() && self.metadata_location.is_some() {
391 Err("unexpected 'metadata_location' property: the 'metadata_location' property is not supported when an Iceberg catalog is configured using 'catalog_type'".to_string())
392 } else {
393 Ok(())
394 }
395 }
396}
397
398fn ensure_glue_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
399 if property.is_some() {
400 Err(format!(
401 r#"unexpected 'glue.{name}' property—Glue catalog configuration properties are only valid when "catalog_type" = "glue""#
402 ))
403 } else {
404 Ok(())
405 }
406}
407
408fn ensure_rest_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
409 if property.is_some() {
410 Err(format!(
411 r#"unexpected 'rest.{name}' property—Rest catalog configuration properties are only valid when "catalog_type" = "rest""#
412 ))
413 } else {
414 Ok(())
415 }
416}
417
418fn ensure_s3tables_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
419 if property.is_some() {
420 Err(format!(
421 r#"unexpected 's3tables.{name}' property—S3 Tables catalog configuration properties are only valid when "catalog_type" = "s3tables""#
422 ))
423 } else {
424 Ok(())
425 }
426}
427
428impl IcebergReaderConfig {
429 pub fn snapshot(&self) -> bool {
431 matches!(
432 &self.mode,
433 IcebergIngestMode::Snapshot | IcebergIngestMode::SnapshotAndFollow
434 )
435 }
436
437 pub fn follow(&self) -> bool {
440 matches!(
441 &self.mode,
442 IcebergIngestMode::SnapshotAndFollow | IcebergIngestMode::Follow
443 )
444 }
445}
446
447#[cfg(test)]
448mod test {
449 use super::*;
450 use serde_json::json;
451
452 fn config(value: serde_json::Value) -> IcebergReaderConfig {
453 serde_json::from_value(value).unwrap()
454 }
455
456 #[test]
457 fn s3tables_config_deserializes() {
458 let config = config(json!({
459 "mode": "snapshot",
460 "catalog_type": "s3tables",
461 "table_name": "namespace.table",
462 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
463 "s3tables.region": "us-east-2",
464 "s3tables.access-key-id": "key",
465 "s3tables.secret-access-key": "secret",
466 "s3tables.session-token": "token",
467 "s3tables.profile-name": "profile",
468 "s3tables.endpoint": "http://localhost:4566",
469 }));
470
471 let s3tables = &config.s3tables_catalog_config;
474 assert_eq!(
475 s3tables.table_bucket_arn.as_deref(),
476 Some("arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket")
477 );
478 assert_eq!(s3tables.region.as_deref(), Some("us-east-2"));
479 assert_eq!(s3tables.access_key_id.as_deref(), Some("key"));
480 assert_eq!(s3tables.secret_access_key.as_deref(), Some("secret"));
481 assert_eq!(s3tables.session_token.as_deref(), Some("token"));
482 assert_eq!(s3tables.profile_name.as_deref(), Some("profile"));
483 assert_eq!(s3tables.endpoint.as_deref(), Some("http://localhost:4566"));
484 assert!(config.fileio_config.is_empty());
485
486 config.validate_catalog_config().unwrap();
487 }
488
489 #[test]
490 fn s3tables_requires_table_bucket_arn() {
491 let err = config(json!({
492 "mode": "snapshot",
493 "catalog_type": "s3tables",
494 "table_name": "namespace.table",
495 }))
496 .validate_catalog_config()
497 .unwrap_err();
498 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
499 }
500
501 #[test]
502 fn s3tables_props_rejected_for_other_catalog() {
503 let err = config(json!({
504 "mode": "snapshot",
505 "catalog_type": "glue",
506 "table_name": "namespace.table",
507 "glue.warehouse": "s3://warehouse/",
508 "s3tables.region": "us-east-2",
509 }))
510 .validate_catalog_config()
511 .unwrap_err();
512 assert!(err.contains("s3tables.region"), "{err}");
513 }
514
515 #[test]
516 fn s3tables_props_rejected_without_catalog() {
517 let err = config(json!({
518 "mode": "snapshot",
519 "metadata_location": "s3://warehouse/metadata.json",
520 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
521 }))
522 .validate_catalog_config()
523 .unwrap_err();
524 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
525 }
526}