Skip to content

Commit 6ebd5dd

Browse files
authored
Merge pull request #49 from nickolay/select-distinct
Support SELECT DISTINCT, and a few minor tweaks
2 parents d9591cd + bbf1805 commit 6ebd5dd

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

src/sqlast/query.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl ToString for SQLSetOperator {
110110
/// to a set operation like `UNION`.
111111
#[derive(Debug, Clone, PartialEq)]
112112
pub struct SQLSelect {
113+
pub distinct: bool,
113114
/// projection expressions
114115
pub projection: Vec<SQLSelectItem>,
115116
/// FROM
@@ -127,7 +128,8 @@ pub struct SQLSelect {
127128
impl ToString for SQLSelect {
128129
fn to_string(&self) -> String {
129130
let mut s = format!(
130-
"SELECT {}",
131+
"SELECT{} {}",
132+
if self.distinct { " DISTINCT" } else { "" },
131133
self.projection
132134
.iter()
133135
.map(|p| p.to_string())

src/sqlast/sql_operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// SQL Operator
2-
#[derive(Debug, PartialEq, Clone)]
2+
#[derive(Debug, Clone, PartialEq)]
33
pub enum SQLOperator {
44
Plus,
55
Minus,

src/sqlast/sqltype.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ impl ToString for SQLType {
7676
SQLType::Decimal(precision, scale) => {
7777
if let Some(scale) = scale {
7878
format!("numeric({},{})", precision.unwrap(), scale)
79+
} else if let Some(precision) = precision {
80+
format!("numeric({})", precision)
7981
} else {
80-
if let Some(precision) = precision {
81-
format!("numeric({})", precision)
82-
} else {
83-
format!("numeric")
84-
}
82+
format!("numeric")
8583
}
8684
}
8785
SQLType::SmallInt => "smallint".to_string(),

src/sqlast/table_key.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{SQLIdent, SQLObjectName};
22

3-
#[derive(Debug, PartialEq, Clone)]
3+
#[derive(Debug, Clone, PartialEq)]
44
pub enum AlterOperation {
55
AddConstraint(TableKey),
66
RemoveConstraint { name: SQLIdent },
@@ -17,13 +17,13 @@ impl ToString for AlterOperation {
1717
}
1818
}
1919

20-
#[derive(Debug, PartialEq, Clone)]
20+
#[derive(Debug, Clone, PartialEq)]
2121
pub struct Key {
2222
pub name: SQLIdent,
2323
pub columns: Vec<SQLIdent>,
2424
}
2525

26-
#[derive(Debug, PartialEq, Clone)]
26+
#[derive(Debug, Clone, PartialEq)]
2727
pub enum TableKey {
2828
PrimaryKey(Key),
2929
UniqueKey(Key),

src/sqlparser.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,14 @@ impl Parser {
239239

240240
pub fn parse_function(&mut self, id: SQLIdent) -> Result<ASTNode, ParserError> {
241241
self.expect_token(&Token::LParen)?;
242-
if self.consume_token(&Token::RParen) {
243-
Ok(ASTNode::SQLFunction {
244-
id: id,
245-
args: vec![],
246-
})
242+
let args = if self.consume_token(&Token::RParen) {
243+
vec![]
247244
} else {
248245
let args = self.parse_expr_list()?;
249246
self.expect_token(&Token::RParen)?;
250-
Ok(ASTNode::SQLFunction { id, args })
251-
}
247+
args
248+
};
249+
Ok(ASTNode::SQLFunction { id, args })
252250
}
253251

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

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

13851384
Ok(SQLSelect {
1385+
distinct,
13861386
projection,
13871387
selection,
13881388
relation,

tests/sqlparser_generic.rs

+12
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,23 @@ fn parse_where_delete_statement() {
5050
fn parse_simple_select() {
5151
let sql = "SELECT id, fname, lname FROM customer WHERE id = 1 LIMIT 5";
5252
let select = verified_only_select(sql);
53+
assert_eq!(false, select.distinct);
5354
assert_eq!(3, select.projection.len());
5455
let select = verified_query(sql);
5556
assert_eq!(Some(ASTNode::SQLValue(Value::Long(5))), select.limit);
5657
}
5758

59+
#[test]
60+
fn parse_select_distinct() {
61+
let sql = "SELECT DISTINCT name FROM customer";
62+
let select = verified_only_select(sql);
63+
assert_eq!(true, select.distinct);
64+
assert_eq!(
65+
&SQLSelectItem::UnnamedExpression(ASTNode::SQLIdentifier("name".to_string())),
66+
only(&select.projection)
67+
);
68+
}
69+
5870
#[test]
5971
fn parse_select_wildcard() {
6072
let sql = "SELECT * FROM foo";

0 commit comments

Comments
 (0)