manuzhang commented on code in PR #3071:
URL: https://github.com/apache/iceberg-rust/pull/3071#discussion_r4338226244


##########
crates/catalog/sql/src/catalog.rs:
##########
@@ -338,22 +339,32 @@ pub enum SchemaVersion {
 impl SchemaVersion {
     /// Detect the schema version of an existing catalog table by 
introspecting its columns.
     async fn detect(pool: &AnyPool) -> Result<Self> {
-        let catalog_table_description = pool
-            .describe(&format!("SELECT * FROM {CATALOG_TABLE_NAME}"))
-            .await
-            .map_err(from_sqlx_error)?;
-
-        let has_type_column = 
catalog_table_description.columns().iter().any(|column| {
+        Self::probe(pool).await.map_err(from_sqlx_error)
+    }
+
+    async fn probe(pool: &AnyPool) -> sqlx::Result<Self> {
+        let mut transaction = pool.begin().await?;
+        // Run both reads as one unprepared batch in a transaction. The empty 
read refreshes
+        // the schema before the second statement prepares its column 
metadata. The second
+        // statement returns one NULL row even for an empty catalog.
+        let probe = format!(
+            "SELECT * FROM {CATALOG_TABLE_NAME} WHERE 1 = 0; \
+             SELECT catalog.* FROM (SELECT 1) AS probe \
+             LEFT JOIN {CATALOG_TABLE_NAME} AS catalog ON 1 = 0"
+        );
+        let catalog_row = transaction
+            .fetch_all(sqlx::raw_sql(&probe))
+            .await?
+            .pop()
+            .ok_or(sqlx::Error::RowNotFound)?;

Review Comment:
   I'm hesitating to add MySQL or Postgres in unit tests. @blackmwk WDYT?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to