Skip to content

Commit

Permalink
Initial partial AST pretty-printer (#471)
Browse files Browse the repository at this point in the history
Initial partial AST pretty-printer
  • Loading branch information
jpschorr authored Jul 9, 2024
1 parent 93622bd commit a54868e
Show file tree
Hide file tree
Showing 12 changed files with 1,582 additions and 61 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Changed
- *BREAKING:* partiql-ast: changed modeling of `BagOpExpr` `setq` field to be an `Option`
- *BREAKING:* partiql-ast: changed modeling of `GroupByExpr` `strategy` field to be an `Option`
- *BREAKING:* partiql-ast: changed modeling of `PathStep` to split `PathExpr` to `PathIndex` (e.g., `[2]`) and `PathProject` (e.g., `.a`)
- *BREAKING:* partiql-ast: changed modeling of `PathStep` to rename `PathWildcard` to `PathForEach` (for `[*]`)

### Added
- partiql-ast: Pretty-printing of AST via `ToPretty` trait

### Fixed
- Minor documentation issues

## [0.8.0]
### Changed
Expand Down
5 changes: 3 additions & 2 deletions extension/partiql-extension-visualize/src/ast_to_dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,9 @@ impl ToDot<ast::Path> for AstToDot {
impl ToDot<ast::PathStep> for AstToDot {
fn to_dot(&mut self, out: &mut Scope<'_, '_>, ast: &ast::PathStep) -> Targets {
match &ast {
ast::PathStep::PathExpr(e) => self.to_dot(out, e),
ast::PathStep::PathWildCard => vec![out.node_auto_labelled("*").id()],
ast::PathStep::PathProject(e) => self.to_dot(out, e),
ast::PathStep::PathIndex(e) => self.to_dot(out, e),
ast::PathStep::PathForEach => vec![out.node_auto_labelled("*").id()],
ast::PathStep::PathUnpivot => vec![out.node_auto_labelled("Unpivot").id()],
}
}
Expand Down
3 changes: 2 additions & 1 deletion partiql-ast-passes/src/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ fn infer_alias(expr: &ast::Expr) -> Option<ast::SymbolPrimitive> {
match expr {
ast::Expr::VarRef(ast::AstNode { node, .. }) => Some(node.name.clone()),
ast::Expr::Path(ast::AstNode { node, .. }) => match node.steps.last() {
Some(ast::PathStep::PathExpr(expr)) => infer_alias(&expr.index),
Some(ast::PathStep::PathProject(expr)) => infer_alias(&expr.index),
Some(ast::PathStep::PathIndex(expr)) => infer_alias(&expr.index),
_ => None,
},
_ => None,
Expand Down
15 changes: 9 additions & 6 deletions partiql-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ readme = "../README.md"
keywords = ["sql", "ast", "query", "compilers", "interpreters"]
categories = ["database", "compilers", "ast-implementations"]
exclude = [
"**/.git/**",
"**/.github/**",
"**/.git/**",
"**/.github/**",
]
version.workspace = true
edition.workspace = true
Expand All @@ -23,16 +23,19 @@ bench = false
indexmap = { version = "1.9", default-features = false }
rust_decimal = { version = "1.25.0", default-features = false, features = ["std"] }
serde = { version = "1.*", features = ["derive"], optional = true }
pretty = "0.12"
thiserror = "1.0"

[dev-dependencies]
partiql-parser = { path = "../partiql-parser", version = "0.8" }

[features]
default = []
serde = [
"dep:serde",
"rust_decimal/serde-with-str",
"rust_decimal/serde",
"indexmap/serde",
"dep:serde",
"rust_decimal/serde-with-str",
"rust_decimal/serde",
"indexmap/serde",
]

[dependencies.partiql-ast-macros]
Expand Down
11 changes: 6 additions & 5 deletions partiql-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub struct BagOpExpr {
#[visit(skip)]
pub bag_op: BagOperator,
#[visit(skip)]
pub setq: SetQuantifier,
pub setq: Option<SetQuantifier>,
pub lhs: Box<AstNode<Query>>,
pub rhs: Box<AstNode<Query>>,
}
Expand Down Expand Up @@ -644,7 +644,7 @@ pub enum CallArg {
PositionalType(Type),
/// named argument to a function call (e.g., the `"from" : 2` in `substring(a, "from":2)`
Named(CallArgNamed),
/// E.g. `AS: VARCHAR` in `CAST('abc' AS VARCHAR`
/// E.g. `AS: VARCHAR` in `CAST('abc' AS VARCHAR)`
NamedType(CallArgNamedType),
}

Expand Down Expand Up @@ -676,9 +676,10 @@ pub struct Path {
#[derive(Visit, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PathStep {
PathExpr(PathExpr),
PathProject(PathExpr),
PathIndex(PathExpr),
#[visit(skip)]
PathWildCard,
PathForEach,
#[visit(skip)]
PathUnpivot,
}
Expand Down Expand Up @@ -789,7 +790,7 @@ pub enum JoinSpec {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GroupByExpr {
#[visit(skip)]
pub strategy: GroupingStrategy,
pub strategy: Option<GroupingStrategy>,
pub keys: Vec<AstNode<GroupKey>>,
#[visit(skip)]
pub group_as_alias: Option<SymbolPrimitive>,
Expand Down
3 changes: 3 additions & 0 deletions partiql-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
//! This API is currently unstable and subject to change.

pub mod ast;

pub mod pretty;

pub mod visit;
Loading

1 comment on commit a54868e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PartiQL (rust) Benchmark

Benchmark suite Current: a54868e Previous: 93622bd Ratio
arith_agg-avg 768879 ns/iter (± 44474) 767550 ns/iter (± 25529) 1.00
arith_agg-avg_distinct 852871 ns/iter (± 4563) 853211 ns/iter (± 3789) 1.00
arith_agg-count 818561 ns/iter (± 24924) 811172 ns/iter (± 12702) 1.01
arith_agg-count_distinct 846734 ns/iter (± 3407) 848275 ns/iter (± 8242) 1.00
arith_agg-min 832601 ns/iter (± 5272) 815662 ns/iter (± 4178) 1.02
arith_agg-min_distinct 859514 ns/iter (± 2662) 850462 ns/iter (± 18696) 1.01
arith_agg-max 827881 ns/iter (± 8412) 821044 ns/iter (± 2913) 1.01
arith_agg-max_distinct 860484 ns/iter (± 2589) 859104 ns/iter (± 3372) 1.00
arith_agg-sum 823950 ns/iter (± 4769) 820101 ns/iter (± 15137) 1.00
arith_agg-sum_distinct 850306 ns/iter (± 2494) 854531 ns/iter (± 3329) 1.00
arith_agg-avg-count-min-max-sum 960769 ns/iter (± 2950) 966254 ns/iter (± 5223) 0.99
arith_agg-avg-count-min-max-sum-group_by 1270408 ns/iter (± 9721) 1236532 ns/iter (± 9954) 1.03
arith_agg-avg-count-min-max-sum-group_by-group_as 1880159 ns/iter (± 11463) 1814702 ns/iter (± 85856) 1.04
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct 1240402 ns/iter (± 20711) 1227968 ns/iter (± 4560) 1.01
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct-group_by 1581066 ns/iter (± 63203) 1582041 ns/iter (± 4251) 1.00
arith_agg-avg_distinct-count_distinct-min_distinct-max_distinct-sum_distinct-group_by-group_as 2187305 ns/iter (± 11171) 2170637 ns/iter (± 14894) 1.01
parse-1 4244 ns/iter (± 32) 4329 ns/iter (± 20) 0.98
parse-15 40550 ns/iter (± 352) 38661 ns/iter (± 519) 1.05
parse-30 78545 ns/iter (± 2822) 76090 ns/iter (± 138) 1.03
compile-1 4394 ns/iter (± 114) 4319 ns/iter (± 14) 1.02
compile-15 33209 ns/iter (± 207) 32940 ns/iter (± 127) 1.01
compile-30 68529 ns/iter (± 190) 68083 ns/iter (± 285) 1.01
plan-1 68549 ns/iter (± 487) 67448 ns/iter (± 2332) 1.02
plan-15 1078118 ns/iter (± 10086) 1059015 ns/iter (± 22871) 1.02
plan-30 2156664 ns/iter (± 8444) 2114208 ns/iter (± 9437) 1.02
eval-1 13199986 ns/iter (± 178346) 13323166 ns/iter (± 626795) 0.99
eval-15 88062096 ns/iter (± 1058303) 89188292 ns/iter (± 342895) 0.99
eval-30 168580958 ns/iter (± 556192) 169856980 ns/iter (± 1103692) 0.99
join 9884 ns/iter (± 93) 9820 ns/iter (± 138) 1.01
simple 2488 ns/iter (± 17) 2542 ns/iter (± 5) 0.98
simple-no 427 ns/iter (± 11) 427 ns/iter (± 1) 1
numbers 57 ns/iter (± 0) 57 ns/iter (± 0) 1
parse-simple 565 ns/iter (± 7) 578 ns/iter (± 8) 0.98
parse-ion 1750 ns/iter (± 9) 1811 ns/iter (± 4) 0.97
parse-group 5900 ns/iter (± 19) 6210 ns/iter (± 49) 0.95
parse-complex 14823 ns/iter (± 106) 15803 ns/iter (± 22) 0.94
parse-complex-fexpr 21985 ns/iter (± 877) 23725 ns/iter (± 142) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.