leaves12138 commented on code in PR #1427:
URL: https://github.com/apache/paimon-rust/pull/1427#discussion_r4596682368
##########
crates/integrations/datafusion/src/sql_function.rs:
##########
@@ -35,6 +36,258 @@ const MAX_FUNCTION_REFERENCES: usize = 1024;
const MAX_EXPANDED_CALLS: usize = 1024;
type FunctionReference = (String, Identifier);
+/// Bind persistent catalog dependencies in a view's stored SQL to
+/// the namespace used to create its logical plan. Keep CTEs, table functions,
+/// and native function calls in their original scopes.
+pub(crate) fn bind_view_query(
+ state: &SessionState,
+ sql: &str,
+ current_catalog: &str,
+ current_database: &str,
+) -> DFResult<String> {
+ let regex_sql = crate::regex_operators::parseable_sql(sql)?;
+ let parseable = crate::lateral_views::parseable_sql(regex_sql.as_ref())?;
+ let mut statements = Parser::parse_sql(&GenericDialect {},
parseable.as_ref())
+ .map_err(|error| DataFusionError::Plan(format!("Invalid view SQL:
{error}")))?;
+ if statements.len() != 1 || !matches!(statements.first(),
Some(Statement::Query(_))) {
+ return Err(DataFusionError::Plan(
+ "View SQL must contain one query".into(),
+ ));
+ }
+ struct Binder<'a> {
+ state: &'a SessionState,
+ catalog: &'a str,
+ database: &'a str,
+ scopes: crate::catalog::ViewQueryScopes,
+ table_functions: HashSet<usize>,
+ }
+ impl VisitorMut for Binder<'_> {
+ type Break = ();
+
+ fn pre_visit_query(&mut self, query: &mut Query) ->
ControlFlow<Self::Break> {
+ self.scopes.enter(
+ query,
+ self.state
+ .config_options()
+ .sql_parser
+ .enable_ident_normalization,
+ );
+ ControlFlow::Continue(())
+ }
+
+ fn post_visit_query(&mut self, _query: &mut Query) ->
ControlFlow<Self::Break> {
+ self.scopes.leave();
+ ControlFlow::Continue(())
+ }
+
+ fn pre_visit_table_factor(&mut self, factor: &mut TableFactor) ->
ControlFlow<Self::Break> {
+ if let TableFactor::TableFunction {
+ expr: SqlExpr::Function(call),
+ ..
+ } = factor
+ {
+ self.table_functions.insert(call as *const _ as usize);
+ return ControlFlow::Continue(());
+ }
+ let TableFactor::Table {
+ name, args: None, ..
+ } = factor
+ else {
+ return ControlFlow::Continue(());
+ };
+ let normalize = self
+ .state
+ .config_options()
+ .sql_parser
+ .enable_ident_normalization;
+ if self.scopes.is_cte(name, normalize) {
Review Comment:
[P2] Bind physical dependencies before recursive CTE names become visible
`bind_view_query` reuses `ViewQueryScopes`, which makes every alias in `WITH
RECURSIVE` visible throughout every CTE body. DataFusion instead resolves CTEs
sequentially and only exposes a recursive work table in the recursive branch,
not its static anchor. Consequently this early return skips qualification of a
real physical dependency, even when the view is created through
`SQLContext::sql` and its creation namespace is known. The new export validator
then rejects a valid wrapper-created view as having an unavailable creation
namespace.
I reproduced this with `datafusion.public.source` containing `id=1`,
`datafusion.other.source` containing `id=2`, and default schema `public`:
```sql
CREATE VIEW datafusion.other.anchor AS
WITH RECURSIVE
seed AS (
SELECT id FROM datafusion.public.source
UNION ALL SELECT id FROM datafusion.other.source
),
source AS (
SELECT id FROM source
UNION ALL SELECT id + 1 FROM source WHERE id < 2
)
SELECT id FROM source WHERE id = 1;
SET datafusion.catalog.default_schema = 'other';
SHOW CREATE VIEW datafusion.other.anchor;
```
Creation succeeds and selecting the existing view still returns `1`, but
`SHOW CREATE VIEW` fails with `Cannot export view with unqualified dependency
'source': its creation namespace is unavailable`. Explicitly qualifying only
the physical anchor as `datafusion.public.source` makes the export/recreate
round trip succeed. The same issue occurs when an earlier CTE reads a physical
table whose name is reused by a later CTE in a `WITH RECURSIVE` block. These
cases differ from direct `ctx.ctx().sql(...)` views, for which rejecting
unknown bindings is appropriate. Please apply the same sequential/anchor-aware
visibility used by `validate_bound_view_query` to binding too, and cover the
wrapper creation path in the regression tests.
--
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]