Skip to content

Commit 5ba4a55

Browse files
committed
Add serde to AST nodes
1 parent 75c8326 commit 5ba4a55

File tree

8 files changed

+67
-53
lines changed

8 files changed

+67
-53
lines changed

Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ path = "src/lib.rs"
2222
cst = ["rowan"] # Retain a concrete synatax tree, available as `parser.syntax()`
2323

2424
[dependencies]
25-
bigdecimal = { version = "0.1.0", optional = true }
25+
bigdecimal = { version = "0.1.0", optional = true, features = ["serde"] }
2626
log = "0.4.5"
27-
rowan = { version = "0.10.0", optional = true }
27+
rowan = { version = "0.10.0", optional = true, features = ["serde1"] }
28+
serde = { version = "1.0.106", features = ["derive"] }
29+
serde_json = "1.0.52"
2830

2931
[dev-dependencies]
3032
simple_logger = "1.0.1"

examples/cli.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ fn main() {
7070
println!("Parse results:\n{:#?}", statements);
7171
} else {
7272
#[cfg(feature = "cst")]
73-
println!("Parse tree:\n{:#?}", parser.syntax());
73+
{
74+
let syn = parser.syntax();
75+
println!("Parse tree:\n{:#?}", syn);
76+
let serialized = serde_json::to_string(&syn).unwrap();
77+
println!("Parse tree as json:\n{}", serialized);
78+
let serialized = serde_json::to_string(&statements).unwrap();
79+
println!("AST as JSON:\n{}", serialized);
80+
}
7481
}
7582
}
7683
Err(e) => {

src/ast/data_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
// limitations under the License.
1212

1313
use super::ObjectName;
14+
use serde::{Deserialize, Serialize};
1415
use std::fmt;
1516

1617
/// SQL data types
17-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
1819
pub enum DataType {
1920
/// Fixed-length character type e.g. CHAR(10)
2021
Char(Option<u64>),

src/ast/ddl.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
//! AST types specific to CREATE/ALTER variants of [Statement]
1414
//! (commonly referred to as Data Definition Language, or DDL)
1515
use super::{display_comma_separated, DataType, Expr, Ident, ObjectName};
16+
use serde::{Deserialize, Serialize};
1617
use std::fmt;
1718

1819
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
19-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
2021
pub enum AlterTableOperation {
2122
/// `ADD <table_constraint>`
2223
AddConstraint(TableConstraint),
@@ -35,7 +36,7 @@ impl fmt::Display for AlterTableOperation {
3536

3637
/// A table-level constraint, specified in a `CREATE TABLE` or an
3738
/// `ALTER TABLE ADD <constraint>` statement.
38-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
3940
pub enum TableConstraint {
4041
/// `[ CONSTRAINT <name> ] { PRIMARY KEY | UNIQUE } (<columns>)`
4142
Unique {
@@ -94,7 +95,7 @@ impl fmt::Display for TableConstraint {
9495
}
9596

9697
/// SQL column definition
97-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
98+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9899
pub struct ColumnDef {
99100
pub name: Ident,
100101
pub data_type: DataType,
@@ -128,7 +129,7 @@ impl fmt::Display for ColumnDef {
128129
/// For maximum flexibility, we don't distinguish between constraint and
129130
/// non-constraint options, lumping them all together under the umbrella of
130131
/// "column options," and we allow any column option to be named.
131-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
132+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
132133
pub struct ColumnOptionDef {
133134
pub name: Option<Ident>,
134135
pub option: ColumnOption,
@@ -142,7 +143,7 @@ impl fmt::Display for ColumnOptionDef {
142143

143144
/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
144145
/// TABLE` statement.
145-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
146+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
146147
pub enum ColumnOption {
147148
/// `NULL`
148149
Null,
@@ -219,7 +220,7 @@ fn display_constraint_name<'a>(name: &'a Option<Ident>) -> impl fmt::Display + '
219220
/// { RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }`
220221
///
221222
/// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options.
222-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
223+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
223224
pub enum ReferentialAction {
224225
Restrict,
225226
Cascade,

src/ast/mod.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod operator;
1818
mod query;
1919
mod value;
2020

21+
use serde::{Deserialize, Serialize};
2122
use std::fmt;
2223

2324
pub use self::data_type::DataType;
@@ -70,7 +71,7 @@ where
7071
}
7172

7273
/// An identifier, decomposed into its value or character data and the quote style.
73-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
74+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7475
pub struct Ident {
7576
/// The value of the identifier without quotes.
7677
pub value: String,
@@ -126,7 +127,7 @@ impl fmt::Display for Ident {
126127
}
127128

128129
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
129-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
130+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
130131
pub struct ObjectName(pub Vec<Ident>);
131132

132133
impl fmt::Display for ObjectName {
@@ -140,7 +141,7 @@ impl fmt::Display for ObjectName {
140141
/// The parser does not distinguish between expressions of different types
141142
/// (e.g. boolean vs string), so the caller must handle expressions of
142143
/// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary.
143-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
144+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
144145
pub enum Expr {
145146
/// Identifier e.g. table name or column name
146147
Identifier(Ident),
@@ -303,7 +304,7 @@ impl fmt::Display for Expr {
303304
/// Note: we only recognize a complete single expression as `<condition>`,
304305
/// not `< 0` nor `1, 2, 3` as allowed in a `<simple when clause>` per
305306
/// <https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause>
306-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
307+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
307308
pub struct WhenClause {
308309
pub condition: Expr,
309310
pub result: Expr,
@@ -316,7 +317,7 @@ impl fmt::Display for WhenClause {
316317
}
317318

318319
/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
319-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
320+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
320321
pub struct WindowSpec {
321322
pub partition_by: Vec<Expr>,
322323
pub order_by: Vec<OrderByExpr>,
@@ -361,7 +362,7 @@ impl fmt::Display for WindowSpec {
361362
///
362363
/// Note: The parser does not validate the specified bounds; the caller should
363364
/// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution.
364-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
365+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
365366
pub struct WindowFrame {
366367
pub units: WindowFrameUnits,
367368
pub start_bound: WindowFrameBound,
@@ -372,7 +373,7 @@ pub struct WindowFrame {
372373
// TBD: EXCLUDE
373374
}
374375

375-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
376+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
376377
pub enum WindowFrameUnits {
377378
Rows,
378379
Range,
@@ -406,7 +407,7 @@ impl FromStr for WindowFrameUnits {
406407
}
407408

408409
/// Specifies [WindowFrame]'s `start_bound` and `end_bound`
409-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
410+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
410411
pub enum WindowFrameBound {
411412
/// `CURRENT ROW`
412413
CurrentRow,
@@ -430,7 +431,7 @@ impl fmt::Display for WindowFrameBound {
430431

431432
/// A top-level statement (SELECT, INSERT, CREATE, etc.)
432433
#[allow(clippy::large_enum_variant)]
433-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
434+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
434435
pub enum Statement {
435436
/// SELECT
436437
Query(Box<Query>),
@@ -774,7 +775,7 @@ impl fmt::Display for Statement {
774775
}
775776

776777
/// SQL assignment `foo = expr` as used in SQLUpdate
777-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
778+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
778779
pub struct Assignment {
779780
pub id: Ident,
780781
pub value: Expr,
@@ -787,7 +788,7 @@ impl fmt::Display for Assignment {
787788
}
788789

789790
/// A function call
790-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
791+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
791792
pub struct Function {
792793
pub name: ObjectName,
793794
pub args: Vec<Expr>,
@@ -813,7 +814,7 @@ impl fmt::Display for Function {
813814
}
814815

815816
/// External table's available file format
816-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
817+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
817818
pub enum FileFormat {
818819
TEXTFILE,
819820
SEQUENCEFILE,
@@ -864,7 +865,7 @@ impl FromStr for FileFormat {
864865

865866
/// A `LISTAGG` invocation `LISTAGG( [ DISTINCT ] <expr>[, <separator> ] [ON OVERFLOW <on_overflow>] ) )
866867
/// [ WITHIN GROUP (ORDER BY <within_group1>[, ...] ) ]`
867-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
868+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
868869
pub struct ListAgg {
869870
pub distinct: bool,
870871
pub expr: Box<Expr>,
@@ -900,7 +901,7 @@ impl fmt::Display for ListAgg {
900901
}
901902

902903
/// The `ON OVERFLOW` clause of a LISTAGG invocation
903-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
904+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
904905
pub enum ListAggOnOverflow {
905906
/// `ON OVERFLOW ERROR`
906907
Error,
@@ -933,7 +934,7 @@ impl fmt::Display for ListAggOnOverflow {
933934
}
934935
}
935936

936-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
937+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
937938
pub enum ObjectType {
938939
Table,
939940
View,
@@ -952,7 +953,7 @@ impl fmt::Display for ObjectType {
952953
}
953954
}
954955

955-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
956+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
956957
pub struct SqlOption {
957958
pub name: Ident,
958959
pub value: Value,
@@ -964,7 +965,7 @@ impl fmt::Display for SqlOption {
964965
}
965966
}
966967

967-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
968+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
968969
pub enum TransactionMode {
969970
AccessMode(TransactionAccessMode),
970971
IsolationLevel(TransactionIsolationLevel),
@@ -980,7 +981,7 @@ impl fmt::Display for TransactionMode {
980981
}
981982
}
982983

983-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
984+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
984985
pub enum TransactionAccessMode {
985986
ReadOnly,
986987
ReadWrite,
@@ -996,7 +997,7 @@ impl fmt::Display for TransactionAccessMode {
996997
}
997998
}
998999

999-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1000+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10001001
pub enum TransactionIsolationLevel {
10011002
ReadUncommitted,
10021003
ReadCommitted,
@@ -1016,7 +1017,7 @@ impl fmt::Display for TransactionIsolationLevel {
10161017
}
10171018
}
10181019

1019-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1020+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10201021
pub enum ShowStatementFilter {
10211022
Like(String),
10221023
Where(Expr),
@@ -1032,7 +1033,7 @@ impl fmt::Display for ShowStatementFilter {
10321033
}
10331034
}
10341035

1035-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1036+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10361037
pub enum SetVariableValue {
10371038
Ident(Ident),
10381039
Literal(Value),

src/ast/operator.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
use serde::{Deserialize, Serialize};
1314
use std::fmt;
1415

1516
/// Unary operators
16-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
1718
pub enum UnaryOperator {
1819
Plus,
1920
Minus,
@@ -31,7 +32,7 @@ impl fmt::Display for UnaryOperator {
3132
}
3233

3334
/// Binary operators
34-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
35+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
3536
pub enum BinaryOperator {
3637
Plus,
3738
Minus,

0 commit comments

Comments
 (0)