leaves12138 commented on code in PR #1427:
URL: https://github.com/apache/paimon-rust/pull/1427#discussion_r4596227834


##########
crates/integrations/datafusion/src/sql_context.rs:
##########
@@ -1438,6 +1445,100 @@ impl SQLContext {
         )
     }
 
+    async fn handle_show_create_view(&self, name: &ObjectName) -> 
DFResult<DataFrame> {
+        if let Some((catalog, database, view, provider)) = 
self.temp_table_for(name).await? {
+            return self.show_create_provider_view(catalog, database, view, 
provider, true);
+        }
+        let table_ref =
+            object_name_to_table_reference(name.clone(), 
self.ctx.enable_ident_normalization())?;
+        if !self.is_paimon_catalog_ref(&table_ref) {
+            let provider = self.ctx.table_provider(table_ref.clone()).await?;
+            let config = self.ctx.copied_config();
+            let config = &config.options().catalog;
+            let resolved = table_ref.resolve(&config.default_catalog, 
&config.default_schema);
+            return self.show_create_provider_view(
+                resolved.catalog.to_string(),
+                resolved.schema.to_string(),
+                resolved.table.to_string(),
+                provider,
+                false,
+            );
+        }
+        let config = self.ctx.copied_config();
+        let defaults = &config.options().catalog;
+        let resolved = table_ref.resolve(&defaults.default_catalog, 
&defaults.default_schema);
+        let catalog_name = resolved.catalog.to_string();
+        let catalog = self
+            .catalogs
+            .get(&catalog_name)
+            .ok_or_else(|| plan_datafusion_err!("Unknown catalog 
'{catalog_name}'"))?;
+        let identifier = Identifier::new(resolved.schema.to_string(), 
resolved.table.to_string());
+        // Read the stored definition directly: SHOW CREATE must not execute or
+        // plan the view's query, including when a dependency no longer exists.
+        let view = catalog
+            .get_view(&identifier)
+            .await
+            .map_err(to_datafusion_error)?;
+        let columns = view
+            .schema()
+            .fields()
+            .iter()
+            .map(|field| field.name().to_string())
+            .collect::<Vec<_>>();
+        let definition = view_definition(
+            &catalog_name,
+            identifier.database(),
+            identifier.object(),
+            &columns,
+            &typed_view_query(&view)?,
+            false,
+        );
+        self.show_create_result(
+            catalog_name,
+            identifier.database().into(),
+            identifier.object().into(),
+            Some(&definition),
+            false,
+        )
+    }
+
+    fn show_create_provider_view(
+        &self,
+        catalog: String,
+        database: String,
+        view: String,
+        provider: Arc<dyn TableProvider>,
+        temporary: bool,
+    ) -> DFResult<DataFrame> {
+        if provider.table_type() != datafusion::datasource::TableType::View {
+            return Err(plan_datafusion_err!(
+                "'{}.{}.{}' is not a view",
+                catalog,
+                database,
+                view
+            ));
+        }
+        let columns = provider
+            .schema()
+            .fields()
+            .iter()
+            .map(|field| field.name().clone())
+            .collect::<Vec<_>>();
+        let definition = provider.get_table_definition().map(|stored| {
+            // DataFusion stores CREATE VIEW, whereas Paimon temporary views
+            // store only their query. Return a fully qualified DDL in both 
cases.
+            let query = match parse_sql_statements(stored) {
+                Ok(statements) => match statements.as_slice() {
+                    [Statement::CreateView(create)] => 
create.query.to_string(),

Review Comment:
   [P2] Preserve bindings for views created through the native SessionContext 
API
   
   Re-reviewed at `5d55e0554fa36e510d7bb8ced2b3f44ba6b3d80d`. The original 
`SQLContext::sql` reproduction is fixed, including when the view's destination 
schema differs from its creation namespace. However, the fix only runs in that 
CREATE VIEW interception path. `SHOW CREATE VIEW` still accepts session-native 
providers and exports their stored query verbatim here, so native views created 
through the public `SQLContext::ctx()` API remain affected.
   
   Reproduction with `datafusion.public.source` containing `id=1` and 
`datafusion.other.source` containing `id=2`:
   
   1. With the default schema set to `public`, create the view via the 
underlying native API: `ctx.ctx().sql("CREATE VIEW 
datafusion.public.direct_native AS SELECT id FROM 
source").await?.collect().await?`.
   2. Set the default schema to `other`. Querying the existing view through 
SQLContext still returns `1`.
   3. `SHOW CREATE VIEW datafusion.public.direct_native` returns `CREATE VIEW 
"datafusion"."public"."direct_native" ("id") AS SELECT id FROM source`.
   4. Drop the view and recreate that exact DDL through `ctx.sql(...)`. The 
recreated view returns `2`, not `1`.
   
   An independent probe reproduces this consistently on the reviewed head. 
Explicitly qualifying the exported source as `"datafusion"."public"."source"` 
preserves `1` in the control run.
   
   Please preserve dependency bindings for these native ViewTable providers as 
well (for example, using the already-bound logical plan), or explicitly reject 
exports whose bindings cannot be preserved instead of returning misleading 
recreatable DDL. Add a cross-schema regression that creates the source view 
through `ctx.ctx().sql(...)`, not only through the new SQLContext interception 
branch. This remains the same native-provider export issue at 
`sql_context.rs:1553`, rather than a new GROUP_CONCAT/syntax concern.
   



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

Reply via email to