Skip to content

Apache DataFusion SQL Query Engine

ARROW-10001 [Rust] [DataFusion] Added developer guide to README.

Jorge C. Leitao329368424703
diff --git a/rust/datafusion/README.md b/rust/datafusion/README.md
index a6b305afb2..bf3c60f3f4 100644
--- a/rust/datafusion/README.md
+++ b/rust/datafusion/README.md
@@ -119,3 +119,105 @@ are mapped to Arrow types according to the following table
| `BYTEA` | *Not yet supported* |
| `CUSTOM` | *Not yet supported* |
| `ARRAY` | *Not yet supported* |
+
+# Developer's guide
+
+This section describes how you can get started at developing DataFusion.
+
+## Core concepts
+
+### Dynamic typing
+
+DataFusion's memory layout is the columnar format Arrow. Because a column type is only known
+at runtime, DataFusion, like Arrow's create, uses dynamic typing throughout most of its code. Thus, a central aspect of DataFusion's query engine is keeping track of an expression's `datatype`.
+
+### Nullability
+
+Arrow's columnar format natively supports the notion of null values, and DataFusion also. Like types,
+DataFusion keeps track of an expression's `nullability` throughout planning and execution.
+
+### Field and Schema
+
+Arrow's implementation in rust has a `Field` that contains information about a column:
+
+* name
+* datatype
+* nullability
+
+A `Schema` is essentially a vector of fields.
+
+### parse, plan, optimize, execute
+
+When a query is sent to DataFusion, there are different steps that it passes through until a result is
+obtained. Broadly, they are:
+
+1. The string is parsed to an Abstract syntax tree (AST). We use [sqlparser](https://docs.rs/sqlparser/0.6.1/sqlparser/) for this.
+2. The AST is converted to a logical plan ([src/sql](src/sql/planner.rs))
+3. The logical plan is optimized to a new logical plan ([src/optimizer](src/optimizer))
+4. The logical plan is converted to a physical plan ([src/physical_plan/planner](src/physical_plan/planner.rs))
+5. The physical plan is executed ([src/execution/context.rs](src/execution/context.rs))
+
+Phases 1-4 are typically cheap/fast when compared to phase 5, and thus DataFusion puts a lot of effort to ensure that phase 5 runs without errors.
+
+#### Logical plan
+
+A logical plan is a representation of the plan without details of how it is executed. In general,
+
+* given a data schema and a logical plan, the resulting schema is known.
+* given data and a logical plan, we agree on the result, irrespectively of how it is computed.
+
+A logical plan is composed by nodes (called `LogicalPlan`), and each node is composed by logical expressions (called `Expr`). All of these are located in [src/logical_plan/mod.rs](src/logical_plan/mod.rs).
+
+#### Physical plan
+
+A Physical plan is a plan that can be executed. Contrarily to a logical plan, the physical plan has specific
+information about how the calculation should be performed (e.g. what actual rust functions are used).
+
+A physical plan is composed by nodes (implement the trait `ExecutionPlan`), and each node is composed by physical expressions (implement the trait `PhysicalExpr`) or aggreagate expressions (implement the trait `AggregateExpr`). All of these are located in [src/physical_plan](src/physical_plan).
+
+Physical expressions are evaluated against `RecordBatch` (a group of `Array`s and a `Schema`).
+
+### Bootstrap environment
+
+DataFusion is written in Rust and it uses a standard rust toolkit:
+
+* `cargo build`
+* `cargo fmt` to format the code
+* `cargo test` to test
+* etc.
+
+## How to add a new scalar function
+
+Below is a checklist of what you need to do to add a new scalar function to DataFusion:
+
+* Add the actual implementation of the function:
+ * [here](src/physical_plan/string_expressions.rs) for string functions
+ * [here](src/physical_plan/math_expressions.rs) for math functions
+ * [here](src/physical_plan/datetime_expressions.rs) for datetime functions
+ * create a new module [here](src/physical_plan) for other functions
+* In [src/physical_plan/functions](src/physical_plan/functions.rs), add:
+ * a new variant to `BuiltinScalarFunction`
+ * a new entry to `FromStr` with the name of the function as called by SQL
+ * a new line in `return_type` with the expected return type of the function, given an incoming type
+ * a new line in `signature` with the signature of the function (number and types of its arguments)
+ * a new line in `create_physical_expr` mapping the built-in to the implementation
+ * tests to the function.
+* In [tests/sql.rs](tests/sql.rs), add a new test where the function is called through SQL against well known data and returns the expected result.
+
+## How to add a new aggregate function
+
+Below is a checklist of what you need to do to add a new aggregate function to DataFusion:
+
+* Add the actual implementation of an `Accumulator` and `AggregateExpr`:
+ * [here](src/physical_plan/string_expressions.rs) for string functions
+ * [here](src/physical_plan/math_expressions.rs) for math functions
+ * [here](src/physical_plan/datetime_expressions.rs) for datetime functions
+ * create a new module [here](src/physical_plan) for other functions
+* In [src/physical_plan/aggregates](src/physical_plan/aggregates.rs), add:
+ * a new variant to `BuiltinAggregateFunction`
+ * a new entry to `FromStr` with the name of the function as called by SQL
+ * a new line in `return_type` with the expected return type of the function, given an incoming type
+ * a new line in `signature` with the signature of the function (number and types of its arguments)
+ * a new line in `create_aggregate_expr` mapping the built-in to the implementation
+ * tests to the function.
+* In [tests/sql.rs](tests/sql.rs), add a new test where the function is called through SQL against well known data and returns the expected result.