Skip to content

Commit db19d83

Browse files
committed
locations: collection location of selection
1 parent dccebcb commit db19d83

11 files changed

+85
-75
lines changed

src/ast/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub struct Select {
212212
/// LATERAL VIEWs
213213
pub lateral_views: Vec<LateralView>,
214214
/// WHERE
215-
pub selection: Option<Expr>,
215+
pub selection: Option<WithSpan<Expr>>,
216216
/// GROUP BY
217217
pub group_by: Vec<Expr>,
218218
/// CLUSTER BY (Hive)

src/parser.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,7 +5470,10 @@ impl<'a> Parser<'a> {
54705470
}
54715471

54725472
let selection = if self.parse_keyword(Keyword::WHERE) {
5473-
Some(self.parse_expr()?)
5473+
let start_idx = self.index;
5474+
let expr = self.parse_expr()?;
5475+
5476+
Some(expr.spanning(self.span_from_index(start_idx)))
54745477
} else {
54755478
None
54765479
};
@@ -7322,7 +7325,7 @@ impl<'a> Parser<'a> {
73227325
}
73237326
let start_span = start_token.span;
73247327

7325-
let mut idx = self.index.max(start_index).min(self.tokens.len());
7328+
let mut idx = self.index.max(start_index).min(self.tokens.len()-1);
73267329
loop {
73277330
if idx <= start_index || idx >= self.tokens.len() {
73287331
break;
@@ -7336,6 +7339,13 @@ impl<'a> Parser<'a> {
73367339
idx -= 1;
73377340
continue;
73387341
}
7342+
TokenWithLocation {
7343+
token: Token::Comma,
7344+
span: _,
7345+
} => {
7346+
idx -= 1;
7347+
continue;
7348+
}
73397349
TokenWithLocation {
73407350
token: Token::Word(word),
73417351
span: _,

tests/sqlparser_bigquery.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn parse_like() {
217217
negated,
218218
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
219219
escape_char: None,
220-
},
220+
}.empty_span(),
221221
select.selection.unwrap()
222222
);
223223

@@ -233,7 +233,7 @@ fn parse_like() {
233233
negated,
234234
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
235235
escape_char: Some('\\'),
236-
},
236+
}.empty_span(),
237237
select.selection.unwrap()
238238
);
239239

@@ -250,7 +250,7 @@ fn parse_like() {
250250
negated,
251251
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
252252
escape_char: None,
253-
})),
253+
})).empty_span(),
254254
select.selection.unwrap()
255255
);
256256
}
@@ -272,7 +272,7 @@ fn parse_similar_to() {
272272
negated,
273273
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
274274
escape_char: None,
275-
},
275+
}.empty_span(),
276276
select.selection.unwrap()
277277
);
278278

@@ -290,7 +290,7 @@ fn parse_similar_to() {
290290
negated,
291291
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
292292
escape_char: Some('\\'),
293-
},
293+
}.empty_span(),
294294
select.selection.unwrap()
295295
);
296296

@@ -308,7 +308,7 @@ fn parse_similar_to() {
308308
negated,
309309
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
310310
escape_char: Some('\\'),
311-
})),
311+
})).empty_span(),
312312
select.selection.unwrap()
313313
);
314314
}

tests/sqlparser_clickhouse.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn parse_array_access_expr() {
101101
op: BinaryOperator::NotEq,
102102
right: Box::new(Expr::Value(Value::SingleQuotedString("foo".to_string())))
103103
})
104-
}),
104+
}.empty_span()),
105105
group_by: vec![],
106106
cluster_by: vec![],
107107
distribute_by: vec![],
@@ -239,7 +239,7 @@ fn parse_like() {
239239
negated,
240240
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
241241
escape_char: None,
242-
},
242+
}.empty_span(),
243243
select.selection.unwrap()
244244
);
245245

@@ -255,7 +255,7 @@ fn parse_like() {
255255
negated,
256256
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
257257
escape_char: Some('\\'),
258-
},
258+
}.empty_span(),
259259
select.selection.unwrap()
260260
);
261261

@@ -272,7 +272,7 @@ fn parse_like() {
272272
negated,
273273
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
274274
escape_char: None,
275-
})),
275+
})).empty_span(),
276276
select.selection.unwrap()
277277
);
278278
}
@@ -294,7 +294,7 @@ fn parse_similar_to() {
294294
negated,
295295
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
296296
escape_char: None,
297-
},
297+
}.empty_span(),
298298
select.selection.unwrap()
299299
);
300300

@@ -310,7 +310,7 @@ fn parse_similar_to() {
310310
negated,
311311
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
312312
escape_char: Some('\\'),
313-
},
313+
}.empty_span(),
314314
select.selection.unwrap()
315315
);
316316

@@ -326,7 +326,7 @@ fn parse_similar_to() {
326326
negated,
327327
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
328328
escape_char: Some('\\'),
329-
})),
329+
})).empty_span(),
330330
select.selection.unwrap()
331331
);
332332
}

tests/sqlparser_common.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ fn parse_escaped_single_quote_string_predicate() {
10321032
right: Box::new(Expr::Value(Value::SingleQuotedString(
10331033
"Jim's salary".to_string()
10341034
))),
1035-
}),
1035+
}.empty_span()),
10361036
ast.selection,
10371037
);
10381038
}
@@ -1275,7 +1275,7 @@ fn parse_ilike() {
12751275
negated,
12761276
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
12771277
escape_char: None,
1278-
},
1278+
}.empty_span(),
12791279
select.selection.unwrap()
12801280
);
12811281

@@ -1291,7 +1291,7 @@ fn parse_ilike() {
12911291
negated,
12921292
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
12931293
escape_char: Some('^'),
1294-
},
1294+
}.empty_span(),
12951295
select.selection.unwrap()
12961296
);
12971297

@@ -1308,7 +1308,7 @@ fn parse_ilike() {
13081308
negated,
13091309
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
13101310
escape_char: None,
1311-
})),
1311+
})).empty_span(),
13121312
select.selection.unwrap()
13131313
);
13141314
}
@@ -1332,7 +1332,7 @@ fn parse_in_list() {
13321332
Expr::Value(Value::SingleQuotedString("MED".to_string())),
13331333
],
13341334
negated,
1335-
},
1335+
}.empty_span(),
13361336
select.selection.unwrap()
13371337
);
13381338
}
@@ -1349,7 +1349,7 @@ fn parse_in_subquery() {
13491349
expr: Box::new(Expr::Identifier(Ident::new("segment").empty_span())),
13501350
subquery: Box::new(verified_query("SELECT segm FROM bar")),
13511351
negated: false,
1352-
},
1352+
}.empty_span(),
13531353
select.selection.unwrap()
13541354
);
13551355
}
@@ -1367,7 +1367,7 @@ fn parse_in_unnest() {
13671367
expr: Box::new(Expr::Identifier(Ident::new("segment").empty_span())),
13681368
array_expr: Box::new(verified_expr("expr")),
13691369
negated,
1370-
},
1370+
}.empty_span(),
13711371
select.selection.unwrap()
13721372
);
13731373
}
@@ -1516,7 +1516,7 @@ fn parse_between() {
15161516
low: Box::new(Expr::Value(number("25"))),
15171517
high: Box::new(Expr::Value(number("32"))),
15181518
negated,
1519-
},
1519+
}.empty_span(),
15201520
select.selection.unwrap()
15211521
);
15221522
}
@@ -1543,7 +1543,7 @@ fn parse_between_with_expr() {
15431543
right: Box::new(Expr::Value(number("4"))),
15441544
}),
15451545
negated: false,
1546-
})),
1546+
})).empty_span(),
15471547
select.selection.unwrap()
15481548
);
15491549

@@ -1567,7 +1567,7 @@ fn parse_between_with_expr() {
15671567
high: Box::new(Expr::Value(number("2"))),
15681568
negated: false,
15691569
}),
1570-
},
1570+
}.empty_span(),
15711571
select.selection.unwrap(),
15721572
)
15731573
}
@@ -3826,7 +3826,7 @@ fn parse_interval_and_or_xor() {
38263826
})),
38273827
}),
38283828
}),
3829-
}),
3829+
}.empty_span()),
38303830
group_by: vec![],
38313831
cluster_by: vec![],
38323832
distribute_by: vec![],
@@ -4968,7 +4968,7 @@ fn parse_exists_subquery() {
49684968
Expr::Exists {
49694969
negated: false,
49704970
subquery: Box::new(expected_inner.clone()),
4971-
},
4971+
}.empty_span(),
49724972
select.selection.unwrap(),
49734973
);
49744974

@@ -4978,7 +4978,7 @@ fn parse_exists_subquery() {
49784978
Expr::Exists {
49794979
negated: true,
49804980
subquery: Box::new(expected_inner),
4981-
},
4981+
}.empty_span(),
49824982
select.selection.unwrap(),
49834983
);
49844984

@@ -6391,7 +6391,7 @@ fn test_placeholder() {
63916391
left: Box::new(Expr::Identifier(Ident::new("id").empty_span())),
63926392
op: BinaryOperator::Eq,
63936393
right: Box::new(Expr::Value(Value::Placeholder("?".into()))),
6394-
})
6394+
}.empty_span())
63956395
);
63966396

63976397
let dialects = TestedDialects {
@@ -6416,7 +6416,7 @@ fn test_placeholder() {
64166416
left: Box::new(Expr::Identifier(Ident::new("id").empty_span())),
64176417
op: BinaryOperator::Eq,
64186418
right: Box::new(Expr::Value(Value::Placeholder("$Id1".into()))),
6419-
})
6419+
}.empty_span())
64206420
);
64216421

64226422
let sql = "SELECT * FROM student LIMIT $1 OFFSET $2";

tests/sqlparser_hive.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn parse_like() {
381381
negated,
382382
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
383383
escape_char: None,
384-
},
384+
}.empty_span(),
385385
select.selection.unwrap()
386386
);
387387

@@ -397,7 +397,7 @@ fn parse_like() {
397397
negated,
398398
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
399399
escape_char: Some('\\'),
400-
},
400+
}.empty_span(),
401401
select.selection.unwrap()
402402
);
403403

@@ -414,7 +414,7 @@ fn parse_like() {
414414
negated,
415415
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
416416
escape_char: None,
417-
})),
417+
})).empty_span(),
418418
select.selection.unwrap()
419419
);
420420
}
@@ -436,7 +436,7 @@ fn parse_similar_to() {
436436
negated,
437437
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
438438
escape_char: None,
439-
},
439+
}.empty_span(),
440440
select.selection.unwrap()
441441
);
442442

@@ -452,7 +452,7 @@ fn parse_similar_to() {
452452
negated,
453453
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
454454
escape_char: Some('\\'),
455-
},
455+
}.empty_span(),
456456
select.selection.unwrap()
457457
);
458458

@@ -468,7 +468,7 @@ fn parse_similar_to() {
468468
negated,
469469
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
470470
escape_char: Some('\\'),
471-
})),
471+
})).empty_span(),
472472
select.selection.unwrap()
473473
);
474474
}

0 commit comments

Comments
 (0)