Skip to content

Commit 3989efe

Browse files
committed
add AttachedToken::empty
1 parent 903f24a commit 3989efe

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

src/ast/helpers/attached_token.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
1919
use core::fmt::{self, Debug, Formatter};
2020
use core::hash::{Hash, Hasher};
2121

22-
use crate::tokenizer::TokenWithLocation;
22+
use crate::tokenizer::{Token, TokenWithLocation};
2323

2424
#[cfg(feature = "serde")]
2525
use serde::{Deserialize, Serialize};
@@ -35,6 +35,12 @@ use sqlparser_derive::{Visit, VisitMut};
3535
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3636
pub struct AttachedToken(pub TokenWithLocation);
3737

38+
impl AttachedToken {
39+
pub fn empty() -> Self {
40+
AttachedToken(TokenWithLocation::wrap(Token::EOF))
41+
}
42+
}
43+
3844
// Conditional Implementations
3945
impl Debug for AttachedToken {
4046
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {

tests/sqlparser_clickhouse.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#[macro_use]
2222
mod test_utils;
2323

24+
use helpers::attached_token::AttachedToken;
2425
use sqlparser::tokenizer::{Span, Token, TokenWithLocation};
2526
use test_utils::*;
2627

@@ -40,7 +41,7 @@ fn parse_map_access_expr() {
4041
assert_eq!(
4142
Select {
4243
distinct: None,
43-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
44+
select_token: AttachedToken::empty(),
4445
top: None,
4546
top_before_distinct: false,
4647
projection: vec![UnnamedExpr(MapAccess {

tests/sqlparser_common.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
2626
extern crate core;
2727

28+
use helpers::attached_token::AttachedToken;
2829
use matches::assert_matches;
2930
use sqlparser::ast::SelectItem::UnnamedExpr;
3031
use sqlparser::ast::TableFactor::{Pivot, Unpivot};
@@ -378,8 +379,7 @@ fn parse_update_set_from() {
378379
subquery: Box::new(Query {
379380
with: None,
380381
body: Box::new(SetExpr::Select(Box::new(Select {
381-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT"))
382-
.into(),
382+
select_token: AttachedToken::empty().into(),
383383
distinct: None,
384384
top: None,
385385
top_before_distinct: false,
@@ -4661,7 +4661,7 @@ fn test_parse_named_window() {
46614661
ORDER BY C3";
46624662
let actual_select_only = verified_only_select(sql);
46634663
let expected = Select {
4664-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECTI")).into(),
4664+
select_token: AttachedToken::empty(),
46654665
distinct: None,
46664666
top: None,
46674667
top_before_distinct: false,
@@ -5317,7 +5317,7 @@ fn parse_interval_and_or_xor() {
53175317
let expected_ast = vec![Statement::Query(Box::new(Query {
53185318
with: None,
53195319
body: Box::new(SetExpr::Select(Box::new(Select {
5320-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
5320+
select_token: AttachedToken::empty(),
53215321
distinct: None,
53225322
top: None,
53235323
top_before_distinct: false,
@@ -6444,7 +6444,7 @@ fn parse_recursive_cte() {
64446444
query: Box::new(cte_query),
64456445
from: None,
64466446
materialized: None,
6447-
closing_paren_token: TokenWithLocation::wrap(Token::RParen).into(),
6447+
closing_paren_token: AttachedToken::empty(),
64486448
};
64496449
assert_eq!(with.cte_tables.first().unwrap(), &expected);
64506450
}
@@ -7407,7 +7407,7 @@ fn lateral_function() {
74077407
let sql = "SELECT * FROM customer LEFT JOIN LATERAL generate_series(1, customer.id)";
74087408
let actual_select_only = verified_only_select(sql);
74097409
let expected = Select {
7410-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
7410+
select_token: AttachedToken::empty(),
74117411
distinct: None,
74127412
top: None,
74137413
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions::default())],
@@ -8254,8 +8254,7 @@ fn parse_merge() {
82548254
subquery: Box::new(Query {
82558255
with: None,
82568256
body: Box::new(SetExpr::Select(Box::new(Select {
8257-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT"))
8258-
.into(),
8257+
select_token: AttachedToken::empty().into(),
82598258
distinct: None,
82608259
top: None,
82618260
top_before_distinct: false,
@@ -9894,7 +9893,7 @@ fn parse_unload() {
98949893
Statement::Unload {
98959894
query: Box::new(Query {
98969895
body: Box::new(SetExpr::Select(Box::new(Select {
9897-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
9896+
select_token: AttachedToken::empty(),
98989897
distinct: None,
98999898
top: None,
99009899
top_before_distinct: false,
@@ -10073,7 +10072,7 @@ fn parse_map_access_expr() {
1007310072
#[test]
1007410073
fn parse_connect_by() {
1007510074
let expect_query = Select {
10076-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
10075+
select_token: AttachedToken::empty(),
1007710076
distinct: None,
1007810077
top: None,
1007910078
top_before_distinct: false,
@@ -10161,7 +10160,7 @@ fn parse_connect_by() {
1016110160
assert_eq!(
1016210161
all_dialects_where(|d| d.supports_connect_by()).verified_only_select(connect_by_3),
1016310162
Select {
10164-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
10163+
select_token: AttachedToken::empty(),
1016510164
distinct: None,
1016610165
top: None,
1016710166
top_before_distinct: false,

tests/sqlparser_duckdb.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#[macro_use]
1919
mod test_utils;
2020

21+
use helpers::attached_token::AttachedToken;
2122
use sqlparser::tokenizer::{Span, Token, TokenWithLocation};
2223
use test_utils::*;
2324

@@ -260,7 +261,7 @@ fn test_select_union_by_name() {
260261
op: SetOperator::Union,
261262
set_quantifier: *expected_quantifier,
262263
left: Box::<SetExpr>::new(SetExpr::Select(Box::new(Select {
263-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
264+
select_token: AttachedToken::empty(),
264265
distinct: None,
265266
top: None,
266267
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions::default())],
@@ -297,7 +298,7 @@ fn test_select_union_by_name() {
297298
connect_by: None,
298299
}))),
299300
right: Box::<SetExpr>::new(SetExpr::Select(Box::new(Select {
300-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
301+
select_token: AttachedToken::empty(),
301302
distinct: None,
302303
top: None,
303304
projection: vec![SelectItem::Wildcard(WildcardAdditionalOptions::default())],

tests/sqlparser_mssql.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#[macro_use]
2323
mod test_utils;
2424

25+
use helpers::attached_token::AttachedToken;
2526
use sqlparser::tokenizer::{Span, Token, TokenWithLocation};
2627
use test_utils::*;
2728

@@ -113,7 +114,7 @@ fn parse_create_procedure() {
113114
settings: None,
114115
format_clause: None,
115116
body: Box::new(SetExpr::Select(Box::new(Select {
116-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
117+
select_token: AttachedToken::empty(),
117118
distinct: None,
118119
top: None,
119120
top_before_distinct: false,
@@ -525,7 +526,7 @@ fn parse_substring_in_select() {
525526
with: None,
526527

527528
body: Box::new(SetExpr::Select(Box::new(Select {
528-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
529+
select_token: AttachedToken::empty(),
529530
distinct: Some(Distinct::Distinct),
530531
top: None,
531532
top_before_distinct: false,

tests/sqlparser_mysql.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! Test SQL syntax specific to MySQL. The parser based on the generic dialect
2020
//! is also tested (on the inputs it can handle).
2121
22+
use helpers::attached_token::AttachedToken;
2223
use matches::assert_matches;
2324

2425
use sqlparser::ast::MysqlInsertPriority::{Delayed, HighPriority, LowPriority};
@@ -966,7 +967,7 @@ fn parse_escaped_quote_identifiers_with_escape() {
966967
Statement::Query(Box::new(Query {
967968
with: None,
968969
body: Box::new(SetExpr::Select(Box::new(Select {
969-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
970+
select_token: AttachedToken::empty(),
970971
distinct: None,
971972
top: None,
972973
top_before_distinct: false,
@@ -1019,8 +1020,7 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
10191020
Statement::Query(Box::new(Query {
10201021
with: None,
10211022
body: Box::new(SetExpr::Select(Box::new(Select {
1022-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1023-
1023+
select_token: AttachedToken::empty(),
10241024
distinct: None,
10251025
top: None,
10261026
top_before_distinct: false,
@@ -1066,7 +1066,7 @@ fn parse_escaped_backticks_with_escape() {
10661066
Statement::Query(Box::new(Query {
10671067
with: None,
10681068
body: Box::new(SetExpr::Select(Box::new(Select {
1069-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1069+
select_token: AttachedToken::empty(),
10701070

10711071
distinct: None,
10721072
top: None,
@@ -1117,7 +1117,7 @@ fn parse_escaped_backticks_with_no_escape() {
11171117
Statement::Query(Box::new(Query {
11181118
with: None,
11191119
body: Box::new(SetExpr::Select(Box::new(Select {
1120-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1120+
select_token: AttachedToken::empty(),
11211121

11221122
distinct: None,
11231123
top: None,
@@ -1765,7 +1765,7 @@ fn parse_select_with_numeric_prefix_column_name() {
17651765
assert_eq!(
17661766
q.body,
17671767
Box::new(SetExpr::Select(Box::new(Select {
1768-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1768+
select_token: AttachedToken::empty(),
17691769

17701770
distinct: None,
17711771
top: None,
@@ -1822,7 +1822,7 @@ fn parse_select_with_concatenation_of_exp_number_and_numeric_prefix_column() {
18221822
assert_eq!(
18231823
q.body,
18241824
Box::new(SetExpr::Select(Box::new(Select {
1825-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1825+
select_token: AttachedToken::empty(),
18261826

18271827
distinct: None,
18281828
top: None,
@@ -2334,7 +2334,7 @@ fn parse_substring_in_select() {
23342334
Box::new(Query {
23352335
with: None,
23362336
body: Box::new(SetExpr::Select(Box::new(Select {
2337-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
2337+
select_token: AttachedToken::empty(),
23382338
distinct: Some(Distinct::Distinct),
23392339
top: None,
23402340
top_before_distinct: false,
@@ -2659,7 +2659,7 @@ fn parse_hex_string_introducer() {
26592659
Statement::Query(Box::new(Query {
26602660
with: None,
26612661
body: Box::new(SetExpr::Select(Box::new(Select {
2662-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
2662+
select_token: AttachedToken::empty(),
26632663
distinct: None,
26642664
top: None,
26652665
top_before_distinct: false,

tests/sqlparser_postgres.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
#[macro_use]
2323
mod test_utils;
24+
use helpers::attached_token::AttachedToken;
2425
use sqlparser::tokenizer::{Span, Token, TokenWithLocation};
2526
use test_utils::*;
2627

@@ -1164,7 +1165,7 @@ fn parse_copy_to() {
11641165
source: CopySource::Query(Box::new(Query {
11651166
with: None,
11661167
body: Box::new(SetExpr::Select(Box::new(Select {
1167-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
1168+
select_token: AttachedToken::empty(),
11681169
distinct: None,
11691170
top: None,
11701171
top_before_distinct: false,
@@ -2520,7 +2521,7 @@ fn parse_array_subquery_expr() {
25202521
op: SetOperator::Union,
25212522
set_quantifier: SetQuantifier::None,
25222523
left: Box::new(SetExpr::Select(Box::new(Select {
2523-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
2524+
select_token: AttachedToken::empty(),
25242525
distinct: None,
25252526
top: None,
25262527
top_before_distinct: false,
@@ -2542,7 +2543,7 @@ fn parse_array_subquery_expr() {
25422543
connect_by: None,
25432544
}))),
25442545
right: Box::new(SetExpr::Select(Box::new(Select {
2545-
select_token: TokenWithLocation::wrap(Token::make_keyword("SELECT")).into(),
2546+
select_token: AttachedToken::empty(),
25462547
distinct: None,
25472548
top: None,
25482549
top_before_distinct: false,

0 commit comments

Comments
 (0)