Skip to content

Commit

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

## [Unreleased]
### Changed
- *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

### 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
14 changes: 8 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,18 @@ 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"

[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
9 changes: 5 additions & 4 deletions partiql-ast/src/ast.rs
Original file line number Diff line number Diff line change
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

0 comments on commit a264233

Please sign in to comment.