stuxuhai opened a new pull request, #14909:
URL: https://github.com/apache/iceberg/pull/14909

   ### Purpose
   This PR fixes a bug where `CREATE VIEW IF NOT EXISTS` fails with a 
`NoSuchIcebergViewException: Not an iceberg view` (wrapped in 
`QueryExecutionException`) instead of succeeding silently when a non-Iceberg 
view (e.g., a Hive view) already exists in the `SparkSessionCatalog`.
   
   ### The Problem
   When `SparkSessionCatalog` is configured with 
`spark.sql.catalog.spark_catalog=org.apache.iceberg.spark.SparkSessionCatalog` 
and Iceberg view support is enabled:
   
   1. A user executes `CREATE VIEW IF NOT EXISTS db.view_name AS ...`.
   2. If `db.view_name` already exists as a **Hive View** (or any non-Iceberg 
table/view).
   3. `SparkSessionCatalog.createView` currently delegates directly to the 
underlying Iceberg catalog (`asViewCatalog.createView`).
   4. The Iceberg catalog (e.g., HiveCatalog) attempts to load the view. Since 
it is not an Iceberg view, it throws `NoSuchIcebergViewException`.
   5. Spark expects `ViewAlreadyExistsException` to handle the `IF NOT EXISTS` 
logic. Because it receives a different exception, the query fails entirely.
   
   ### The Fix
   Before delegating the creation to the Iceberg catalog, we explicitly check 
if the identifier already exists in the underlying session catalog (which is 
the source of truth for the global namespace).
   
   If `getSessionCatalog().tableExists(ident)` returns true, we immediately 
throw `ViewAlreadyExistsException`. This allows Spark's analysis rules to 
correctly catch the exception and ignore the operation as per `IF NOT EXISTS` 
semantics.
   
   ### Verification
   *   Added a new unit test in `TestSparkSessionCatalog` to verify that 
`CREATE VIEW IF NOT EXISTS` succeeds when a Hive view exists.
   *   Verified that `CREATE VIEW` (without if not exists) correctly throws 
`AnalysisException` (Table or view already exists).


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