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 {
216 std::thread::available_parallelism().map_or(4, |n| n.get().min(16) as u32)
217}
218
219#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
221pub struct IcebergReaderConfig {
222 pub mode: IcebergIngestMode,
224
225 #[serde(default)]
230 pub transaction_mode: IcebergTransactionMode,
231
232 pub timestamp_column: Option<String>,
257
258 pub snapshot_filter: Option<String>,
269
270 pub snapshot_id: Option<i64>,
281
282 pub datetime: Option<String>,
295
296 pub end_snapshot_id: Option<i64>,
307
308 pub metadata_location: Option<String>,
313
314 pub table_name: Option<String>,
318
319 pub catalog_type: Option<IcebergCatalogType>,
324
325 #[serde(default, skip_serializing_if = "Option::is_none")]
334 #[schema(minimum = 1)]
335 pub num_parsers: Option<u32>,
336
337 #[serde(default, skip_serializing_if = "Option::is_none")]
346 pub max_retries: Option<u32>,
347
348 #[serde(flatten)]
349 pub glue_catalog_config: GlueCatalogConfig,
350
351 #[serde(flatten)]
352 pub rest_catalog_config: RestCatalogConfig,
353
354 #[serde(flatten)]
355 pub s3tables_catalog_config: S3TablesCatalogConfig,
356
357 #[serde(flatten)]
361 pub fileio_config: HashMap<String, String>,
362}
363
364impl IcebergReaderConfig {
365 pub fn validate_catalog_config(&self) -> Result<(), String> {
366 self.validate_metadata_location()?;
367 self.validate_table_name()?;
368 self.validate_glue_catalog_config()?;
369 self.validate_rest_catalog_config()?;
370 self.validate_s3tables_catalog_config()?;
371
372 Ok(())
373 }
374
375 pub fn validate_glue_catalog_config(&self) -> Result<(), String> {
377 if self.catalog_type == Some(IcebergCatalogType::Glue) {
378 if self.glue_catalog_config.warehouse.is_none() {
379 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());
380 }
381 } else {
382 ensure_glue_property_not_set(&self.glue_catalog_config.warehouse, "warehouse")?;
383 ensure_glue_property_not_set(&self.glue_catalog_config.endpoint, "uri")?;
384 ensure_glue_property_not_set(&self.glue_catalog_config.access_key_id, "access-key-id")?;
385 ensure_glue_property_not_set(
386 &self.glue_catalog_config.secret_access_key,
387 "secret-access-key",
388 )?;
389 ensure_glue_property_not_set(&self.glue_catalog_config.profile_name, "profile-name")?;
390 ensure_glue_property_not_set(&self.glue_catalog_config.region, "region")?;
391 ensure_glue_property_not_set(&self.glue_catalog_config.session_token, "session-token")?;
392 ensure_glue_property_not_set(&self.glue_catalog_config.id, "id")?;
393 }
394
395 Ok(())
396 }
397
398 pub fn validate_rest_catalog_config(&self) -> Result<(), String> {
400 if self.catalog_type == Some(IcebergCatalogType::Rest) {
401 if self.rest_catalog_config.uri.is_none() {
402 return Err(r#"missing Iceberg Rest catalog URI—set the 'rest.uri' property when using "catalog_type" = "rest""#.to_string());
403 }
404 } else {
405 ensure_rest_property_not_set(&self.rest_catalog_config.uri, "uri")?;
406 ensure_rest_property_not_set(&self.rest_catalog_config.warehouse, "warehouse")?;
407 ensure_rest_property_not_set(
408 &self.rest_catalog_config.oauth2_server_uri,
409 "oauth2_server_uri",
410 )?;
411 ensure_rest_property_not_set(&self.rest_catalog_config.credential, "credential")?;
412 ensure_rest_property_not_set(&self.rest_catalog_config.token, "token")?;
413 ensure_rest_property_not_set(&self.rest_catalog_config.scope, "scope")?;
414 ensure_rest_property_not_set(&self.rest_catalog_config.prefix, "prefix")?;
415 ensure_rest_property_not_set(&self.rest_catalog_config.headers, "headers")?;
416 ensure_rest_property_not_set(&self.rest_catalog_config.audience, "audience")?;
417 ensure_rest_property_not_set(&self.rest_catalog_config.resource, "resource")?;
418 }
419
420 Ok(())
421 }
422
423 pub fn validate_s3tables_catalog_config(&self) -> Result<(), String> {
425 if self.catalog_type == Some(IcebergCatalogType::S3Tables) {
426 if self.s3tables_catalog_config.table_bucket_arn.is_none() {
427 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());
428 }
429 } else {
430 ensure_s3tables_property_not_set(
431 &self.s3tables_catalog_config.table_bucket_arn,
432 "table-bucket-arn",
433 )?;
434 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.endpoint, "endpoint")?;
435 ensure_s3tables_property_not_set(
436 &self.s3tables_catalog_config.access_key_id,
437 "access-key-id",
438 )?;
439 ensure_s3tables_property_not_set(
440 &self.s3tables_catalog_config.secret_access_key,
441 "secret-access-key",
442 )?;
443 ensure_s3tables_property_not_set(
444 &self.s3tables_catalog_config.session_token,
445 "session-token",
446 )?;
447 ensure_s3tables_property_not_set(
448 &self.s3tables_catalog_config.profile_name,
449 "profile-name",
450 )?;
451 ensure_s3tables_property_not_set(&self.s3tables_catalog_config.region, "region")?;
452 }
453
454 Ok(())
455 }
456
457 pub fn validate_table_name(&self) -> Result<(), String> {
459 if self.catalog_type.is_none() && self.table_name.is_some() {
460 Err("unexpected 'table_name' property: the 'table_name' property is valid only when an Iceberg catalog is configured using 'catalog_type'".to_string())
461 } else if self.catalog_type.is_some() && self.table_name.is_none() {
462 Err("missing 'table_name' property—'table_name' must be specified when Iceberg catalog is configured using 'catalog_type'".to_string())
463 } else {
464 Ok(())
465 }
466 }
467
468 pub fn validate_metadata_location(&self) -> Result<(), String> {
470 if self.catalog_type.is_none() && self.metadata_location.is_none() {
471 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())
472 } else if self.catalog_type.is_some() && self.metadata_location.is_some() {
473 Err("unexpected 'metadata_location' property: the 'metadata_location' property is not supported when an Iceberg catalog is configured using 'catalog_type'".to_string())
474 } else {
475 Ok(())
476 }
477 }
478}
479
480fn ensure_glue_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
481 if property.is_some() {
482 Err(format!(
483 r#"unexpected 'glue.{name}' property—Glue catalog configuration properties are only valid when "catalog_type" = "glue""#
484 ))
485 } else {
486 Ok(())
487 }
488}
489
490fn ensure_rest_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
491 if property.is_some() {
492 Err(format!(
493 r#"unexpected 'rest.{name}' property—Rest catalog configuration properties are only valid when "catalog_type" = "rest""#
494 ))
495 } else {
496 Ok(())
497 }
498}
499
500fn ensure_s3tables_property_not_set<T>(property: &Option<T>, name: &str) -> Result<(), String> {
501 if property.is_some() {
502 Err(format!(
503 r#"unexpected 's3tables.{name}' property—S3 Tables catalog configuration properties are only valid when "catalog_type" = "s3tables""#
504 ))
505 } else {
506 Ok(())
507 }
508}
509
510impl IcebergReaderConfig {
511 pub fn max_retries(&self) -> u32 {
513 self.max_retries.unwrap_or(u32::MAX)
514 }
515
516 pub fn num_parsers(&self) -> u32 {
521 self.num_parsers.unwrap_or_else(default_num_parsers)
522 }
523
524 pub fn read_concurrency(&self) -> usize {
528 self.num_parsers.unwrap_or(4) as usize
529 }
530
531 pub fn snapshot(&self) -> bool {
533 matches!(
534 &self.mode,
535 IcebergIngestMode::Snapshot | IcebergIngestMode::SnapshotAndFollow
536 )
537 }
538
539 pub fn follow(&self) -> bool {
542 matches!(
543 &self.mode,
544 IcebergIngestMode::SnapshotAndFollow | IcebergIngestMode::Follow
545 )
546 }
547}
548
549impl crate::sensitive::SensitiveValues for IcebergReaderConfig {
550 fn visit_sensitive_values(&self, visit: &mut dyn FnMut(&str, &str)) {
551 use crate::sensitive::{credential_header, visit_optional, visit_url};
552 visit_optional(
553 "glue.secret-access-key",
554 self.glue_catalog_config.secret_access_key.as_deref(),
555 visit,
556 );
557 visit_optional(
558 "glue.session-token",
559 self.glue_catalog_config.session_token.as_deref(),
560 visit,
561 );
562 visit_optional(
563 "s3tables.secret-access-key",
564 self.s3tables_catalog_config.secret_access_key.as_deref(),
565 visit,
566 );
567 visit_optional(
568 "s3tables.session-token",
569 self.s3tables_catalog_config.session_token.as_deref(),
570 visit,
571 );
572 visit_optional(
573 "rest.credential",
574 self.rest_catalog_config.credential.as_deref(),
575 visit,
576 );
577 visit_optional(
578 "rest.token",
579 self.rest_catalog_config.token.as_deref(),
580 visit,
581 );
582 if let Some(uri) = &self.rest_catalog_config.uri {
583 visit_url("rest.uri", uri, visit);
584 }
585 if let Some(headers) = &self.rest_catalog_config.headers {
586 for (name, value) in headers {
587 if credential_header(name) {
588 visit(
589 &format!("rest.headers.{}", name.to_ascii_lowercase()),
590 value,
591 );
592 }
593 }
594 }
595 for (key, value) in &self.fileio_config {
601 if matches!(
602 key.as_str(),
603 "s3.secret-access-key"
604 | "s3.session-token"
605 | "s3.sse.key"
606 | "gcs.credentials-json"
607 | "gcs.oauth2.token"
608 | "oss.access-key-secret"
609 | "adls.connection-string"
610 | "adls.account-key"
611 | "adls.sas-token"
612 | "adls.client-secret"
613 ) {
614 visit(key, value);
615 }
616 }
617 }
618}
619
620#[cfg(test)]
621mod test {
622 use super::*;
623 use serde_json::json;
624
625 fn config(value: serde_json::Value) -> IcebergReaderConfig {
626 serde_json::from_value(value).unwrap()
627 }
628
629 #[test]
630 fn s3tables_config_deserializes() {
631 let config = config(json!({
632 "mode": "snapshot",
633 "catalog_type": "s3tables",
634 "table_name": "namespace.table",
635 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
636 "s3tables.region": "us-east-2",
637 "s3tables.access-key-id": "key",
638 "s3tables.secret-access-key": "secret",
639 "s3tables.session-token": "token",
640 "s3tables.profile-name": "profile",
641 "s3tables.endpoint": "http://localhost:4566",
642 }));
643
644 let s3tables = &config.s3tables_catalog_config;
647 assert_eq!(
648 s3tables.table_bucket_arn.as_deref(),
649 Some("arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket")
650 );
651 assert_eq!(s3tables.region.as_deref(), Some("us-east-2"));
652 assert_eq!(s3tables.access_key_id.as_deref(), Some("key"));
653 assert_eq!(s3tables.secret_access_key.as_deref(), Some("secret"));
654 assert_eq!(s3tables.session_token.as_deref(), Some("token"));
655 assert_eq!(s3tables.profile_name.as_deref(), Some("profile"));
656 assert_eq!(s3tables.endpoint.as_deref(), Some("http://localhost:4566"));
657 assert!(config.fileio_config.is_empty());
658
659 config.validate_catalog_config().unwrap();
660 }
661
662 #[test]
663 fn s3tables_requires_table_bucket_arn() {
664 let err = config(json!({
665 "mode": "snapshot",
666 "catalog_type": "s3tables",
667 "table_name": "namespace.table",
668 }))
669 .validate_catalog_config()
670 .unwrap_err();
671 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
672 }
673
674 #[test]
675 fn s3tables_props_rejected_for_other_catalog() {
676 let err = config(json!({
677 "mode": "snapshot",
678 "catalog_type": "glue",
679 "table_name": "namespace.table",
680 "glue.warehouse": "s3://warehouse/",
681 "s3tables.region": "us-east-2",
682 }))
683 .validate_catalog_config()
684 .unwrap_err();
685 assert!(err.contains("s3tables.region"), "{err}");
686 }
687
688 #[test]
689 fn s3tables_props_rejected_without_catalog() {
690 let err = config(json!({
691 "mode": "snapshot",
692 "metadata_location": "s3://warehouse/metadata.json",
693 "s3tables.table-bucket-arn": "arn:aws:s3tables:us-east-2:123456789012:bucket/my-bucket",
694 }))
695 .validate_catalog_config()
696 .unwrap_err();
697 assert!(err.contains("s3tables.table-bucket-arn"), "{err}");
698 }
699
700 #[test]
701 fn num_parsers_and_max_retries_defaults() {
702 let config: IcebergReaderConfig = serde_json::from_str(
704 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
705 )
706 .unwrap();
707 assert_eq!(config.num_parsers(), default_num_parsers());
708 assert_eq!(config.read_concurrency(), 4);
709 assert!(
711 !serde_json::to_string(&config)
712 .unwrap()
713 .contains("num_parsers")
714 );
715 assert_eq!(config.max_retries, None);
716 assert_eq!(config.max_retries(), u32::MAX);
717 }
718
719 #[test]
720 fn num_parsers_and_max_retries_explicit() {
721 let config: IcebergReaderConfig = serde_json::from_str(
722 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":8,"max_retries":0}"#,
723 )
724 .unwrap();
725 assert_eq!(config.num_parsers(), 8);
726 assert_eq!(config.read_concurrency(), 8);
727 assert_eq!(config.max_retries, Some(0));
728 assert_eq!(config.max_retries(), 0);
730 }
731
732 #[test]
733 fn transaction_mode_defaults_to_none() {
734 let config: IcebergReaderConfig = serde_json::from_str(
735 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json"}"#,
736 )
737 .unwrap();
738 assert_eq!(config.transaction_mode, IcebergTransactionMode::None);
739 }
740
741 #[test]
742 fn transaction_mode_snapshot_parses() {
743 let config: IcebergReaderConfig = serde_json::from_str(
744 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","transaction_mode":"snapshot"}"#,
745 )
746 .unwrap();
747 assert_eq!(config.transaction_mode, IcebergTransactionMode::Snapshot);
748 }
749
750 #[test]
751 fn reader_config_roundtrips() {
752 let config: IcebergReaderConfig = serde_json::from_str(
753 r#"{"mode":"snapshot","metadata_location":"file:///tmp/t/metadata.json","num_parsers":2}"#,
754 )
755 .unwrap();
756 let serialized = serde_json::to_string(&config).unwrap();
757 let reparsed: IcebergReaderConfig = serde_json::from_str(&serialized).unwrap();
758 assert_eq!(config, reparsed);
759 assert_eq!(reparsed.num_parsers(), 2);
760 }
761}