-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add some initial content about creating logical plans #7952
Changes from 9 commits
928c415
1b220a1
653bace
4db03cc
feedaf7
3de5a04
fb35b4a
8d77197
d130281
ac0859a
ee5d4cb
ec7ecdf
313469b
3190a3f
388f091
3db45de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
[package] | ||
name = "datafusion-docs" | ||
description = "DataFusion Documentation" | ||
publish = false | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
readme = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = "1.70" | ||
|
||
[dependencies] | ||
datafusion = { path = "../datafusion/core", version = "32.0.0", default-features = false } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,135 @@ | |
|
||
# Building Logical Plans | ||
|
||
Coming Soon | ||
A logical plan is a structured representation of a database query that describes the high-level operations and | ||
transformations needed to retrieve data from a database or data source. It abstracts away specific implementation | ||
details and focuses on the logical flow of the query, including operations like filtering, sorting, and joining tables. | ||
|
||
This logical plan serves as an intermediate step before generating an optimized physical execution plan. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to point them at the architecture guide here for more detail: https://docs.rs/datafusion/latest/datafusion/index.html#query-planning-and-execution-overview |
||
|
||
## Building Logical Plans Manually | ||
|
||
DataFusion's [LogicalPlan] is an enum containing variants representing all the supported operators, and also | ||
contains an `Extension` variant that allows projects building on DataFusion to add custom logical operators. | ||
|
||
It is possible to create logical plans by directly creating instances of the [LogicalPlan] enum as follows, but is is | ||
much easier to use the [LogicalPlanBuilder], which is described in the next section. | ||
|
||
Here is an example of building a logical plan directly: | ||
|
||
<!-- source for this example is in datafusion_docs::library_logical_plan::plan_1 --> | ||
|
||
```rust | ||
// create a logical table source | ||
let schema = Schema::new(vec![ | ||
Field::new("id", DataType::Int32, true), | ||
Field::new("name", DataType::Utf8, true), | ||
]); | ||
let table_source = LogicalTableSource::new(SchemaRef::new(schema)); | ||
|
||
// create a TableScan plan | ||
let projection = None; // optional projection | ||
let filters = vec![]; // optional filters to push down | ||
let fetch = None; // optional LIMIT | ||
let table_scan = LogicalPlan::TableScan(TableScan::try_new( | ||
"person", | ||
Arc::new(table_source), | ||
projection, | ||
filters, | ||
fetch, | ||
)?); | ||
|
||
// create a Filter plan that wraps the TableScan | ||
andygrove marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let filter_expr = col("id").gt(lit(500)); | ||
let plan = LogicalPlan::Filter(Filter::try_new(filter_expr, Arc::new(table_scan))?); | ||
|
||
// print the plan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are going to automatically run the tests, you might consider using something like the following:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll address this one in a future PR once I have the initial automation in place - I need to implement some code so that I have more control over which code from the test makes it into the docs |
||
println!("{}", plan.display_indent_schema()); | ||
``` | ||
|
||
This example produces the following plan: | ||
|
||
``` | ||
Filter: person.id > Int32(500) [id:Int32;N, name:Utf8;N] | ||
TableScan: person [id:Int32;N, name:Utf8;N] | ||
``` | ||
|
||
## Building Logical Plans with LogicalPlanBuilder | ||
|
||
DataFusion logical plans are typically created using the [LogicalPlanBuilder] struct. The following associated functions can be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a good place to also mention that |
||
used to create a new builder: | ||
|
||
- `empty` - create an empty plan with no fields | ||
- `values` - create a plan from a set of literal values | ||
- `scan` - create a plan representing a table scan | ||
- `scan_with_filters` - create a plan representing a table scan with filters | ||
|
||
Once the builder is created, transformation methods can be called to declare that further operations should be | ||
performed on the plan. Note that all we are doing at this stage is building up the logical plan structure. No query | ||
execution will be performed. | ||
|
||
Here are some examples of transformation methods, but for a full list, refer to the [LogicalPlanBuilder] API documentation. | ||
|
||
- `filter` | ||
- `limit` | ||
- `sort` | ||
- `distinct` | ||
- `join` | ||
|
||
The following example demonstrates building a simple query consisting of a table scan followed by a filter. | ||
andygrove marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<!-- source for this example is in datafusion_docs::library_logical_plan::plan_builder_1 --> | ||
|
||
```rust | ||
// create a logical table source | ||
let schema = Schema::new(vec![ | ||
Field::new("id", DataType::Int32, true), | ||
Field::new("name", DataType::Utf8, true), | ||
]); | ||
let table_source = LogicalTableSource::new(SchemaRef::new(schema)); | ||
|
||
// optional projection | ||
let projection = None; | ||
|
||
// create a LogicalPlanBuilder for a table scan | ||
let builder = LogicalPlanBuilder::scan("person", Arc::new(table_source), projection)?; | ||
|
||
// perform a filter operation and build the plan | ||
let plan = builder | ||
.filter(col("id").gt(lit(500)))? // WHERE id > 500 | ||
.build()?; | ||
|
||
// print the plan | ||
println!("{}", plan.display_indent_schema()); | ||
``` | ||
|
||
This example produces the following plan: | ||
|
||
``` | ||
Filter: person.id > Int32(500) [id:Int32;N, name:Utf8;N] | ||
TableScan: person [id:Int32;N, name:Utf8;N] | ||
``` | ||
|
||
## Table Sources | ||
|
||
The previous example used a [LogicalTableSource], which is used for tests and documentation in DataFusion, and is also | ||
suitable if you are using DataFusion to build logical plans but do not use DataFusion's physical plan. However, if you | ||
want to use a [TableSource] that can be executed in DataFusion then you will need to use [DefaultTableSource], which is a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rationale for being able to use planner without physical plan feels like it might be more discoverable as a doc comment in https://docs.rs/datafusion/latest/datafusion/datasource/default_table_source/struct.DefaultTableSource.html (it Perhaps this text could just explain LogicalTableSource and link to those docs |
||
wrapper for a [TableProvider]. | ||
|
||
Both [LogicalTableSource] and [DefaultTableSource] implement the [TableSource] trait. [DefaultTableSource] acts as a | ||
bridge between DataFusion's logical and physical plans and is necessary because the logical plan is contained in | ||
the `datafusion_expr` crate, which does not know about DataFusion's physical plans. | ||
|
||
```rust | ||
pub struct DefaultTableSource { | ||
pub table_provider: Arc<dyn TableProvider>, | ||
} | ||
``` | ||
|
||
[logicalplan]: https://docs.rs/datafusion-expr/latest/datafusion_expr/logical_plan/enum.LogicalPlan.html | ||
[logicalplanbuilder]: https://docs.rs/datafusion-expr/latest/datafusion_expr/logical_plan/builder/struct.LogicalPlanBuilder.html | ||
[logicaltablesource]: https://docs.rs/datafusion-expr/latest/datafusion_expr/logical_plan/builder/struct.LogicalTableSource.html | ||
[defaulttablesource]: https://docs.rs/datafusion/latest/datafusion/datasource/default_table_source/struct.DefaultTableSource.html | ||
[tableprovider]: https://docs.rs/datafusion/latest/datafusion/datasource/provider/trait.TableProvider.html | ||
[tablesource]: https://docs.rs/datafusion-expr/latest/datafusion_expr/trait.TableSource.html |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#[cfg(test)] | ||
mod library_logical_plan; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef}; | ||
use datafusion::error::Result; | ||
use datafusion::logical_expr::builder::LogicalTableSource; | ||
use datafusion::logical_expr::{Filter, LogicalPlan, LogicalPlanBuilder, TableScan}; | ||
use datafusion::prelude::*; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the duplication here is fine for the time being as you have plans to automate it I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I have a follow-on PR almost ready to remove the duplication There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the PR: #7956 |
||
fn plan_1() -> Result<()> { | ||
// create a logical table source | ||
let schema = Schema::new(vec![ | ||
Field::new("id", DataType::Int32, true), | ||
Field::new("name", DataType::Utf8, true), | ||
]); | ||
let table_source = LogicalTableSource::new(SchemaRef::new(schema)); | ||
|
||
// create a TableScan plan | ||
let projection = None; // optional projection | ||
let filters = vec![]; // optional filters to push down | ||
let fetch = None; // optional LIMIT | ||
let table_scan = LogicalPlan::TableScan(TableScan::try_new( | ||
"person", | ||
Arc::new(table_source), | ||
projection, | ||
filters, | ||
fetch, | ||
)?); | ||
|
||
// create a Filter plan that wraps the TableScan | ||
let filter_expr = col("id").gt(lit(500)); | ||
let plan = LogicalPlan::Filter(Filter::try_new(filter_expr, Arc::new(table_scan))?); | ||
|
||
// print the plan | ||
println!("{}", plan.display_indent_schema()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn plan_builder_1() -> Result<()> { | ||
// create a logical table source | ||
let schema = Schema::new(vec![ | ||
Field::new("id", DataType::Int32, true), | ||
Field::new("name", DataType::Utf8, true), | ||
]); | ||
let table_source = LogicalTableSource::new(SchemaRef::new(schema)); | ||
|
||
// optional projection | ||
let projection = None; | ||
|
||
// create a LogicalPlanBuilder for a table scan | ||
let builder = LogicalPlanBuilder::scan("person", Arc::new(table_source), projection)?; | ||
|
||
// perform a filter operation and build the plan | ||
let plan = builder.filter(col("id").gt(lit(500)))?.build()?; | ||
|
||
// print the plan | ||
println!("{}", plan.display_indent_schema()); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are tests, I recommend making that clear. Perhaps via:
Maybe we could even rename the crate
datafusion-docs-tests
to make it clear 🤔