Skip to content

Commit daeef00

Browse files
committed
locations: collection location of selection
1 parent fc8e44a commit daeef00

7 files changed

+47
-44
lines changed

src/ast/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub struct Select {
293293
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/select/prewhere)
294294
pub prewhere: Option<Expr>,
295295
/// WHERE
296-
pub selection: Option<Expr>,
296+
pub selection: Option<WithSpan<Expr>>,
297297
/// GROUP BY
298298
pub group_by: GroupByExpr,
299299
/// CLUSTER BY (Hive)

src/parser/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9284,7 +9284,10 @@ impl<'a> Parser<'a> {
92849284
};
92859285

92869286
let selection = if self.parse_keyword(Keyword::WHERE) {
9287-
Some(self.parse_expr()?)
9287+
let start_idx = self.index;
9288+
let expr = self.parse_expr()?;
9289+
9290+
Some(expr.spanning(self.span_from_index(start_idx)))
92889291
} else {
92899292
None
92909293
};

tests/sqlparser_clickhouse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn parse_map_access_expr() {
9999
op: BinaryOperator::NotEq,
100100
right: Box::new(Expr::Value(Value::SingleQuotedString("foo".to_string()))),
101101
}),
102-
}),
102+
}.empty_span()),
103103
group_by: GroupByExpr::Expressions(vec![], vec![]),
104104
cluster_by: vec![],
105105
distribute_by: vec![],
@@ -1324,7 +1324,7 @@ fn test_prewhere() {
13241324
left: Box::new(Identifier(Ident::new("y").empty_span())),
13251325
op: BinaryOperator::Eq,
13261326
right: Box::new(Expr::Value(Value::Number("2".parse().unwrap(), false))),
1327-
})
1327+
}.empty_span())
13281328
);
13291329
}
13301330
_ => unreachable!(),

tests/sqlparser_common.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ fn parse_escaped_single_quote_string_predicate_with_escape() {
13291329
right: Box::new(Expr::Value(Value::SingleQuotedString(
13301330
"Jim's salary".to_string()
13311331
))),
1332-
}),
1332+
}.empty_span()),
13331333
ast.selection,
13341334
);
13351335
}
@@ -1355,7 +1355,7 @@ fn parse_escaped_single_quote_string_predicate_with_no_escape() {
13551355
right: Box::new(Expr::Value(Value::SingleQuotedString(
13561356
"Jim''s salary".to_string()
13571357
))),
1358-
}),
1358+
}.empty_span()),
13591359
ast.selection,
13601360
);
13611361
}
@@ -1698,7 +1698,7 @@ fn parse_ilike() {
16981698
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
16991699
escape_char: None,
17001700
any: false,
1701-
},
1701+
}.empty_span(),
17021702
select.selection.unwrap()
17031703
);
17041704

@@ -1715,7 +1715,7 @@ fn parse_ilike() {
17151715
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
17161716
escape_char: Some('^'.to_string()),
17171717
any: false,
1718-
},
1718+
}.empty_span(),
17191719
select.selection.unwrap()
17201720
);
17211721

@@ -1733,7 +1733,7 @@ fn parse_ilike() {
17331733
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
17341734
escape_char: None,
17351735
any: false,
1736-
})),
1736+
})).empty_span(),
17371737
select.selection.unwrap()
17381738
);
17391739
}
@@ -1756,7 +1756,7 @@ fn parse_like() {
17561756
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
17571757
escape_char: None,
17581758
any: false,
1759-
},
1759+
}.empty_span(),
17601760
select.selection.unwrap()
17611761
);
17621762

@@ -1773,7 +1773,7 @@ fn parse_like() {
17731773
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
17741774
escape_char: Some('^'.to_string()),
17751775
any: false,
1776-
},
1776+
}.empty_span(),
17771777
select.selection.unwrap()
17781778
);
17791779

@@ -1791,7 +1791,7 @@ fn parse_like() {
17911791
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
17921792
escape_char: None,
17931793
any: false,
1794-
})),
1794+
})).empty_span(),
17951795
select.selection.unwrap()
17961796
);
17971797
}
@@ -1813,7 +1813,7 @@ fn parse_similar_to() {
18131813
negated,
18141814
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
18151815
escape_char: None,
1816-
},
1816+
}.empty_span(),
18171817
select.selection.unwrap()
18181818
);
18191819

@@ -1829,7 +1829,7 @@ fn parse_similar_to() {
18291829
negated,
18301830
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
18311831
escape_char: Some('^'.to_string()),
1832-
},
1832+
}.empty_span(),
18331833
select.selection.unwrap()
18341834
);
18351835

@@ -1845,7 +1845,7 @@ fn parse_similar_to() {
18451845
negated,
18461846
pattern: Box::new(Expr::Value(Value::SingleQuotedString("%a".to_string()))),
18471847
escape_char: Some('^'.to_string()),
1848-
})),
1848+
})).empty_span(),
18491849
select.selection.unwrap()
18501850
);
18511851
}
@@ -1869,7 +1869,7 @@ fn parse_in_list() {
18691869
Expr::Value(Value::SingleQuotedString("MED".to_string())),
18701870
],
18711871
negated,
1872-
},
1872+
}.empty_span(),
18731873
select.selection.unwrap()
18741874
);
18751875
}
@@ -1886,7 +1886,7 @@ fn parse_in_subquery() {
18861886
expr: Box::new(Expr::Identifier(Ident::new("segment").empty_span())),
18871887
subquery: Box::new(verified_query("SELECT segm FROM bar")),
18881888
negated: false,
1889-
},
1889+
}.empty_span(),
18901890
select.selection.unwrap()
18911891
);
18921892
}
@@ -1904,7 +1904,7 @@ fn parse_in_unnest() {
19041904
expr: Box::new(Expr::Identifier(Ident::new("segment").empty_span())),
19051905
array_expr: Box::new(verified_expr("expr")),
19061906
negated,
1907-
},
1907+
}.empty_span(),
19081908
select.selection.unwrap()
19091909
);
19101910
}
@@ -2057,7 +2057,7 @@ fn parse_between() {
20572057
low: Box::new(Expr::Value(number("25"))),
20582058
high: Box::new(Expr::Value(number("32"))),
20592059
negated,
2060-
},
2060+
}.empty_span(),
20612061
select.selection.unwrap()
20622062
);
20632063
}
@@ -2084,7 +2084,7 @@ fn parse_between_with_expr() {
20842084
right: Box::new(Expr::Value(number("4"))),
20852085
}),
20862086
negated: false,
2087-
})),
2087+
})).empty_span(),
20882088
select.selection.unwrap()
20892089
);
20902090

@@ -2108,7 +2108,7 @@ fn parse_between_with_expr() {
21082108
high: Box::new(Expr::Value(number("2"))),
21092109
negated: false,
21102110
}),
2111-
},
2111+
}.empty_span(),
21122112
select.selection.unwrap(),
21132113
)
21142114
}
@@ -5532,7 +5532,7 @@ fn parse_interval_and_or_xor() {
55325532
})),
55335533
}),
55345534
}),
5535-
}),
5535+
}.empty_span()),
55365536
group_by: GroupByExpr::Expressions(vec![], vec![]),
55375537
cluster_by: vec![],
55385538
distribute_by: vec![],
@@ -6836,7 +6836,7 @@ fn parse_exists_subquery() {
68366836
Expr::Exists {
68376837
negated: false,
68386838
subquery: Box::new(expected_inner.clone()),
6839-
},
6839+
}.empty_span(),
68406840
select.selection.unwrap(),
68416841
);
68426842

@@ -6846,7 +6846,7 @@ fn parse_exists_subquery() {
68466846
Expr::Exists {
68476847
negated: true,
68486848
subquery: Box::new(expected_inner),
6849-
},
6849+
}.empty_span(),
68506850
select.selection.unwrap(),
68516851
);
68526852

@@ -8762,7 +8762,7 @@ fn test_placeholder() {
87628762
left: Box::new(Expr::Identifier(Ident::new("id").empty_span())),
87638763
op: BinaryOperator::Eq,
87648764
right: Box::new(Expr::Value(Value::Placeholder("$Id1".into()))),
8765-
})
8765+
}.empty_span())
87668766
);
87678767

87688768
let sql = "SELECT * FROM student LIMIT $1 OFFSET $2";
@@ -8799,7 +8799,7 @@ fn test_placeholder() {
87998799
left: Box::new(Expr::Identifier(Ident::new("id").empty_span())),
88008800
op: BinaryOperator::Eq,
88018801
right: Box::new(Expr::Value(Value::Placeholder("?".into()))),
8802-
})
8802+
}.empty_span())
88038803
);
88048804

88058805
let sql = "SELECT $fromage_français, :x, ?123";
@@ -10325,7 +10325,7 @@ fn parse_connect_by() {
1032510325
left: Box::new(Expr::Identifier(Ident::new("employee_id").empty_span())),
1032610326
op: BinaryOperator::NotEq,
1032710327
right: Box::new(Expr::Value(number("42"))),
10328-
}),
10328+
}.empty_span()),
1032910329
group_by: GroupByExpr::Expressions(vec![], vec![]),
1033010330
cluster_by: vec![],
1033110331
distribute_by: vec![],

tests/sqlparser_postgres.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2799,21 +2799,21 @@ fn test_json() {
27992799
right: Box::new(Expr::Value(Value::SingleQuotedString(
28002800
"{\"a\": 1}".to_string()
28012801
))),
2802-
},
2802+
}.empty_span(),
28032803
select.selection.unwrap(),
28042804
);
28052805

28062806
let sql = "SELECT info FROM orders WHERE '{\"a\": 1}' <@ info";
28072807
let select = pg().verified_only_select(sql);
28082808
assert_eq!(
2809-
Expr::BinaryOp {
2809+
Some(Expr::BinaryOp {
28102810
left: Box::new(Expr::Value(Value::SingleQuotedString(
28112811
"{\"a\": 1}".to_string()
28122812
))),
28132813
op: BinaryOperator::ArrowAt,
28142814
right: Box::new(Expr::Identifier(Ident::new("info").empty_span())),
2815-
},
2816-
select.selection.unwrap(),
2815+
}.empty_span()),
2816+
select.selection,
28172817
);
28182818

28192819
let sql = "SELECT info #- ARRAY['a', 'b'] FROM orders";
@@ -2841,7 +2841,7 @@ fn test_json() {
28412841
left: Box::new(Expr::Identifier(Ident::from("info").empty_span())),
28422842
op: BinaryOperator::AtQuestion,
28432843
right: Box::new(Expr::Value(Value::SingleQuotedString("$.a".to_string())),),
2844-
},
2844+
}.empty_span(),
28452845
select.selection.unwrap(),
28462846
);
28472847

@@ -2852,7 +2852,7 @@ fn test_json() {
28522852
left: Box::new(Expr::Identifier(Ident::from("info").empty_span())),
28532853
op: BinaryOperator::AtAt,
28542854
right: Box::new(Expr::Value(Value::SingleQuotedString("$.a".to_string())),),
2855-
},
2855+
}.empty_span(),
28562856
select.selection.unwrap(),
28572857
);
28582858

@@ -2863,7 +2863,7 @@ fn test_json() {
28632863
left: Box::new(Expr::Identifier(Ident::new("info").empty_span())),
28642864
op: BinaryOperator::Question,
28652865
right: Box::new(Expr::Value(Value::SingleQuotedString("b".to_string()))),
2866-
},
2866+
}.empty_span(),
28672867
select.selection.unwrap(),
28682868
);
28692869

@@ -2880,7 +2880,7 @@ fn test_json() {
28802880
],
28812881
named: true
28822882
}))
2883-
},
2883+
}.empty_span(),
28842884
select.selection.unwrap(),
28852885
);
28862886

@@ -2897,7 +2897,7 @@ fn test_json() {
28972897
],
28982898
named: true
28992899
}))
2900-
},
2900+
}.empty_span(),
29012901
select.selection.unwrap(),
29022902
);
29032903
}
@@ -2945,7 +2945,7 @@ fn test_composite_value() {
29452945
}),
29462946
op: BinaryOperator::Gt,
29472947
right: Box::new(Expr::Value(number("9")))
2948-
})
2948+
}.empty_span())
29492949
);
29502950

29512951
let sql = "SELECT (information_schema._pg_expandarray(ARRAY['i', 'i'])).n";
@@ -3290,7 +3290,7 @@ fn parse_custom_operator() {
32903290
"~".into()
32913291
]),
32923292
right: Box::new(Expr::Value(Value::SingleQuotedString("^(table)$".into())))
3293-
})
3293+
}.empty_span())
32943294
);
32953295

32963296
// operator with a schema
@@ -3308,7 +3308,7 @@ fn parse_custom_operator() {
33083308
)),
33093309
op: BinaryOperator::PGCustomBinaryOperator(vec!["pg_catalog".into(), "~".into()]),
33103310
right: Box::new(Expr::Value(Value::SingleQuotedString("^(table)$".into())))
3311-
})
3311+
}.empty_span())
33123312
);
33133313

33143314
// custom operator without a schema
@@ -3326,7 +3326,7 @@ fn parse_custom_operator() {
33263326
)),
33273327
op: BinaryOperator::PGCustomBinaryOperator(vec!["~".into()]),
33283328
right: Box::new(Expr::Value(Value::SingleQuotedString("^(table)$".into())))
3329-
})
3329+
}.empty_span())
33303330
);
33313331
}
33323332

tests/sqlparser_snowflake.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ fn parse_comma_outer_join() {
24622462
Ident::new("t2").empty_span(),
24632463
Ident::new("c2").empty_span()
24642464
]))))
2465-
})
2465+
}.empty_span())
24662466
);
24672467

24682468
// regular identifiers
@@ -2476,7 +2476,7 @@ fn parse_comma_outer_join() {
24762476
right: Box::new(Expr::OuterJoin(Box::new(Expr::Identifier(
24772477
Ident::new("c2").empty_span()
24782478
))))
2479-
})
2479+
}.empty_span())
24802480
);
24812481

24822482
// ensure we can still parse function calls with a unary plus arg
@@ -2494,7 +2494,7 @@ fn parse_comma_outer_join() {
24942494
expr: Box::new(Expr::Value(number("42")))
24952495
}]
24962496
)),
2497-
})
2497+
}.empty_span())
24982498
);
24992499

25002500
// permissive with whitespace

tests/sqlparser_sqlite.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ fn parse_update_tuple_row_values() {
502502
fn parse_where_in_empty_list() {
503503
let sql = "SELECT * FROM t1 WHERE a IN ()";
504504
let select = sqlite().verified_only_select(sql);
505-
if let Expr::InList { list, .. } = select.selection.as_ref().unwrap() {
505+
if let Expr::InList { list, .. } = select.selection.as_ref().unwrap().clone().unwrap() {
506506
assert_eq!(list.len(), 0);
507507
} else {
508508
unreachable!()

0 commit comments

Comments
 (0)