ailake-catalog 0.1.3

Iceberg Spec v2 catalog backends (Hadoop, REST, Glue, Nessie, JDBC) for AI-Lake
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// SPDX-License-Identifier: MIT OR Apache-2.0
// Iceberg REST Catalog (https://iceberg.apache.org/spec/#rest-catalog).
// Covers: Apache Polaris, Azure Databricks Unity Catalog, GCP BigLake Metastore,
// AWS S3 Tables, Project Nessie (REST mode), and any spec-compliant server.
//
// Auth strategies:
//   RestCatalogAuth::None    — open catalogs (local Nessie/Polaris dev setup)
//   RestCatalogAuth::Bearer  — pre-obtained token (CI pipelines, Workload Identity)
//   RestCatalogAuth::OAuth2  — client credentials flow with token caching (production)
//
// Object storage:
//   Manifests are written to the provided Store (LocalStore / ObjectStoreBackend).
//   The REST server manages metadata.json; we write our JSON manifests independently.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use ailake_core::{AilakeError, AilakeResult};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;

use crate::metadata::IcebergMetadata;
use crate::provider::{
    CatalogProvider, DataFileEntry, EqualityDeleteFile, NewSnapshot, SnapshotId, TableIdent,
    TableMetadata, TableProperties,
};
use crate::schema_evolution::SchemaEvolution;
use ailake_store::Store;

// ── Public configuration types ───────────────────────────────────────────────

/// Authentication strategy for the REST catalog.
#[derive(Debug, Clone)]
pub enum RestCatalogAuth {
    /// No authentication. Works with open Nessie / Polaris dev setups.
    None,

    /// Pre-obtained Bearer token. Use for Workload Identity, CI tokens, etc.
    Bearer(String),

    /// OAuth2 client credentials flow.
    /// Tokens are cached until (expiry − 30 s) to avoid clock-edge failures.
    OAuth2 {
        /// Token endpoint URL (e.g. "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token")
        token_endpoint: String,
        client_id: String,
        client_secret: String,
        /// Optional scope (e.g. "https://management.azure.com/.default")
        scope: Option<String>,
    },
}

#[derive(Debug, Clone)]
pub struct RestCatalogConfig {
    /// Base URI of the REST catalog (no trailing slash).
    ///
    /// Examples:
    ///   - Polaris (Snowflake):    "https://<account>.snowflakecomputing.com/polaris/api/catalog"
    ///   - Unity Catalog (Azure):  "https://<workspace>.azuredatabricks.net/api/2.1/unity-catalog/iceberg"
    ///   - BigLake (GCP):          "https://biglake.googleapis.com/iceberg/v1beta1"
    ///   - S3 Tables (AWS):        "https://s3tables.<region>.amazonaws.com/iceberg"
    ///   - Nessie:                 "http://localhost:19120/api"
    ///   - Gravitino:              "http://localhost:8090/iceberg"
    pub uri: String,

    /// Optional path prefix inserted between /v1 and /namespaces.
    ///
    /// Polaris: catalog name. Nessie: branch name (e.g. "main").
    /// Unity Catalog / BigLake: leave None.
    pub prefix: Option<String>,

    /// Base storage location for new tables (e.g. "s3://my-bucket/warehouse").
    /// Required for create_table. Unused if the server auto-assigns locations.
    pub warehouse: Option<String>,

    pub auth: RestCatalogAuth,
}

// ── RestCatalog ───────────────────────────────────────────────────────────────

struct CachedToken {
    value: String,
    expires_at: Instant,
}

pub struct RestCatalog {
    config: RestCatalogConfig,
    client: reqwest::Client,
    token_cache: Mutex<Option<CachedToken>>,
    /// Store backend used to write/read manifest JSON files.
    store: Arc<dyn Store>,
}

impl RestCatalog {
    pub fn new(config: RestCatalogConfig, store: Arc<dyn Store>) -> Self {
        Self {
            config,
            client: reqwest::Client::new(),
            token_cache: Mutex::new(None),
            store,
        }
    }

    // ── URL helpers ──────────────────────────────────────────────────────────

    fn base_url(&self) -> String {
        let uri = self.config.uri.trim_end_matches('/');
        match &self.config.prefix {
            Some(p) if !p.is_empty() => format!("{uri}/v1/{p}"),
            _ => format!("{uri}/v1"),
        }
    }

    fn namespace_tables_url(&self, ns: &str) -> String {
        format!("{}/namespaces/{}/tables", self.base_url(), ns)
    }

    fn table_url(&self, table: &TableIdent) -> String {
        format!(
            "{}/namespaces/{}/tables/{}",
            self.base_url(),
            table.namespace,
            table.name
        )
    }

    fn table_storage_root(&self, table: &TableIdent) -> String {
        let warehouse = self
            .config
            .warehouse
            .as_deref()
            .unwrap_or("")
            .trim_end_matches('/');
        format!("{}/{}/{}", warehouse, table.namespace, table.name)
    }

    // ── Auth ─────────────────────────────────────────────────────────────────

    async fn get_token(&self) -> AilakeResult<Option<String>> {
        match &self.config.auth {
            RestCatalogAuth::None => Ok(None),
            RestCatalogAuth::Bearer(t) => Ok(Some(t.clone())),
            RestCatalogAuth::OAuth2 {
                token_endpoint,
                client_id,
                client_secret,
                scope,
            } => {
                {
                    let cache = self.token_cache.lock().await;
                    if let Some(cached) = &*cache {
                        if cached.expires_at > Instant::now() + Duration::from_secs(30) {
                            return Ok(Some(cached.value.clone()));
                        }
                    }
                }

                let mut params = vec![
                    ("grant_type", "client_credentials"),
                    ("client_id", client_id.as_str()),
                    ("client_secret", client_secret.as_str()),
                ];
                let scope_str;
                if let Some(s) = scope {
                    scope_str = s.clone();
                    params.push(("scope", scope_str.as_str()));
                }

                let resp = self
                    .client
                    .post(token_endpoint)
                    .form(&params)
                    .send()
                    .await
                    .map_err(|e| AilakeError::Store(e.to_string()))?;

                if !resp.status().is_success() {
                    let status = resp.status();
                    let body = resp.text().await.unwrap_or_default();
                    return Err(AilakeError::Catalog(format!(
                        "OAuth2 token request failed: HTTP {status}: {body}"
                    )));
                }

                let token_resp: OAuthTokenResponse = resp
                    .json()
                    .await
                    .map_err(|e| AilakeError::Catalog(format!("OAuth2 token parse: {e}")))?;

                let ttl = token_resp.expires_in.unwrap_or(3600);
                let cached = CachedToken {
                    value: token_resp.access_token.clone(),
                    expires_at: Instant::now() + Duration::from_secs(ttl),
                };
                *self.token_cache.lock().await = Some(cached);
                Ok(Some(token_resp.access_token))
            }
        }
    }

    // ── HTTP helpers ─────────────────────────────────────────────────────────

    async fn get(&self, url: &str) -> AilakeResult<reqwest::Response> {
        let mut req = self.client.get(url);
        if let Some(token) = self.get_token().await? {
            req = req.bearer_auth(token);
        }
        req.send()
            .await
            .map_err(|e| AilakeError::Store(e.to_string()))
    }

    async fn post<T: Serialize>(&self, url: &str, body: &T) -> AilakeResult<reqwest::Response> {
        let mut req = self.client.post(url).json(body);
        if let Some(token) = self.get_token().await? {
            req = req.bearer_auth(token);
        }
        req.send()
            .await
            .map_err(|e| AilakeError::Store(e.to_string()))
    }

    async fn delete(&self, url: &str) -> AilakeResult<reqwest::Response> {
        let mut req = self.client.delete(url);
        if let Some(token) = self.get_token().await? {
            req = req.bearer_auth(token);
        }
        req.send()
            .await
            .map_err(|e| AilakeError::Store(e.to_string()))
    }

    async fn require_ok(resp: reqwest::Response, ctx: &str) -> AilakeResult<reqwest::Response> {
        if resp.status().is_success() {
            return Ok(resp);
        }
        let status = resp.status();
        let body = resp.text().await.unwrap_or_default();
        Err(AilakeError::Catalog(format!(
            "{ctx}: HTTP {status}: {body}"
        )))
    }

    /// Fetch current table metadata from the REST server.
    async fn load_metadata(&self, table: &TableIdent) -> AilakeResult<IcebergMetadata> {
        let resp = self.get(&self.table_url(table)).await?;
        let resp = Self::require_ok(resp, "load_metadata").await?;
        let result: LoadTableResult = resp
            .json()
            .await
            .map_err(|e| AilakeError::Catalog(format!("load_metadata parse: {e}")))?;
        Ok(result.metadata)
    }
}

// ── CatalogProvider ───────────────────────────────────────────────────────────

#[async_trait]
impl CatalogProvider for RestCatalog {
    async fn create_table(&self, name: &TableIdent, props: &TableProperties) -> AilakeResult<()> {
        let location = self.table_storage_root(name);

        // Reuse the exact same schema/partition-spec/properties construction the
        // file-based backends use (IcebergMetadata::new) instead of duplicating it —
        // guarantees this backend's tables carry the same ailake.* properties and
        // real (not empty) schema/partition-spec as Hadoop/Glue/Jdbc for the same
        // TableProperties. format-version isn't a field the REST CreateTableRequest
        // accepts (checked against the spec's CreateTableRequest schema) — servers
        // create V2 by default, so a V3 request needs a follow-up
        // `upgrade-format-version` commit right after create.
        let pct = props
            .partition_column_type
            .as_deref()
            .or(props.policy.partition_column_type.as_deref());
        let meta = IcebergMetadata::new(
            &location,
            &props.policy,
            props.format_version,
            pct,
            &props.policy.partition_fields,
        );
        let mut properties = meta.properties.clone();
        for (k, v) in &props.extra {
            properties.insert(k.clone(), v.clone());
        }
        let schema = meta
            .schemas
            .first()
            .cloned()
            .unwrap_or_else(RestSchema::empty_value);
        let partition_spec = if meta.default_spec_id > 0 {
            meta.partition_specs
                .get(meta.default_spec_id as usize)
                .cloned()
        } else {
            None
        };

        let req = CreateTableRequest {
            name: name.name.clone(),
            location: Some(location),
            schema,
            partition_spec,
            properties,
        };

        let url = self.namespace_tables_url(&name.namespace);
        let resp = self.post(&url, &req).await?;
        Self::require_ok(resp, "create_table").await?;

        if meta.format_version >= 3 {
            let commit_req = CommitTableRequest {
                identifier: TableIdentifier {
                    namespace: vec![name.namespace.clone()],
                    name: name.name.clone(),
                },
                requirements: vec![],
                updates: vec![TableUpdate::UpgradeFormatVersion {
                    format_version: meta.format_version,
                }],
            };
            let resp = self.post(&self.table_url(name), &commit_req).await?;
            Self::require_ok(resp, "create_table (upgrade-format-version)").await?;
        }
        Ok(())
    }

    async fn load_table(&self, name: &TableIdent) -> AilakeResult<TableMetadata> {
        let resp = self.get(&self.table_url(name)).await?;
        let resp = Self::require_ok(resp, "load_table").await?;
        let result: LoadTableResult = resp
            .json()
            .await
            .map_err(|e| AilakeError::Catalog(format!("load_table parse: {e}")))?;
        Ok(result.metadata.to_table_metadata())
    }

    async fn commit_snapshot(
        &self,
        table: &TableIdent,
        snapshot: NewSnapshot,
    ) -> AilakeResult<SnapshotId> {
        let snap_id = snapshot.snapshot_id;
        let table_root = self.table_storage_root(table);

        // Real OCC: read current state, build the real Avro manifest against it
        // (`build_commit` — shared with Hadoop/Glue/Jdbc, gives this backend the
        // same V3 first_row_id/Puffin-stats/partition-stats support they have),
        // commit with an `assert-ref-snapshot-id` requirement pinned to the
        // snapshot we read. If another writer moves `main` first, the server
        // rejects the commit (409) and we retry from a fresh read — same
        // retry×5 w/ backoff pattern glue.rs/jdbc.rs use for their own CAS
        // mechanisms.
        const MAX_RETRIES: u32 = 5;
        for attempt in 0..MAX_RETRIES {
            let resp = self.get(&self.table_url(table)).await?;
            let resp = Self::require_ok(resp, "commit_snapshot (read current state)").await?;
            let result: LoadTableResult = resp
                .json()
                .await
                .map_err(|e| AilakeError::Catalog(format!("commit_snapshot parse: {e}")))?;
            let meta = result.metadata;
            let current_snapshot_id = meta.current_snapshot_id;
            let current_schema_id = meta.current_schema_id;

            let artifacts = crate::manifest_commit::build_commit(
                &*self.store,
                &table_root,
                self.config.warehouse.as_deref().unwrap_or(""),
                &meta,
                snapshot.clone(),
            )
            .await?;

            let rest_snap = RestSnapshot {
                snapshot_id: artifacts.snapshot.snapshot_id,
                parent_snapshot_id: artifacts.snapshot.parent_snapshot_id,
                sequence_number: artifacts.snapshot.sequence_number,
                timestamp_ms: artifacts.snapshot.timestamp_ms,
                manifest_list: artifacts.snapshot.manifest_list.clone(),
                summary: artifacts.snapshot.summary.clone(),
                schema_id: artifacts.snapshot.schema_id,
            };

            let mut updates = vec![
                TableUpdate::AddSnapshot {
                    snapshot: rest_snap,
                },
                TableUpdate::SetSnapshotRef {
                    ref_name: "main".into(),
                    snapshot_type: "branch".into(),
                    snapshot_id: snap_id,
                },
            ];
            let mut requirements = vec![TableRequirement::AssertRefSnapshotId {
                r#ref: "main".into(),
                snapshot_id: current_snapshot_id,
            }];

            // Phase I: schema/partition-spec patch (only present when the caller
            // supplied `NewSnapshot::iceberg_schema` — same trigger as Hadoop/Glue/Jdbc).
            if let Some(patch) = artifacts.schema_patch {
                let new_schema_id = current_schema_id + 1;
                updates.push(TableUpdate::AddSchema {
                    schema: serde_json::json!({
                        "schema-id": new_schema_id,
                        "type": "struct",
                        "fields": patch.new_schema_fields,
                    }),
                });
                updates.push(TableUpdate::SetCurrentSchema { schema_id: -1 });
                updates.push(TableUpdate::SetProperties {
                    updates: HashMap::from([(
                        "schema.name-mapping.default".to_string(),
                        patch.name_mapping_json,
                    )]),
                });
                // The REST protocol has no "modify an existing spec's source-id in
                // place" update — the closest spec-correct equivalent is adding the
                // corrected spec as new and making it the default. Only the *last*
                // (highest spec-id) entry in `remapped_partition_specs` is genuinely
                // new — the earlier ones are the same specs already registered.
                if let Some(new_spec) = patch.remapped_partition_specs.last() {
                    updates.push(TableUpdate::AddPartitionSpec {
                        spec: new_spec.clone(),
                    });
                    updates.push(TableUpdate::SetDefaultSpec { spec_id: -1 });
                }
                requirements.push(TableRequirement::AssertCurrentSchemaId { current_schema_id });
            }
            if !artifacts.extra_properties.is_empty() {
                updates.push(TableUpdate::SetProperties {
                    updates: artifacts.extra_properties,
                });
            }
            // Phase F / Phase J: Puffin vector+BM25 stats and partition-stats Parquet
            // refs, mapped 1:1 onto the REST spec's own `StatisticsFile`/
            // `PartitionStatisticsFile` shapes (confirmed field-for-field against
            // apache/iceberg's rest-catalog-open-api.yaml, including the
            // `blob-metadata` key name already pinned to match it in `metadata.rs`).
            if let Some(stats) = &artifacts.statistics {
                updates.push(TableUpdate::SetStatistics {
                    statistics: serde_json::to_value(stats)
                        .map_err(|e| AilakeError::Catalog(format!("stats serialize: {e}")))?,
                });
            }
            if let Some(pstats) = &artifacts.partition_statistics {
                updates.push(TableUpdate::SetPartitionStatistics {
                    partition_statistics: serde_json::to_value(pstats).map_err(|e| {
                        AilakeError::Catalog(format!("partition stats serialize: {e}"))
                    })?,
                });
            }

            let commit_req = CommitTableRequest {
                identifier: TableIdentifier {
                    namespace: vec![table.namespace.clone()],
                    name: table.name.clone(),
                },
                requirements,
                updates,
            };

            let resp = self.post(&self.table_url(table), &commit_req).await?;
            if resp.status().as_u16() == 409 {
                if attempt + 1 < MAX_RETRIES {
                    tokio::time::sleep(std::time::Duration::from_millis(50 << attempt)).await;
                    continue;
                }
                return Err(AilakeError::Catalog(format!(
                    "commit_snapshot: {MAX_RETRIES} retries exhausted (concurrent modification)"
                )));
            }
            Self::require_ok(resp, "commit_snapshot").await?;
            return Ok(snap_id);
        }
        unreachable!("loop always returns via Ok/Err before exhausting MAX_RETRIES iterations")
    }

    async fn list_files(
        &self,
        table: &TableIdent,
        snapshot_id: Option<SnapshotId>,
    ) -> AilakeResult<Vec<DataFileEntry>> {
        let meta = self.load_metadata(table).await?;
        crate::manifest_commit::list_files_from_metadata(&*self.store, &meta, snapshot_id).await
    }

    async fn drop_table(&self, name: &TableIdent) -> AilakeResult<()> {
        let resp = self.delete(&self.table_url(name)).await?;
        if resp.status().as_u16() == 404 {
            return Ok(());
        }
        Self::require_ok(resp, "drop_table").await?;
        Ok(())
    }

    async fn evolve_schema(
        &self,
        table: &TableIdent,
        evolution: SchemaEvolution,
    ) -> AilakeResult<i32> {
        let resp = self.get(&self.table_url(table)).await?;
        let resp = Self::require_ok(resp, "evolve_schema (read current state)").await?;
        let result: LoadTableResult = resp
            .json()
            .await
            .map_err(|e| AilakeError::Catalog(format!("evolve_schema parse: {e}")))?;
        let meta = &result.metadata;

        let current_schema = meta
            .schemas
            .iter()
            .find(|s| s["schema-id"].as_i64() == Some(meta.current_schema_id as i64))
            .cloned()
            .unwrap_or_else(|| serde_json::json!({"schema-id": 0, "type": "struct", "fields": []}));
        let mut fields: Vec<serde_json::Value> = current_schema["fields"]
            .as_array()
            .cloned()
            .unwrap_or_default();

        for rename in &evolution.renames {
            for field in fields.iter_mut() {
                if field["name"].as_str() == Some(rename.old_name.as_str()) {
                    field["name"] = serde_json::Value::String(rename.new_name.clone());
                }
            }
        }

        let mut last_col_id = meta.last_column_id;
        for add in &evolution.adds {
            last_col_id += 1;
            let mut field = serde_json::json!({
                "id": last_col_id,
                "name": add.name,
                "required": add.required,
                "type": add.iceberg_type,
            });
            let init_default = add
                .initial_default
                .clone()
                .or_else(|| add.write_default.clone());
            if let Some(d) = init_default {
                field["initial-default"] = d;
            }
            if let Some(wd) = &add.write_default {
                field["write-default"] = wd.clone();
            }
            if let Some(doc) = &add.doc {
                field["doc"] = serde_json::Value::String(doc.clone());
            }
            fields.push(field);
        }

        let new_schema_id = meta.current_schema_id + 1;
        let new_schema = serde_json::json!({
            "schema-id": new_schema_id,
            "type": "struct",
            "fields": fields,
        });

        let mut updates = vec![
            TableUpdate::AddSchema { schema: new_schema },
            TableUpdate::SetCurrentSchema { schema_id: -1 },
        ];
        if !evolution.extra_properties.is_empty() {
            updates.push(TableUpdate::SetProperties {
                updates: evolution.extra_properties.clone(),
            });
        }

        let commit_req = CommitTableRequest {
            identifier: TableIdentifier {
                namespace: vec![table.namespace.clone()],
                name: table.name.clone(),
            },
            requirements: vec![TableRequirement::AssertCurrentSchemaId {
                current_schema_id: meta.current_schema_id,
            }],
            updates,
        };
        let resp = self.post(&self.table_url(table), &commit_req).await?;
        Self::require_ok(resp, "evolve_schema").await?;
        Ok(new_schema_id)
    }

    async fn list_equality_deletes(
        &self,
        table: &TableIdent,
        snapshot_id: Option<SnapshotId>,
    ) -> AilakeResult<Vec<EqualityDeleteFile>> {
        let meta = self.load_metadata(table).await?;
        crate::manifest_commit::list_equality_deletes_from_metadata(
            &*self.store,
            &meta,
            snapshot_id,
        )
        .await
    }
}

// ── REST protocol types ───────────────────────────────────────────────────────

#[derive(Deserialize)]
struct OAuthTokenResponse {
    access_token: String,
    expires_in: Option<u64>,
}

#[derive(Serialize)]
struct CreateTableRequest {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    location: Option<String>,
    schema: serde_json::Value,
    #[serde(rename = "partition-spec", skip_serializing_if = "Option::is_none")]
    partition_spec: Option<serde_json::Value>,
    properties: HashMap<String, String>,
}

impl RestSchema {
    /// Fallback empty struct schema (`{"type":"struct","fields":[]}`) for the
    /// unreachable case `IcebergMetadata::new` produces no schema at all.
    fn empty_value() -> serde_json::Value {
        serde_json::json!({"schema-id": 0, "type": "struct", "fields": []})
    }
}

/// Marker type only used to namespace `empty_value()` — `CreateTableRequest.schema`
/// itself is a raw `serde_json::Value` (reusing `IcebergMetadata::new`'s output
/// directly, see `create_table`), not a fixed Rust shape.
struct RestSchema;

/// Subset of `LoadTableResult` — only fields AI-Lake needs.
#[derive(Deserialize)]
struct LoadTableResult {
    metadata: IcebergMetadata,
}

#[derive(Serialize)]
struct CommitTableRequest {
    identifier: TableIdentifier,
    requirements: Vec<TableRequirement>,
    updates: Vec<TableUpdate>,
}

#[derive(Serialize)]
struct TableIdentifier {
    namespace: Vec<String>,
    name: String,
}

/// `TableRequirement` — Iceberg REST OCC guards sent with a commit. Server
/// rejects (409) if the named ref/schema doesn't match what's asserted here,
/// preventing a commit from silently clobbering a concurrent writer's changes.
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
enum TableRequirement {
    AssertRefSnapshotId {
        r#ref: String,
        #[serde(rename = "snapshot-id")]
        snapshot_id: Option<SnapshotId>,
    },
    AssertCurrentSchemaId {
        #[serde(rename = "current-schema-id")]
        current_schema_id: i32,
    },
}

#[derive(Serialize)]
#[serde(tag = "action", rename_all = "kebab-case")]
enum TableUpdate {
    AddSnapshot {
        snapshot: RestSnapshot,
    },
    SetSnapshotRef {
        #[serde(rename = "ref-name")]
        ref_name: String,
        #[serde(rename = "type")]
        snapshot_type: String,
        #[serde(rename = "snapshot-id")]
        snapshot_id: SnapshotId,
    },
    UpgradeFormatVersion {
        #[serde(rename = "format-version")]
        format_version: i32,
    },
    AddSchema {
        schema: serde_json::Value,
    },
    SetCurrentSchema {
        #[serde(rename = "schema-id")]
        schema_id: i32,
    },
    SetProperties {
        updates: HashMap<String, String>,
    },
    /// Phase F: Puffin vector-stats + BM25-bloom file ref. Payload is a
    /// `StatisticsFile`-shaped JSON value (see `manifest_commit::CommitArtifacts`).
    SetStatistics {
        statistics: serde_json::Value,
    },
    /// Phase J: partition-stats Parquet file ref (`PartitionStatisticsFile`-shaped).
    SetPartitionStatistics {
        #[serde(rename = "partition-statistics")]
        partition_statistics: serde_json::Value,
    },
    /// Phase I: register the partition-spec-with-corrected-source-id — the REST
    /// spec has no update to edit an existing spec's `source-id` in place.
    AddPartitionSpec {
        spec: serde_json::Value,
    },
    SetDefaultSpec {
        #[serde(rename = "spec-id")]
        spec_id: i32,
    },
}

#[derive(Serialize)]
struct RestSnapshot {
    #[serde(rename = "snapshot-id")]
    snapshot_id: SnapshotId,
    #[serde(rename = "parent-snapshot-id", skip_serializing_if = "Option::is_none")]
    parent_snapshot_id: Option<SnapshotId>,
    #[serde(rename = "sequence-number")]
    sequence_number: i64,
    #[serde(rename = "timestamp-ms")]
    timestamp_ms: i64,
    #[serde(rename = "manifest-list")]
    manifest_list: String,
    summary: HashMap<String, String>,
    #[serde(rename = "schema-id", skip_serializing_if = "Option::is_none")]
    schema_id: Option<i32>,
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use ailake_store::LocalStore;

    fn catalog(prefix: Option<&str>) -> RestCatalog {
        let store = Arc::new(LocalStore::new("/tmp"));
        RestCatalog::new(
            RestCatalogConfig {
                uri: "https://catalog.example.com".into(),
                prefix: prefix.map(|s| s.to_string()),
                warehouse: Some("s3://my-bucket/warehouse".into()),
                auth: RestCatalogAuth::None,
            },
            store,
        )
    }

    #[test]
    fn base_url_no_prefix() {
        let c = catalog(None);
        assert_eq!(c.base_url(), "https://catalog.example.com/v1");
    }

    #[test]
    fn base_url_with_prefix() {
        let c = catalog(Some("main"));
        assert_eq!(c.base_url(), "https://catalog.example.com/v1/main");
    }

    #[test]
    fn table_url_format() {
        let c = catalog(Some("main"));
        let tbl = TableIdent::new("default", "docs");
        assert_eq!(
            c.table_url(&tbl),
            "https://catalog.example.com/v1/main/namespaces/default/tables/docs"
        );
    }

    #[test]
    fn namespace_tables_url_format() {
        let c = catalog(None);
        assert_eq!(
            c.namespace_tables_url("prod"),
            "https://catalog.example.com/v1/namespaces/prod/tables"
        );
    }

    #[test]
    fn table_storage_root() {
        let c = catalog(None);
        let tbl = TableIdent::new("default", "docs");
        assert_eq!(
            c.table_storage_root(&tbl),
            "s3://my-bucket/warehouse/default/docs"
        );
    }

    #[test]
    fn commit_request_serializes_correctly() {
        let snap = RestSnapshot {
            snapshot_id: 42,
            parent_snapshot_id: None,
            sequence_number: 1,
            timestamp_ms: 1_000_000,
            manifest_list: "s3://bucket/snap-42.json".into(),
            summary: HashMap::from([("operation".into(), "append".into())]),
            schema_id: Some(0),
        };
        let req = CommitTableRequest {
            identifier: TableIdentifier {
                namespace: vec!["default".into()],
                name: "docs".into(),
            },
            requirements: vec![],
            updates: vec![
                TableUpdate::AddSnapshot { snapshot: snap },
                TableUpdate::SetSnapshotRef {
                    ref_name: "main".into(),
                    snapshot_type: "branch".into(),
                    snapshot_id: 42,
                },
            ],
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"action\":\"add-snapshot\""));
        assert!(json.contains("\"action\":\"set-snapshot-ref\""));
        assert!(json.contains("\"snapshot-id\":42"));
        assert!(json.contains("\"ref-name\":\"main\""));
    }

    #[test]
    fn create_table_request_includes_ailake_properties() {
        let properties: HashMap<String, String> = HashMap::from([
            ("ailake.vector-column".into(), "embedding".into()),
            ("ailake.vector-dim".into(), "1536".into()),
            ("ailake.vector-metric".into(), "cosine".into()),
            ("ailake.vector-precision".into(), "f16".into()),
        ]);

        let req = CreateTableRequest {
            name: "docs".into(),
            location: Some("s3://bucket/warehouse/default/docs".into()),
            schema: RestSchema::empty_value(),
            partition_spec: None,
            properties: properties.clone(),
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("ailake.vector-column"));
        assert!(json.contains("ailake.vector-dim"));
    }

    #[test]
    fn schema_update_serializes_correctly() {
        let req = CommitTableRequest {
            identifier: TableIdentifier {
                namespace: vec!["default".into()],
                name: "docs".into(),
            },
            requirements: vec![TableRequirement::AssertCurrentSchemaId {
                current_schema_id: 0,
            }],
            updates: vec![
                TableUpdate::AddSchema {
                    schema: serde_json::json!({"schema-id": 1, "type": "struct", "fields": []}),
                },
                TableUpdate::SetCurrentSchema { schema_id: -1 },
            ],
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"action\":\"add-schema\""));
        assert!(json.contains("\"action\":\"set-current-schema\""));
        assert!(json.contains("\"schema-id\":-1"));
        assert!(json.contains("\"type\":\"assert-current-schema-id\""));
        assert!(json.contains("\"current-schema-id\":0"));
    }

    #[test]
    fn assert_ref_snapshot_id_serializes_correctly() {
        let req = TableRequirement::AssertRefSnapshotId {
            r#ref: "main".into(),
            snapshot_id: Some(42),
        };
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"type\":\"assert-ref-snapshot-id\""));
        assert!(json.contains("\"ref\":\"main\""));
        assert!(json.contains("\"snapshot-id\":42"));
    }
}