dannycjones commented on code in PR #3032:
URL: https://github.com/apache/iceberg-rust/pull/3032#discussion_r3830789514


##########
crates/catalog/sql/src/catalog.rs:
##########
@@ -268,6 +305,62 @@ pub struct SqlCatalog {
     sql_bind_style: SqlBindStyle,
     runtime: Runtime,
     kms_client: Option<Arc<dyn KeyManagementClient>>,
+    schema_version: SchemaVersion,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, strum::EnumString, strum::Display)]
+#[strum(ascii_case_insensitive)]
+/// Schema version of the `iceberg_tables` catalog table.
+pub enum SchemaVersion {
+    /// Original schema without the `iceberg_type` column.
+    V0,
+    /// Extended schema with the `iceberg_type` column for view support.
+    V1,
+}
+
+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| column.name() == CATALOG_FIELD_RECORD_TYPE);
+
+        Ok(if has_type_column {
+            SchemaVersion::V1
+        } else {
+            SchemaVersion::V0
+        })
+    }
+
+    /// The trailing SQL `AND` clause used to exclude view rows when querying 
for tables.
+    ///
+    /// `V1` schemas carry an `iceberg_type` column, so table rows are those 
tagged `TABLE`
+    /// (or `NULL`, for rows written before the column existed). `V0` schemas 
have no such
+    /// column, so no filter is applied.
+    fn record_type_filter(self) -> &'static str {
+        match self {
+            SchemaVersion::V1 => "AND (iceberg_type = 'TABLE' OR iceberg_type 
IS NULL)",
+            SchemaVersion::V0 => "",
+        }

Review Comment:
   I identified this issue in the previous PR.
   
   I don't want to bloat this PR any further, but I think it would make sense 
to reject table creation explicitly. I will add that error handling and leave 
table creation/registration as a future follow-up if that's desired.



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