Skip to content

Improve consistency of binary/unary op nodes #112

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 2 commits into from
Jun 11, 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
27 changes: 13 additions & 14 deletions src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ pub use self::query::{
Cte, Fetch, Join, JoinConstraint, JoinOperator, SQLOrderByExpr, SQLQuery, SQLSelect,
SQLSelectItem, SQLSetExpr, SQLSetOperator, SQLValues, TableAlias, TableFactor, TableWithJoins,
};
pub use self::sql_operator::{SQLBinaryOperator, SQLUnaryOperator};
pub use self::sqltype::SQLType;
pub use self::value::{SQLDateTimeField, Value};

pub use self::sql_operator::SQLOperator;

/// Like `vec.join(", ")`, but for any types implementing ToString.
fn comma_separated_string<I>(iter: I) -> String
where
Expand Down Expand Up @@ -89,12 +88,17 @@ pub enum ASTNode {
low: Box<ASTNode>,
high: Box<ASTNode>,
},
/// Binary expression e.g. `1 + 1` or `foo > bar`
SQLBinaryExpr {
/// Binary operation e.g. `1 + 1` or `foo > bar`
SQLBinaryOp {
left: Box<ASTNode>,
op: SQLOperator,
op: SQLBinaryOperator,
right: Box<ASTNode>,
},
/// Unary operation e.g. `NOT foo`
SQLUnaryOp {
op: SQLUnaryOperator,
expr: Box<ASTNode>,
},
/// CAST an expression to a different data type e.g. `CAST(foo AS VARCHAR(123))`
SQLCast {
expr: Box<ASTNode>,
Expand All @@ -111,11 +115,6 @@ pub enum ASTNode {
},
/// Nested expression e.g. `(foo > bar)` or `(1)`
SQLNested(Box<ASTNode>),
/// Unary expression
SQLUnary {
operator: SQLOperator,
expr: Box<ASTNode>,
},
/// SQLValue
SQLValue(Value),
/// Scalar function call e.g. `LEFT(foo, 5)`
Expand Down Expand Up @@ -179,12 +178,15 @@ impl ToString for ASTNode {
low.to_string(),
high.to_string()
),
ASTNode::SQLBinaryExpr { left, op, right } => format!(
ASTNode::SQLBinaryOp { left, op, right } => format!(
"{} {} {}",
left.as_ref().to_string(),
op.to_string(),
right.as_ref().to_string()
),
ASTNode::SQLUnaryOp { op, expr } => {
format!("{} {}", op.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLCast { expr, data_type } => format!(
"CAST({} AS {})",
expr.as_ref().to_string(),
Expand All @@ -199,9 +201,6 @@ impl ToString for ASTNode {
collation.to_string()
),
ASTNode::SQLNested(ast) => format!("({})", ast.as_ref().to_string()),
ASTNode::SQLUnary { operator, expr } => {
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLValue(v) => v.to_string(),
ASTNode::SQLFunction(f) => f.to_string(),
ASTNode::SQLCase {
Expand Down
56 changes: 36 additions & 20 deletions src/sqlast/sql_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// SQL Operator
/// Unary operators
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum SQLOperator {
pub enum SQLUnaryOperator {
Plus,
Minus,
Not,
}

impl ToString for SQLUnaryOperator {
fn to_string(&self) -> String {
match self {
SQLUnaryOperator::Plus => "+".to_string(),
SQLUnaryOperator::Minus => "-".to_string(),
SQLUnaryOperator::Not => "NOT".to_string(),
}
}
}

/// Binary operators
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum SQLBinaryOperator {
Plus,
Minus,
Multiply,
Expand All @@ -26,30 +44,28 @@ pub enum SQLOperator {
NotEq,
And,
Or,
Not,
Like,
NotLike,
}

impl ToString for SQLOperator {
impl ToString for SQLBinaryOperator {
fn to_string(&self) -> String {
match self {
SQLOperator::Plus => "+".to_string(),
SQLOperator::Minus => "-".to_string(),
SQLOperator::Multiply => "*".to_string(),
SQLOperator::Divide => "/".to_string(),
SQLOperator::Modulus => "%".to_string(),
SQLOperator::Gt => ">".to_string(),
SQLOperator::Lt => "<".to_string(),
SQLOperator::GtEq => ">=".to_string(),
SQLOperator::LtEq => "<=".to_string(),
SQLOperator::Eq => "=".to_string(),
SQLOperator::NotEq => "<>".to_string(),
SQLOperator::And => "AND".to_string(),
SQLOperator::Or => "OR".to_string(),
SQLOperator::Not => "NOT".to_string(),
SQLOperator::Like => "LIKE".to_string(),
SQLOperator::NotLike => "NOT LIKE".to_string(),
SQLBinaryOperator::Plus => "+".to_string(),
SQLBinaryOperator::Minus => "-".to_string(),
SQLBinaryOperator::Multiply => "*".to_string(),
SQLBinaryOperator::Divide => "/".to_string(),
SQLBinaryOperator::Modulus => "%".to_string(),
SQLBinaryOperator::Gt => ">".to_string(),
SQLBinaryOperator::Lt => "<".to_string(),
SQLBinaryOperator::GtEq => ">=".to_string(),
SQLBinaryOperator::LtEq => "<=".to_string(),
SQLBinaryOperator::Eq => "=".to_string(),
SQLBinaryOperator::NotEq => "<>".to_string(),
SQLBinaryOperator::And => "AND".to_string(),
SQLBinaryOperator::Or => "OR".to_string(),
SQLBinaryOperator::Like => "LIKE".to_string(),
SQLBinaryOperator::NotLike => "NOT LIKE".to_string(),
}
}
}
46 changes: 23 additions & 23 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl Parser {
"EXISTS" => self.parse_exists_expression(),
"EXTRACT" => self.parse_extract_expression(),
"INTERVAL" => self.parse_literal_interval(),
"NOT" => Ok(ASTNode::SQLUnary {
operator: SQLOperator::Not,
"NOT" => Ok(ASTNode::SQLUnaryOp {
op: SQLUnaryOperator::Not,
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
}),
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
Expand Down Expand Up @@ -224,13 +224,13 @@ impl Parser {
}, // End of Token::SQLWord
Token::Mult => Ok(ASTNode::SQLWildcard),
tok @ Token::Minus | tok @ Token::Plus => {
let operator = if tok == Token::Plus {
SQLOperator::Plus
let op = if tok == Token::Plus {
SQLUnaryOperator::Plus
} else {
SQLOperator::Minus
SQLUnaryOperator::Minus
};
Ok(ASTNode::SQLUnary {
operator,
Ok(ASTNode::SQLUnaryOp {
op,
expr: Box::new(self.parse_subexpr(Self::PLUS_MINUS_PREC)?),
})
}
Expand Down Expand Up @@ -513,24 +513,24 @@ impl Parser {
let tok = self.next_token().unwrap(); // safe as EOF's precedence is the lowest

let regular_binary_operator = match tok {
Token::Eq => Some(SQLOperator::Eq),
Token::Neq => Some(SQLOperator::NotEq),
Token::Gt => Some(SQLOperator::Gt),
Token::GtEq => Some(SQLOperator::GtEq),
Token::Lt => Some(SQLOperator::Lt),
Token::LtEq => Some(SQLOperator::LtEq),
Token::Plus => Some(SQLOperator::Plus),
Token::Minus => Some(SQLOperator::Minus),
Token::Mult => Some(SQLOperator::Multiply),
Token::Mod => Some(SQLOperator::Modulus),
Token::Div => Some(SQLOperator::Divide),
Token::Eq => Some(SQLBinaryOperator::Eq),
Token::Neq => Some(SQLBinaryOperator::NotEq),
Token::Gt => Some(SQLBinaryOperator::Gt),
Token::GtEq => Some(SQLBinaryOperator::GtEq),
Token::Lt => Some(SQLBinaryOperator::Lt),
Token::LtEq => Some(SQLBinaryOperator::LtEq),
Token::Plus => Some(SQLBinaryOperator::Plus),
Token::Minus => Some(SQLBinaryOperator::Minus),
Token::Mult => Some(SQLBinaryOperator::Multiply),
Token::Mod => Some(SQLBinaryOperator::Modulus),
Token::Div => Some(SQLBinaryOperator::Divide),
Token::SQLWord(ref k) => match k.keyword.as_ref() {
"AND" => Some(SQLOperator::And),
"OR" => Some(SQLOperator::Or),
"LIKE" => Some(SQLOperator::Like),
"AND" => Some(SQLBinaryOperator::And),
"OR" => Some(SQLBinaryOperator::Or),
"LIKE" => Some(SQLBinaryOperator::Like),
"NOT" => {
if self.parse_keyword("LIKE") {
Some(SQLOperator::NotLike)
Some(SQLBinaryOperator::NotLike)
} else {
None
}
Expand All @@ -541,7 +541,7 @@ impl Parser {
};

if let Some(op) = regular_binary_operator {
Ok(ASTNode::SQLBinaryExpr {
Ok(ASTNode::SQLBinaryOp {
left: Box::new(expr),
op,
right: Box::new(self.parse_subexpr(precedence)?),
Expand Down
Loading