Skip to content

Support SELECT DISTINCT, and a few minor tweaks #49

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 5 commits into from
Apr 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
4 changes: 3 additions & 1 deletion src/sqlast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl ToString for SQLSetOperator {
/// to a set operation like `UNION`.
#[derive(Debug, Clone, PartialEq)]
pub struct SQLSelect {
pub distinct: bool,
/// projection expressions
pub projection: Vec<SQLSelectItem>,
/// FROM
Expand All @@ -127,7 +128,8 @@ pub struct SQLSelect {
impl ToString for SQLSelect {
fn to_string(&self) -> String {
let mut s = format!(
"SELECT {}",
"SELECT{} {}",
if self.distinct { " DISTINCT" } else { "" },
self.projection
.iter()
.map(|p| p.to_string())
Expand Down
2 changes: 1 addition & 1 deletion src/sqlast/sql_operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// SQL Operator
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum SQLOperator {
Plus,
Minus,
Expand Down
8 changes: 3 additions & 5 deletions src/sqlast/sqltype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ impl ToString for SQLType {
SQLType::Decimal(precision, scale) => {
if let Some(scale) = scale {
format!("numeric({},{})", precision.unwrap(), scale)
} else if let Some(precision) = precision {
format!("numeric({})", precision)
} else {
if let Some(precision) = precision {
format!("numeric({})", precision)
} else {
format!("numeric")
}
format!("numeric")
}
}
SQLType::SmallInt => "smallint".to_string(),
Expand Down
6 changes: 3 additions & 3 deletions src/sqlast/table_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{SQLIdent, SQLObjectName};

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum AlterOperation {
AddConstraint(TableKey),
RemoveConstraint { name: SQLIdent },
Expand All @@ -17,13 +17,13 @@ impl ToString for AlterOperation {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Key {
pub name: SQLIdent,
pub columns: Vec<SQLIdent>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum TableKey {
PrimaryKey(Key),
UniqueKey(Key),
Expand Down
16 changes: 8 additions & 8 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,14 @@ impl Parser {

pub fn parse_function(&mut self, id: SQLIdent) -> Result<ASTNode, ParserError> {
self.expect_token(&Token::LParen)?;
if self.consume_token(&Token::RParen) {
Ok(ASTNode::SQLFunction {
id: id,
args: vec![],
})
let args = if self.consume_token(&Token::RParen) {
vec![]
} else {
let args = self.parse_expr_list()?;
self.expect_token(&Token::RParen)?;
Ok(ASTNode::SQLFunction { id, args })
}
args
};
Ok(ASTNode::SQLFunction { id, args })
}

pub fn parse_case_expression(&mut self) -> Result<ASTNode, ParserError> {
Expand Down Expand Up @@ -328,7 +326,7 @@ impl Parser {
})
} else {
parser_err!(format!(
"Expected IN or LIKE after NOT, found {:?}",
"Expected BETWEEN, IN or LIKE after NOT, found {:?}",
self.peek_token()
))
}
Expand Down Expand Up @@ -1354,6 +1352,7 @@ impl Parser {
/// Parse a restricted `SELECT` statement (no CTEs / `UNION` / `ORDER BY`),
/// assuming the initial `SELECT` was already consumed
pub fn parse_select(&mut self) -> Result<SQLSelect, ParserError> {
let distinct = self.parse_keyword("DISTINCT");
let projection = self.parse_select_list()?;

let (relation, joins) = if self.parse_keyword("FROM") {
Expand Down Expand Up @@ -1383,6 +1382,7 @@ impl Parser {
};

Ok(SQLSelect {
distinct,
projection,
selection,
relation,
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,23 @@ fn parse_where_delete_statement() {
fn parse_simple_select() {
let sql = "SELECT id, fname, lname FROM customer WHERE id = 1 LIMIT 5";
let select = verified_only_select(sql);
assert_eq!(false, select.distinct);
assert_eq!(3, select.projection.len());
let select = verified_query(sql);
assert_eq!(Some(ASTNode::SQLValue(Value::Long(5))), select.limit);
}

#[test]
fn parse_select_distinct() {
let sql = "SELECT DISTINCT name FROM customer";
let select = verified_only_select(sql);
assert_eq!(true, select.distinct);
assert_eq!(
&SQLSelectItem::UnnamedExpression(ASTNode::SQLIdentifier("name".to_string())),
only(&select.projection)
);
}

#[test]
fn parse_select_wildcard() {
let sql = "SELECT * FROM foo";
Expand Down