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, Default)]
66pub enum IcebergTransactionMode {
67 #[default]
68 #[serde(rename = "none")]
69 None,
70 #[serde(rename = "snapshot")]
71 Snapshot,
72}
73
74#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
76pub struct GlueCatalogConfig {
77 #[serde(rename = "glue.warehouse")]
81 pub warehouse: Option<String>,
82
83 #[serde(rename = "glue.endpoint")]
87 pub endpoint: Option<String>,
88
89 #[serde(rename = "glue.access-key-id")]
91 pub access_key_id: Option<String>,
92
93 #[serde(rename = "glue.secret-access-key")]
95 pub secret_access_key: Option<String>,
96
97 #[serde(rename = "glue.profile-name")]
99 pub profile_name: Option<String>,
100
101 #[serde(rename = "glue.region")]
103 pub region: Option<String>,
104
105 #[serde(rename = "glue.session-token")]
107 pub session_token: Option<String>,
108
109 #[serde(rename = "glue.id")]
111 pub id: Option<String>,
112}
113
114#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
116pub struct RestCatalogConfig {
117 #[serde(rename = "rest.uri")]
119 pub uri: Option<String>,
120
121 #[serde(rename = "rest.warehouse")]
123 pub warehouse: Option<String>,
124
125 #[serde(rename = "rest.oauth2-server-uri")]
127 pub oauth2_server_uri: Option<String>,
128
129 #[serde(rename = "rest.credential")]
133 pub credential: Option<String>,
134
135 #[serde(rename = "rest.token")]
137 pub token: Option<String>,
138
139 #[serde(rename = "rest.scope")]
141 pub scope: Option<String>,
142
143 #[serde(rename = "rest.prefix")]
148 pub prefix: Option<String>,
149
150 #[serde(default)]
152 #[serde(rename = "rest.headers")]
153 pub headers: Option<Vec<(String, String)>>,
154
155 #[serde(rename = "rest.audience")]
157 pub audience: Option<String>,
158
159 #[serde(rename = "rest.resource")]
161 pub resource: Option<String>,
162}
163
164#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
166pub struct S3TablesCatalogConfig {
167 #[serde(rename = "s3tables.table-bucket-arn")]
172 pub table_bucket_arn: Option<String>,
173
174 #[serde(rename = "s3tables.endpoint")]
179 pub endpoint: Option<String>,
180
181 #[serde(rename = "s3tables.access-key-id")]
183 pub access_key_id: Option<String>,
184
185 #[serde(rename = "s3tables.secret-access-key")]
187 pub secret_access_key: Option<String>,
188
189 #[serde(rename = "s3tables.session-token")]
191 pub session_token: Option<String>,
192
193 #[serde(rename = "s3tables.profile-name")]
195 pub profile_name: Option<String>,
196
197 #[serde(rename = "s3tables.region")]
199 pub region: Option<String>,
200}
201
202fn default_num_parsers() -> u32 {
203 4
204}
205
206#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
208pub struct IcebergReaderConfig {
209 pub mode: IcebergIngestMode,
211
212 #[serde(default)]
217 pub transaction_mode: IcebergTransactionMode,
218
219 pub timestamp_column: Option<String>,
244
245 pub snapshot_filter: Option<String>,
256
257 pub snapshot_id: Option<i64>,
268
269 pub datetime: Option<String>,
282
283 pub metadata_location: Option<String>,
288
289 pub table_name: Option<String>,
293
294 pub catalog_type: Option<IcebergCatalogType>,
299
300 #[serde(default = "default_num_parsers")]
304 #[schema(minimum = 1)]
305 pub num_parsers: u32,
306
307 #[serde(default, skip_serializing_if = "Option::is_none")]
316 pub max_retries: Option<u32>,
317
318 #[serde(flatten)]
319 pub glue_catalog_config: GlueCatalogConfig,
320
321 #[serde(flatten)]
322 pub rest_catalog_config: RestCatalogConfig,
323
324 #[serde(flatten)]
325 pub s3tables_catalog_config: S3TablesCatalogConfig,
326
327 #[serde(flatten)]
331 pub fileio_config: HashMap<String, String>,
332}
333
334impl IcebergReaderConfig {
335 pub fn validate_catalog_config(&self) -> Result<(), String> {
336 self.validate_metadata_location()?;
337 self.validate_table_name()?;
338 self.validate_glue_catalog_config()?;
339 self.validate_rest_catalog_config()?;
340 self.validate_s3tables_catalog_config()?;
341
342 Ok(())
343 }
344
345 pub fn validate_glue_catalog_config(&self) -> Result<(), String> {
347 if self.catalog_type == Some(IcebergCatalogType::Glue) {
348 if self.glue_catalog_config.warehouse.is_none() {
349 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());
350 }
351 } else {
352 ensure_glue_property_not_set(&self.glue_catalog_config.warehouse, "warehouse")?;
353 ensure_glue_property_not_set(&self.glue_catalog_config.endpoint, "uri")?;
354 ensure_glue_property_not_set(&self.glue_catalog_config.access_key_id, "access-key-id")?;
355 ensure_glue_property_not_set(
356 &self.glue_catalog_config.secret_access_key,
357 "secret-access-key",
358 )?;
359 ensure_glue_property_not_set(&self.glue_catalog_config.profile_name, "profile-name")?;
360 ensure_glue_property_not_set(&self.glue_catalog_config.region, "region")?;
361 ensure_glue_property_not_set(&self.glue_catalog_config.session_token, "session-token")?;
362 ensure_glue_property_not_set(&self.glue_catalog_config.id, "id")?;
363 }
364
365 Ok(())
366 }
367
368 pub fn validate_rest_catalog_config(&self) -> Result<(), String> {
370 if self.catalog_type == Some(IcebergCatalogType::Rest) {
371 if self.rest_catalog_config.uri.is_none() {
372 return Err(r#"missing Iceberg Rest catalog URI—set the 'rest.uri' property when using "catalog_type" = "rest""#.to_string());
373 }
374 } else {
375 ensure_rest_property_not_set(&self.rest_catalog_config.uri, "uri")?;
376 ensure_rest_property_not_set(&self.rest_catalog_config.warehouse, "warehouse")?;
377 ensure_rest_property_not_set(
378 &self.rest_catalog_config.oauth2_server_uri,
379 "oauth2_server_uri",
380 )?;
381 ensure_rest_property_not_set(&self.rest_catalog_config.credential, "credential")?;
382 ensure_rest_property_not_set(&self.rest_catalog_config.token, "token")?;
383 ensure_rest_property_not_set(&self.rest_catalog_config.scope, "scope")?;
384 ensure_rest_property_not_set(&self.rest_catalog_config.prefix, "prefix")?;
385 ensure_rest_property_not_set(&self.rest_catalog_config.headers, "headers")?;
386 ensure_rest_property_not_set(&self.rest_catalog_config.audience, "audience")?;
387 ensure_rest_property_not_set(&self.rest_catalog_config.resource, "resource")?;
388 }
389
390 Ok(())
391 }
392
393 pub fn validate_s3tables_catalog_config(&self) -> Result<(), String> {
395 if self.catalog_type == Some(IcebergCatalogType::S3Tables) {
396 if self.s3tables_catalog_config.table_bucket_arn.is_none() {
397 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());
398 }
399 } else {
400 ensure_s3tables_property_not_set(
401 &self.s3tables_catalog_config.table_bucket_arn,
402 "table-bucket-arn",
403 )?;
404 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.endpoint, "endpoint")?;
405 ensure_s3tables_property_not_set(
406 &self.s3tables_catalog_config.access_key_id,
407 "access-key-id",
408 )?;
409 ensure_s3tables_property_not_set(
410 &self.s3tables_catalog_config.secret_access_key,
411 "secret-access-key",
412 )?;
413 ensure_s3tables_property_not_set(
414 &self.s3tables_catalog_config.session_token,
415 "session-token",
416 )?;
417 ensure_s3tables_property_not_set(
418 &self.s3tables_catalog_config.profile_name,
419 "profile-name",
420 )?;
421 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.region, "region")?;
422 }
423
424 Ok(())
425 }
426
427 pub fn validate_table_name(&self) -> Result<(), String> {
429 if self.catalog_type.is_none() && self.table_name.is_some() {
430 Err("unexpected 'table_name' property: the 'table_name' property is valid only when an Iceberg catalog is configured using 'catalog_type'".to_string())
431 } else if self.catalog_type.is_some() && self.table_name.is_none() {
432 Err("missing 'table_name' property—'table_name' must be specified when Iceberg catalog is configured using 'catalog_type'".to_string())
433 } else {
434 Ok(())
435 }
436 }
437
438 pub fn validate_metadata_location(&self) -> Result<(), String> {
440 if self.catalog_type.is_none() && self.metadata_location.is_none() {
441 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())
442 } else if self.catalog_type.is_some() && self.metadata_location.is_some() {
443 Err("unexpected 'metadata_location' property: the 'metadata_location' property is not supported when an Iceberg catalog is configured using 'catalog_type'".to_string())
444 } else {
445 Ok(())
446 }
447 }
448}
449
450fn ensure_glue_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
451 if property.is_some() {
452 Err(format!(
453 r#"unexpected 'glue.{name}' property—Glue catalog configuration properties are only valid when "catalog_type" = "glue""#
454 ))
455 } else {
456 Ok(())
457 }
458}
459
460fn ensure_rest_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
461 if property.is_some() {
462 Err(format!(
463 r#"unexpected 'rest.{name}' property—Rest catalog configuration properties are only valid when "catalog_type" = "rest""#
464 ))
465 } else {
466 Ok(())
467 }
468}
469
470fn ensure_s3tables_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
471 if property.is_some() {
472 Err(format!(
473 r#"unexpected 's3tables.{name}' property—S3 Tables catalog configuration properties are only valid when "catalog_type" = "s3tables""#
474 ))
475 } else {
476 Ok(())
477 }
478}
479
480impl IcebergReaderConfig {
481 pub fn max_retries(&self) -> u32 {
483 self.max_retries.unwrap_or(u32::MAX)
484 }
485
486 pub fn snapshot(&self) -> bool {
488 matches!(
489 &self.mode,
490 IcebergIngestMode::Snapshot | IcebergIngestMode::SnapshotAndFollow
491 )
492 }
493
494 pub fn follow(&self) -> bool {
497 matches!(
498 &self.mode,
499 IcebergIngestMode::SnapshotAndFollow | IcebergIngestMode::Follow
500 )
501 }
502}
503
504#[cfg(test)]
505mod test {
506 use super::*;
507 use serde_json::json;
508
509 fn config(value: serde_json::Value) -> IcebergReaderConfig {
510 serde_json::from_value(value).unwrap()
511 }
512
513 #[test]
514 fn s3tables_config_deserializes() {
515 let config = config(json!({
516 "mode": "snapshot",
517 "catalog_type": "s3tables",
518 "table_name": "namespace.table",
519 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
520 "s3tables.region": "us-east-2",
521 "s3tables.access-key-id": "key",
522 "s3tables.secret-access-key": "secret",
523 "s3tables.session-token": "token",
524 "s3tables.profile-name": "profile",
525 "s3tables.endpoint": "http://localhost:4566",
526 }));
527
528 let s3tables = &config.s3tables_catalog_config;
531 assert_eq!(
532 s3tables.table_bucket_arn.as_deref(),
533 Some("arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket")
534 );
535 assert_eq!(s3tables.region.as_deref(), Some("us-east-2"));
536 assert_eq!(s3tables.access_key_id.as_deref(), Some("key"));
537 assert_eq!(s3tables.secret_access_key.as_deref(), Some("secret"));
538 assert_eq!(s3tables.session_token.as_deref(), Some("token"));
539 assert_eq!(s3tables.profile_name.as_deref(), Some("profile"));
540 assert_eq!(s3tables.endpoint.as_deref(), Some("http://localhost:4566"));
541 assert!(config.fileio_config.is_empty());
542
543 config.validate_catalog_config().unwrap();
544 }
545
546 #[test]
547 fn s3tables_requires_table_bucket_arn() {
548 let err = config(json!({
549 "mode": "snapshot",
550 "catalog_type": "s3tables",
551 "table_name": "namespace.table",
552 }))
553 .validate_catalog_config()
554 .unwrap_err();
555 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
556 }
557
558 #[test]
559 fn s3tables_props_rejected_for_other_catalog() {
560 let err = config(json!({
561 "mode": "snapshot",
562 "catalog_type": "glue",
563 "table_name": "namespace.table",
564 "glue.warehouse": "s3://warehouse/",
565 "s3tables.region": "us-east-2",
566 }))
567 .validate_catalog_config()
568 .unwrap_err();
569 assert!(err.contains("s3tables.region"), "{err}");
570 }
571
572 #[test]
573 fn s3tables_props_rejected_without_catalog() {
574 let err = config(json!({
575 "mode": "snapshot",
576 "metadata_location": "s3://warehouse/metadata.json",
577 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
578 }))
579 .validate_catalog_config()
580 .unwrap_err();
581 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
582 }
583
584 #[test]
585 fn num_parsers_and_max_retries_defaults() {
586 let config: IcebergReaderConfig = serde_json::from_str(
588 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
589 )
590 .unwrap();
591 assert_eq!(config.num_parsers, 4);
592 assert_eq!(config.max_retries, None);
593 assert_eq!(config.max_retries(), u32::MAX);
594 }
595
596 #[test]
597 fn num_parsers_and_max_retries_explicit() {
598 let config: IcebergReaderConfig = serde_json::from_str(
599 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":8,"max_retries":0}"#,
600 )
601 .unwrap();
602 assert_eq!(config.num_parsers, 8);
603 assert_eq!(config.max_retries, Some(0));
604 assert_eq!(config.max_retries(), 0);
606 }
607
608 #[test]
609 fn transaction_mode_defaults_to_none() {
610 let config: IcebergReaderConfig = serde_json::from_str(
611 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
612 )
613 .unwrap();
614 assert_eq!(config.transaction_mode, IcebergTransactionMode::None);
615 }
616
617 #[test]
618 fn transaction_mode_snapshot_parses() {
619 let config: IcebergReaderConfig = serde_json::from_str(
620 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","transaction_mode":"snapshot"}"#,
621 )
622 .unwrap();
623 assert_eq!(config.transaction_mode, IcebergTransactionMode::Snapshot);
624 }
625
626 #[test]
627 fn reader_config_roundtrips() {
628 let config: IcebergReaderConfig = serde_json::from_str(
629 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":2}"#,
630 )
631 .unwrap();
632 let serialized = serde_json::to_string(&config).unwrap();
633 let reparsed: IcebergReaderConfig = serde_json::from_str(&serialized).unwrap();
634 assert_eq!(config, reparsed);
635 assert_eq!(reparsed.num_parsers, 2);
636 }
637}