Skip to content

Commit 19a7468

Browse files
authored
Track Parens<T>'s span (#2291)
1 parent 9a70c42 commit 19a7468

File tree

9 files changed

+120
-66
lines changed

9 files changed

+120
-66
lines changed

src/ast/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use helpers::{
3030
};
3131

3232
use core::cmp::Ordering;
33-
use core::ops::Deref;
33+
use core::ops::{Deref, DerefMut};
3434
use core::{
3535
fmt::{self, Display},
3636
hash,
@@ -200,6 +200,45 @@ fn format_statement_list(f: &mut fmt::Formatter, statements: &[Statement]) -> fm
200200
write!(f, ";")
201201
}
202202

203+
/// A item `T` enclosed in a pair of parentheses
204+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
205+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
206+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
207+
pub struct Parens<T> {
208+
/// the opening parenthesis token, i.e. `(`
209+
pub opening_token: AttachedToken,
210+
/// content enclosed in parentheses
211+
pub content: T,
212+
/// the closing parenthesis token, i.e. `)`
213+
pub closing_token: AttachedToken,
214+
}
215+
216+
impl<T> Parens<T> {
217+
/// Constructor wrapping `content` into `Parens` with an empty span;
218+
/// useful for testing purposes.
219+
pub fn with_empty_span(content: T) -> Self {
220+
Self {
221+
opening_token: AttachedToken::empty(),
222+
content,
223+
closing_token: AttachedToken::empty(),
224+
}
225+
}
226+
}
227+
228+
impl<T> Deref for Parens<T> {
229+
type Target = T;
230+
231+
fn deref(&self) -> &Self::Target {
232+
&self.content
233+
}
234+
}
235+
236+
impl<T> DerefMut for Parens<T> {
237+
fn deref_mut(&mut self) -> &mut Self::Target {
238+
&mut self.content
239+
}
240+
}
241+
203242
/// An identifier, decomposed into its value or character data and the quote style.
204243
#[derive(Debug, Clone)]
205244
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3634,7 +3634,7 @@ pub struct Values {
36343634
/// <https://dev.mysql.com/doc/refman/9.2/en/insert.html>
36353635
pub value_keyword: bool,
36363636
/// The list of rows, each row is a list of expressions.
3637-
pub rows: Vec<Vec<Expr>>,
3637+
pub rows: Vec<Parens<Vec<Expr>>>,
36383638
}
36393639

36403640
impl fmt::Display for Values {

src/ast/spans.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ use super::{
4141
MatchRecognizePattern, Measure, Merge, MergeAction, MergeClause, MergeInsertExpr,
4242
MergeInsertKind, MergeUpdateExpr, NamedParenthesizedList, NamedWindowDefinition, ObjectName,
4343
ObjectNamePart, Offset, OnConflict, OnConflictAction, OnInsert, OpenStatement, OrderBy,
44-
OrderByExpr, OrderByKind, OutputClause, Partition, PartitionBoundValue, PivotValueSource,
45-
ProjectionSelect, Query, RaiseStatement, RaiseStatementValue, ReferentialAction,
46-
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
47-
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
48-
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins, Update,
49-
UpdateTableFromKind, Use, Values, ViewColumnDef, WhileStatement, WildcardAdditionalOptions,
50-
With, WithFill,
44+
OrderByExpr, OrderByKind, OutputClause, Parens, Partition, PartitionBoundValue,
45+
PivotValueSource, ProjectionSelect, Query, RaiseStatement, RaiseStatementValue,
46+
ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select,
47+
SelectInto, SelectItem, SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias,
48+
TableAliasColumnDef, TableConstraint, TableFactor, TableObject, TableOptionsClustered,
49+
TableWithJoins, Update, UpdateTableFromKind, Use, Values, ViewColumnDef, WhileStatement,
50+
WildcardAdditionalOptions, With, WithFill,
5151
};
5252

5353
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -106,6 +106,12 @@ impl Spanned for TokenWithSpan {
106106
}
107107
}
108108

109+
impl<T> Spanned for Parens<T> {
110+
fn span(&self) -> Span {
111+
self.opening_token.0.span.union(&self.closing_token.0.span)
112+
}
113+
}
114+
109115
impl Spanned for Query {
110116
fn span(&self) -> Span {
111117
let Query {
@@ -239,10 +245,11 @@ impl Spanned for Values {
239245
rows,
240246
} = self;
241247

242-
union_spans(
243-
rows.iter()
244-
.map(|row| union_spans(row.iter().map(|expr| expr.span()))),
245-
)
248+
match &rows[..] {
249+
[] => Span::empty(),
250+
[f] => f.span(),
251+
[f, .., l] => f.span().union(&l.span()),
252+
}
246253
}
247254
}
248255

src/parser/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19028,16 +19028,15 @@ impl<'a> Parser<'a> {
1902819028
if parser.parse_keyword(Keyword::ROW) {
1902919029
explicit_row = true;
1903019030
}
19031-
19032-
parser.expect_token(&Token::LParen)?;
19033-
if allow_empty && parser.peek_token().token == Token::RParen {
19034-
parser.next_token();
19035-
Ok(vec![])
19036-
} else {
19037-
let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
19038-
parser.expect_token(&Token::RParen)?;
19039-
Ok(exprs)
19040-
}
19031+
Ok(Parens {
19032+
opening_token: parser.expect_token(&Token::LParen)?.into(),
19033+
content: if allow_empty && parser.peek_token_ref().token == Token::RParen {
19034+
vec![]
19035+
} else {
19036+
parser.parse_comma_separated(Parser::parse_expr)?
19037+
},
19038+
closing_token: parser.expect_token(&Token::RParen)?.into(),
19039+
})
1904119040
})?;
1904219041
Ok(Values {
1904319042
explicit_row,

tests/sqlparser_bigquery.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,10 @@ fn parse_merge() {
18211821
kind: MergeInsertKind::Values(Values {
18221822
value_keyword: false,
18231823
explicit_row: false,
1824-
rows: vec![vec![Expr::value(number("1")), Expr::value(number("2"))]],
1824+
rows: vec![Parens::with_empty_span(vec![
1825+
Expr::value(number("1")),
1826+
Expr::value(number("2")),
1827+
])],
18251828
}),
18261829
insert_predicate: None,
18271830
});
@@ -2003,10 +2006,10 @@ fn parse_merge() {
20032006
kind: MergeInsertKind::Values(Values {
20042007
value_keyword: false,
20052008
explicit_row: false,
2006-
rows: vec![vec![
2009+
rows: vec![Parens::with_empty_span(vec![
20072010
Expr::value(number("1")),
20082011
Expr::Identifier(Ident::new("DEFAULT")),
2009-
]]
2012+
])]
20102013
}),
20112014
insert_predicate: None,
20122015
})
@@ -2022,10 +2025,10 @@ fn parse_merge() {
20222025
kind: MergeInsertKind::Values(Values {
20232026
value_keyword: false,
20242027
explicit_row: false,
2025-
rows: vec![vec![
2028+
rows: vec![Parens::with_empty_span(vec![
20262029
Expr::value(number("1")),
20272030
Expr::Identifier(Ident::new("DEFAULT")),
2028-
]]
2031+
])]
20292032
}),
20302033
insert_predicate: None,
20312034
})

tests/sqlparser_common.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ fn parse_insert_values() {
100100
Expr::value(number("2")),
101101
Expr::value(number("3")),
102102
];
103-
let rows1 = vec![row.clone()];
104-
let rows2 = vec![row.clone(), row];
103+
let rows1 = vec![Parens::with_empty_span(row.clone())];
104+
let rows2 = vec![
105+
Parens::with_empty_span(row.clone()),
106+
Parens::with_empty_span(row),
107+
];
105108

106109
let sql = "INSERT customer VALUES (1, 2, 3)";
107110
check_one(sql, "customer", &[], &rows1, false);
@@ -140,7 +143,7 @@ fn parse_insert_values() {
140143
sql: &str,
141144
expected_table_name: &str,
142145
expected_columns: &[String],
143-
expected_rows: &[Vec<Expr>],
146+
expected_rows: &[Parens<Vec<Expr>>],
144147
expected_value_keyword: bool,
145148
) {
146149
match verified_stmt(sql) {
@@ -10101,7 +10104,7 @@ fn parse_merge() {
1010110104
kind: MergeInsertKind::Values(Values {
1010210105
value_keyword: false,
1010310106
explicit_row: false,
10104-
rows: vec![vec![
10107+
rows: vec![Parens::with_empty_span(vec![
1010510108
Expr::CompoundIdentifier(vec![
1010610109
Ident::new("stg"),
1010710110
Ident::new("A")
@@ -10114,7 +10117,7 @@ fn parse_merge() {
1011410117
Ident::new("stg"),
1011510118
Ident::new("C")
1011610119
]),
10117-
]]
10120+
])]
1011810121
}),
1011910122
insert_predicate: None,
1012010123
}),

tests/sqlparser_databricks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ fn test_values_clause() {
174174
value_keyword: false,
175175
explicit_row: false,
176176
rows: vec![
177-
vec![
177+
Parens::with_empty_span(vec![
178178
Expr::Value((Value::DoubleQuotedString("one".to_owned())).with_empty_span()),
179179
Expr::value(number("1")),
180-
],
181-
vec![
180+
]),
181+
Parens::with_empty_span(vec![
182182
Expr::Value((Value::SingleQuotedString("two".to_owned())).with_empty_span()),
183183
Expr::value(number("2")),
184-
],
184+
]),
185185
],
186186
};
187187

tests/sqlparser_mysql.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,27 +1940,27 @@ fn parse_simple_insert() {
19401940
value_keyword: false,
19411941
explicit_row: false,
19421942
rows: vec![
1943-
vec![
1943+
Parens::with_empty_span(vec![
19441944
Expr::Value(
19451945
(Value::SingleQuotedString("Test Some Inserts".to_string()))
19461946
.with_empty_span()
19471947
),
19481948
Expr::value(number("1"))
1949-
],
1950-
vec![
1949+
]),
1950+
Parens::with_empty_span(vec![
19511951
Expr::Value(
19521952
(Value::SingleQuotedString("Test Entry 2".to_string()))
19531953
.with_empty_span()
19541954
),
19551955
Expr::value(number("2"))
1956-
],
1957-
vec![
1956+
]),
1957+
Parens::with_empty_span(vec![
19581958
Expr::Value(
19591959
(Value::SingleQuotedString("Test Entry 3".to_string()))
19601960
.with_empty_span()
19611961
),
19621962
Expr::value(number("3"))
1963-
]
1963+
])
19641964
]
19651965
})),
19661966
order_by: None,
@@ -2011,13 +2011,13 @@ fn parse_ignore_insert() {
20112011
body: Box::new(SetExpr::Values(Values {
20122012
value_keyword: false,
20132013
explicit_row: false,
2014-
rows: vec![vec![
2014+
rows: vec![Parens::with_empty_span(vec![
20152015
Expr::Value(
20162016
(Value::SingleQuotedString("Test Some Inserts".to_string()))
20172017
.with_empty_span()
20182018
),
20192019
Expr::value(number("1"))
2020-
]]
2020+
])]
20212021
})),
20222022
order_by: None,
20232023
limit_clause: None,
@@ -2067,13 +2067,13 @@ fn parse_priority_insert() {
20672067
body: Box::new(SetExpr::Values(Values {
20682068
value_keyword: false,
20692069
explicit_row: false,
2070-
rows: vec![vec![
2070+
rows: vec![Parens::with_empty_span(vec![
20712071
Expr::Value(
20722072
(Value::SingleQuotedString("Test Some Inserts".to_string()))
20732073
.with_empty_span()
20742074
),
20752075
Expr::value(number("1"))
2076-
]]
2076+
])]
20772077
})),
20782078
order_by: None,
20792079
limit_clause: None,
@@ -2120,13 +2120,13 @@ fn parse_priority_insert() {
21202120
body: Box::new(SetExpr::Values(Values {
21212121
value_keyword: false,
21222122
explicit_row: false,
2123-
rows: vec![vec![
2123+
rows: vec![Parens::with_empty_span(vec![
21242124
Expr::Value(
21252125
(Value::SingleQuotedString("Test Some Inserts".to_string()))
21262126
.with_empty_span()
21272127
),
21282128
Expr::value(number("1"))
2129-
]]
2129+
])]
21302130
})),
21312131
order_by: None,
21322132
limit_clause: None,
@@ -2176,9 +2176,9 @@ fn parse_insert_as() {
21762176
body: Box::new(SetExpr::Values(Values {
21772177
value_keyword: false,
21782178
explicit_row: false,
2179-
rows: vec![vec![Expr::Value(
2179+
rows: vec![Parens::with_empty_span(vec![Expr::Value(
21802180
(Value::SingleQuotedString("2024-01-01".to_string())).with_empty_span()
2181-
)]]
2181+
)])]
21822182
})),
21832183
order_by: None,
21842184
limit_clause: None,
@@ -2239,13 +2239,13 @@ fn parse_insert_as() {
22392239
body: Box::new(SetExpr::Values(Values {
22402240
value_keyword: false,
22412241
explicit_row: false,
2242-
rows: vec![vec![
2242+
rows: vec![Parens::with_empty_span(vec![
22432243
Expr::value(number("1")),
22442244
Expr::Value(
22452245
(Value::SingleQuotedString("2024-01-01".to_string()))
22462246
.with_empty_span()
22472247
)
2248-
]]
2248+
])]
22492249
})),
22502250
order_by: None,
22512251
limit_clause: None,
@@ -2296,13 +2296,13 @@ fn parse_replace_insert() {
22962296
body: Box::new(SetExpr::Values(Values {
22972297
value_keyword: false,
22982298
explicit_row: false,
2299-
rows: vec![vec![
2299+
rows: vec![Parens::with_empty_span(vec![
23002300
Expr::Value(
23012301
(Value::SingleQuotedString("Test Some Inserts".to_string()))
23022302
.with_empty_span()
23032303
),
23042304
Expr::value(number("1"))
2305-
]]
2305+
])]
23062306
})),
23072307
order_by: None,
23082308
limit_clause: None,
@@ -2344,7 +2344,10 @@ fn parse_empty_row_insert() {
23442344
body: Box::new(SetExpr::Values(Values {
23452345
value_keyword: false,
23462346
explicit_row: false,
2347-
rows: vec![vec![], vec![]]
2347+
rows: vec![
2348+
Parens::with_empty_span(vec![]),
2349+
Parens::with_empty_span(vec![])
2350+
]
23482351
})),
23492352
order_by: None,
23502353
limit_clause: None,
@@ -2395,7 +2398,7 @@ fn parse_insert_with_on_duplicate_update() {
23952398
body: Box::new(SetExpr::Values(Values {
23962399
value_keyword: false,
23972400
explicit_row: false,
2398-
rows: vec![vec![
2401+
rows: vec![Parens::with_empty_span(vec![
23992402
Expr::Value(
24002403
(Value::SingleQuotedString("accounting_manager".to_string()))
24012404
.with_empty_span()
@@ -2410,7 +2413,7 @@ fn parse_insert_with_on_duplicate_update() {
24102413
Expr::Value((Value::Boolean(true)).with_empty_span()),
24112414
Expr::Value((Value::Boolean(true)).with_empty_span()),
24122415
Expr::Value((Value::Boolean(true)).with_empty_span()),
2413-
]]
2416+
])]
24142417
})),
24152418
order_by: None,
24162419
limit_clause: None,

0 commit comments

Comments
 (0)