apache / apache/datafusion

Examples of using `TreeNode` APIs to walk and manipulate LogicalPlans

Open
#10,628 7 comments 2 reactions 0 assignees View on GitHub
documentation enhancement help wanted
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Is your feature request related to a problem or challenge?

The usecase of walking `LogicalPlan` to either analyze the plan or rewrite it has come up multiple times recently

Specifically I think both @goldmedal and @cisaacson have asked about this recently and so I think the documentation is not clear enough

### Describe the solution you'd like

In order to help people do this, I would like to add some examples of how to walk the tree

I suggest first adding simple doc example in the [docs of `LogicalPlan` itself ](https://github.com/apache/datafusion/blob/b14e92f30057868da8e7b428223e66c6300773a1/datafusion/expr/src/logical_plan/plan.rs#L68-L69)

The example would do somethig like create a plan using `LogicalPlanBuilder`
```
Project
Filter
Scan
```

And then show how to:
1. Use `TreeNode::apply` to recursively find something about the scan (maybe the table name?)
2. Use the `TreeNode::rewrite` to recursively rewrite and remove the Filter (or something)

I think those would be relatively short examples to write and would illustrate the key apis

### Describe alternatives you've considered

Bonus points for a more full featured example (perhaps linked from the `LogicalPlan` docs) of how use / walk / manipulate LogicalPlans

Similarly to https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/expr_api.rs, perahaps `datafusion-examples/examples/plan_walk.rs` or something

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the LogicalPlan documentation in datafusion/expr/src/logical_plan/plan.rs and review the related style in datafusion-examples/examples/expr_api.rs. Use the issue's Project, Filter, and Scan plan shape to demonstrate TreeNode::apply and TreeNode::rewrite, potentially in a linked plan_walk.rs example. Done means the documentation clearly shows walking a plan and rewriting it to remove a Filter.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.