├── examples ├── q50.qpml ├── minimal.png ├── nested-join.png ├── spark-plan.png ├── minimal.md ├── minimal.yaml ├── minimal.dot ├── nested-join.yaml ├── spark-plan.txt ├── nested-join.dot ├── spark-plan.qpml └── spark-plan.dot ├── testdata └── test.csv ├── .gitignore ├── dev └── make-examples.sh ├── Cargo.toml ├── README.md ├── src ├── bin │ └── main.rs └── lib.rs ├── docs └── README.md └── LICENSE /examples/q50.qpml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /testdata/test.csv: -------------------------------------------------------------------------------- 1 | id,name 2 | 1,foo -------------------------------------------------------------------------------- /examples/minimal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andygrove/qpml/HEAD/examples/minimal.png -------------------------------------------------------------------------------- /examples/nested-join.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andygrove/qpml/HEAD/examples/nested-join.png -------------------------------------------------------------------------------- /examples/spark-plan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andygrove/qpml/HEAD/examples/spark-plan.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # Generated by Cargo 4 | # will have compiled files and executables 5 | /target/ 6 | 7 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 8 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 9 | Cargo.lock 10 | 11 | # These are backup files generated by rustfmt 12 | **/*.rs.bk 13 | -------------------------------------------------------------------------------- /examples/minimal.md: -------------------------------------------------------------------------------- 1 | ```mermaid 2 | flowchart TD 3 | node0[Inner Join: w_warehouse_sk = inv_warehouse_sk] --> node0_0[Inner Join: cs_item_sk = inv_item_sk] 4 | node0_0[Inner Join: cs_item_sk = inv_item_sk] --> node0_0_0[catalog_sales] 5 | node0_0[Inner Join: cs_item_sk = inv_item_sk] --> node0_0_1[inventory] 6 | node0[Inner Join: w_warehouse_sk = inv_warehouse_sk] --> node0_1[warehouse] 7 | ``` 8 | -------------------------------------------------------------------------------- /examples/minimal.yaml: -------------------------------------------------------------------------------- 1 | diagram: 2 | title: 'Inner Join: w_warehouse_sk = inv_warehouse_sk' 3 | style: join 4 | inputs: 5 | - title: 'Inner Join: cs_item_sk = inv_item_sk' 6 | style: join 7 | inputs: 8 | - title: catalog_sales 9 | style: table 10 | - title: inventory 11 | style: table 12 | - title: warehouse 13 | style: table 14 | styles: 15 | - name: table 16 | color: lightblue 17 | shape: rectangle 18 | - name: join 19 | color: lightgreen 20 | shape: rectangle -------------------------------------------------------------------------------- /dev/make-examples.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cargo build --release 3 | 4 | ./target/release/qpml mermaid examples/minimal.yaml > examples/minimal.md 5 | ./target/release/qpml dot examples/minimal.yaml > examples/minimal.dot 6 | dot -Tpng examples/minimal.dot > examples/minimal.png 7 | 8 | ./target/release/qpml dot examples/nested-join.yaml > examples/nested-join.dot 9 | dot -Tpng examples/nested-join.dot > examples/nested-join.png 10 | 11 | ./target/release/qpml import-text examples/spark-plan.txt > examples/spark-plan.qpml 12 | ./target/release/qpml dot examples/spark-plan.qpml > examples/spark-plan.dot 13 | dot -Tpng examples/spark-plan.dot > examples/spark-plan.png 14 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "qpml" 3 | description = "Query Plan Markup Language (QPML)" 4 | version = "0.14.0" 5 | authors = ["Andy Grove "] 6 | license = "Apache-2.0" 7 | homepage = "https://github.com/andygrove/qpml" 8 | repository = "https://github.com/andygrove/qpml" 9 | edition = "2021" 10 | 11 | [[bin]] 12 | name = "qpml" 13 | path = "src/bin/main.rs" 14 | 15 | [lib] 16 | name = "qpml" 17 | path = "src/lib.rs" 18 | 19 | [dependencies] 20 | datafusion = "34.0" 21 | datafusion-substrait = "34.0" 22 | serde = { version = "1.0", features = ["derive"] } 23 | serde_yaml = "0.9" 24 | structopt = { version = "0.3", default-features = false } 25 | tokio = "1.0" 26 | 27 | -------------------------------------------------------------------------------- /examples/minimal.dot: -------------------------------------------------------------------------------- 1 | digraph G { 2 | 3 | node0 [shape=box; label="Inner Join: w_warehouse_sk = i\nnv_warehouse_sk"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 4 | node0 -> node0_0; 5 | node0_0 [shape=box; label="Inner Join: cs_item_sk = inv_i\ntem_sk"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 6 | node0_0 -> node0_0_0; 7 | node0_0_0 [shape=box; label="catalog_sales"; color="lightblue"; fillcolor="lightblue"; style="filled"]; 8 | node0_0 -> node0_0_1; 9 | node0_0_1 [shape=box; label="inventory"; color="lightblue"; fillcolor="lightblue"; style="filled"]; 10 | node0 -> node0_1; 11 | node0_1 [shape=box; label="warehouse"; color="lightblue"; fillcolor="lightblue"; style="filled"]; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /examples/nested-join.yaml: -------------------------------------------------------------------------------- 1 | diagram: 2 | title: 'Inner Join:\ncs_ship_date_sk = d3.d_date_sk' 3 | style: join 4 | inputs: 5 | - title: 'Inner Join:\ninv_date_sk = d2.d_date_sk' 6 | style: join 7 | inputs: 8 | - title: 'Inner Join:\ncs_sold_date_sk = d1.d_date_sk' 9 | style: join 10 | inputs: 11 | - title: 'Inner Join:\ncs_bill_hdemo_sk = hd_demo_sk' 12 | style: join 13 | inputs: 14 | - title: 'Inner Join:\ncs_bill_cdemo_sk = cd_demo_sk' 15 | style: join 16 | inputs: 17 | - title: 'Inner Join:\ni_item_sk = cs_item_sk' 18 | style: join 19 | inputs: 20 | - title: 'Inner Join:\nw_warehouse_sk = inv_warehouse_sk' 21 | style: join 22 | inputs: 23 | - title: 'Inner Join:\ncs_item_sk = inv_item_sk' 24 | style: join 25 | inputs: 26 | - title: catalog_sales 27 | style: scan_fact 28 | - title: inventory 29 | style: scan_fact 30 | - title: warehouse 31 | style: scan_dim 32 | - title: item 33 | style: scan_dim 34 | - title: customer_demographics 35 | style: scan_dim 36 | - title: household_demographics 37 | style: scan_dim 38 | - title: d1 39 | style: scan_dim 40 | - title: d2 41 | style: scan_dim 42 | - title: d3 43 | style: scan_dim 44 | styles: 45 | - name: scan_fact 46 | color: lightblue 47 | shape: rectangle 48 | - name: scan_dim 49 | color: lightgreen 50 | shape: rectangle 51 | -------------------------------------------------------------------------------- /examples/spark-plan.txt: -------------------------------------------------------------------------------- 1 | - LocalLimit 10 2 | +- Sort [revenue#289 DESC NULLS LAST, o_orderdate#60 ASC NULLS FIRST], true 3 | +- Aggregate [l_orderkey#16L, o_orderdate#60, o_shippriority#63], [l_orderkey#16L, sum(CheckOverflow((promote_precision(cast(l_extendedprice#21 as decimal(12,2))) * promote_precision(CheckOverflow((1.00 - promote_precision(cast(l_discount#22 as decimal(12,2)))), DecimalType(12,2), true))), DecimalType(24,4), true)) AS revenue#289, o_orderdate#60, o_shippriority#63] 4 | +- Project [o_orderdate#60, o_shippriority#63, l_orderkey#16L, l_extendedprice#21, l_discount#22] 5 | +- Join Inner, (l_orderkey#16L = o_orderkey#56L) 6 | :- Project [o_orderkey#56L, o_orderdate#60, o_shippriority#63] 7 | : +- Join Inner, (c_custkey#0L = o_custkey#57L) 8 | : :- Project [c_custkey#0L] 9 | : : +- Filter ((isnotnull(c_mktsegment#6) AND (c_mktsegment#6 = HOUSEHOLD)) AND isnotnull(c_custkey#0L)) 10 | : : +- Relation [c_custkey#0L,c_name#1,c_address#2,c_nationkey#3L,c_phone#4,c_acctbal#5,c_mktsegment#6,c_comment#7] parquet 11 | : +- Project [o_orderkey#56L, o_custkey#57L, o_orderdate#60, o_shippriority#63] 12 | : +- Filter ((isnotnull(o_orderdate#60) AND (o_orderdate#60 < 1995-03-21)) AND (isnotnull(o_custkey#57L) AND isnotnull(o_orderkey#56L))) 13 | : +- Relation [o_orderkey#56L,o_custkey#57L,o_orderstatus#58,o_totalprice#59,o_orderdate#60,o_orderpriority#61,o_clerk#62,o_shippriority#63,o_comment#64] parquet 14 | +- Project [l_orderkey#16L, l_extendedprice#21, l_discount#22] 15 | +- Filter ((isnotnull(l_shipdate#26) AND (l_shipdate#26 > 1995-03-21)) AND isnotnull(l_orderkey#16L)) 16 | +- Relation [l_orderkey#16L,l_partkey#17L,l_suppkey#18L,l_linenumber#19,l_quantity#20,l_extendedprice#21,l_discount#22,l_tax#23,l_returnflag#24,l_linestatus#25,l_shipdate#26,l_commitdate#27,l_receiptdate#28,l_shipinstruct#29,l_shipmode#30,l_comment#31] parquet -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QPML 2 | 3 | QPML is a utility for visualizing query plans, intended to help produce documentation and presentations. 4 | 5 | Query plan diagrams can easily be hand-coded in the YAML-based Query Plan Markup Language, or can be imported 6 | from Substrait query plans, or from text representations of query plans, as displayed by an EXPLAIN command. 7 | 8 | Here is a minimal example of a QPML file. See [examples/nested-join.yaml](examples/nested-join.yaml) for a fuller example. 9 | 10 | ```yaml 11 | diagram: 12 | title: 'Inner Join: w_warehouse_sk = inv_warehouse_sk' 13 | style: join 14 | inputs: 15 | - title: 'Inner Join: cs_item_sk = inv_item_sk' 16 | style: join 17 | inputs: 18 | - title: catalog_sales 19 | style: table 20 | - title: inventory 21 | style: table 22 | - title: warehouse 23 | style: table 24 | styles: 25 | - name: table 26 | color: lightblue 27 | shape: rectangle 28 | - name: join 29 | color: lightgreen 30 | shape: rectangle 31 | ``` 32 | 33 | # Example Generated Output 34 | 35 | ## GraphViz 36 | 37 | ```shell 38 | qpml dot minimal.qpml > minimal.dot 39 | dot -Tpng minimal.dot > minimal.png 40 | ``` 41 | 42 | ![Example Diagram](examples/minimal.png) 43 | 44 | ## GitHub Mermaid Diagram 45 | 46 | ```shell 47 | $ qpml mermaid minimal.qpml > minmal.md 48 | ``` 49 | 50 | ```mermaid 51 | flowchart TD 52 | node0[Inner Join: w_warehouse_sk = inv_warehouse_sk] --> node0_0[Inner Join: cs_item_sk = inv_item_sk] 53 | node0_0[Inner Join: cs_item_sk = inv_item_sk] --> node0_0_0[catalog_sales] 54 | node0_0[Inner Join: cs_item_sk = inv_item_sk] --> node0_0_1[inventory] 55 | node0[Inner Join: w_warehouse_sk = inv_warehouse_sk] --> node0_1[warehouse] 56 | ``` 57 | 58 | ## Text 59 | 60 | ```shell 61 | $ qpml print minimal.qpml 62 | ``` 63 | 64 | ```text 65 | Inner Join: w_warehouse_sk = inv_warehouse_sk 66 | Inner Join: cs_item_sk = inv_item_sk 67 | catalog_sales 68 | inventory 69 | warehouse 70 | ``` 71 | 72 | # Creating QPML from Existing Query Plans 73 | 74 | See the [QPML Documentation](./docs/README.md) for more information. 75 | -------------------------------------------------------------------------------- /examples/nested-join.dot: -------------------------------------------------------------------------------- 1 | digraph G { 2 | 3 | node0 [shape=box; label="Inner Join:\ncs_ship_date_sk =\n d3.d_date_sk"]; 4 | node0 -> node0_0; 5 | node0_0 [shape=box; label="Inner Join:\ninv_date_sk = d2.\nd_date_sk"]; 6 | node0_0 -> node0_0_0; 7 | node0_0_0 [shape=box; label="Inner Join:\ncs_sold_date_sk =\n d1.d_date_sk"]; 8 | node0_0_0 -> node0_0_0_0; 9 | node0_0_0_0 [shape=box; label="Inner Join:\ncs_bill_hdemo_sk \n= hd_demo_sk"]; 10 | node0_0_0_0 -> node0_0_0_0_0; 11 | node0_0_0_0_0 [shape=box; label="Inner Join:\ncs_bill_cdemo_sk \n= cd_demo_sk"]; 12 | node0_0_0_0_0 -> node0_0_0_0_0_0; 13 | node0_0_0_0_0_0 [shape=box; label="Inner Join:\ni_item_sk = cs_it\nem_sk"]; 14 | node0_0_0_0_0_0 -> node0_0_0_0_0_0_0; 15 | node0_0_0_0_0_0_0 [shape=box; label="Inner Join:\nw_warehouse_sk = \ninv_warehouse_sk"]; 16 | node0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0; 17 | node0_0_0_0_0_0_0_0 [shape=box; label="Inner Join:\ncs_item_sk = inv_\nitem_sk"]; 18 | node0_0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0_0; 19 | node0_0_0_0_0_0_0_0_0 [shape=box; label="catalog_sales"; color="lightblue"; fillcolor="lightblue"; style="filled"]; 20 | node0_0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0_1; 21 | node0_0_0_0_0_0_0_0_1 [shape=box; label="inventory"; color="lightblue"; fillcolor="lightblue"; style="filled"]; 22 | node0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_1; 23 | node0_0_0_0_0_0_0_1 [shape=box; label="warehouse"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 24 | node0_0_0_0_0_0 -> node0_0_0_0_0_0_1; 25 | node0_0_0_0_0_0_1 [shape=box; label="item"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 26 | node0_0_0_0_0 -> node0_0_0_0_0_1; 27 | node0_0_0_0_0_1 [shape=box; label="customer_demographics"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 28 | node0_0_0_0 -> node0_0_0_0_1; 29 | node0_0_0_0_1 [shape=box; label="household_demographics"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 30 | node0_0_0 -> node0_0_0_1; 31 | node0_0_0_1 [shape=box; label="d1"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 32 | node0_0 -> node0_0_1; 33 | node0_0_1 [shape=box; label="d2"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 34 | node0 -> node0_1; 35 | node0_1 [shape=box; label="d3"; color="lightgreen"; fillcolor="lightgreen"; style="filled"]; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /examples/spark-plan.qpml: -------------------------------------------------------------------------------- 1 | diagram: 2 | title: LocalLimit 10 3 | inputs: 4 | - title: Sort [revenue#289 DESC NULLS LAST, o_orderdate#60 ASC NULLS FIRST], true 5 | inputs: 6 | - title: Aggregate [l_orderkey#16L, o_orderdate#60, o_shippriority#63], [l_orderkey#16L, sum(CheckOverflow((promote_precision(cast(l_extendedprice#21 as decimal(12,2))) * promote_precision(CheckOverflow((1.00 - promote_precision(cast(l_discount#22 as decimal(12,2)))), DecimalType(12,2), true))), DecimalType(24,4), true)) AS revenue#289, o_orderdate#60, o_shippriority#63] 7 | inputs: 8 | - title: Project [o_orderdate#60, o_shippriority#63, l_orderkey#16L, l_extendedprice#21, l_discount#22] 9 | inputs: 10 | - title: Join Inner, (l_orderkey#16L = o_orderkey#56L) 11 | inputs: 12 | - title: Project [o_orderkey#56L, o_orderdate#60, o_shippriority#63] 13 | inputs: 14 | - title: Join Inner, (c_custkey#0L = o_custkey#57L) 15 | inputs: 16 | - title: Project [c_custkey#0L] 17 | inputs: 18 | - title: Filter ((isnotnull(c_mktsegment#6) AND (c_mktsegment#6 = HOUSEHOLD)) AND isnotnull(c_custkey#0L)) 19 | inputs: 20 | - title: Relation [c_custkey#0L,c_name#1,c_address#2,c_nationkey#3L,c_phone#4,c_acctbal#5,c_mktsegment#6,c_comment#7] parquet 21 | - title: Project [o_orderkey#56L, o_custkey#57L, o_orderdate#60, o_shippriority#63] 22 | inputs: 23 | - title: Filter ((isnotnull(o_orderdate#60) AND (o_orderdate#60 < 1995-03-21)) AND (isnotnull(o_custkey#57L) AND isnotnull(o_orderkey#56L))) 24 | inputs: 25 | - title: Relation [o_orderkey#56L,o_custkey#57L,o_orderstatus#58,o_totalprice#59,o_orderdate#60,o_orderpriority#61,o_clerk#62,o_shippriority#63,o_comment#64] parquet 26 | - title: Project [l_orderkey#16L, l_extendedprice#21, l_discount#22] 27 | inputs: 28 | - title: Filter ((isnotnull(l_shipdate#26) AND (l_shipdate#26 > 1995-03-21)) AND isnotnull(l_orderkey#16L)) 29 | inputs: 30 | - title: Relation [l_orderkey#16L,l_partkey#17L,l_suppkey#18L,l_linenumber#19,l_quantity#20,l_extendedprice#21,l_discount#22,l_tax#23,l_returnflag#24,l_linestatus#25,l_shipdate#26,l_commitdate#27,l_receiptdate#28,l_shipinstruct#29,l_shipmode#30,l_comment#31] parquet 31 | 32 | -------------------------------------------------------------------------------- /src/bin/main.rs: -------------------------------------------------------------------------------- 1 | use qpml::{from_text_plan, import_sql, import_substrait}; 2 | use std::fs; 3 | use std::path::PathBuf; 4 | use structopt::StructOpt; 5 | 6 | #[derive(StructOpt)] 7 | #[structopt(about = "Query Plan Markup Language")] 8 | enum Opt { 9 | /// Print a textual representation 10 | Print { 11 | #[structopt(parse(from_os_str))] 12 | input: PathBuf, 13 | }, 14 | /// Generate a DOT diagram 15 | Dot { 16 | #[structopt(parse(from_os_str))] 17 | input: PathBuf, 18 | #[structopt(long)] 19 | inverted: bool, 20 | }, 21 | /// Generate a Mermaid diagram 22 | Mermaid { 23 | #[structopt(parse(from_os_str))] 24 | input: PathBuf, 25 | #[structopt(long)] 26 | inverted: bool, 27 | }, 28 | /// Import a text plan and convert to QPML 29 | ImportText { 30 | #[structopt(parse(from_os_str))] 31 | input: PathBuf, 32 | }, 33 | /// Import a Substrait plan and convert to QPML 34 | ImportSubstrait { 35 | #[structopt(parse(from_os_str))] 36 | input: PathBuf, 37 | }, 38 | /// Generate from a SQL query file 39 | ImportSql { 40 | #[structopt(parse(from_os_str))] 41 | /// Path to file containing SQL query 42 | input: PathBuf, 43 | #[structopt(parse(from_os_str))] 44 | /// Path to data files 45 | tables: PathBuf, 46 | }, 47 | } 48 | 49 | #[tokio::main] 50 | async fn main() { 51 | match Opt::from_args() { 52 | Opt::Print { input } => { 53 | let yaml = fs::read_to_string(input).expect("Unable to read file"); 54 | let doc: qpml::Document = serde_yaml::from_str(&yaml).unwrap(); 55 | qpml::generate_text(&doc); 56 | } 57 | Opt::Dot { input, inverted } => { 58 | let yaml = fs::read_to_string(input).expect("Unable to read file"); 59 | let doc: qpml::Document = serde_yaml::from_str(&yaml).unwrap(); 60 | qpml::generate_dot(&doc, inverted); 61 | } 62 | Opt::Mermaid { input, inverted } => { 63 | let yaml = fs::read_to_string(input).expect("Unable to read file"); 64 | let doc: qpml::Document = serde_yaml::from_str(&yaml).unwrap(); 65 | qpml::generate_mermaid(&doc, inverted); 66 | } 67 | Opt::ImportText { input } => { 68 | let doc = from_text_plan(&input).unwrap(); 69 | let str = serde_yaml::to_string(&doc).unwrap(); 70 | println!("{}", str); 71 | } 72 | Opt::ImportSubstrait { input } => { 73 | let doc = import_substrait(&input).await.unwrap(); 74 | let str = serde_yaml::to_string(&doc).unwrap(); 75 | println!("{}", str); 76 | } 77 | Opt::ImportSql { input, tables } => { 78 | let doc = import_sql(&input, &tables).await.unwrap(); 79 | let str = serde_yaml::to_string(&doc).unwrap(); 80 | println!("{}", str); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /examples/spark-plan.dot: -------------------------------------------------------------------------------- 1 | digraph G { 2 | 3 | node0 [shape=box; label="LocalLimit 10"]; 4 | node0 -> node0_0; 5 | node0_0 [shape=box; label="Sort [revenue#289 DESC NULLS L\nAST, o_orderdate#60 ASC NULLS \nFIRST], true"]; 6 | node0_0 -> node0_0_0; 7 | node0_0_0 [shape=box; label="Aggregate [l_orderkey#16L, o_o\nrderdate#60, o_shippriority#63\n], [l_orderkey#16L, sum(CheckO\nverflow((promote_precision(cas\nt(l_extendedprice#21 as decima\nl(12,2))) * promote_precision(\nCheckOverflow((1.00 - promote_\nprecision(cast(l_discount#22 a\ns decimal(12,2)))), DecimalTyp\ne(12,2), true))), DecimalType(\n24,4), true)) AS revenue#289, \no_orderdate#60, o_shippriority\n#63]"]; 8 | node0_0_0 -> node0_0_0_0; 9 | node0_0_0_0 [shape=box; label="Project [o_orderdate#60, o_shi\nppriority#63, l_orderkey#16L, \nl_extendedprice#21, l_discount\n#22]"]; 10 | node0_0_0_0 -> node0_0_0_0_0; 11 | node0_0_0_0_0 [shape=box; label="Join Inner, (l_orderkey#16L = \no_orderkey#56L)"]; 12 | node0_0_0_0_0 -> node0_0_0_0_0_0; 13 | node0_0_0_0_0_0 [shape=box; label="Project [o_orderkey#56L, o_ord\nerdate#60, o_shippriority#63]"]; 14 | node0_0_0_0_0_0 -> node0_0_0_0_0_0_0; 15 | node0_0_0_0_0_0_0 [shape=box; label="Join Inner, (c_custkey#0L = o_\ncustkey#57L)"]; 16 | node0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0; 17 | node0_0_0_0_0_0_0_0 [shape=box; label="Project [c_custkey#0L]"]; 18 | node0_0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0_0; 19 | node0_0_0_0_0_0_0_0_0 [shape=box; label="Filter ((isnotnull(c_mktsegmen\nt#6) AND (c_mktsegment#6 = HOU\nSEHOLD)) AND isnotnull(c_custk\ney#0L))"]; 20 | node0_0_0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_0_0_0; 21 | node0_0_0_0_0_0_0_0_0_0 [shape=box; label="Relation [c_custkey#0L,c_name#\n1,c_address#2,c_nationkey#3L,c\n_phone#4,c_acctbal#5,c_mktsegm\nent#6,c_comment#7] parquet"]; 22 | node0_0_0_0_0_0_0 -> node0_0_0_0_0_0_0_1; 23 | node0_0_0_0_0_0_0_1 [shape=box; label="Project [o_orderkey#56L, o_cus\ntkey#57L, o_orderdate#60, o_sh\nippriority#63]"]; 24 | node0_0_0_0_0_0_0_1 -> node0_0_0_0_0_0_0_1_0; 25 | node0_0_0_0_0_0_0_1_0 [shape=box; label="Filter ((isnotnull(o_orderdate\n#60) AND (o_orderdate#60 < 199\n5-03-21)) AND (isnotnull(o_cus\ntkey#57L) AND isnotnull(o_orde\nrkey#56L)))"]; 26 | node0_0_0_0_0_0_0_1_0 -> node0_0_0_0_0_0_0_1_0_0; 27 | node0_0_0_0_0_0_0_1_0_0 [shape=box; label="Relation [o_orderkey#56L,o_cus\ntkey#57L,o_orderstatus#58,o_to\ntalprice#59,o_orderdate#60,o_o\nrderpriority#61,o_clerk#62,o_s\nhippriority#63,o_comment#64] p\narquet"]; 28 | node0_0_0_0_0 -> node0_0_0_0_0_1; 29 | node0_0_0_0_0_1 [shape=box; label="Project [l_orderkey#16L, l_ext\nendedprice#21, l_discount#22]"]; 30 | node0_0_0_0_0_1 -> node0_0_0_0_0_1_0; 31 | node0_0_0_0_0_1_0 [shape=box; label="Filter ((isnotnull(l_shipdate#\n26) AND (l_shipdate#26 > 1995-\n03-21)) AND isnotnull(l_orderk\ney#16L))"]; 32 | node0_0_0_0_0_1_0 -> node0_0_0_0_0_1_0_0; 33 | node0_0_0_0_0_1_0_0 [shape=box; label="Relation [l_orderkey#16L,l_par\ntkey#17L,l_suppkey#18L,l_linen\number#19,l_quantity#20,l_exten\ndedprice#21,l_discount#22,l_ta\nx#23,l_returnflag#24,l_linesta\ntus#25,l_shipdate#26,l_commitd\nate#27,l_receiptdate#28,l_ship\ninstruct#29,l_shipmode#30,l_co\nmment#31] parquet"]; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # QPML Documentation 2 | 3 | # Converting Existing Query Plans to QPML 4 | 5 | ## Text Plans 6 | 7 | The easiest way to import a plan into QPML is from a text representation of a plan. Here is an example of an 8 | Apache Spark query plan but QPML supports any text plan that uses indentation. Any leading non-alphabetic characters 9 | are ignored. 10 | 11 | ```text 12 | - LocalLimit 10 13 | +- Sort [revenue#289 DESC NULLS LAST, o_orderdate#60 ASC NULLS FIRST], true 14 | +- Aggregate [l_orderkey#16L, o_orderdate#60, o_shippriority#63], [l_orderkey#16L, sum(CheckOverflow((promote_precision(cast(l_extendedprice#21 as decimal(12,2))) * promote_precision(CheckOverflow((1.00 - promote_precision(cast(l_discount#22 as decimal(12,2)))), DecimalType(12,2), true))), DecimalType(24,4), true)) AS revenue#289, o_orderdate#60, o_shippriority#63] 15 | +- Project [o_orderdate#60, o_shippriority#63, l_orderkey#16L, l_extendedprice#21, l_discount#22] 16 | +- Join Inner, (l_orderkey#16L = o_orderkey#56L) 17 | :- Project [o_orderkey#56L, o_orderdate#60, o_shippriority#63] 18 | : +- Join Inner, (c_custkey#0L = o_custkey#57L) 19 | : :- Project [c_custkey#0L] 20 | : : +- Filter ((isnotnull(c_mktsegment#6) AND (c_mktsegment#6 = HOUSEHOLD)) AND isnotnull(c_custkey#0L)) 21 | : : +- Relation [c_custkey#0L,c_name#1,c_address#2,c_nationkey#3L,c_phone#4,c_acctbal#5,c_mktsegment#6,c_comment#7] parquet 22 | : +- Project [o_orderkey#56L, o_custkey#57L, o_orderdate#60, o_shippriority#63] 23 | : +- Filter ((isnotnull(o_orderdate#60) AND (o_orderdate#60 < 1995-03-21)) AND (isnotnull(o_custkey#57L) AND isnotnull(o_orderkey#56L))) 24 | : +- Relation [o_orderkey#56L,o_custkey#57L,o_orderstatus#58,o_totalprice#59,o_orderdate#60,o_orderpriority#61,o_clerk#62,o_shippriority#63,o_comment#64] parquet 25 | +- Project [l_orderkey#16L, l_extendedprice#21, l_discount#22] 26 | +- Filter ((isnotnull(l_shipdate#26) AND (l_shipdate#26 > 1995-03-21)) AND isnotnull(l_orderkey#16L)) 27 | +- Relation [l_orderkey#16L,l_partkey#17L,l_suppkey#18L,l_linenumber#19,l_quantity#20,l_extendedprice#21,l_discount#22,l_tax#23,l_returnflag#24,l_linestatus#25,l_shipdate#26,l_commitdate#27,l_receiptdate#28,l_shipinstruct#29,l_shipmode#30,l_comment#31] parquet``` 28 | ``` 29 | 30 | This plan can be imported into QPML format with the following command. 31 | 32 | ```bash 33 | qpml import-text spark-plan.txt > spark-plan.qpml 34 | ``` 35 | 36 | ## Apache Spark 37 | 38 | Add a dependency on `jackson-dataformat-yaml`: 39 | ```xml 40 | 41 | com.fasterxml.jackson.dataformat 42 | jackson-dataformat-yaml 43 | 2.12.3 44 | 45 | ``` 46 | 47 | Sample code for generating QPML text from a Spark logical plan: 48 | 49 | ```scala 50 | import com.fasterxml.jackson.annotation.JsonProperty 51 | import com.fasterxml.jackson.databind.ObjectMapper 52 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory 53 | import com.fasterxml.jackson.module.scala.DefaultScalaModule 54 | import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan, Project} 55 | import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, LogicalRelation} 56 | 57 | case class Node(@JsonProperty("title") title: String, 58 | @JsonProperty("operator") operator: String, 59 | @JsonProperty("inputs") inputs: java.util.List[Node]) 60 | 61 | object Qpml { 62 | 63 | def fromLogicalPlan(plan: LogicalPlan): String = { 64 | 65 | def _fromLogicalPlan(plan: LogicalPlan): Node = { 66 | import collection.JavaConverters._ 67 | val children = plan.children.map(_fromLogicalPlan).asJava 68 | plan match { 69 | case f: LogicalRelation => 70 | val title = f.relation.asInstanceOf[HadoopFsRelation].location.rootPaths.head.getName 71 | Node(title, "scan", children) 72 | case j: Join => 73 | val title = s"${j.joinType} Join: ${j.condition}" 74 | Node(title, "join", children) 75 | case p: Project => 76 | val title = s"Projection: ${p.projectList.mkString(", ")}" 77 | Node(title, "projection", children) 78 | case f: Filter => 79 | val title = s"Filter: ${f.condition}" 80 | Node(title, "filter", children) 81 | case _ => 82 | val title = plan.simpleStringWithNodeId() 83 | Node(title, plan.getClass.getSimpleName, children) 84 | } 85 | } 86 | 87 | val mapper = new ObjectMapper(new YAMLFactory()) 88 | mapper.registerModule(DefaultScalaModule) 89 | mapper.writeValueAsString(_fromLogicalPlan(plan)) 90 | } 91 | 92 | } 93 | ``` 94 | 95 | ## Apache Arrow DataFusion & Ballista 96 | 97 | The qpml crate includes a utility function `from_datafusion` for converting DataFusion logical plans into QPML 98 | format. 99 | 100 | The following dependencies are required: 101 | 102 | ```toml 103 | datafusion = "15.0" 104 | qpml = "0.5" 105 | serde = { version = "1.0", features = ["derive"] } 106 | serde_yaml = "0.9" 107 | ``` 108 | 109 | Here is a code sample for writing a DataFusion logical plan to disk in QPML format. 110 | 111 | ```rust 112 | use qpml::from_datafusion; 113 | use std::fs::File; 114 | use std::io::BufWriter; 115 | 116 | let qpml = from_datafusion(&plan); 117 | let filename = format!("q{}.qpml", query_no); 118 | let file = File::create(&filename)?; 119 | let mut file = BufWriter::new(file); 120 | serde_yaml::to_writer(&mut file, &qpml).unwrap(); 121 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use datafusion::error::DataFusionError; 2 | use datafusion::logical_expr::LogicalPlan; 3 | use datafusion::parquet::errors::ParquetError; 4 | use datafusion::prelude::{ 5 | AvroReadOptions, CsvReadOptions, DataFrame, NdJsonReadOptions, ParquetReadOptions, 6 | SessionContext, 7 | }; 8 | use datafusion_substrait::logical_plan::consumer::from_substrait_plan; 9 | use datafusion_substrait::serializer::deserialize; 10 | use serde::{Deserialize, Serialize}; 11 | use std::cell::RefCell; 12 | use std::collections::HashMap; 13 | use std::fmt; 14 | use std::fmt::Formatter; 15 | use std::fs; 16 | use std::fs::File; 17 | use std::io::{BufRead, ErrorKind}; 18 | use std::io::{BufReader, Error}; 19 | use std::path::{Path, PathBuf}; 20 | use std::rc::Rc; 21 | 22 | #[derive(Debug, PartialEq, Serialize, Deserialize, Default)] 23 | #[allow(clippy::vec_box)] 24 | pub struct Document { 25 | diagram: Box, 26 | #[serde(skip_serializing_if = "Vec::is_empty", default)] 27 | styles: Vec