You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Examples of using TreeNode APIs to walk and manipulate LogicalPlans #10628
Moving out of slack into Github so it might be more easily found
If your usecase is to to get the list of filters and tables that appear in a query, one way to do this is:
Get the list of tables by finding all LogicalPlan::TableScan (any Join will have an input from a TableScan)
Get the list of filters/predicates, look in LogicalPlan::Filter and potentially the filters on the LogicalPlan::TableScan
So I think it could look something like (untested)
letmut referenced_tables = HashSet::new();letmut filters = vec![];// recursively visit all nodes in the tree (including subqueries)
logical_plan.apply_with_subqueries(|plan| {match plan {// record table namesLogicalPlan::TableScan(table_scan) => {
referenced_tables.insert(table_scan.table_name);},// record filtersLogicalPlan::Filter(filter) => {
filters.push(filter.predicate);}// ignore other nodes
_ => {}Ok(TreeNodeRecursion::Continue)}})?;
This looks very good, pretty much what I implemented. The only question I have remaining is:
If TableScan has filters why would that not catch all filters? What does LogicalPlan::Filter catch that the TableScan.filters would not? For my specific use case I only need filters on the tables that are present in the query.
Activity
cisaacson commentedon May 23, 2024
@alamb This is a great idea. As I learn more perhaps I can help.
alamb commentedon May 23, 2024
Thank you @cisaacson
alamb commentedon May 24, 2024
Moving out of slack into Github so it might be more easily found
If your usecase is to to get the list of filters and tables that appear in a query, one way to do this is:
LogicalPlan::TableScan(any Join will have an input from a TableScan)LogicalPlan::Filterand potentially the filters on theLogicalPlan::TableScanSo I think it could look something like (untested)
cisaacson commentedon May 24, 2024
This looks very good, pretty much what I implemented. The only question I have remaining is:
If
TableScanhasfilterswhy would that not catch all filters? What doesLogicalPlan::Filtercatch that theTableScan.filterswould not? For my specific use case I only need filters on the tables that are present in the query.alamb commentedon May 24, 2024
Depending on the value of https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.TableProviderFilterPushDown.html sometimes filters would not be pushed down into the provider
Also filters can be duplicated in the table scan and filter (again depending on the value of the FilterPushDown
cisaacson commentedon May 24, 2024
Got it, that makes total sense. I will only care about the accepted pushdown filters, so the
TableScanwill work for what we need.mac119 commentedon Apr 27, 2026
take
I'd like to work on this. My plan:
mattsu2020 commentedon Oct 1, 2026
take
docs: add examples for walking and rewriting logical plans (apache#25954