Copilot commented on code in PR #27782:
URL: https://github.com/apache/datafusion/pull/27782#discussion_r4713308184
##########
datafusion/optimizer/src/utils.rs:
##########
@@ -144,20 +144,65 @@ fn visit_referenced_indices(
Ok(())
}
-/// Returns true if `expr` contains all columns in `schema_cols`
+/// Returns true if every `Expr::Column` in `expr` and every column referenced
+/// via `Subquery::outer_ref_columns` of an embedded subquery (`Exists`,
+/// `InSubquery`, `SetComparison`, `ScalarSubquery`) is present in
`schema_cols`.
+///
+/// Direct top-level `Expr::OuterReferenceColumn` nodes are intentionally
+/// ignored because they are bound by an enclosing query outside the current
+/// node's scope (for example, a join inside a correlated subquery), whereas
+/// `outer_ref_columns` on an embedded subquery correlate to the current scope
+/// (any grandparent reference there is conservatively rejected). Relies on
+/// `Subquery::outer_ref_columns` as populated on the subquery node, matching
+/// [`for_each_referenced_index`].
pub(crate) fn has_all_column_refs(
expr: &Expr,
schema_cols: &HashSet<ColumnReference>,
) -> bool {
- let column_refs = expr.column_refs();
- // note can't use HashSet::intersect because of different types (owned vs
References)
- column_refs
- .iter()
- .filter(|c| {
- schema_cols.contains(&ColumnReference::new(c.relation.as_ref(),
c.name()))
- })
- .count()
- == column_refs.len()
+ has_all_column_refs_inner(expr, schema_cols, false)
+}
+
+fn has_all_column_refs_inner(
+ expr: &Expr,
+ schema_cols: &HashSet<ColumnReference>,
+ check_outer_ref_columns: bool,
+) -> bool {
+ let mut all_present = true;
+ expr.apply(|e| {
Review Comment:
Please include baseline-versus-changed `sql_planner` benchmark results in
the PR description. This change adds subquery-reference traversal to
`has_all_column_refs`, which is called when the optimizer classifies join
predicates.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]