Skip to content

Experiment implementation for catalog builder #1231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions crates/catalog/loader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "iceberg-catalog-loader"
edition = { workspace = true }
homepage = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

categories = ["database"]
description = "Apache Iceberg Catalog Loader API"
keywords = ["iceberg", "catalog"]
license = { workspace = true }
repository = { workspace = true }

[dependencies]
iceberg = { workspace = true }
iceberg-catalog-rest = {workspace = true}
tokio = { workspace = true }
64 changes: 64 additions & 0 deletions crates/catalog/loader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use iceberg::{Catalog, CatalogBuilder, Error, ErrorKind, Result};
use iceberg_catalog_rest::RestCatalogBuilder;

type BoxedCatalogBuilderFuture = Pin<Box<dyn Future<Output = Result<Arc<dyn Catalog>>>>>;

pub trait BoxedCatalogBuilder {
fn name(&mut self, name: String);
fn uri(&mut self, uri: String);
fn warehouse(&mut self, warehouse: String);
fn with_prop(&mut self, key: String, value: String);

fn build(self: Box<Self>) -> BoxedCatalogBuilderFuture;
}

impl<T: CatalogBuilder + 'static> BoxedCatalogBuilder for T {
fn name(&mut self, name: String) {
self.name(name);
}

fn uri(&mut self, uri: String) {
self.uri(uri);
}

fn warehouse(&mut self, warehouse: String) {
self.warehouse(warehouse);
}

fn with_prop(&mut self, key: String, value: String) {
self.with_prop(key, value);
}

fn build(self: Box<Self>) -> BoxedCatalogBuilderFuture {
let builder = *self;
Box::pin(async move { Ok(Arc::new(builder.build().await.unwrap()) as Arc<dyn Catalog>) })
}
}

pub fn load(r#type: &str) -> Result<Box<dyn BoxedCatalogBuilder>> {
match r#type {
"rest" => Ok(Box::new(RestCatalogBuilder::default()) as Box<dyn BoxedCatalogBuilder>),
_ => Err(Error::new(
ErrorKind::FeatureUnsupported,
format!("Unsupported catalog type: {}", r#type),
)),
}
}

#[cfg(test)]
mod tests {
use crate::load;

#[tokio::test]
async fn test_load() {
let mut catalog = load("rest").unwrap();
catalog.name("rest".to_string());
catalog.with_prop("key".to_string(), "value".to_string());

catalog.build().await.unwrap();
}
}
88 changes: 86 additions & 2 deletions crates/catalog/rest/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
//! This module contains the iceberg REST catalog implementation.

use std::collections::HashMap;
use std::future::Future;
use std::str::FromStr;

use async_trait::async_trait;
use iceberg::io::FileIO;
use iceberg::table::Table;
use iceberg::{
Catalog, Error, ErrorKind, Namespace, NamespaceIdent, Result, TableCommit, TableCreation,
TableIdent,
Catalog, CatalogBuilder, Error, ErrorKind, Namespace, NamespaceIdent, Result, TableCommit,
TableCreation, TableIdent,
};
use itertools::Itertools;
use reqwest::header::{
Expand All @@ -51,6 +52,8 @@ const PATH_V1: &str = "v1";
/// Rest catalog configuration.
#[derive(Clone, Debug, TypedBuilder)]
pub struct RestCatalogConfig {
#[builder(default, setter(strip_option))]
name: Option<String>,
uri: String,

#[builder(default, setter(strip_option(fallback = warehouse_opt)))]
Expand Down Expand Up @@ -223,6 +226,74 @@ impl RestCatalogConfig {
}
}

/// Builder for [`RestCatalog`].
#[derive(Debug)]
pub struct RestCatalogBuilder(RestCatalogConfig);

impl Default for RestCatalogBuilder {
fn default() -> Self {
Self(RestCatalogConfig {
name: None,
uri: "".to_string(),
warehouse: None,
props: HashMap::new(),
client: None,
})
}
}

impl CatalogBuilder for RestCatalogBuilder {
type C = RestCatalog;

fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.0.name = Some(name.into());
self
}

fn uri(&mut self, uri: impl Into<String>) -> &mut Self {
self.0.uri = uri.into();
self
}

fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self {
self.0.warehouse = Some(warehouse.into());
self
}

fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
self.0.props.insert(key.into(), value.into());
self
}

fn build(self) -> impl Future<Output = Result<Self::C>> {
let result = {
if self.0.name.is_none() {
Err(Error::new(
ErrorKind::DataInvalid,
"Catalog name is required",
))
} else if self.0.uri.is_empty() {
Err(Error::new(
ErrorKind::DataInvalid,
"Catalog uri is required",
))
} else {
Ok(RestCatalog::new(self.0))
}
};

std::future::ready(result)
}
}

impl RestCatalogBuilder {
/// Configures the catalog with a custom HTTP client.
pub fn with_client(&mut self, client: Client) -> &mut Self {
self.0.client = Some(client);
self
}
}

#[derive(Debug)]
struct RestContext {
client: HttpClient,
Expand Down Expand Up @@ -2257,4 +2328,17 @@ mod tests {
config_mock.assert_async().await;
update_table_mock.assert_async().await;
}

#[tokio::test]
async fn test_create_rest_catalog() {
let mut builder = RestCatalogBuilder::default();
builder
.name("test")
.uri("http://localhost:8080")
.with_client(Client::new())
.with_prop("a", "b");
let catalog = builder.build().await;

assert!(catalog.is_ok());
}
}
17 changes: 17 additions & 0 deletions crates/iceberg/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::future::Future;
use std::mem::take;
use std::ops::Deref;

Expand Down Expand Up @@ -96,6 +97,22 @@ pub trait Catalog: Debug + Sync + Send {
async fn update_table(&self, commit: TableCommit) -> Result<Table>;
}

/// Common interface for all catalog builders.
pub trait CatalogBuilder: Default + Debug + Send + Sync {
/// The catalog type that this builder creates.
type C: Catalog;
/// Configure the name of the catalog.
fn name(&mut self, name: impl Into<String>) -> &mut Self;
/// Configure uri of the catalog.
fn uri(&mut self, uri: impl Into<String>) -> &mut Self;
/// Configure the warehouse location of the catalog.
fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self;
/// Configure properties of the catalog.
fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self;
/// Create the catalog
fn build(self) -> impl Future<Output = Result<Self::C>>;
}

/// NamespaceIdent represents the identifier of a namespace in the catalog.
///
/// The namespace identifier is a list of strings, where each string is a
Expand Down
5 changes: 1 addition & 4 deletions crates/iceberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ pub use error::{Error, ErrorKind, Result};

mod catalog;

pub use catalog::{
Catalog, Namespace, NamespaceIdent, TableCommit, TableCreation, TableIdent, TableRequirement,
TableUpdate, ViewCreation,
};
pub use catalog::*;

pub mod table;

Expand Down
Loading