Skip to content

Commit 9f4f0c5

Browse files
committed
refactor: move collect_subquery_cols() to common utils crate
1 parent 83d2c02 commit 9f4f0c5

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

datafusion/expr/src/logical_plan/invariants.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::collections::BTreeSet;
19-
2018
use recursive::recursive;
2119

2220
use datafusion_common::{
2321
plan_err,
2422
tree_node::{TreeNode, TreeNodeRecursion},
25-
Column, DFSchema, DFSchemaRef, DataFusionError, Result,
23+
DFSchemaRef, DataFusionError, Result,
2624
};
2725

2826
use crate::{
2927
expr::{Exists, InSubquery},
3028
expr_rewriter::strip_outer_reference,
31-
utils::split_conjunction,
29+
utils::{collect_subquery_cols, split_conjunction},
3230
Aggregate, Expr, Filter, Join, JoinType, LogicalPlan, Window,
3331
};
3432

@@ -383,23 +381,6 @@ fn check_mixed_out_refer_in_window(window: &Window) -> Result<()> {
383381
}
384382
}
385383

386-
fn collect_subquery_cols(
387-
exprs: &[Expr],
388-
subquery_schema: &DFSchema,
389-
) -> Result<BTreeSet<Column>> {
390-
exprs.iter().try_fold(BTreeSet::new(), |mut cols, expr| {
391-
let mut using_cols: Vec<Column> = vec![];
392-
for col in expr.column_refs().into_iter() {
393-
if subquery_schema.has_column(col) {
394-
using_cols.push(col.clone());
395-
}
396-
}
397-
398-
cols.extend(using_cols);
399-
Result::<_>::Ok(cols)
400-
})
401-
}
402-
403384
#[cfg(test)]
404385
mod test {
405386
use std::cmp::Ordering;

datafusion/expr/src/utils.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! Expression utilities
1919
2020
use std::cmp::Ordering;
21-
use std::collections::HashSet;
21+
use std::collections::{BTreeSet, HashSet};
2222
use std::ops::Deref;
2323
use std::sync::Arc;
2424

@@ -1402,6 +1402,24 @@ pub fn format_state_name(name: &str, state_name: &str) -> String {
14021402
format!("{name}[{state_name}]")
14031403
}
14041404

1405+
/// Determine the set of [`Column`]s produced by the subquery.
1406+
pub fn collect_subquery_cols(
1407+
exprs: &[Expr],
1408+
subquery_schema: &DFSchema,
1409+
) -> Result<BTreeSet<Column>> {
1410+
exprs.iter().try_fold(BTreeSet::new(), |mut cols, expr| {
1411+
let mut using_cols: Vec<Column> = vec![];
1412+
for col in expr.column_refs().into_iter() {
1413+
if subquery_schema.has_column(col) {
1414+
using_cols.push(col.clone());
1415+
}
1416+
}
1417+
1418+
cols.extend(using_cols);
1419+
Result::<_>::Ok(cols)
1420+
})
1421+
}
1422+
14051423
#[cfg(test)]
14061424
mod tests {
14071425
use super::*;

datafusion/optimizer/src/decorrelate.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ use std::ops::Deref;
2222
use std::sync::Arc;
2323

2424
use crate::simplify_expressions::ExprSimplifier;
25-
use crate::utils::collect_subquery_cols;
2625

2726
use datafusion_common::tree_node::{
2827
Transformed, TransformedResult, TreeNode, TreeNodeRecursion, TreeNodeRewriter,
2928
};
3029
use datafusion_common::{plan_err, Column, DFSchemaRef, HashMap, Result, ScalarValue};
3130
use datafusion_expr::expr::Alias;
3231
use datafusion_expr::simplify::SimplifyContext;
33-
use datafusion_expr::utils::{conjunction, find_join_exprs, split_conjunction};
32+
use datafusion_expr::utils::{
33+
collect_subquery_cols, conjunction, find_join_exprs, split_conjunction,
34+
};
3435
use datafusion_expr::{
3536
expr, lit, BinaryExpr, Cast, EmptyRelation, Expr, FetchType, LogicalPlan,
3637
LogicalPlanBuilder, Operator,

datafusion/optimizer/src/utils.rs

-17
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,6 @@ pub(crate) fn has_all_column_refs(expr: &Expr, schema_cols: &HashSet<Column>) ->
8787
== column_refs.len()
8888
}
8989

90-
pub(crate) fn collect_subquery_cols(
91-
exprs: &[Expr],
92-
subquery_schema: &DFSchema,
93-
) -> Result<BTreeSet<Column>> {
94-
exprs.iter().try_fold(BTreeSet::new(), |mut cols, expr| {
95-
let mut using_cols: Vec<Column> = vec![];
96-
for col in expr.column_refs().into_iter() {
97-
if subquery_schema.has_column(col) {
98-
using_cols.push(col.clone());
99-
}
100-
}
101-
102-
cols.extend(using_cols);
103-
Result::<_>::Ok(cols)
104-
})
105-
}
106-
10790
pub(crate) fn replace_qualified_name(
10891
expr: Expr,
10992
cols: &BTreeSet<Column>,

0 commit comments

Comments
 (0)