Skip to content

Commit 80f594e

Browse files
authored
Merge pull request #73 from benesch/option-vec
Replace Option<Vec<T>> with Vec<T>
2 parents bc1ec47 + 5652b46 commit 80f594e

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

src/sqlast/query.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct SQLQuery {
99
/// SELECT or UNION / EXCEPT / INTECEPT
1010
pub body: SQLSetExpr,
1111
/// ORDER BY
12-
pub order_by: Option<Vec<SQLOrderByExpr>>,
12+
pub order_by: Vec<SQLOrderByExpr>,
1313
/// LIMIT
1414
pub limit: Option<ASTNode>,
1515
}
@@ -21,8 +21,8 @@ impl ToString for SQLQuery {
2121
s += &format!("WITH {} ", comma_separated_string(&self.ctes))
2222
}
2323
s += &self.body.to_string();
24-
if let Some(ref order_by) = self.order_by {
25-
s += &format!(" ORDER BY {}", comma_separated_string(order_by));
24+
if !self.order_by.is_empty() {
25+
s += &format!(" ORDER BY {}", comma_separated_string(&self.order_by));
2626
}
2727
if let Some(ref limit) = self.limit {
2828
s += &format!(" LIMIT {}", limit.to_string());
@@ -106,7 +106,7 @@ pub struct SQLSelect {
106106
/// WHERE
107107
pub selection: Option<ASTNode>,
108108
/// GROUP BY
109-
pub group_by: Option<Vec<ASTNode>>,
109+
pub group_by: Vec<ASTNode>,
110110
/// HAVING
111111
pub having: Option<ASTNode>,
112112
}
@@ -127,8 +127,8 @@ impl ToString for SQLSelect {
127127
if let Some(ref selection) = self.selection {
128128
s += &format!(" WHERE {}", selection.to_string());
129129
}
130-
if let Some(ref group_by) = self.group_by {
131-
s += &format!(" GROUP BY {}", comma_separated_string(group_by));
130+
if !self.group_by.is_empty() {
131+
s += &format!(" GROUP BY {}", comma_separated_string(&self.group_by));
132132
}
133133
if let Some(ref having) = self.having {
134134
s += &format!(" HAVING {}", having.to_string());
@@ -193,7 +193,7 @@ pub enum TableFactor {
193193
/// Arguments of a table-valued function, as supported by Postgres
194194
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
195195
/// will also be parsed as `args`.
196-
args: Option<Vec<ASTNode>>,
196+
args: Vec<ASTNode>,
197197
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
198198
with_hints: Vec<ASTNode>,
199199
},
@@ -213,7 +213,7 @@ impl ToString for TableFactor {
213213
with_hints,
214214
} => {
215215
let mut s = name.to_string();
216-
if let Some(args) = args {
216+
if !args.is_empty() {
217217
s += &format!("({})", comma_separated_string(args))
218218
};
219219
if let Some(alias) = alias {

src/sqlparser.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,9 @@ impl Parser {
12141214
let body = self.parse_query_body(0)?;
12151215

12161216
let order_by = if self.parse_keywords(vec!["ORDER", "BY"]) {
1217-
Some(self.parse_order_by_expr_list()?)
1217+
self.parse_order_by_expr_list()?
12181218
} else {
1219-
None
1219+
vec![]
12201220
};
12211221

12221222
let limit = if self.parse_keyword("LIMIT") {
@@ -1334,9 +1334,9 @@ impl Parser {
13341334
};
13351335

13361336
let group_by = if self.parse_keywords(vec!["GROUP", "BY"]) {
1337-
Some(self.parse_expr_list()?)
1337+
self.parse_expr_list()?
13381338
} else {
1339-
None
1339+
vec![]
13401340
};
13411341

13421342
let having = if self.parse_keyword("HAVING") {
@@ -1367,9 +1367,9 @@ impl Parser {
13671367
let name = self.parse_object_name()?;
13681368
// Postgres, MSSQL: table-valued functions:
13691369
let args = if self.consume_token(&Token::LParen) {
1370-
Some(self.parse_optional_args()?)
1370+
self.parse_optional_args()?
13711371
} else {
1372-
None
1372+
vec![]
13731373
};
13741374
let alias = self.parse_optional_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
13751375
// MSSQL-specific table hints:

tests/sqlparser_common.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ fn parse_select_order_by() {
455455
fn chk(sql: &str) {
456456
let select = verified_query(sql);
457457
assert_eq!(
458-
Some(vec![
458+
vec![
459459
SQLOrderByExpr {
460460
expr: ASTNode::SQLIdentifier("lname".to_string()),
461461
asc: Some(true),
@@ -468,7 +468,7 @@ fn parse_select_order_by() {
468468
expr: ASTNode::SQLIdentifier("id".to_string()),
469469
asc: None,
470470
},
471-
]),
471+
],
472472
select.order_by
473473
);
474474
}
@@ -484,7 +484,7 @@ fn parse_select_order_by_limit() {
484484
ORDER BY lname ASC, fname DESC LIMIT 2";
485485
let select = verified_query(sql);
486486
assert_eq!(
487-
Some(vec![
487+
vec![
488488
SQLOrderByExpr {
489489
expr: ASTNode::SQLIdentifier("lname".to_string()),
490490
asc: Some(true),
@@ -493,7 +493,7 @@ fn parse_select_order_by_limit() {
493493
expr: ASTNode::SQLIdentifier("fname".to_string()),
494494
asc: Some(false),
495495
},
496-
]),
496+
],
497497
select.order_by
498498
);
499499
assert_eq!(Some(ASTNode::SQLValue(Value::Long(2))), select.limit);
@@ -504,10 +504,10 @@ fn parse_select_group_by() {
504504
let sql = "SELECT id, fname, lname FROM customer GROUP BY lname, fname";
505505
let select = verified_only_select(sql);
506506
assert_eq!(
507-
Some(vec![
507+
vec![
508508
ASTNode::SQLIdentifier("lname".to_string()),
509509
ASTNode::SQLIdentifier("fname".to_string()),
510-
]),
510+
],
511511
select.group_by
512512
);
513513
}
@@ -746,7 +746,7 @@ fn parse_delimited_identifiers() {
746746
} => {
747747
assert_eq!(vec![r#""a table""#.to_string()], name.0);
748748
assert_eq!(r#""alias""#, alias.unwrap());
749-
assert!(args.is_none());
749+
assert!(args.is_empty());
750750
assert!(with_hints.is_empty());
751751
}
752752
_ => panic!("Expecting TableFactor::Table"),
@@ -870,7 +870,7 @@ fn parse_implicit_join() {
870870
relation: TableFactor::Table {
871871
name: SQLObjectName(vec!["t2".to_string()]),
872872
alias: None,
873-
args: None,
873+
args: vec![],
874874
with_hints: vec![],
875875
},
876876
join_operator: JoinOperator::Implicit
@@ -888,7 +888,7 @@ fn parse_cross_join() {
888888
relation: TableFactor::Table {
889889
name: SQLObjectName(vec!["t2".to_string()]),
890890
alias: None,
891-
args: None,
891+
args: vec![],
892892
with_hints: vec![],
893893
},
894894
join_operator: JoinOperator::Cross
@@ -908,7 +908,7 @@ fn parse_joins_on() {
908908
relation: TableFactor::Table {
909909
name: SQLObjectName(vec![relation.into()]),
910910
alias,
911-
args: None,
911+
args: vec![],
912912
with_hints: vec![],
913913
},
914914
join_operator: f(JoinConstraint::On(ASTNode::SQLBinaryExpr {
@@ -961,7 +961,7 @@ fn parse_joins_using() {
961961
relation: TableFactor::Table {
962962
name: SQLObjectName(vec![relation.into()]),
963963
alias,
964-
args: None,
964+
args: vec![],
965965
with_hints: vec![],
966966
},
967967
join_operator: f(JoinConstraint::Using(vec!["c1".into()])),

0 commit comments

Comments
 (0)