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
use std::sync::Arc;

use async_trait::async_trait;
use dashmap::DashMap;
use futures::lock::Mutex;
use iceberg_rust::{
    catalog::{
        bucket::{parse_bucket, Bucket},
        commit::{
            apply_table_updates, apply_view_updates, check_table_requirements,
            check_view_requirements, CommitTable, CommitView, TableRequirement,
        },
        identifier::Identifier,
        namespace::Namespace,
        tabular::Tabular,
        Catalog, CatalogList,
    },
    error::Error as IcebergError,
    materialized_view::MaterializedView,
    spec::{
        materialized_view_metadata::MaterializedViewMetadata,
        table_metadata::{new_metadata_location, TableMetadata},
        tabular::TabularMetadata,
        view_metadata::ViewMetadata,
    },
    table::Table,
    util::strip_prefix,
    view::View,
};
use object_store::ObjectStore;
use sqlx::{
    any::{install_default_drivers, AnyConnectOptions, AnyRow},
    AnyConnection, ConnectOptions, Connection, Row,
};
use uuid::Uuid;

use crate::error::Error;

#[derive(Debug)]
pub struct SqlCatalog {
    name: String,
    connection: Arc<Mutex<AnyConnection>>,
    object_store: Arc<dyn ObjectStore>,
    cache: Arc<DashMap<Identifier, (String, TabularMetadata)>>,
}

pub mod error;

impl SqlCatalog {
    pub async fn new(
        url: &str,
        name: &str,
        object_store: Arc<dyn ObjectStore>,
    ) -> Result<Self, Error> {
        install_default_drivers();

        let mut connection =
            AnyConnectOptions::connect(&AnyConnectOptions::from_url(&url.try_into()?)?).await?;

        connection
            .transaction(|txn| {
                Box::pin(async move {
                    sqlx::query(
                        "create table if not exists iceberg_tables (
                                catalog_name text not null,
                                table_namespace text not null,
                                table_name text not null,
                                metadata_location text not null,
                                previous_metadata_location text,
                                primary key (catalog_name, table_namespace, table_name)
                            );",
                    )
                    .execute(&mut **txn)
                    .await
                })
            })
            .await?;

        connection
            .transaction(|txn| {
                Box::pin(async move {
                    sqlx::query(
                        "create table if not exists iceberg_namespace_properties (
                                catalog_name text not null,
                                namespace text not null,
                                property_key text,
                                property_value text,
                                primary key (catalog_name, namespace, property_key)
                            );",
                    )
                    .execute(&mut **txn)
                    .await
                })
            })
            .await?;

        Ok(SqlCatalog {
            name: name.to_owned(),
            connection: Arc::new(Mutex::new(connection)),
            object_store,
            cache: Arc::new(DashMap::new()),
        })
    }

    pub fn catalog_list(&self) -> Arc<SqlCatalogList> {
        Arc::new(SqlCatalogList {
            connection: self.connection.clone(),
            object_store: self.object_store.clone(),
        })
    }
}

#[derive(Debug)]
struct TableRef {
    table_namespace: String,
    table_name: String,
    metadata_location: String,
    _previous_metadata_location: Option<String>,
}

fn query_map(row: &AnyRow) -> Result<TableRef, sqlx::Error> {
    Ok(TableRef {
        table_namespace: row.try_get(0)?,
        table_name: row.try_get(1)?,
        metadata_location: row.try_get(2)?,
        _previous_metadata_location: row.try_get::<String, _>(3).map(Some).or_else(|err| {
            if let sqlx::Error::ColumnDecode {
                index: _,
                source: _,
            } = err
            {
                Ok(None)
            } else {
                Err(err)
            }
        })?,
    })
}

#[async_trait]
impl Catalog for SqlCatalog {
    async fn list_tables(&self, namespace: &Namespace) -> Result<Vec<Identifier>, IcebergError> {
        let mut connection = self.connection.lock().await;
        let rows = connection.transaction(|txn|{
            let name = self.name.clone();
            let namespace = namespace.to_string();
            Box::pin(async move {
            sqlx::query(&format!("select table_namespace, table_name, metadata_location, previous_metadata_location from iceberg_tables where catalog_name = '{}' and table_namespace = '{}';",&name, &namespace)).fetch_all(&mut **txn).await
        })}).await.map_err(Error::from)?;
        let iter = rows.iter().map(query_map);

        Ok(iter
            .map(|x| {
                x.and_then(|y| {
                    Identifier::parse(&(y.table_namespace.to_string() + "." + &y.table_name))
                        .map_err(|err| sqlx::Error::Decode(Box::new(err)))
                })
            })
            .collect::<Result<_, sqlx::Error>>()
            .map_err(Error::from)?)
    }
    async fn list_namespaces(&self, _parent: Option<&str>) -> Result<Vec<Namespace>, IcebergError> {
        let mut connection = self.connection.lock().await;
        let rows = connection.transaction(|txn|{
            let name = self.name.clone();
            Box::pin(async move {
            sqlx::query(&format!("select distinct table_namespace from iceberg_tables where catalog_name = '{}';",&name)).fetch_all(&mut **txn).await
        })}).await.map_err(Error::from)?;
        let iter = rows.iter().map(|row| row.try_get::<String, _>(0));

        Ok(iter
            .map(|x| {
                x.and_then(|y| {
                    Namespace::try_new(&y.split('.').map(ToString::to_string).collect::<Vec<_>>())
                        .map_err(|err| sqlx::Error::Decode(Box::new(err)))
                })
            })
            .collect::<Result<_, sqlx::Error>>()
            .map_err(Error::from)?)
    }
    async fn table_exists(&self, identifier: &Identifier) -> Result<bool, IcebergError> {
        let mut connection = self.connection.lock().await;
        let rows = connection.transaction(|txn|{
            let catalog_name = self.name.clone();
            let namespace = identifier.namespace().to_string();
            let name = identifier.name().to_string();
            Box::pin(async move {
            sqlx::query(&format!("select table_namespace, table_name, metadata_location, previous_metadata_location from iceberg_tables where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';",&catalog_name,
                &namespace,
                &name)).fetch_all(&mut **txn).await
        })}).await.map_err(Error::from)?;
        let mut iter = rows.iter().map(query_map);

        Ok(iter.next().is_some())
    }
    async fn drop_table(&self, identifier: &Identifier) -> Result<(), IcebergError> {
        let mut connection = self.connection.lock().await;
        connection.transaction(|txn|{
            let catalog_name = self.name.clone();
            let namespace = identifier.namespace().to_string();
            let name = identifier.name().to_string();
            Box::pin(async move {
            sqlx::query(&format!("delete from iceberg_tables where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';",&catalog_name,
                &namespace,
                &name)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
        Ok(())
    }
    async fn load_tabular(
        self: Arc<Self>,
        identifier: &Identifier,
    ) -> Result<Tabular, IcebergError> {
        let path = {
            let mut connection = self.connection.lock().await;
            let row = connection.transaction(|txn|{
            let catalog_name = self.name.clone();
            let namespace = identifier.namespace().to_string();
            let name = identifier.name().to_string();
                Box::pin(async move {
            sqlx::query(&format!("select table_namespace, table_name, metadata_location, previous_metadata_location from iceberg_tables where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';",&catalog_name,
                    &namespace,
                    &name)).fetch_one(&mut **txn).await
        })}).await.map_err(Error::from)?;
            let row = query_map(&row).map_err(Error::from)?;

            row.metadata_location
        };
        let bytes = &self
            .object_store
            .get(&strip_prefix(&path).as_str().into())
            .await?
            .bytes()
            .await?;
        let metadata: TabularMetadata = serde_json::from_str(std::str::from_utf8(bytes)?)?;
        self.cache
            .insert(identifier.clone(), (path.clone(), metadata.clone()));
        match metadata {
            TabularMetadata::Table(metadata) => Ok(Tabular::Table(
                Table::new(identifier.clone(), self.clone(), metadata).await?,
            )),
            TabularMetadata::View(metadata) => Ok(Tabular::View(
                View::new(identifier.clone(), self.clone(), metadata).await?,
            )),
            TabularMetadata::MaterializedView(metadata) => Ok(Tabular::MaterializedView(
                MaterializedView::new(identifier.clone(), self.clone(), metadata).await?,
            )),
        }
    }

    async fn create_table(
        self: Arc<Self>,
        identifier: Identifier,
        metadata: TableMetadata,
    ) -> Result<Table, IcebergError> {
        // Create metadata
        let location = metadata.location.to_string();

        // Write metadata to object_store
        let bucket = parse_bucket(&location)?;
        let object_store = self.object_store(bucket);

        let uuid = Uuid::new_v4();
        let version = &metadata.last_sequence_number;
        let metadata_json = serde_json::to_string(&metadata)?;
        let metadata_location = location
            + "/metadata/"
            + &version.to_string()
            + "-"
            + &uuid.to_string()
            + ".metadata.json";
        object_store
            .put(
                &strip_prefix(&metadata_location).into(),
                metadata_json.into(),
            )
            .await?;
        {
            let mut connection = self.connection.lock().await;
            connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_location = metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("insert into iceberg_tables (catalog_name, table_namespace, table_name, metadata_location) values ('{}', '{}', '{}', '{}');",catalog_name,namespace,name, metadata_location)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
        }
        self.clone()
            .load_tabular(&identifier)
            .await
            .and_then(|x| match x {
                Tabular::Table(table) => Ok(table),
                _ => Err(IcebergError::InvalidFormat(
                    "Table update on an entity that is nor a table".to_owned(),
                )),
            })
    }

    async fn create_view(
        self: Arc<Self>,
        identifier: Identifier,
        metadata: ViewMetadata,
    ) -> Result<View, IcebergError> {
        // Create metadata
        let location = metadata.location.to_string();

        // Write metadata to object_store
        let bucket = parse_bucket(&location)?;
        let object_store = self.object_store(bucket);

        let uuid = Uuid::new_v4();
        let version = &metadata.current_version_id;
        let metadata_json = serde_json::to_string(&metadata)?;
        let metadata_location = location
            + "/metadata/"
            + &version.to_string()
            + "-"
            + &uuid.to_string()
            + ".metadata.json";
        object_store
            .put(
                &strip_prefix(&metadata_location).into(),
                metadata_json.into(),
            )
            .await?;
        {
            let mut connection = self.connection.lock().await;
            connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_location = metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("insert into iceberg_tables (catalog_name, table_namespace, table_name, metadata_location) values ('{}', '{}', '{}', '{}');",catalog_name,namespace,name, metadata_location)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
        }
        self.clone()
            .load_tabular(&identifier)
            .await
            .and_then(|x| match x {
                Tabular::View(view) => Ok(view),
                _ => Err(IcebergError::InvalidFormat(
                    "Table update on an entity that is nor a table".to_owned(),
                )),
            })
    }

    async fn create_materialized_view(
        self: Arc<Self>,
        identifier: Identifier,
        metadata: MaterializedViewMetadata,
    ) -> Result<MaterializedView, IcebergError> {
        // Create metadata
        let location = metadata.location.to_string();

        // Write metadata to object_store
        let bucket = parse_bucket(&location)?;
        let object_store = self.object_store(bucket);

        let uuid = Uuid::new_v4();
        let version = &metadata.current_version_id;
        let metadata_json = serde_json::to_string(&metadata)?;
        let metadata_location = location
            + "/metadata/"
            + &version.to_string()
            + "-"
            + &uuid.to_string()
            + ".metadata.json";
        object_store
            .put(
                &strip_prefix(&metadata_location).into(),
                metadata_json.into(),
            )
            .await?;
        {
            let mut connection = self.connection.lock().await;
            connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_location = metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("insert into iceberg_tables (catalog_name, table_namespace, table_name, metadata_location) values ('{}', '{}', '{}', '{}');",catalog_name,namespace,name, metadata_location)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
        }
        self.clone()
            .load_tabular(&identifier)
            .await
            .and_then(|x| match x {
                Tabular::MaterializedView(matview) => Ok(matview),
                _ => Err(IcebergError::InvalidFormat(
                    "Table update on an entity that is nor a table".to_owned(),
                )),
            })
    }

    async fn update_table(self: Arc<Self>, commit: CommitTable) -> Result<Table, IcebergError> {
        let identifier = commit.identifier;
        match self.cache.get(&identifier) {
            None => {
                if !matches!(commit.requirements[0], TableRequirement::AssertCreate) {
                    return Err(IcebergError::InvalidFormat(
                        "Create table assertion".to_owned(),
                    ));
                } else {
                    return Err(IcebergError::InvalidFormat(
                        "Create table assertion".to_owned(),
                    ));
                }
            }
            Some(entry) => {
                let (previous_metadata_location, metadata) = entry.value();

                let TabularMetadata::Table(mut metadata) = metadata.clone() else {
                    return Err(IcebergError::InvalidFormat(
                        "Table update on entity that is not a table".to_owned(),
                    ));
                };
                if !check_table_requirements(&commit.requirements, &metadata) {
                    return Err(IcebergError::InvalidFormat(
                        "Table requirements not valid".to_owned(),
                    ));
                }
                apply_table_updates(&mut metadata, commit.updates)?;
                let metadata_location = new_metadata_location((&metadata).into());
                self.object_store
                    .put(
                        &strip_prefix(&metadata_location).into(),
                        serde_json::to_string(&metadata)?.into(),
                    )
                    .await?;
                let mut connection = self.connection.lock().await;
                connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_file_location = metadata_location.to_string();
                let previous_metadata_file_location = previous_metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("update iceberg_tables set metadata_location = '{}', previous_metadata_location = '{}' where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';", metadata_file_location, previous_metadata_file_location,catalog_name,namespace,name)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
            }
        }
        self.clone()
            .load_tabular(&identifier)
            .await
            .and_then(|x| match x {
                Tabular::Table(table) => Ok(table),
                _ => Err(IcebergError::InvalidFormat(
                    "Table update on an entity that is nor a table".to_owned(),
                )),
            })
    }

    async fn update_view(self: Arc<Self>, commit: CommitView) -> Result<View, IcebergError> {
        let identifier = commit.identifier;
        match self.cache.get(&identifier) {
            None => {
                return Err(IcebergError::InvalidFormat(
                    "Create table assertion".to_owned(),
                ))
            }
            Some(entry) => {
                let (previous_metadata_location, metadata) = entry.value();
                let mut metadata = metadata.clone();
                let metadata_location = match &mut metadata {
                    TabularMetadata::View(metadata) => {
                        if !check_view_requirements(&commit.requirements, metadata) {
                            return Err(IcebergError::InvalidFormat(
                                "View requirements not valid".to_owned(),
                            ));
                        }
                        apply_view_updates(metadata, commit.updates)?;
                        let metadata_location = metadata.location.to_string()
                            + "/metadata/"
                            + &metadata.current_version_id.to_string()
                            + "-"
                            + &Uuid::new_v4().to_string()
                            + ".metadata.json";
                        self.object_store
                            .put(
                                &strip_prefix(&metadata_location).into(),
                                serde_json::to_string(&metadata)?.into(),
                            )
                            .await?;
                        Ok(metadata_location)
                    }
                    TabularMetadata::MaterializedView(metadata) => {
                        if !check_view_requirements(&commit.requirements, metadata) {
                            return Err(IcebergError::InvalidFormat(
                                "Materialized view requirements not valid".to_owned(),
                            ));
                        }
                        apply_view_updates(metadata, commit.updates)?;
                        let metadata_location = metadata.location.to_string()
                            + "/metadata/"
                            + &metadata.current_version_id.to_string()
                            + "-"
                            + &Uuid::new_v4().to_string()
                            + ".metadata.json";
                        self.object_store
                            .put(
                                &strip_prefix(&metadata_location).into(),
                                serde_json::to_string(&metadata)?.into(),
                            )
                            .await?;
                        Ok(metadata_location)
                    }
                    _ => Err(IcebergError::InvalidFormat(
                        "Table update on entity that is not a table".to_owned(),
                    )),
                }?;

                let mut connection = self.connection.lock().await;
                connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_file_location = metadata_location.to_string();
                let previous_metadata_file_location = previous_metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("update iceberg_tables set metadata_location = '{}', previous_metadata_location = '{}' where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';", metadata_file_location, previous_metadata_file_location,catalog_name,namespace,name)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
            }
        }
        if let Tabular::View(view) = self.clone().load_tabular(&identifier).await? {
            Ok(view)
        } else {
            Err(IcebergError::InvalidFormat(
                "Entity is not a view".to_owned(),
            ))
        }
    }
    async fn update_materialized_view(
        self: Arc<Self>,
        commit: CommitView,
    ) -> Result<MaterializedView, IcebergError> {
        let identifier = commit.identifier;
        match self.cache.get(&identifier) {
            None => {
                return Err(IcebergError::InvalidFormat(
                    "Create table assertion".to_owned(),
                ))
            }
            Some(entry) => {
                let (previous_metadata_location, metadata) = entry.value();
                let mut metadata = metadata.clone();
                let metadata_location = match &mut metadata {
                    TabularMetadata::View(metadata) => {
                        if !check_view_requirements(&commit.requirements, metadata) {
                            return Err(IcebergError::InvalidFormat(
                                "View requirements not valid".to_owned(),
                            ));
                        }
                        apply_view_updates(metadata, commit.updates)?;
                        let metadata_location = metadata.location.to_string()
                            + "/metadata/"
                            + &metadata.current_version_id.to_string()
                            + "-"
                            + &Uuid::new_v4().to_string()
                            + ".metadata.json";
                        self.object_store
                            .put(
                                &strip_prefix(&metadata_location).into(),
                                serde_json::to_string(&metadata)?.into(),
                            )
                            .await?;
                        Ok(metadata_location)
                    }
                    TabularMetadata::MaterializedView(metadata) => {
                        if !check_view_requirements(&commit.requirements, metadata) {
                            return Err(IcebergError::InvalidFormat(
                                "Materialized view requirements not valid".to_owned(),
                            ));
                        }
                        apply_view_updates(metadata, commit.updates)?;
                        let metadata_location = metadata.location.to_string()
                            + "/metadata/"
                            + &metadata.current_version_id.to_string()
                            + "-"
                            + &Uuid::new_v4().to_string()
                            + ".metadata.json";
                        self.object_store
                            .put(
                                &strip_prefix(&metadata_location).into(),
                                serde_json::to_string(&metadata)?.into(),
                            )
                            .await?;
                        Ok(metadata_location)
                    }
                    _ => Err(IcebergError::InvalidFormat(
                        "Table update on entity that is not a table".to_owned(),
                    )),
                }?;

                let mut connection = self.connection.lock().await;
                connection.transaction(|txn|{
                let catalog_name = self.name.clone();
                let namespace = identifier.namespace().to_string();
                let name = identifier.name().to_string();
                let metadata_file_location = metadata_location.to_string();
                let previous_metadata_file_location = previous_metadata_location.to_string();
                Box::pin(async move {
            sqlx::query(&format!("update iceberg_tables set metadata_location = '{}', previous_metadata_location = '{}' where catalog_name = '{}' and table_namespace = '{}' and table_name = '{}';", metadata_file_location, previous_metadata_file_location,catalog_name,namespace,name)).execute(&mut **txn).await
        })}).await.map_err(Error::from)?;
            }
        }
        if let Tabular::MaterializedView(matview) = self.clone().load_tabular(&identifier).await? {
            Ok(matview)
        } else {
            Err(IcebergError::InvalidFormat(
                "Entity is not a materialized view".to_owned(),
            ))
        }
    }

    fn object_store(&self, _: Bucket) -> Arc<dyn object_store::ObjectStore> {
        self.object_store.clone()
    }
}

impl SqlCatalog {
    pub fn duplicate(&self, name: &str) -> Self {
        Self {
            name: name.to_owned(),
            connection: self.connection.clone(),
            object_store: self.object_store.clone(),
            cache: Arc::new(DashMap::new()),
        }
    }
}

#[derive(Debug)]
pub struct SqlCatalogList {
    connection: Arc<Mutex<AnyConnection>>,
    object_store: Arc<dyn ObjectStore>,
}

impl SqlCatalogList {
    pub async fn new(url: &str, object_store: Arc<dyn ObjectStore>) -> Result<Self, Error> {
        install_default_drivers();

        let mut connection =
            AnyConnectOptions::connect(&AnyConnectOptions::from_url(&url.try_into()?)?).await?;

        connection
            .transaction(|txn| {
                Box::pin(async move {
                    sqlx::query(
                        "create table if not exists iceberg_tables (
                                catalog_name text not null,
                                table_namespace text not null,
                                table_name text not null,
                                metadata_location text not null,
                                previous_metadata_location text,
                                primary key (catalog_name, table_namespace, table_name)
                            );",
                    )
                    .execute(&mut **txn)
                    .await
                })
            })
            .await?;

        Ok(SqlCatalogList {
            connection: Arc::new(Mutex::new(connection)),
            object_store,
        })
    }
}

#[async_trait]
impl CatalogList for SqlCatalogList {
    async fn catalog(&self, name: &str) -> Option<Arc<dyn Catalog>> {
        Some(Arc::new(SqlCatalog {
            name: name.to_owned(),
            connection: self.connection.clone(),
            object_store: self.object_store.clone(),
            cache: Arc::new(DashMap::new()),
        }))
    }
    async fn list_catalogs(&self) -> Vec<String> {
        let mut connection = self.connection.lock().await;
        let rows = connection
            .transaction(|txn| {
                Box::pin(async move {
                    sqlx::query("select distinct catalog_name from iceberg_tables;")
                        .fetch_all(&mut **txn)
                        .await
                })
            })
            .await
            .map_err(Error::from)
            .unwrap_or_default();
        let iter = rows.iter().map(|row| row.try_get::<String, _>(0));

        iter.collect::<Result<_, sqlx::Error>>()
            .map_err(Error::from)
            .unwrap_or_default()
    }
}

#[cfg(test)]
pub mod tests {
    use iceberg_rust::{
        catalog::{identifier::Identifier, namespace::Namespace, Catalog},
        spec::{
            schema::Schema,
            types::{PrimitiveType, StructField, StructType, Type},
        },
        table::table_builder::TableBuilder,
    };
    use object_store::{memory::InMemory, ObjectStore};
    use std::sync::Arc;

    use crate::SqlCatalog;

    #[tokio::test]
    async fn test_create_update_drop_table() {
        let object_store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let catalog: Arc<dyn Catalog> = Arc::new(
            SqlCatalog::new("sqlite://", "test", object_store)
                .await
                .unwrap(),
        );
        let identifier = Identifier::parse("load_table.table3").unwrap();
        let schema = Schema::builder()
            .with_schema_id(1)
            .with_identifier_field_ids(vec![1, 2])
            .with_fields(
                StructType::builder()
                    .with_struct_field(StructField {
                        id: 1,
                        name: "one".to_string(),
                        required: false,
                        field_type: Type::Primitive(PrimitiveType::String),
                        doc: None,
                    })
                    .with_struct_field(StructField {
                        id: 2,
                        name: "two".to_string(),
                        required: false,
                        field_type: Type::Primitive(PrimitiveType::String),
                        doc: None,
                    })
                    .build()
                    .unwrap(),
            )
            .build()
            .unwrap();

        let mut builder = TableBuilder::new(&identifier, catalog.clone())
            .expect("Failed to create table builder.");
        builder
            .location("/")
            .with_schema((1, schema))
            .current_schema_id(1);
        let mut table = builder.build().await.expect("Failed to create table.");

        let exists = Arc::clone(&catalog)
            .table_exists(&identifier)
            .await
            .expect("Table doesn't exist");
        assert!(exists);

        let tables = catalog
            .clone()
            .list_tables(
                &Namespace::try_new(&["load_table".to_owned()])
                    .expect("Failed to create namespace"),
            )
            .await
            .expect("Failed to list Tables");
        assert_eq!(tables[0].to_string(), "load_table.table3".to_owned());

        let namespaces = catalog
            .clone()
            .list_namespaces(None)
            .await
            .expect("Failed to list namespaces");
        assert_eq!(namespaces[0].to_string(), "load_table");

        let transaction = table.new_transaction(None);
        transaction.commit().await.expect("Transaction failed.");

        catalog
            .drop_table(&identifier)
            .await
            .expect("Failed to drop table.");

        let exists = Arc::clone(&catalog)
            .table_exists(&identifier)
            .await
            .expect("Table exists failed");
        assert!(!exists);
    }
}