Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 0cb70c8

Browse files
committed
docs: add properties and df repr
Signed-off-by: Alex Chi <[email protected]>
1 parent 1659526 commit 0cb70c8

13 files changed

+309
-24
lines changed

docs/src/SUMMARY.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
- [Plan Representation](./plan_repr.md)
77
- [Rule Engine](./rule_engine.md)
88
- [Cost Model](./cost_model.md)
9-
10-
---
11-
12-
- [(WIP) Properties](./properties.md)
9+
- [Properties](./properties.md)
1310

1411
# Integration
1512

16-
- [(WIP) Apache Arrow Datafusion](./datafusion.md)
13+
- [Apache Arrow Datafusion](./datafusion.md)
1714

1815
# Adaptive Optimization
1916

docs/src/datafusion.md

+99
Original file line numberDiff line numberDiff line change
@@ -1 +1,100 @@
11
# Integration with Datafusion
2+
3+
optd is currently used as a physical optimizer for Apache Arrow Datafusion. To interact with Datafusion, you may use the following command to start the Datafusion cli.
4+
5+
```bash
6+
cargo run --bin datafusion-optd-cli
7+
cargo run --bin datafusion-optd-cli -- -f tpch/test.sql # run TPC-H queries
8+
```
9+
10+
optd is designed as a flexible optimizer framework that can be used in any database systems. The core of optd is in `optd-core`, which contains the Cascades optimizer implementation and the definition of key structures in the optimization process. Users can implement the interfaces and use optd in their own database systems by using the `optd-core` crate.
11+
12+
The optd Datafusion representation contains Datafusion plan nodes, SQL expressions, optimizer rules, properties, and cost models, as in the `optd-datafusion-repr` crate.
13+
14+
The `optd-datafusion-bridge` crate contains necessary code to convert Datafusion logical plans into optd Datafusion representation and convert optd Datafusion representation back into Datafusion physical plans. It implements the `QueryPlanner` trait so that it can be easily integrated into Datafusion.
15+
16+
![integration with Datafusion](./optd-cascades/optd-datafusion-overview.svg)
17+
18+
## Plan Nodes
19+
20+
This is an incomplete list of all Datafusion plan nodes and their representations that we have implemented in the system.
21+
22+
```
23+
Join(type) left:PlanNode right:PlanNode cond:Expr
24+
Projection expr_list:ExprList
25+
Agg child:PlanNode expr_list:ExprList groups:ExprList
26+
Scan table:String
27+
ExprList ...children:Expr
28+
Sort child:PlanNode sort_exprs:ExprList <- requiring SortExprs
29+
... and others
30+
```
31+
32+
Note that only `ExprList` or `List` can have variable number of children. All plan nodes only have a fixed number of children. For projections and aggregations where users will need to provide a list of expressions, they will have `List` node as their direct child.
33+
34+
Developers can use the `define_plan_node` macro to add new plan nodes into the optd-datafusion-repr.
35+
36+
```rust
37+
#[derive(Clone, Debug)]
38+
pub struct LogicalJoin(pub PlanNode);
39+
40+
define_plan_node!(
41+
LogicalJoin : PlanNode,
42+
Join, [
43+
{ 0, left: PlanNode },
44+
{ 1, right: PlanNode }
45+
], [
46+
{ 2, cond: Expr }
47+
], { join_type: JoinType }
48+
);
49+
```
50+
51+
Developers will also need to add the plan node type into the `OptRelNodeTyp` enum, implement `is_plan_node` and `is_expression` for them, and implement the explain format in `explain`.
52+
53+
## Expressions
54+
55+
SQL Expressions are also a kind of `RelNode`. We have binary expressions, function calls, etc. in the representation.
56+
57+
Notably, we convert all column references into column indexes in the Datafusion bridge. For example, if Datafusion yields a logical plan of:
58+
59+
```
60+
LogicalJoin { a = b }
61+
Scan t1 [a, v1, v2]
62+
Scan t2 [b, v3, v4]
63+
```
64+
65+
It will be converted to:
66+
67+
```
68+
LogicalJoin { #0 = #3 }
69+
Scan t1
70+
Scan t2
71+
```
72+
73+
in the optd representation.
74+
75+
## Explain
76+
77+
We use risinglightdb's pretty-xmlish crate and implement a custom explain format for Datafusion plan nodes.
78+
79+
```rust
80+
PhysicalProjection { exprs: [ #0 ] }
81+
└── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ] }
82+
├── PhysicalProjection { exprs: [ #0 ] }
83+
│ └── PhysicalScan { table: t1 }
84+
└── PhysicalProjection { exprs: [ #0 ] }
85+
└── PhysicalScan { table: t2 }
86+
```
87+
88+
This is different from the default Lisp-representation of the `RelNode`.
89+
90+
## Rules
91+
92+
Currently, we have a few rules that pulls filters and projections up and down through joins. Also, we have join assoc and join commute rules to reorder the joins.
93+
94+
## Properties
95+
96+
We have the `Schema` property that will be used in the optimizer rules to determine number of columns of each plan nodes so that we can rewrite column reference expressions correctly.
97+
98+
## Cost Model
99+
100+
We have a simple cost model that computes I/O cost and compute cost based on number of rows of the children plan nodes.

docs/src/optd-cascades/optd-cascades-1.svg

+2-2
Loading

docs/src/optd-cascades/optd-cascades-2.svg

+2-2
Loading

docs/src/optd-cascades/optd-cascades-3.svg

+2-2
Loading

docs/src/optd-cascades/optd-cascades-4.svg

+2-2
Loading
Loading

docs/src/optd-cascades/optd-plan-repr-1.svg

+2-2
Loading

docs/src/optd-cascades/optd-plan-repr-2.svg

+2-2
Loading

docs/src/optd-cascades/optd-rule-1.svg

+2-2
Loading

0 commit comments

Comments
 (0)