Skip to content

Rename ASTNode to Expr #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sqlast/ddl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! AST types specific to CREATE/ALTER variants of `SQLStatement`
//! (commonly referred to as Data Definition Language, or DDL)
use super::{ASTNode, SQLIdent, SQLObjectName, SQLType};
use super::{Expr, SQLIdent, SQLObjectName, SQLType};

/// An `ALTER TABLE` (`SQLStatement::SQLAlterTable`) operation
#[derive(Debug, Clone, PartialEq, Hash)]
Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum TableConstraint {
/// `[ CONSTRAINT <name> ] CHECK (<expr>)`
Check {
name: Option<SQLIdent>,
expr: Box<ASTNode>,
expr: Box<Expr>,
},
}

Expand Down Expand Up @@ -145,7 +145,7 @@ pub enum ColumnOption {
/// `NOT NULL`
NotNull,
/// `DEFAULT <restricted-expr>`
Default(ASTNode),
Default(Expr),
/// `{ PRIMARY KEY | UNIQUE }`
Unique {
is_primary: bool,
Expand All @@ -157,7 +157,7 @@ pub enum ColumnOption {
referred_columns: Vec<SQLIdent>,
},
// `CHECK (<expr>)`
Check(ASTNode),
Check(Expr),
}

impl ToString for ColumnOption {
Expand Down
95 changes: 46 additions & 49 deletions src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub type SQLIdent = String;
/// (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, Hash)]
pub enum ASTNode {
pub enum Expr {
/// Identifier e.g. table name or column name
SQLIdentifier(SQLIdent),
/// Unqualified wildcard (`*`). SQL allows this in limited contexts, such as:
Expand All @@ -69,55 +69,52 @@ pub enum ASTNode {
/// Multi-part identifier, e.g. `table_alias.column` or `schema.table.col`
SQLCompoundIdentifier(Vec<SQLIdent>),
/// `IS NULL` expression
SQLIsNull(Box<ASTNode>),
SQLIsNull(Box<Expr>),
/// `IS NOT NULL` expression
SQLIsNotNull(Box<ASTNode>),
SQLIsNotNull(Box<Expr>),
/// `[ NOT ] IN (val1, val2, ...)`
SQLInList {
expr: Box<ASTNode>,
list: Vec<ASTNode>,
expr: Box<Expr>,
list: Vec<Expr>,
negated: bool,
},
/// `[ NOT ] IN (SELECT ...)`
SQLInSubquery {
expr: Box<ASTNode>,
expr: Box<Expr>,
subquery: Box<SQLQuery>,
negated: bool,
},
/// `<expr> [ NOT ] BETWEEN <low> AND <high>`
SQLBetween {
expr: Box<ASTNode>,
expr: Box<Expr>,
negated: bool,
low: Box<ASTNode>,
high: Box<ASTNode>,
low: Box<Expr>,
high: Box<Expr>,
},
/// Binary operation e.g. `1 + 1` or `foo > bar`
SQLBinaryOp {
left: Box<ASTNode>,
left: Box<Expr>,
op: SQLBinaryOperator,
right: Box<ASTNode>,
right: Box<Expr>,
},
/// Unary operation e.g. `NOT foo`
SQLUnaryOp {
op: SQLUnaryOperator,
expr: Box<ASTNode>,
expr: Box<Expr>,
},
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
SQLCast {
expr: Box<ASTNode>,
data_type: SQLType,
},
SQLCast { expr: Box<Expr>, data_type: SQLType },
SQLExtract {
field: SQLDateTimeField,
expr: Box<ASTNode>,
expr: Box<Expr>,
},
/// `expr COLLATE collation`
SQLCollate {
expr: Box<ASTNode>,
expr: Box<Expr>,
collation: SQLObjectName,
},
/// Nested expression e.g. `(foo > bar)` or `(1)`
SQLNested(Box<ASTNode>),
SQLNested(Box<Expr>),
/// SQLValue
SQLValue(Value),
/// Scalar function call e.g. `LEFT(foo, 5)`
Expand All @@ -128,10 +125,10 @@ pub enum ASTNode {
/// 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>
SQLCase {
operand: Option<Box<ASTNode>>,
conditions: Vec<ASTNode>,
results: Vec<ASTNode>,
else_result: Option<Box<ASTNode>>,
operand: Option<Box<Expr>>,
conditions: Vec<Expr>,
results: Vec<Expr>,
else_result: Option<Box<Expr>>,
},
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
/// `WHERE EXISTS (SELECT ...)`.
Expand All @@ -141,16 +138,16 @@ pub enum ASTNode {
SQLSubquery(Box<SQLQuery>),
}

impl ToString for ASTNode {
impl ToString for Expr {
fn to_string(&self) -> String {
match self {
ASTNode::SQLIdentifier(s) => s.to_string(),
ASTNode::SQLWildcard => "*".to_string(),
ASTNode::SQLQualifiedWildcard(q) => q.join(".") + ".*",
ASTNode::SQLCompoundIdentifier(s) => s.join("."),
ASTNode::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
ASTNode::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
ASTNode::SQLInList {
Expr::SQLIdentifier(s) => s.to_string(),
Expr::SQLWildcard => "*".to_string(),
Expr::SQLQualifiedWildcard(q) => q.join(".") + ".*",
Expr::SQLCompoundIdentifier(s) => s.join("."),
Expr::SQLIsNull(ast) => format!("{} IS NULL", ast.as_ref().to_string()),
Expr::SQLIsNotNull(ast) => format!("{} IS NOT NULL", ast.as_ref().to_string()),
Expr::SQLInList {
expr,
list,
negated,
Expand All @@ -160,7 +157,7 @@ impl ToString for ASTNode {
if *negated { "NOT " } else { "" },
comma_separated_string(list)
),
ASTNode::SQLInSubquery {
Expr::SQLInSubquery {
expr,
subquery,
negated,
Expand All @@ -170,7 +167,7 @@ impl ToString for ASTNode {
if *negated { "NOT " } else { "" },
subquery.to_string()
),
ASTNode::SQLBetween {
Expr::SQLBetween {
expr,
negated,
low,
Expand All @@ -182,32 +179,32 @@ impl ToString for ASTNode {
low.to_string(),
high.to_string()
),
ASTNode::SQLBinaryOp { left, op, right } => format!(
Expr::SQLBinaryOp { left, op, right } => format!(
"{} {} {}",
left.as_ref().to_string(),
op.to_string(),
right.as_ref().to_string()
),
ASTNode::SQLUnaryOp { op, expr } => {
Expr::SQLUnaryOp { op, expr } => {
format!("{} {}", op.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLCast { expr, data_type } => format!(
Expr::SQLCast { expr, data_type } => format!(
"CAST({} AS {})",
expr.as_ref().to_string(),
data_type.to_string()
),
ASTNode::SQLExtract { field, expr } => {
Expr::SQLExtract { field, expr } => {
format!("EXTRACT({} FROM {})", field.to_string(), expr.to_string())
}
ASTNode::SQLCollate { expr, collation } => format!(
Expr::SQLCollate { expr, collation } => format!(
"{} COLLATE {}",
expr.as_ref().to_string(),
collation.to_string()
),
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
ASTNode::SQLValue(v) => v.to_string(),
ASTNode::SQLFunction(f) => f.to_string(),
ASTNode::SQLCase {
Expr::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
Expr::SQLValue(v) => v.to_string(),
Expr::SQLFunction(f) => f.to_string(),
Expr::SQLCase {
operand,
conditions,
results,
Expand All @@ -228,16 +225,16 @@ impl ToString for ASTNode {
}
s + " END"
}
ASTNode::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
ASTNode::SQLSubquery(s) => format!("({})", s.to_string()),
Expr::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
Expr::SQLSubquery(s) => format!("({})", s.to_string()),
}
}
}

/// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`)
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SQLWindowSpec {
pub partition_by: Vec<ASTNode>,
pub partition_by: Vec<Expr>,
pub order_by: Vec<SQLOrderByExpr>,
pub window_frame: Option<SQLWindowFrame>,
}
Expand Down Expand Up @@ -374,14 +371,14 @@ pub enum SQLStatement {
/// Column assignments
assignments: Vec<SQLAssignment>,
/// WHERE
selection: Option<ASTNode>,
selection: Option<Expr>,
},
/// DELETE
SQLDelete {
/// FROM
table_name: SQLObjectName,
/// WHERE
selection: Option<ASTNode>,
selection: Option<Expr>,
},
/// CREATE VIEW
SQLCreateView {
Expand Down Expand Up @@ -604,7 +601,7 @@ impl ToString for SQLObjectName {
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SQLAssignment {
pub id: SQLIdent,
pub value: ASTNode,
pub value: Expr,
}

impl ToString for SQLAssignment {
Expand All @@ -617,7 +614,7 @@ impl ToString for SQLAssignment {
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SQLFunction {
pub name: SQLObjectName,
pub args: Vec<ASTNode>,
pub args: Vec<Expr>,
pub over: Option<SQLWindowSpec>,
// aggregate functions may specify eg `COUNT(DISTINCT x)`
pub distinct: bool,
Expand Down
30 changes: 15 additions & 15 deletions src/sqlast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub struct SQLQuery {
/// ORDER BY
pub order_by: Vec<SQLOrderByExpr>,
/// `LIMIT { <N> | ALL }`
pub limit: Option<ASTNode>,
pub limit: Option<Expr>,
/// `OFFSET <N> { ROW | ROWS }`
pub offset: Option<ASTNode>,
pub offset: Option<Expr>,
/// `FETCH { FIRST | NEXT } <N> [ PERCENT ] { ROW | ROWS } | { ONLY | WITH TIES }`
pub fetch: Option<Fetch>,
}
Expand Down Expand Up @@ -127,11 +127,11 @@ pub struct SQLSelect {
/// FROM
pub from: Vec<TableWithJoins>,
/// WHERE
pub selection: Option<ASTNode>,
pub selection: Option<Expr>,
/// GROUP BY
pub group_by: Vec<ASTNode>,
pub group_by: Vec<Expr>,
/// HAVING
pub having: Option<ASTNode>,
pub having: Option<Expr>,
}

impl ToString for SQLSelect {
Expand Down Expand Up @@ -177,9 +177,9 @@ impl ToString for Cte {
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum SQLSelectItem {
/// Any expression, not followed by `[ AS ] alias`
UnnamedExpression(ASTNode),
UnnamedExpr(Expr),
/// An expression, followed by `[ AS ] alias`
ExpressionWithAlias { expr: ASTNode, alias: SQLIdent },
ExprWithAlias { expr: Expr, alias: SQLIdent },
/// `alias.*` or even `schema.table.*`
QualifiedWildcard(SQLObjectName),
/// An unqualified `*`
Expand All @@ -189,8 +189,8 @@ pub enum SQLSelectItem {
impl ToString for SQLSelectItem {
fn to_string(&self) -> String {
match &self {
SQLSelectItem::UnnamedExpression(expr) => expr.to_string(),
SQLSelectItem::ExpressionWithAlias { expr, alias } => {
SQLSelectItem::UnnamedExpr(expr) => expr.to_string(),
SQLSelectItem::ExprWithAlias { expr, alias } => {
format!("{} AS {}", expr.to_string(), alias)
}
SQLSelectItem::QualifiedWildcard(prefix) => format!("{}.*", prefix.to_string()),
Expand Down Expand Up @@ -224,9 +224,9 @@ pub enum TableFactor {
/// Arguments of a table-valued function, as supported by Postgres
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
/// will also be parsed as `args`.
args: Vec<ASTNode>,
args: Vec<Expr>,
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
with_hints: Vec<ASTNode>,
with_hints: Vec<Expr>,
},
Derived {
lateral: bool,
Expand Down Expand Up @@ -361,15 +361,15 @@ pub enum JoinOperator {

#[derive(Debug, Clone, PartialEq, Hash)]
pub enum JoinConstraint {
On(ASTNode),
On(Expr),
Using(Vec<SQLIdent>),
Natural,
}

/// SQL ORDER BY expression
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SQLOrderByExpr {
pub expr: ASTNode,
pub expr: Expr,
pub asc: Option<bool>,
}

Expand All @@ -387,7 +387,7 @@ impl ToString for SQLOrderByExpr {
pub struct Fetch {
pub with_ties: bool,
pub percent: bool,
pub quantity: Option<ASTNode>,
pub quantity: Option<Expr>,
}

impl ToString for Fetch {
Expand All @@ -408,7 +408,7 @@ impl ToString for Fetch {
}

#[derive(Debug, Clone, PartialEq, Hash)]
pub struct SQLValues(pub Vec<Vec<ASTNode>>);
pub struct SQLValues(pub Vec<Vec<Expr>>);

impl ToString for SQLValues {
fn to_string(&self) -> String {
Expand Down
Loading