callum-ryan commented on code in PR #534:
URL: https://github.com/apache/iceberg-rust/pull/534#discussion_r1722170737


##########
crates/catalog/sql/src/catalog.rs:
##########
@@ -167,43 +177,335 @@ impl SqlCatalog {
             .await
             .map_err(from_sqlx_error)
     }
+
+    /// Execute statements in a transaction, provided or not
+    pub async fn execute(
+        &self,
+        query: &str,
+        args: Vec<Option<&str>>,
+        transaction: Option<&mut Transaction<'_, Any>>,
+    ) -> Result<AnyQueryResult> {
+        let query_with_placeholders = self.replace_placeholders(query);
+
+        let mut sqlx_query = sqlx::query(&query_with_placeholders);
+        for arg in args {
+            sqlx_query = sqlx_query.bind(arg);
+        }
+
+        match transaction {
+            Some(t) => sqlx_query.execute(&mut 
**t).await.map_err(from_sqlx_error),
+            None => {
+                let mut tx = 
self.connection.begin().await.map_err(from_sqlx_error)?;
+                let result = sqlx_query.execute(&mut 
*tx).await.map_err(from_sqlx_error);
+                let _ = tx.commit().await.map_err(from_sqlx_error);
+                result
+            }
+        }
+    }
 }
 
 #[async_trait]
 impl Catalog for SqlCatalog {
     async fn list_namespaces(
         &self,
-        _parent: Option<&NamespaceIdent>,
+        parent: Option<&NamespaceIdent>,
     ) -> Result<Vec<NamespaceIdent>> {
-        todo!()
+        let table_namespaces_stmt = format!(
+            "SELECT {CATALOG_FIELD_TABLE_NAMESPACE}
+             FROM {CATALOG_TABLE_NAME}
+             WHERE {CATALOG_FIELD_CATALOG_NAME} = ?"
+        );
+        let namespaces_stmt = format!(
+            "SELECT {NAMESPACE_FIELD_NAME}
+             FROM {NAMESPACE_TABLE_NAME}
+             WHERE {CATALOG_FIELD_CATALOG_NAME} = ?"
+        );
+
+        match parent {
+            Some(parent) => {
+                if self.namespace_exists(parent).await? {
+                    let parent_str = parent.join(".");
+                    let parent_table_namespaces_stmt = format!(
+                        "{table_namespaces_stmt} AND 
{CATALOG_FIELD_TABLE_NAMESPACE} LIKE CONCAT(?, '%')"
+                    );
+                    let parent_namespaces_stmt =
+                        format!("{namespaces_stmt} AND {NAMESPACE_FIELD_NAME} 
LIKE CONCAT(?, '%')");
+
+                    let namespace_rows = self
+                        .fetch_rows(
+                            &format!(
+                                "{parent_namespaces_stmt} UNION 
{parent_table_namespaces_stmt}"
+                            ),
+                            vec![
+                                Some(&self.name),
+                                Some(&parent_str),
+                                Some(&self.name),
+                                Some(&parent_str),
+                            ],
+                        )
+                        .await?;
+
+                    let mut namespaces = 
Vec::<NamespaceIdent>::with_capacity(namespace_rows.len());
+
+                    for row in namespace_rows {
+                        let nsp = row.try_get::<String, 
_>(0).map_err(from_sqlx_error)?;
+                        if nsp != parent_str {

Review Comment:
   I've added a comment here to the loop - all of the returned rows from the 
query will start with parent_str, this is to exclude the parent from the list 
of returned namespaceident.



-- 
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