liurenjie1024 commented on code in PR #237:
URL: https://github.com/apache/iceberg-rust/pull/237#discussion_r1518567066


##########
crates/catalog/hms/src/utils.rs:
##########
@@ -40,3 +54,258 @@ pub fn from_io_error(error: io::Error) -> Error {
     )
     .with_source(error)
 }
+
+/// Create and extract properties from `hive_metastore::hms::Database`.
+pub fn properties_from_database(database: &Database) -> HashMap<String, 
String> {

Review Comment:
   ```suggestion
   pub(crate) fn properties_from_database(database: &Database) -> 
HashMap<String, String> {
   ```



##########
crates/catalog/hms/src/utils.rs:
##########
@@ -40,3 +54,258 @@ pub fn from_io_error(error: io::Error) -> Error {
     )
     .with_source(error)
 }
+
+/// Create and extract properties from `hive_metastore::hms::Database`.
+pub fn properties_from_database(database: &Database) -> HashMap<String, 
String> {
+    let mut properties = HashMap::new();
+
+    if let Some(description) = &database.description {
+        properties.insert(COMMENT.to_string(), description.to_string());
+    };
+
+    if let Some(location) = &database.location_uri {
+        properties.insert(LOCATION.to_string(), location.to_string());
+    };
+
+    if let Some(owner) = &database.owner_name {
+        properties.insert(HMS_DB_OWNER.to_string(), owner.to_string());
+    };
+
+    if let Some(owner_type) = &database.owner_type {
+        let value = match owner_type {
+            PrincipalType::User => "User",
+            PrincipalType::Group => "Group",
+            PrincipalType::Role => "Role",
+        };
+
+        properties.insert(HMS_DB_OWNER_TYPE.to_string(), value.to_string());
+    };
+
+    if let Some(params) = &database.parameters {
+        params.iter().for_each(|(k, v)| {
+            properties.insert(k.clone().into(), v.clone().into());
+        });
+    };
+
+    properties
+}
+
+/// Converts name and properties into `hive_metastore::hms::Database`
+/// after validating the `namespace` and `owner-settings`.
+pub fn convert_to_database(

Review Comment:
   ```suggestion
   pub(crate) fn convert_to_database(
   ```



##########
crates/catalog/hms/src/utils.rs:
##########
@@ -40,3 +54,258 @@ pub fn from_io_error(error: io::Error) -> Error {
     )
     .with_source(error)
 }
+
+/// Create and extract properties from `hive_metastore::hms::Database`.
+pub fn properties_from_database(database: &Database) -> HashMap<String, 
String> {
+    let mut properties = HashMap::new();
+
+    if let Some(description) = &database.description {
+        properties.insert(COMMENT.to_string(), description.to_string());
+    };
+
+    if let Some(location) = &database.location_uri {
+        properties.insert(LOCATION.to_string(), location.to_string());
+    };
+
+    if let Some(owner) = &database.owner_name {
+        properties.insert(HMS_DB_OWNER.to_string(), owner.to_string());
+    };
+
+    if let Some(owner_type) = &database.owner_type {
+        let value = match owner_type {
+            PrincipalType::User => "User",
+            PrincipalType::Group => "Group",
+            PrincipalType::Role => "Role",
+        };
+
+        properties.insert(HMS_DB_OWNER_TYPE.to_string(), value.to_string());
+    };
+
+    if let Some(params) = &database.parameters {
+        params.iter().for_each(|(k, v)| {
+            properties.insert(k.clone().into(), v.clone().into());
+        });
+    };
+
+    properties
+}
+
+/// Converts name and properties into `hive_metastore::hms::Database`
+/// after validating the `namespace` and `owner-settings`.
+pub fn convert_to_database(
+    namespace: &NamespaceIdent,
+    properties: &HashMap<String, String>,
+) -> Result<Database> {
+    let name = validate_namespace(namespace)?;
+    validate_owner_settings(properties)?;
+
+    let mut db = Database::default();
+    let mut parameters = AHashMap::new();
+
+    db.name = Some(name.into());
+
+    for (k, v) in properties {
+        match k.as_str() {
+            COMMENT => db.description = Some(v.clone().into()),
+            LOCATION => db.location_uri = 
Some(format_location_uri(v.clone()).into()),
+            HMS_DB_OWNER => db.owner_name = Some(v.clone().into()),
+            HMS_DB_OWNER_TYPE => {
+                let owner_type = match v.to_lowercase().as_str() {
+                    "user" => PrincipalType::User,
+                    "group" => PrincipalType::Group,
+                    "role" => PrincipalType::Role,
+                    _ => {
+                        return Err(Error::new(
+                            ErrorKind::DataInvalid,
+                            format!("Invalid value for setting 'owner_type': 
{}", v),
+                        ))
+                    }
+                };
+                db.owner_type = Some(owner_type);
+            }
+            _ => {
+                parameters.insert(
+                    FastStr::from_string(k.clone()),
+                    FastStr::from_string(v.clone()),
+                );
+            }
+        }
+    }
+
+    db.parameters = Some(parameters);
+
+    // Set default owner, if none provided
+    // 
https://github.com/apache/iceberg/blob/main/hive-metastore/src/main/java/org/apache/iceberg/hive/HiveHadoopUtil.java#L44
+    if db.owner_name.is_none() {
+        db.owner_name = Some(HMS_DEFAULT_DB_OWNER.into());
+        db.owner_type = Some(PrincipalType::User);
+    }
+
+    Ok(db)
+}
+
+/// Checks if provided `NamespaceIdent` is valid.
+pub fn validate_namespace(namespace: &NamespaceIdent) -> Result<String> {

Review Comment:
   ```suggestion
   pub(crate) fn validate_namespace(namespace: &NamespaceIdent) -> 
Result<String> {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to