Skip to content

Commit

Permalink
fix: Proper hashing for AST
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Nov 26, 2024
1 parent 6695866 commit 8b12ca9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 22 deletions.
38 changes: 35 additions & 3 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{

use crate::Literal;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(tag = "@type")]
pub enum Operation {
Add,
Expand All @@ -37,7 +37,7 @@ pub enum Operation {
NotLike,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(tag = "@type")]
pub enum RangeKind {
Between,
Expand All @@ -46,66 +46,78 @@ pub enum RangeKind {

#[derive(Debug, Serialize, Deserialize, Clone, Derivative)]
#[serde(tag = "@type")]
#[derivative(Eq, PartialEq)]
#[derivative(Eq, PartialEq, Hash)]
pub enum Expr {
#[serde(rename = "Expr::Select")]
Select {
query: SqlSelect,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Insert")]
Insert {
command: SqlInsert,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Update")]
Update {
command: SqlUpdate,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Delete")]
Delete {
command: SqlDelete,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Variable")]
Variable {
name: Identifier,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Grouping")]
Grouping {
expr: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Literal")]
Expand All @@ -114,9 +126,11 @@ pub enum Expr {
raw: String,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Function")]
Expand All @@ -126,9 +140,11 @@ pub enum Expr {
body: Arc<Vec<Stmt>>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Between")]
Expand All @@ -139,9 +155,11 @@ pub enum Expr {
kind: RangeKind,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Binary")]
Expand All @@ -151,9 +169,11 @@ pub enum Expr {
right: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Unary")]
Expand All @@ -162,9 +182,11 @@ pub enum Expr {
expr: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Assignment")]
Expand All @@ -173,9 +195,11 @@ pub enum Expr {
expr: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Logical")]
Expand All @@ -185,9 +209,11 @@ pub enum Expr {
right: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Call")]
Expand All @@ -196,9 +222,11 @@ pub enum Expr {
args: Vec<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Get")]
Expand All @@ -207,9 +235,11 @@ pub enum Expr {
name: Identifier,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
#[serde(rename = "Expr::Set")]
Expand All @@ -219,9 +249,11 @@ pub enum Expr {
value: Box<Expr>,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
span: Span,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
id: usize,
},
}
Expand Down
32 changes: 16 additions & 16 deletions lykiadb-lang/src/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::expr::Expr;

// Enums

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlDistinct {
#[serde(rename = "SqlDistinct::ImplicitAll")]
Expand All @@ -16,7 +16,7 @@ pub enum SqlDistinct {
Distinct,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlJoinType {
#[serde(rename = "SqlJoinType::Left")]
Expand All @@ -29,7 +29,7 @@ pub enum SqlJoinType {
Cross,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlCompoundOperator {
#[serde(rename = "SqlCompoundOperator::Union")]
Expand All @@ -42,7 +42,7 @@ pub enum SqlCompoundOperator {
Except,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlOrdering {
#[serde(rename = "SqlOrdering::Asc")]
Expand All @@ -52,15 +52,15 @@ pub enum SqlOrdering {
}

//
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlCollectionIdentifier {
pub namespace: Option<Identifier>,
pub name: Identifier,
pub alias: Option<Identifier>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlProjection {
#[serde(rename = "SqlProjection::All")]
Expand All @@ -72,28 +72,28 @@ pub enum SqlProjection {
},
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlLimitClause {
pub count: Box<Expr>,
pub offset: Option<Box<Expr>>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlOrderByClause {
pub expr: Box<Expr>,
pub ordering: SqlOrdering,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlSelectCompound {
pub operator: SqlCompoundOperator,
pub core: SqlSelectCore,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlFrom {
#[serde(rename = "SqlFrom::Group")]
Expand All @@ -114,7 +114,7 @@ pub enum SqlFrom {
},
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlSelectCore {
pub distinct: SqlDistinct,
Expand All @@ -126,15 +126,15 @@ pub struct SqlSelectCore {
pub compound: Option<Box<SqlSelectCompound>>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlSelect {
pub core: SqlSelectCore,
pub order_by: Option<Vec<SqlOrderByClause>>,
pub limit: Option<SqlLimitClause>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub enum SqlValues {
#[serde(rename = "SqlValues::Values")]
Expand All @@ -143,22 +143,22 @@ pub enum SqlValues {
Select(SqlSelect),
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlInsert {
pub collection: SqlCollectionIdentifier,
pub values: SqlValues,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlUpdate {
pub collection: SqlCollectionIdentifier,
pub assignments: Vec<Expr>,
pub r#where: Option<Box<Expr>>,
}

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone, Hash)]
#[serde(tag = "@type")]
pub struct SqlDelete {
pub collection: SqlCollectionIdentifier,
Expand Down
Loading

0 comments on commit 8b12ca9

Please sign in to comment.