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)]
74pub enum IcebergTransactionMode {
75 #[default]
76 #[serde(rename = "none")]
77 None,
78 #[serde(rename = "snapshot")]
79 Snapshot,
80 #[serde(rename = "catchup")]
81 Catchup,
82 #[serde(rename = "always")]
83 Always,
84}
85
86#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
88pub struct GlueCatalogConfig {
89 #[serde(rename = "glue.warehouse")]
93 pub warehouse: Option<String>,
94
95 #[serde(rename = "glue.endpoint")]
99 pub endpoint: Option<String>,
100
101 #[serde(rename = "glue.access-key-id")]
103 pub access_key_id: Option<String>,
104
105 #[serde(rename = "glue.secret-access-key")]
107 pub secret_access_key: Option<String>,
108
109 #[serde(rename = "glue.profile-name")]
111 pub profile_name: Option<String>,
112
113 #[serde(rename = "glue.region")]
115 pub region: Option<String>,
116
117 #[serde(rename = "glue.session-token")]
119 pub session_token: Option<String>,
120
121 #[serde(rename = "glue.id")]
123 pub id: Option<String>,
124}
125
126#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
128pub struct RestCatalogConfig {
129 #[serde(rename = "rest.uri")]
131 pub uri: Option<String>,
132
133 #[serde(rename = "rest.warehouse")]
135 pub warehouse: Option<String>,
136
137 #[serde(rename = "rest.oauth2-server-uri")]
139 pub oauth2_server_uri: Option<String>,
140
141 #[serde(rename = "rest.credential")]
145 pub credential: Option<String>,
146
147 #[serde(rename = "rest.token")]
149 pub token: Option<String>,
150
151 #[serde(rename = "rest.scope")]
153 pub scope: Option<String>,
154
155 #[serde(rename = "rest.prefix")]
160 pub prefix: Option<String>,
161
162 #[serde(default)]
164 #[serde(rename = "rest.headers")]
165 pub headers: Option<Vec<(String, String)>>,
166
167 #[serde(rename = "rest.audience")]
169 pub audience: Option<String>,
170
171 #[serde(rename = "rest.resource")]
173 pub resource: Option<String>,
174}
175
176#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
178pub struct S3TablesCatalogConfig {
179 #[serde(rename = "s3tables.table-bucket-arn")]
184 pub table_bucket_arn: Option<String>,
185
186 #[serde(rename = "s3tables.endpoint")]
191 pub endpoint: Option<String>,
192
193 #[serde(rename = "s3tables.access-key-id")]
195 pub access_key_id: Option<String>,
196
197 #[serde(rename = "s3tables.secret-access-key")]
199 pub secret_access_key: Option<String>,
200
201 #[serde(rename = "s3tables.session-token")]
203 pub session_token: Option<String>,
204
205 #[serde(rename = "s3tables.profile-name")]
207 pub profile_name: Option<String>,
208
209 #[serde(rename = "s3tables.region")]
211 pub region: Option<String>,
212}
213
214fn default_num_parsers() -> u32 {
215 4
216}
217
218#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
220pub struct IcebergReaderConfig {
221 pub mode: IcebergIngestMode,
223
224 #[serde(default)]
229 pub transaction_mode: IcebergTransactionMode,
230
231 pub timestamp_column: Option<String>,
256
257 pub snapshot_filter: Option<String>,
268
269 pub snapshot_id: Option<i64>,
280
281 pub datetime: Option<String>,
294
295 pub end_snapshot_id: Option<i64>,
306
307 pub metadata_location: Option<String>,
312
313 pub table_name: Option<String>,
317
318 pub catalog_type: Option<IcebergCatalogType>,
323
324 #[serde(default = "default_num_parsers")]
328 #[schema(minimum = 1)]
329 pub num_parsers: u32,
330
331 #[serde(default, skip_serializing_if = "Option::is_none")]
340 pub max_retries: Option<u32>,
341
342 #[serde(flatten)]
343 pub glue_catalog_config: GlueCatalogConfig,
344
345 #[serde(flatten)]
346 pub rest_catalog_config: RestCatalogConfig,
347
348 #[serde(flatten)]
349 pub s3tables_catalog_config: S3TablesCatalogConfig,
350
351 #[serde(flatten)]
355 pub fileio_config: HashMap<String, String>,
356}
357
358impl IcebergReaderConfig {
359 pub fn validate_catalog_config(&self) -> Result<(), String> {
360 self.validate_metadata_location()?;
361 self.validate_table_name()?;
362 self.validate_glue_catalog_config()?;
363 self.validate_rest_catalog_config()?;
364 self.validate_s3tables_catalog_config()?;
365
366 Ok(())
367 }
368
369 pub fn validate_glue_catalog_config(&self) -> Result<(), String> {
371 if self.catalog_type == Some(IcebergCatalogType::Glue) {
372 if self.glue_catalog_config.warehouse.is_none() {
373 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());
374 }
375 } else {
376 ensure_glue_property_not_set(&self.glue_catalog_config.warehouse, "warehouse")?;
377 ensure_glue_property_not_set(&self.glue_catalog_config.endpoint, "uri")?;
378 ensure_glue_property_not_set(&self.glue_catalog_config.access_key_id, "access-key-id")?;
379 ensure_glue_property_not_set(
380 &self.glue_catalog_config.secret_access_key,
381 "secret-access-key",
382 )?;
383 ensure_glue_property_not_set(&self.glue_catalog_config.profile_name, "profile-name")?;
384 ensure_glue_property_not_set(&self.glue_catalog_config.region, "region")?;
385 ensure_glue_property_not_set(&self.glue_catalog_config.session_token, "session-token")?;
386 ensure_glue_property_not_set(&self.glue_catalog_config.id, "id")?;
387 }
388
389 Ok(())
390 }
391
392 pub fn validate_rest_catalog_config(&self) -> Result<(), String> {
394 if self.catalog_type == Some(IcebergCatalogType::Rest) {
395 if self.rest_catalog_config.uri.is_none() {
396 return Err(r#"missing Iceberg Rest catalog URI—set the 'rest.uri' property when using "catalog_type" = "rest""#.to_string());
397 }
398 } else {
399 ensure_rest_property_not_set(&self.rest_catalog_config.uri, "uri")?;
400 ensure_rest_property_not_set(&self.rest_catalog_config.warehouse, "warehouse")?;
401 ensure_rest_property_not_set(
402 &self.rest_catalog_config.oauth2_server_uri,
403 "oauth2_server_uri",
404 )?;
405 ensure_rest_property_not_set(&self.rest_catalog_config.credential, "credential")?;
406 ensure_rest_property_not_set(&self.rest_catalog_config.token, "token")?;
407 ensure_rest_property_not_set(&self.rest_catalog_config.scope, "scope")?;
408 ensure_rest_property_not_set(&self.rest_catalog_config.prefix, "prefix")?;
409 ensure_rest_property_not_set(&self.rest_catalog_config.headers, "headers")?;
410 ensure_rest_property_not_set(&self.rest_catalog_config.audience, "audience")?;
411 ensure_rest_property_not_set(&self.rest_catalog_config.resource, "resource")?;
412 }
413
414 Ok(())
415 }
416
417 pub fn validate_s3tables_catalog_config(&self) -> Result<(), String> {
419 if self.catalog_type == Some(IcebergCatalogType::S3Tables) {
420 if self.s3tables_catalog_config.table_bucket_arn.is_none() {
421 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());
422 }
423 } else {
424 ensure_s3tables_property_not_set(
425 &self.s3tables_catalog_config.table_bucket_arn,
426 "table-bucket-arn",
427 )?;
428 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.endpoint, "endpoint")?;
429 ensure_s3tables_property_not_set(
430 &self.s3tables_catalog_config.access_key_id,
431 "access-key-id",
432 )?;
433 ensure_s3tables_property_not_set(
434 &self.s3tables_catalog_config.secret_access_key,
435 "secret-access-key",
436 )?;
437 ensure_s3tables_property_not_set(
438 &self.s3tables_catalog_config.session_token,
439 "session-token",
440 )?;
441 ensure_s3tables_property_not_set(
442 &self.s3tables_catalog_config.profile_name,
443 "profile-name",
444 )?;
445 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.region, "region")?;
446 }
447
448 Ok(())
449 }
450
451 pub fn validate_table_name(&self) -> Result<(), String> {
453 if self.catalog_type.is_none() && self.table_name.is_some() {
454 Err("unexpected 'table_name' property: the 'table_name' property is valid only when an Iceberg catalog is configured using 'catalog_type'".to_string())
455 } else if self.catalog_type.is_some() && self.table_name.is_none() {
456 Err("missing 'table_name' property—'table_name' must be specified when Iceberg catalog is configured using 'catalog_type'".to_string())
457 } else {
458 Ok(())
459 }
460 }
461
462 pub fn validate_metadata_location(&self) -> Result<(), String> {
464 if self.catalog_type.is_none() && self.metadata_location.is_none() {
465 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())
466 } else if self.catalog_type.is_some() && self.metadata_location.is_some() {
467 Err("unexpected 'metadata_location' property: the 'metadata_location' property is not supported when an Iceberg catalog is configured using 'catalog_type'".to_string())
468 } else {
469 Ok(())
470 }
471 }
472}
473
474fn ensure_glue_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
475 if property.is_some() {
476 Err(format!(
477 r#"unexpected 'glue.{name}' property—Glue catalog configuration properties are only valid when "catalog_type" = "glue""#
478 ))
479 } else {
480 Ok(())
481 }
482}
483
484fn ensure_rest_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
485 if property.is_some() {
486 Err(format!(
487 r#"unexpected 'rest.{name}' property—Rest catalog configuration properties are only valid when "catalog_type" = "rest""#
488 ))
489 } else {
490 Ok(())
491 }
492}
493
494fn ensure_s3tables_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
495 if property.is_some() {
496 Err(format!(
497 r#"unexpected 's3tables.{name}' property—S3 Tables catalog configuration properties are only valid when "catalog_type" = "s3tables""#
498 ))
499 } else {
500 Ok(())
501 }
502}
503
504impl IcebergReaderConfig {
505 pub fn max_retries(&self) -> u32 {
507 self.max_retries.unwrap_or(u32::MAX)
508 }
509
510 pub fn snapshot(&self) -> bool {
512 matches!(
513 &self.mode,
514 IcebergIngestMode::Snapshot | IcebergIngestMode::SnapshotAndFollow
515 )
516 }
517
518 pub fn follow(&self) -> bool {
521 matches!(
522 &self.mode,
523 IcebergIngestMode::SnapshotAndFollow | IcebergIngestMode::Follow
524 )
525 }
526}
527
528#[cfg(test)]
529mod test {
530 use super::*;
531 use serde_json::json;
532
533 fn config(value: serde_json::Value) -> IcebergReaderConfig {
534 serde_json::from_value(value).unwrap()
535 }
536
537 #[test]
538 fn s3tables_config_deserializes() {
539 let config = config(json!({
540 "mode": "snapshot",
541 "catalog_type": "s3tables",
542 "table_name": "namespace.table",
543 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
544 "s3tables.region": "us-east-2",
545 "s3tables.access-key-id": "key",
546 "s3tables.secret-access-key": "secret",
547 "s3tables.session-token": "token",
548 "s3tables.profile-name": "profile",
549 "s3tables.endpoint": "http://localhost:4566",
550 }));
551
552 let s3tables = &config.s3tables_catalog_config;
555 assert_eq!(
556 s3tables.table_bucket_arn.as_deref(),
557 Some("arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket")
558 );
559 assert_eq!(s3tables.region.as_deref(), Some("us-east-2"));
560 assert_eq!(s3tables.access_key_id.as_deref(), Some("key"));
561 assert_eq!(s3tables.secret_access_key.as_deref(), Some("secret"));
562 assert_eq!(s3tables.session_token.as_deref(), Some("token"));
563 assert_eq!(s3tables.profile_name.as_deref(), Some("profile"));
564 assert_eq!(s3tables.endpoint.as_deref(), Some("http://localhost:4566"));
565 assert!(config.fileio_config.is_empty());
566
567 config.validate_catalog_config().unwrap();
568 }
569
570 #[test]
571 fn s3tables_requires_table_bucket_arn() {
572 let err = config(json!({
573 "mode": "snapshot",
574 "catalog_type": "s3tables",
575 "table_name": "namespace.table",
576 }))
577 .validate_catalog_config()
578 .unwrap_err();
579 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
580 }
581
582 #[test]
583 fn s3tables_props_rejected_for_other_catalog() {
584 let err = config(json!({
585 "mode": "snapshot",
586 "catalog_type": "glue",
587 "table_name": "namespace.table",
588 "glue.warehouse": "s3://warehouse/",
589 "s3tables.region": "us-east-2",
590 }))
591 .validate_catalog_config()
592 .unwrap_err();
593 assert!(err.contains("s3tables.region"), "{err}");
594 }
595
596 #[test]
597 fn s3tables_props_rejected_without_catalog() {
598 let err = config(json!({
599 "mode": "snapshot",
600 "metadata_location": "s3://warehouse/metadata.json",
601 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
602 }))
603 .validate_catalog_config()
604 .unwrap_err();
605 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
606 }
607
608 #[test]
609 fn num_parsers_and_max_retries_defaults() {
610 let config: IcebergReaderConfig = serde_json::from_str(
612 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
613 )
614 .unwrap();
615 assert_eq!(config.num_parsers, 4);
616 assert_eq!(config.max_retries, None);
617 assert_eq!(config.max_retries(), u32::MAX);
618 }
619
620 #[test]
621 fn num_parsers_and_max_retries_explicit() {
622 let config: IcebergReaderConfig = serde_json::from_str(
623 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":8,"max_retries":0}"#,
624 )
625 .unwrap();
626 assert_eq!(config.num_parsers, 8);
627 assert_eq!(config.max_retries, Some(0));
628 assert_eq!(config.max_retries(), 0);
630 }
631
632 #[test]
633 fn transaction_mode_defaults_to_none() {
634 let config: IcebergReaderConfig = serde_json::from_str(
635 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
636 )
637 .unwrap();
638 assert_eq!(config.transaction_mode, IcebergTransactionMode::None);
639 }
640
641 #[test]
642 fn transaction_mode_snapshot_parses() {
643 let config: IcebergReaderConfig = serde_json::from_str(
644 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","transaction_mode":"snapshot"}"#,
645 )
646 .unwrap();
647 assert_eq!(config.transaction_mode, IcebergTransactionMode::Snapshot);
648 }
649
650 #[test]
651 fn reader_config_roundtrips() {
652 let config: IcebergReaderConfig = serde_json::from_str(
653 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":2}"#,
654 )
655 .unwrap();
656 let serialized = serde_json::to_string(&config).unwrap();
657 let reparsed: IcebergReaderConfig = serde_json::from_str(&serialized).unwrap();
658 assert_eq!(config, reparsed);
659 assert_eq!(reparsed.num_parsers, 2);
660 }
661}