|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::any::Any; |
| 19 | +use std::collections::HashMap; |
| 20 | +use std::fs::read_to_string; |
| 21 | +use std::path::Path; |
| 22 | +use std::sync::Arc; |
| 23 | +use anyhow::anyhow; |
| 24 | +use datafusion::catalog::{CatalogProvider, CatalogProviderList}; |
| 25 | +use iceberg_datafusion::IcebergCatalogProvider; |
| 26 | +use toml::{Table as TomlTable, Value}; |
| 27 | +use iceberg_catalog_rest::{RestCatalog, RestCatalogConfig}; |
| 28 | +use log::error; |
| 29 | + |
| 30 | +const CONFIG_NAME_CATALOGS: &str = "catalogs"; |
| 31 | + |
| 32 | +#[derive(Debug)] |
| 33 | +pub struct IcebergCatalogList { |
| 34 | + catalogs: HashMap<String, Arc<IcebergCatalogProvider>>, |
| 35 | +} |
| 36 | + |
| 37 | +impl IcebergCatalogList { |
| 38 | + pub async fn parse(path: &Path) -> anyhow::Result<Self> { |
| 39 | + let toml_table: TomlTable = toml::from_str(&read_to_string(path)?)?; |
| 40 | + Self::parse_table(&toml_table).await |
| 41 | + } |
| 42 | + |
| 43 | + pub async fn parse_table(configs: &TomlTable) -> anyhow::Result<Self> { |
| 44 | + if let Value::Array(catalogs_config) = |
| 45 | + configs.get(CONFIG_NAME_CATALOGS).ok_or_else(|| { |
| 46 | + anyhow::Error::msg(format!("{CONFIG_NAME_CATALOGS} entry not found in config")) |
| 47 | + })? |
| 48 | + { |
| 49 | + let mut catalogs = HashMap::with_capacity(catalogs_config.len()); |
| 50 | + for config in catalogs_config { |
| 51 | + if let Value::Table(table_config) = config { |
| 52 | + let (name, catalog_provider) = IcebergCatalogList::parse_one(table_config) |
| 53 | + .await?; |
| 54 | + catalogs.insert(name, catalog_provider); |
| 55 | + } else { |
| 56 | + return Err(anyhow!("{CONFIG_NAME_CATALOGS} entry must be a table")); |
| 57 | + } |
| 58 | + } |
| 59 | + Ok(Self { catalogs }) |
| 60 | + } else { |
| 61 | + Err(anyhow!("{CONFIG_NAME_CATALOGS} must be an array of table!")) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + async fn parse_one(config: &TomlTable) |
| 66 | + -> anyhow::Result<(String, Arc<IcebergCatalogProvider>)> { |
| 67 | + let name = config |
| 68 | + .get("name") |
| 69 | + .ok_or_else(|| anyhow::anyhow!("name not found for catalog"))? |
| 70 | + .as_str() |
| 71 | + .ok_or_else(|| anyhow::anyhow!("name is not string"))?; |
| 72 | + |
| 73 | + let r#type = config |
| 74 | + .get("type") |
| 75 | + .ok_or_else(|| anyhow::anyhow!("type not found for catalog"))? |
| 76 | + .as_str() |
| 77 | + .ok_or_else(|| anyhow::anyhow!("type is not string"))?; |
| 78 | + |
| 79 | + if r#type != "rest" { |
| 80 | + return Err(anyhow::anyhow!("Only rest catalog is supported for now!")); |
| 81 | + } |
| 82 | + |
| 83 | + let catalog_config = config |
| 84 | + .get("config") |
| 85 | + .ok_or_else(|| anyhow::anyhow!("config not found for catalog {name}"))? |
| 86 | + .as_table() |
| 87 | + .ok_or_else(|| anyhow::anyhow!("config is not table for catalog {name}"))?; |
| 88 | + |
| 89 | + let uri = catalog_config |
| 90 | + .get("uri") |
| 91 | + .ok_or_else(|| anyhow::anyhow!("uri not found for catalog {name}"))? |
| 92 | + .as_str() |
| 93 | + .ok_or_else(|| anyhow::anyhow!("uri is not string"))?; |
| 94 | + |
| 95 | + let warehouse = catalog_config |
| 96 | + .get("warehouse") |
| 97 | + .ok_or_else(|| anyhow::anyhow!("warehouse not found for catalog {name}"))? |
| 98 | + .as_str() |
| 99 | + .ok_or_else(|| anyhow::anyhow!("warehouse is not string for catalog {name}"))?; |
| 100 | + |
| 101 | + let props_table = catalog_config |
| 102 | + .get("props") |
| 103 | + .ok_or_else(|| anyhow::anyhow!("props not found for catalog {name}"))? |
| 104 | + .as_table() |
| 105 | + .ok_or_else(|| anyhow::anyhow!("props is not table for catalog {name}"))?; |
| 106 | + |
| 107 | + let mut props = HashMap::with_capacity(props_table.len()); |
| 108 | + for (key, value) in props_table { |
| 109 | + let value_str = value.as_str() |
| 110 | + .ok_or_else(|| anyhow::anyhow!("props {key} is not string"))?; |
| 111 | + props.insert(key.to_string(), value_str.to_string()); |
| 112 | + } |
| 113 | + |
| 114 | + let rest_catalog_config = RestCatalogConfig::builder() |
| 115 | + .uri(uri.to_string()) |
| 116 | + .warehouse(warehouse.to_string()) |
| 117 | + .props(props) |
| 118 | + .build(); |
| 119 | + |
| 120 | + Ok((name.to_string(), Arc::new(IcebergCatalogProvider::try_new(Arc::new( |
| 121 | + RestCatalog::new(rest_catalog_config))).await?))) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +impl CatalogProviderList for IcebergCatalogList { |
| 126 | + fn as_any(&self) -> &dyn Any { |
| 127 | + self |
| 128 | + } |
| 129 | + |
| 130 | + fn register_catalog( |
| 131 | + &self, |
| 132 | + _name: String, |
| 133 | + _catalog: Arc<dyn CatalogProvider>, |
| 134 | + ) -> Option<Arc<dyn CatalogProvider>> { |
| 135 | + error!("Registering catalog is not supported yet"); |
| 136 | + None |
| 137 | + } |
| 138 | + |
| 139 | + fn catalog_names(&self) -> Vec<String> { |
| 140 | + self.catalogs.keys().cloned().collect() |
| 141 | + } |
| 142 | + |
| 143 | + fn catalog(&self, name: &str) -> Option<Arc<dyn CatalogProvider>> { |
| 144 | + self.catalogs.get(name) |
| 145 | + .map(|c| c.clone() as Arc<dyn CatalogProvider>) |
| 146 | + } |
| 147 | +} |
0 commit comments