Skip to content

Commit

Permalink
Add serde to AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolay committed Jun 7, 2020
1 parent 75c8326 commit 5ba4a55
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ path = "src/lib.rs"
cst = ["rowan"] # Retain a concrete synatax tree, available as `parser.syntax()`

[dependencies]
bigdecimal = { version = "0.1.0", optional = true }
bigdecimal = { version = "0.1.0", optional = true, features = ["serde"] }
log = "0.4.5"
rowan = { version = "0.10.0", optional = true }
rowan = { version = "0.10.0", optional = true, features = ["serde1"] }
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0.52"

[dev-dependencies]
simple_logger = "1.0.1"
Expand Down
9 changes: 8 additions & 1 deletion examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ fn main() {
println!("Parse results:\n{:#?}", statements);
} else {
#[cfg(feature = "cst")]
println!("Parse tree:\n{:#?}", parser.syntax());
{
let syn = parser.syntax();
println!("Parse tree:\n{:#?}", syn);
let serialized = serde_json::to_string(&syn).unwrap();
println!("Parse tree as json:\n{}", serialized);
let serialized = serde_json::to_string(&statements).unwrap();
println!("AST as JSON:\n{}", serialized);
}
}
}
Err(e) => {
Expand Down
3 changes: 2 additions & 1 deletion src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
// limitations under the License.

use super::ObjectName;
use serde::{Deserialize, Serialize};
use std::fmt;

/// SQL data types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DataType {
/// Fixed-length character type e.g. CHAR(10)
Char(Option<u64>),
Expand Down
13 changes: 7 additions & 6 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
//! AST types specific to CREATE/ALTER variants of [Statement]
//! (commonly referred to as Data Definition Language, or DDL)
use super::{display_comma_separated, DataType, Expr, Ident, ObjectName};
use serde::{Deserialize, Serialize};
use std::fmt;

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

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

/// SQL column definition
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ColumnDef {
pub name: Ident,
pub data_type: DataType,
Expand Down Expand Up @@ -128,7 +129,7 @@ impl fmt::Display for ColumnDef {
/// For maximum flexibility, we don't distinguish between constraint and
/// non-constraint options, lumping them all together under the umbrella of
/// "column options," and we allow any column option to be named.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ColumnOptionDef {
pub name: Option<Ident>,
pub option: ColumnOption,
Expand All @@ -142,7 +143,7 @@ impl fmt::Display for ColumnOptionDef {

/// `ColumnOption`s are modifiers that follow a column definition in a `CREATE
/// TABLE` statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ColumnOption {
/// `NULL`
Null,
Expand Down Expand Up @@ -219,7 +220,7 @@ fn display_constraint_name<'a>(name: &'a Option<Ident>) -> impl fmt::Display + '
/// { RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }`
///
/// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ReferentialAction {
Restrict,
Cascade,
Expand Down
43 changes: 22 additions & 21 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod operator;
mod query;
mod value;

use serde::{Deserialize, Serialize};
use std::fmt;

pub use self::data_type::DataType;
Expand Down Expand Up @@ -70,7 +71,7 @@ where
}

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

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

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

/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WindowSpec {
pub partition_by: Vec<Expr>,
pub order_by: Vec<OrderByExpr>,
Expand Down Expand Up @@ -361,7 +362,7 @@ impl fmt::Display for WindowSpec {
///
/// Note: The parser does not validate the specified bounds; the caller should
/// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WindowFrame {
pub units: WindowFrameUnits,
pub start_bound: WindowFrameBound,
Expand All @@ -372,7 +373,7 @@ pub struct WindowFrame {
// TBD: EXCLUDE
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WindowFrameUnits {
Rows,
Range,
Expand Down Expand Up @@ -406,7 +407,7 @@ impl FromStr for WindowFrameUnits {
}

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

/// A top-level statement (SELECT, INSERT, CREATE, etc.)
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Statement {
/// SELECT
Query(Box<Query>),
Expand Down Expand Up @@ -774,7 +775,7 @@ impl fmt::Display for Statement {
}

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

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

/// External table's available file format
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FileFormat {
TEXTFILE,
SEQUENCEFILE,
Expand Down Expand Up @@ -864,7 +865,7 @@ impl FromStr for FileFormat {

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

/// The `ON OVERFLOW` clause of a LISTAGG invocation
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ListAggOnOverflow {
/// `ON OVERFLOW ERROR`
Error,
Expand Down Expand Up @@ -933,7 +934,7 @@ impl fmt::Display for ListAggOnOverflow {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ObjectType {
Table,
View,
Expand All @@ -952,7 +953,7 @@ impl fmt::Display for ObjectType {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SqlOption {
pub name: Ident,
pub value: Value,
Expand All @@ -964,7 +965,7 @@ impl fmt::Display for SqlOption {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionMode {
AccessMode(TransactionAccessMode),
IsolationLevel(TransactionIsolationLevel),
Expand All @@ -980,7 +981,7 @@ impl fmt::Display for TransactionMode {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionAccessMode {
ReadOnly,
ReadWrite,
Expand All @@ -996,7 +997,7 @@ impl fmt::Display for TransactionAccessMode {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransactionIsolationLevel {
ReadUncommitted,
ReadCommitted,
Expand All @@ -1016,7 +1017,7 @@ impl fmt::Display for TransactionIsolationLevel {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ShowStatementFilter {
Like(String),
Where(Expr),
Expand All @@ -1032,7 +1033,7 @@ impl fmt::Display for ShowStatementFilter {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SetVariableValue {
Ident(Ident),
Literal(Value),
Expand Down
5 changes: 3 additions & 2 deletions src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use serde::{Deserialize, Serialize};
use std::fmt;

/// Unary operators
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum UnaryOperator {
Plus,
Minus,
Expand All @@ -31,7 +32,7 @@ impl fmt::Display for UnaryOperator {
}

/// Binary operators
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BinaryOperator {
Plus,
Minus,
Expand Down
Loading

0 comments on commit 5ba4a55

Please sign in to comment.