SteNicholas commented on code in PR #1448:
URL: https://github.com/apache/paimon-rust/pull/1448#discussion_r4675654095
##########
crates/integrations/datafusion/src/relation_planner.rs:
##########
@@ -39,6 +42,570 @@ use crate::catalog::{
};
use crate::table::{ObjectTableProvider, PaimonTableProvider};
+/// Resolves statement-local TABLE bindings to snapshot scans.
+#[derive(Debug)]
+pub(crate) struct SnapshotRelationPlanner {
+ bindings: HashMap<String, (TableReference, Arc<dyn TableProvider>)>,
+ display_qualifiers: HashMap<String, String>,
+ cte_names: HashSet<String>,
+}
+
+impl SnapshotRelationPlanner {
+ pub(crate) fn try_new(
+ mut bindings: HashMap<String, (TableReference, Arc<dyn
TableProvider>)>,
+ statement: &mut ast::Statement,
+ defaults: &CatalogOptions,
+ normalize: bool,
+ ) -> DFResult<Self> {
+ let mut qualifiers = SnapshotQualifiers {
+ bindings: &bindings,
+ defaults,
+ normalize,
+ private_bindings: HashSet::new(),
+ cte_names: HashSet::new(),
+ physical_tables: physical_table_references(statement, normalize)?,
+ scopes: Vec::new(),
+ query_has_select: Vec::new(),
+ select_sources: Vec::new(),
+ ambiguous_qualifier: None,
+ };
+ let _ = statement.visit(&mut qualifiers);
+ if let Some(qualifier) = qualifiers.ambiguous_qualifier {
+ return Err(plan_datafusion_err!(
+ "Ambiguous snapshot qualifier '{qualifier}'. Use distinct
aliases for the snapshot scans"
+ ));
+ }
+ let cte_names = qualifiers.cte_names;
+ let mut display_qualifiers = HashMap::new();
+ for name in qualifiers.private_bindings {
+ let (table, _) = bindings.get_mut(&name).unwrap();
+ display_qualifiers.insert(name.clone(), table.to_string());
+ *table = TableReference::bare(name);
+ }
+ Ok(Self {
+ bindings,
+ display_qualifiers,
+ cte_names,
+ })
+ }
+
+ /// DataFusion includes qualifiers in default expression names. Restore
+ /// their SQL spelling before a caller persists the schema or prepares it.
+ pub(crate) fn restore_output_names(&self, plan: LogicalPlan) ->
DFResult<LogicalPlan> {
+ if self.display_qualifiers.is_empty() {
+ return Ok(plan);
+ }
+ if let LogicalPlan::Statement(Statement::Prepare(mut prepare)) = plan {
+ prepare.input =
+
Arc::new(self.restore_output_names(Arc::unwrap_or_clone(prepare.input))?);
+ return Ok(LogicalPlan::Statement(Statement::Prepare(prepare)));
+ }
+ let mut renamed = false;
+ let projection = plan
+ .schema()
+ .columns()
+ .into_iter()
+ .map(|column| {
+ let mut name = column.name.clone();
+ for (binding, original) in &self.display_qualifiers {
+ if name.contains(binding) {
+ name = name.replace(binding, original);
+ }
+ }
+ if name == column.name {
+ Expr::Column(column)
+ } else {
+ renamed = true;
+ let qualifier = column.relation.clone();
+ Expr::Column(column).alias_qualified(qualifier, name)
+ }
+ })
+ .collect::<Vec<_>>();
+ if renamed {
+ LogicalPlanBuilder::from(plan).project(projection)?.build()
+ } else {
+ Ok(plan)
+ }
+ }
+}
+
+enum ScopedQualifier<'a> {
+ Original,
+ Snapshots(Vec<&'a str>),
+}
+
+fn physical_table_references(
+ statement: &ast::Statement,
+ normalize: bool,
+) -> DFResult<HashSet<*const ast::ObjectName>> {
+ struct TableVisitor {
+ normalize: bool,
+ query_depth: usize,
+ names: HashSet<*const ast::ObjectName>,
+ }
+
+ impl ast::Visitor for TableVisitor {
+ type Break = DataFusionError;
+
+ fn pre_visit_query(&mut self, query: &ast::Query) ->
ControlFlow<Self::Break> {
+ self.query_depth += 1;
+ if self.query_depth != 1 {
+ return ControlFlow::Continue(());
+ }
+ match crate::sql_function::visit_view_table_dependencies(
+ query,
+ self.normalize,
+ &mut |name| {
+ self.names.insert(std::ptr::from_ref(name));
+ Ok(())
+ },
+ ) {
+ Ok(()) => ControlFlow::Continue(()),
+ Err(error) => ControlFlow::Break(error),
+ }
+ }
+
+ fn post_visit_query(&mut self, _: &ast::Query) ->
ControlFlow<Self::Break> {
+ self.query_depth -= 1;
+ ControlFlow::Continue(())
+ }
+ }
+
+ let mut visitor = TableVisitor {
+ normalize,
+ query_depth: 0,
+ names: HashSet::new(),
+ };
+ match ast::Visit::visit(statement, &mut visitor) {
+ ControlFlow::Continue(()) => Ok(visitor.names),
+ ControlFlow::Break(error) => Err(error),
+ }
+}
+
+/// DataFusion matches a bare qualifier against the last component of a full
+/// table name. Bind colliding snapshots privately and resolve SQL qualifiers
+/// within their original scope before DataFusion plans the join.
+struct SnapshotQualifiers<'a> {
+ bindings: &'a HashMap<String, (TableReference, Arc<dyn TableProvider>)>,
+ defaults: &'a CatalogOptions,
+ normalize: bool,
+ private_bindings: HashSet<String>,
+ cte_names: HashSet<String>,
+ // Node addresses distinguish tables from same-named CTEs. Moving a FROM
+ // vector for traversal leaves its elements at their original addresses.
+ physical_tables: HashSet<*const ast::ObjectName>,
+ scopes: Vec<HashMap<TableReference, ScopedQualifier<'a>>>,
+ query_has_select: Vec<bool>,
+ select_sources: Vec<Vec<ast::TableWithJoins>>,
+ ambiguous_qualifier: Option<TableReference>,
+}
+
+impl<'binding> SnapshotQualifiers<'binding> {
+ fn factors(group: &ast::TableWithJoins) -> impl DoubleEndedIterator<Item =
&TableFactor> {
+ std::iter::once(&group.relation).chain(group.joins.iter().map(|join|
&join.relation))
+ }
+
+ fn visible_factors<'a>(
+ relation: &'a TableFactor,
+ joins: &'a [ast::Join],
+ ) -> impl DoubleEndedIterator<Item = &'a TableFactor> {
+ use ast::JoinOperator::{LeftAnti, LeftSemi, RightAnti, RightSemi};
+
+ let (relation, joins) = match joins
+ .iter()
+ .rposition(|join| matches!(join.join_operator, RightSemi(_) |
RightAnti(_)))
+ {
+ Some(index) => (&joins[index].relation, &joins[index + 1..]),
+ None => (relation, joins),
+ };
+ std::iter::once(relation).chain(joins.iter().filter_map(|join| {
+ (!matches!(join.join_operator, LeftSemi(_) |
LeftAnti(_))).then_some(&join.relation)
+ }))
+ }
+
+ fn table_qualifiers(&self, table: &TableReference) -> [TableReference; 3] {
+ let resolved = table.clone().resolve(
+ &self.defaults.default_catalog,
+ &self.defaults.default_schema,
+ );
+ [
+ TableReference::bare(Arc::clone(&resolved.table)),
+ TableReference::partial(Arc::clone(&resolved.schema),
Arc::clone(&resolved.table)),
+ TableReference::full(resolved.catalog, resolved.schema,
resolved.table),
+ ]
+ }
+
+ fn relation_scope<'relation>(
+ &mut self,
+ factors: impl DoubleEndedIterator<Item = &'relation TableFactor>,
+ visible_only: bool,
+ ) -> HashMap<TableReference, ScopedQualifier<'binding>> {
+ let mut sources = Vec::new();
+ let mut snapshots = Vec::new();
+ let mut scope = HashMap::new();
+ let mut pending: Vec<_> = factors.rev().collect();
+ let normalizer =
datafusion::sql::planner::IdentNormalizer::new(self.normalize);
+ while let Some(factor) = pending.pop() {
+ let alias = match factor {
+ TableFactor::TableFunction {
+ expr: ast::Expr::Identifier(binding),
+ alias: None,
+ } => {
+ if let Some((binding, (table, _))) =
self.bindings.get_key_value(&binding.value)
+ {
+ snapshots.push((table.clone(),
Some(binding.as_str())));
+ }
+ continue;
+ }
+ TableFactor::Table {
+ name,
+ args: None,
+ alias: None,
+ ..
+ } => {
+ if let Ok(table) =
datafusion::sql::planner::object_name_to_table_reference(
+ name.clone(),
+ self.normalize,
+ ) {
+ if
self.physical_tables.contains(&std::ptr::from_ref(name)) {
+ scope.extend(
+ self.table_qualifiers(&table)
+ .into_iter()
+ .map(|qualifier| (qualifier,
ScopedQualifier::Original)),
+ );
+ }
+ sources.push((table, None));
+ }
+ continue;
+ }
+ TableFactor::NestedJoin {
+ table_with_joins,
+ alias: None,
+ } => {
+ if visible_only {
+ pending.extend(
+ Self::visible_factors(
+ &table_with_joins.relation,
+ &table_with_joins.joins,
+ )
+ .rev(),
+ );
+ } else {
+ pending.extend(Self::factors(table_with_joins).rev());
+ }
+ continue;
+ }
+ TableFactor::NestedJoin { alias, .. }
+ | TableFactor::Table { alias, .. }
+ | TableFactor::Derived { alias, .. }
+ | TableFactor::Function { alias, .. }
+ | TableFactor::UNNEST { alias, .. }
+ | TableFactor::TableFunction { alias, .. } => alias,
+ _ => continue,
+ };
+ if let Some(alias) = alias {
+ sources.push((
+
TableReference::bare(normalizer.normalize(alias.name.clone())),
+ None,
+ ));
+ }
+ }
+
+ // Live relations reserve their qualifiers before any implicit
snapshot names.
+ sources.extend(snapshots);
+ let mut reserved = HashMap::<&str, Vec<&TableReference>>::new();
+ for (table, binding) in &sources {
+ let names = reserved.entry(table.table()).or_default();
+ if let Some(binding) = *binding {
+ let qualifiers = self.table_qualifiers(table);
+ // Private snapshot bindings protect outer references during
decorrelation.
+ let overlaps_existing = qualifiers.iter().any(|qualifier| {
+ matches!(scope.get(qualifier),
Some(ScopedQualifier::Original))
+ || self
+ .scopes
+ .iter()
+ .any(|scope| scope.contains_key(qualifier))
+ });
+ if overlaps_existing || names.iter().any(|name|
name.resolved_eq(table)) {
+ self.private_bindings.insert(binding.to_owned());
+ }
+ for qualifier in qualifiers {
+ if let ScopedQualifier::Snapshots(bindings) = scope
+ .entry(qualifier)
+ .or_insert_with(||
ScopedQualifier::Snapshots(Vec::new()))
+ {
+ bindings.push(binding);
+ }
+ }
+ } else {
+ scope.insert(table.clone(), ScopedQualifier::Original);
+ }
+ names.push(table);
+ }
+ scope
+ }
+
+ fn visit_joins(&mut self, group: &mut ast::TableWithJoins, preceding:
&[&TableFactor]) {
+ self.visit_factor(&mut group.relation, preceding.iter().copied());
+ for index in 0..group.joins.len() {
+ let (previous, rest) = group.joins.split_at_mut(index);
+ let join = &mut rest[0];
+ let left = || Self::visible_factors(&group.relation, previous);
+ self.visit_factor(&mut join.relation,
preceding.iter().copied().chain(left()));
+ // The ON clause can see its join inputs and enclosing queries.
+ let join_scope =
+
self.relation_scope(left().chain(std::iter::once(&join.relation)), true);
+ self.scopes.push(join_scope);
+ let _ = join.join_operator.visit(self);
+ self.scopes.pop();
+ }
+ }
+
+ fn visit_factor<'relation>(
+ &mut self,
+ factor: &mut TableFactor,
+ preceding: impl DoubleEndedIterator<Item = &'relation TableFactor>,
+ ) {
+ match factor {
+ TableFactor::NestedJoin {
+ table_with_joins, ..
+ } => {
+ self.relation_scope(Self::factors(table_with_joins), false);
+ self.visit_joins(table_with_joins,
&preceding.collect::<Vec<_>>());
+ }
+ TableFactor::Derived { lateral: true, .. }
+ | TableFactor::Function { lateral: true, .. }
+ | TableFactor::UNNEST { .. } => {
+ let scope = self.relation_scope(preceding, true);
+ self.scopes.push(scope);
+ let _ = factor.visit(self);
+ self.scopes.pop();
+ }
+ _ => {
+ let _ = factor.visit(self);
+ }
+ }
+ }
+
+ fn rewrite_prefix(
+ &mut self,
+ idents: &[ast::Ident],
+ has_column: bool,
+ ) -> Option<(usize, ast::Ident)> {
+ let prefix_len = if has_column {
+ idents.len().saturating_sub(1)
+ } else {
+ idents.len()
+ };
+ let normalizer =
datafusion::sql::planner::IdentNormalizer::new(self.normalize);
+ let candidates: Vec<_> = (1..=prefix_len.min(3))
+ .rev()
+ .filter_map(|len| {
+ let name = ast::ObjectName::from(idents[..len].to_vec());
+ let table =
+
datafusion::sql::planner::object_name_to_table_reference(name, self.normalize)
+ .ok()?;
+ let column = has_column.then(||
normalizer.normalize(idents[len].clone()));
+ Some((len, table, column))
+ })
+ .collect();
+ for scope in self.scopes.iter().rev() {
+ let mut matched_snapshot = false;
+ for (len, table, column) in &candidates {
+ match scope.get(table) {
+ Some(ScopedQualifier::Original) => return None,
+ Some(ScopedQualifier::Snapshots(bindings)) => {
+ matched_snapshot = true;
+ if column.as_ref().is_some_and(|column| {
+ !bindings.iter().any(|binding| {
+ self.bindings[*binding]
+ .1
+ .schema()
+ .column_with_name(column)
+ .is_some()
+ })
+ }) {
+ continue;
+ }
+ let [binding] = bindings.as_slice() else {
+ self.ambiguous_qualifier
+ .get_or_insert_with(|| table.clone());
+ return None;
+ };
+ return self
+ .private_bindings
+ .contains(*binding)
+ .then(|| (*len, ast::Ident::new(*binding)));
+ }
+ None => {}
+ }
+ }
+ if matched_snapshot {
+ // Let DataFusion resolve an unqualified nested field in this
scope.
+ return None;
+ }
+ }
+ None
+ }
+
+ fn rewrite_wildcard(&mut self, name: &mut ast::ObjectName) {
+ let idents: Vec<_> = name
+ .0
+ .iter()
+ .map_while(|part| part.as_ident().cloned())
+ .collect();
+ if let Some((len, binding)) = self.rewrite_prefix(&idents, false) {
+ name.0
+ .splice(..len, [ast::ObjectNamePart::Identifier(binding)]);
+ }
+ }
+}
+
+impl VisitorMut for SnapshotQualifiers<'_> {
+ type Break = ();
+
+ fn pre_visit_query(&mut self, query: &mut ast::Query) -> ControlFlow<()> {
+ if let Some(with) = &query.with {
+ let normalizer =
datafusion::sql::planner::IdentNormalizer::new(self.normalize);
+ self.cte_names.extend(
+ with.cte_tables
+ .iter()
+ .map(|cte| normalizer.normalize(cte.alias.name.clone())),
+ );
+ }
+ self.query_has_select.push(query.body.as_select().is_some());
+ self.scopes.push(HashMap::new());
+ ControlFlow::Continue(())
+ }
+
+ fn post_visit_query(&mut self, _: &mut ast::Query) -> ControlFlow<()> {
+ self.scopes.pop();
+ self.query_has_select.pop();
+ ControlFlow::Continue(())
+ }
+
+ fn pre_visit_select(&mut self, select: &mut ast::Select) ->
ControlFlow<()> {
+ let mut sources = std::mem::take(&mut select.from);
+ // Reserve private bindings before visiting inputs in SQL visibility
order.
+ self.relation_scope(sources.iter().flat_map(Self::factors), false);
+ for index in 0..sources.len() {
+ let (previous, rest) = sources.split_at_mut(index);
+ let preceding = previous
+ .iter()
+ .flat_map(|group| Self::visible_factors(&group.relation,
&group.joins))
+ .collect::<Vec<_>>();
+ self.visit_joins(&mut rest[0], &preceding);
+ }
+ let scope = self.relation_scope(
+ sources
+ .iter()
+ .flat_map(|group| Self::visible_factors(&group.relation,
&group.joins)),
+ true,
+ );
+ self.scopes.push(scope);
+ self.select_sources.push(sources);
+ for item in &mut select.projection {
+ if let ast::SelectItem::QualifiedWildcard(
+ ast::SelectItemQualifiedWildcardKind::ObjectName(name),
+ _,
+ ) = item
+ {
+ self.rewrite_wildcard(name);
+ }
+ }
+ ControlFlow::Continue(())
+ }
+
+ fn post_visit_select(&mut self, select: &mut ast::Select) ->
ControlFlow<()> {
+ select.from = self.select_sources.pop().unwrap();
+ let scope = self.scopes.pop().unwrap();
+ if self.query_has_select.last() == Some(&true) {
+ // ORDER BY belongs to Query and is visited after its SELECT.
+ *self.scopes.last_mut().unwrap() = scope;
+ }
+ ControlFlow::Continue(())
+ }
+
+ fn pre_visit_expr(&mut self, expr: &mut ast::Expr) -> ControlFlow<()> {
+ match expr {
+ ast::Expr::CompoundIdentifier(idents) => {
+ if let Some((len, binding)) = self.rewrite_prefix(idents,
true) {
+ idents.splice(..len, [binding]);
+ }
+ }
+ ast::Expr::QualifiedWildcard(name, _) =>
self.rewrite_wildcard(name),
+ ast::Expr::Function(function) => {
+ if let ast::FunctionArguments::List(args) = &mut function.args
{
+ for arg in &mut args.args {
+ if let
ast::FunctionArg::Unnamed(ast::FunctionArgExpr::QualifiedWildcard(
+ name,
+ )) = arg
+ {
+ self.rewrite_wildcard(name);
+ }
+ }
+ }
+ }
+ _ => {}
+ }
+ ControlFlow::Continue(())
+ }
+}
+
+impl RelationPlanner for SnapshotRelationPlanner {
+ fn plan_relation(
+ &self,
+ mut relation: TableFactor,
+ context: &mut dyn RelationPlannerContext,
+ ) -> DFResult<RelationPlanning> {
+ if let TableFactor::TableFunction {
+ expr: ast::Expr::Identifier(binding),
+ alias,
+ } = &relation
+ {
+ if let Some((table, provider)) = self.bindings.get(&binding.value)
{
+ return plan_scan(alias.clone(), table.clone(),
provider.clone());
Review Comment:
Thanks for the reproducer. Unaliased snapshot scans now use distinct
statement-local qualifiers, and the SQL rewrite maps exact visible snapshot
references to those qualifiers. This lets DataFusion resolve `x.t.id` as the
`id` field of the derived table's `t` struct. [Commit
`39e4a496`](https://github.com/apache/paimon-rust/pull/1448/commits/39e4a496abd6ee81ad166244dcb29a6135bd9be9)
The CTAS regression reads both destination tables back through a fresh SQL
context. Both `t VERSION AS OF 1` and `t VERSION AS OF 1 AS s` persist `[999,
999, 999]`. All 137 tests passed across the five related DataFusion suites.
[CTAS
regression](https://github.com/SteNicholas/paimon-rust/blob/39e4a496abd6ee81ad166244dcb29a6135bd9be9/crates/integrations/datafusion/tests/create_table_as.rs#L573)
--
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]