leaves12138 commented on code in PR #1427:
URL: https://github.com/apache/paimon-rust/pull/1427#discussion_r4597016360
##########
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:
Rechecked at `5769240e7b43fa4f5d6be270d50e4f0a76aa50c6` against actual merge
base `3e8a5dfd58a402fced6e50395449eb38edcb486b`. The new VECTOR view round-trip
regression passes, but this existing P2 is still reproducible; the update does
not change the recursive-CTE visibility used by `bind_view_query`.
I reran both independent wrapper-created cases from this thread on the new
head:
- A recursive CTE whose static anchor reads a physical table with the same
name as the CTE.
- An earlier CTE reading a physical table whose name is reused by a later
CTE in `WITH RECURSIVE`.
Both views are created successfully via `SQLContext::sql` and still read the
original value `1` after changing the default schema. However, `SHOW CREATE
VIEW` fails with `Cannot export view with unqualified dependency 'source': its
creation namespace is unavailable`. The otherwise identical controls with fully
qualified physical references export and recreate successfully.
The new direct-SessionContext rejection tests are correct, but do not cover
this wrapper creation path, where the namespace is available and the binder
should qualify physical references before the corresponding CTE alias becomes
visible. At the new head the problematic early return is at
`sql_function.rs:101`. Please make the binder sequential/anchor-aware as well,
preserving the recursive work-table reference only in its recursive term.
Validation: 424 DataFusion library tests and all 11 SQL compatibility
integration tests passed, including the new VECTOR round trip. The five
independent probes have three passing controls and the same two failing wrapper
cases. This is a follow-up on the existing finding, not a second issue; I am
not approving this revision. Scope remains the DataFusion adapter, with no
shared Paimon commit/snapshot, metadata-contract, or storage-format changes.
--
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]