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

Commit 175ebaf

Browse files
authored
Updated README for sqlplannertest (#239)
Expanded README for sqlplannertest with more instructions on how to write, run, and update tests. Added a simple test for expressions.
1 parent 91cd0a2 commit 175ebaf

File tree

3 files changed

+101
-16
lines changed

3 files changed

+101
-16
lines changed

optd-sqlplannertest/README.md

+50-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
1-
# Usage
1+
# SQL Planner Tests
22

3-
## Update the test cases
3+
These test cases use the [sqlplannertest](https://crates.io/crates/sqlplannertest) crate to execute SQL queries and inspect their output.
4+
They do not check whether a plan is correct, and instead rely on a text-based diff of the query's output to determine whether something is different than the expected output.
45

5-
```shell
6-
cargo run -p optd-sqlplannertest --bin planner_test_apply
7-
# or, supply a list of directories to scan from
8-
cargo run -p optd-sqlplannertest --bin planner_test_apply -- subqueries
9-
```
106

11-
## Verify the test cases
7+
## Execute Test Cases
128

139
```shell
1410
cargo test -p optd-sqlplannertest
1511
# or use nextest
1612
cargo nextest run -p optd-sqlplannertest
1713
```
14+
## Add New Test Case
15+
16+
To add a SQL query tests, create a YAML file in a subdir in "tests".
17+
Each file can contain multiple tests that are executed in sequential order from top to bottom.
18+
19+
```yaml
20+
- sql: |
21+
CREATE TABLE xxx (a INTEGER, b INTEGER);
22+
INSERT INTO xxx VALUES (0, 0), (1, 1), (2, 2);
23+
tasks:
24+
- execute
25+
desc: Database Setup
26+
- sql: |
27+
SELECT * FROM xxx WHERE a = 0;
28+
tasks:
29+
- execute
30+
- explain:logical_optd,physical_optd
31+
desc: Equality predicate
32+
```
33+
| Name | Description |
34+
| ---------- | ------------------------------------------------------------------ |
35+
| `sql` | List of SQL statements to execute separate by newlines |
36+
| `tasks` | How to execute the SQL statements. See [Tasks](#tasks) below |
37+
| `desc` | (Optional) Text description of what the test cases represents |
38+
39+
After adding the YAML file, you then need to use the update command to automatically create the matching SQL file that contains the expected output of the test cases.
40+
41+
## Regenerate Test Case Output
42+
43+
If you change the output of optd's `EXPLAIN` syntax, then you need to update all of the expected output files for each test cases.
44+
The following commands will automatically update all of them for you. You should try to avoid this doing this often since if you introduce a bug, all the outputs will get overwritten and the tests will report false negatives.
45+
46+
```shell
47+
# Update all test cases
48+
cargo run -p optd-sqlplannertest --bin planner_test_apply
49+
# or, supply a list of directories to update
50+
cargo run -p optd-sqlplannertest --bin planner_test_apply -- subqueries
51+
```
1852

1953
## Tasks
2054

@@ -24,19 +58,19 @@ The `explain` and `execute` task will be run with datafusion's logical optimizer
2458

2559
#### Flags
2660

27-
| Name | Description |
28-
| -------------- | ------------------------------------- |
29-
| use_df_logical | Enable Datafusion's logical optimizer |
61+
| Name | Description |
62+
| ---------------- | ------------------------------------- |
63+
| `use_df_logical` | Enable Datafusion's logical optimizer |
3064

3165
### Explain Task
3266

3367
#### Flags
3468

35-
| Name | Description |
36-
| -------------- | ------------------------------------------------------------------ |
37-
| use_df_logical | Enable Datafusion's logical optimizer |
38-
| verbose | Display estimated cost in physical plan |
39-
| logical_rules | Only enable these logical rules (also disable heuristic optimizer) |
69+
| Name | Description |
70+
| ---------------- | ------------------------------------------------------------------ |
71+
| `use_df_logical` | Enable Datafusion's logical optimizer |
72+
| `verbose` | Display estimated cost in physical plan |
73+
| `logical_rules` | Only enable these logical rules (also disable heuristic optimizer) |
4074

4175
Currently we have the following options for the explain task:
4276

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-- Setup Test Table
2+
CREATE TABLE xxx (a INTEGER, b INTEGER);
3+
INSERT INTO xxx VALUES (0, 0), (1, 1), (2, 2);
4+
SELECT * FROM xxx WHERE a = 0;
5+
6+
/*
7+
3
8+
0 0
9+
*/
10+
11+
-- (no id or description)
12+
SELECT * FROM xxx WHERE a + 0 = b + 0;
13+
14+
/*
15+
0 0
16+
1 1
17+
2 2
18+
19+
LogicalProjection { exprs: [ #0, #1 ] }
20+
└── LogicalFilter
21+
├── cond:Eq
22+
│ ├── Add
23+
│ │ ├── Cast { cast_to: Int64, child: #0 }
24+
│ │ └── 0(i64)
25+
│ └── Add
26+
│ ├── Cast { cast_to: Int64, child: #1 }
27+
│ └── 0(i64)
28+
└── LogicalScan { table: xxx }
29+
PhysicalFilter
30+
├── cond:Eq
31+
│ ├── Add
32+
│ │ ├── Cast { cast_to: Int64, child: #0 }
33+
│ │ └── 0(i64)
34+
│ └── Add
35+
│ ├── Cast { cast_to: Int64, child: #1 }
36+
│ └── 0(i64)
37+
└── PhysicalScan { table: xxx }
38+
*/
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- sql: |
2+
CREATE TABLE xxx (a INTEGER, b INTEGER);
3+
INSERT INTO xxx VALUES (0, 0), (1, 1), (2, 2);
4+
SELECT * FROM xxx WHERE a = 0;
5+
tasks:
6+
- execute
7+
desc: Setup Test Table
8+
- sql: |
9+
SELECT * FROM xxx WHERE a + 0 = b + 0;
10+
tasks:
11+
- execute
12+
- explain:logical_optd,physical_optd

0 commit comments

Comments
 (0)