hcrosse commented on code in PR #27782:
URL: https://github.com/apache/datafusion/pull/27782#discussion_r4719089881
##########
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:
Added `sql_planner` Criterion results (`main` vs this branch) to the PR
description. Avoiding the `HashSet` allocation in `has_all_column_refs` shows
no regressions and speeds up join chain and self join planning
(`optimizer_join_chain_8` -7.1%, `physical_many_self_joins` -13.3%).
--
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]