leaves12138 commented on code in PR #1427:
URL: https://github.com/apache/paimon-rust/pull/1427#discussion_r4595265924
##########
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 native-view dependency bindings in exported DDL
The native-provider branch extracts the stored SELECT verbatim and only
qualifies the destination view name, so the exported definition can silently
change which table it reads when recreated under a different default schema. I
reproduced this with native MemTables datafusion.public.source containing id=1
and datafusion.other.source containing id=2: create
datafusion.public.bound_view AS SELECT id FROM source while the default schema
is public, then SET datafusion.catalog.default_schema = 'other'. The existing
view still returns 1, but SHOW CREATE VIEW exports CREATE VIEW
"datafusion"."public"."bound_view" ("id") AS SELECT id FROM source;
dropping/recreating that exact DDL now returns 2. Explicitly qualifying the
dependency as "datafusion"."public"."source" preserves the original result.
Please preserve/canonicalize creation-time dependency bindings for native
DataFusion views too, rather than emitting their unqualified stored query, and
add a cross-namespace native-view round-tri
p regression alongside the literal-only native-view test. The creation
namespace should be captured or derived from bound dependencies, not inferred
from the current session or simply from the destination view's schema.
--
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]