Skip to content

Commit 6f8e7b8

Browse files
authored
Expose values through ValueWithSpan (#2281)
1 parent df0d56c commit 6f8e7b8

File tree

15 files changed

+256
-214
lines changed

15 files changed

+256
-214
lines changed

src/ast/helpers/key_value_options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use serde::{Deserialize, Serialize};
2929
#[cfg(feature = "visitor")]
3030
use sqlparser_derive::{Visit, VisitMut};
3131

32-
use crate::ast::{display_comma_separated, display_separated, Value};
32+
use crate::ast::{display_comma_separated, display_separated, ValueWithSpan};
3333

3434
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3535
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -75,9 +75,9 @@ pub struct KeyValueOption {
7575
/// The kind of value for a key-value option.
7676
pub enum KeyValueOptionKind {
7777
/// A single value.
78-
Single(Value),
78+
Single(ValueWithSpan),
7979
/// Multiple values.
80-
Multi(Vec<Value>),
80+
Multi(Vec<ValueWithSpan>),
8181
/// A nested list of key-value options.
8282
KeyValueOptions(Box<KeyValueOptions>),
8383
}

src/ast/mod.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,9 @@ impl fmt::Display for MapEntry {
624624
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
625625
pub enum CastFormat {
626626
/// A simple cast format specified by a `Value`.
627-
Value(Value),
627+
Value(ValueWithSpan),
628628
/// A cast format with an explicit time zone: `(format, timezone)`.
629-
ValueAtTimeZone(Value, Value),
629+
ValueAtTimeZone(ValueWithSpan, ValueWithSpan),
630630
}
631631

632632
/// An element of a JSON path.
@@ -778,7 +778,7 @@ pub enum CeilFloorKind {
778778
/// `CEIL( <expr> TO <DateTimeField>)`
779779
DateTimeField(DateTimeField),
780780
/// `CEIL( <expr> [, <scale>])`
781-
Scale(Value),
781+
Scale(ValueWithSpan),
782782
}
783783

784784
/// A WHEN clause in a CASE expression containing both
@@ -956,7 +956,7 @@ pub enum Expr {
956956
/// Pattern expression.
957957
pattern: Box<Expr>,
958958
/// Optional escape character.
959-
escape_char: Option<Value>,
959+
escape_char: Option<ValueWithSpan>,
960960
},
961961
/// `ILIKE` (case-insensitive `LIKE`)
962962
ILike {
@@ -970,7 +970,7 @@ pub enum Expr {
970970
/// Pattern expression.
971971
pattern: Box<Expr>,
972972
/// Optional escape character.
973-
escape_char: Option<Value>,
973+
escape_char: Option<ValueWithSpan>,
974974
},
975975
/// `SIMILAR TO` regex
976976
SimilarTo {
@@ -981,7 +981,7 @@ pub enum Expr {
981981
/// Pattern expression.
982982
pattern: Box<Expr>,
983983
/// Optional escape character.
984-
escape_char: Option<Value>,
984+
escape_char: Option<ValueWithSpan>,
985985
},
986986
/// MySQL: `RLIKE` regex or `REGEXP` regex
987987
RLike {
@@ -1146,12 +1146,12 @@ pub enum Expr {
11461146
/// TRIM(<expr>, [, characters]) -- PostgreSQL, DuckDB, Snowflake, BigQuery, Generic
11471147
/// ```
11481148
Trim {
1149-
/// The expression to trim from.
1150-
expr: Box<Expr>,
11511149
/// Which side to trim: `BOTH`, `LEADING`, or `TRAILING`.
11521150
trim_where: Option<TrimWhereField>,
1153-
/// Optional expression specifying what to trim from the value.
1151+
/// Optional expression specifying what to trim from the value `expr`.
11541152
trim_what: Option<Box<Expr>>,
1153+
/// The expression to trim from.
1154+
expr: Box<Expr>,
11551155
/// Optional list of characters to trim (dialect-specific).
11561156
trim_characters: Option<Vec<Expr>>,
11571157
},
@@ -1292,7 +1292,7 @@ pub enum Expr {
12921292
/// `(<col>, <col>, ...)`.
12931293
columns: Vec<ObjectName>,
12941294
/// `<expr>`.
1295-
match_value: Value,
1295+
match_value: ValueWithSpan,
12961296
/// `<search modifier>`
12971297
opt_search_modifier: Option<SearchModifier>,
12981298
},
@@ -3295,7 +3295,7 @@ pub enum Set {
32953295
/// Transaction modes (e.g., ISOLATION LEVEL, READ ONLY).
32963296
modes: Vec<TransactionMode>,
32973297
/// Optional snapshot value for transaction snapshot control.
3298-
snapshot: Option<Value>,
3298+
snapshot: Option<ValueWithSpan>,
32993299
/// `true` when the `SESSION` keyword was used.
33003300
session: bool,
33013301
},
@@ -4630,7 +4630,7 @@ pub enum Statement {
46304630
/// Pragma name (possibly qualified).
46314631
name: ObjectName,
46324632
/// Optional pragma value.
4633-
value: Option<Value>,
4633+
value: Option<ValueWithSpan>,
46344634
/// Whether the pragma used `=`.
46354635
is_eq: bool,
46364636
},
@@ -6752,7 +6752,7 @@ pub enum FetchDirection {
67526752
/// Fetch a specific count of rows.
67536753
Count {
67546754
/// The limit value for the count.
6755-
limit: Value,
6755+
limit: ValueWithSpan,
67566756
},
67576757
/// Fetch the next row.
67586758
Next,
@@ -6765,12 +6765,12 @@ pub enum FetchDirection {
67656765
/// Fetch an absolute row by index.
67666766
Absolute {
67676767
/// The absolute index value.
6768-
limit: Value,
6768+
limit: ValueWithSpan,
67696769
},
67706770
/// Fetch a row relative to the current position.
67716771
Relative {
67726772
/// The relative offset value.
6773-
limit: Value,
6773+
limit: ValueWithSpan,
67746774
},
67756775
/// Fetch all rows.
67766776
All,
@@ -6779,7 +6779,7 @@ pub enum FetchDirection {
67796779
/// Fetch forward by an optional limit.
67806780
Forward {
67816781
/// Optional forward limit.
6782-
limit: Option<Value>,
6782+
limit: Option<ValueWithSpan>,
67836783
},
67846784
/// Fetch all forward rows.
67856785
ForwardAll,
@@ -6788,7 +6788,7 @@ pub enum FetchDirection {
67886788
/// Fetch backward by an optional limit.
67896789
Backward {
67906790
/// Optional backward limit.
6791-
limit: Option<Value>,
6791+
limit: Option<ValueWithSpan>,
67926792
},
67936793
/// Fetch all backward rows.
67946794
BackwardAll,
@@ -8116,7 +8116,7 @@ pub enum FunctionArgumentClause {
81168116
/// The `SEPARATOR` clause to the [`GROUP_CONCAT`] function in MySQL.
81178117
///
81188118
/// [`GROUP_CONCAT`]: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
8119-
Separator(Value),
8119+
Separator(ValueWithSpan),
81208120
/// The `ON NULL` clause for some JSON functions.
81218121
///
81228122
/// [MSSQL `JSON_ARRAY`](https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver16)
@@ -9465,7 +9465,7 @@ impl fmt::Display for CopyLegacyOption {
94659465
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
94669466
pub struct FileSize {
94679467
/// Numeric size value.
9468-
pub size: Value,
9468+
pub size: ValueWithSpan,
94699469
/// Optional unit for the size (MB or GB).
94709470
pub unit: Option<FileSizeUnit>,
94719471
}
@@ -10654,11 +10654,11 @@ pub struct ShowStatementOptions {
1065410654
/// Optional scope to show in (for example: TABLE, SCHEMA).
1065510655
pub show_in: Option<ShowStatementIn>,
1065610656
/// Optional `STARTS WITH` filter value.
10657-
pub starts_with: Option<Value>,
10657+
pub starts_with: Option<ValueWithSpan>,
1065810658
/// Optional `LIMIT` expression.
1065910659
pub limit: Option<Expr>,
1066010660
/// Optional `FROM` value used with `LIMIT`.
10661-
pub limit_from: Option<Value>,
10661+
pub limit_from: Option<ValueWithSpan>,
1066210662
/// Optional filter position (infix or suffix) for `LIKE`/`FILTER`.
1066310663
pub filter_position: Option<ShowStatementFilterPosition>,
1066410664
}
@@ -11474,7 +11474,7 @@ pub struct AlterUserRemoveRoleDelegation {
1147411474
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1147511475
pub struct AlterUserAddMfaMethodOtp {
1147611476
/// Optional OTP count parameter.
11477-
pub count: Option<Value>,
11477+
pub count: Option<ValueWithSpan>,
1147811478
}
1147911479

1148011480
/// ```sql
@@ -11795,7 +11795,7 @@ pub struct VacuumStatement {
1179511795
/// Optional table to run `VACUUM` on.
1179611796
pub table_name: Option<ObjectName>,
1179711797
/// Optional threshold value (percent) for `TO threshold PERCENT`.
11798-
pub threshold: Option<Value>,
11798+
pub threshold: Option<ValueWithSpan>,
1179911799
/// Whether `BOOST` was specified.
1180011800
pub boost: bool,
1180111801
}

src/ast/query.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ pub enum TableFactor {
15521552
json_expr: Expr,
15531553
/// The path to the array or object to be iterated over.
15541554
/// It must evaluate to a json array or object.
1555-
json_path: Value,
1555+
json_path: ValueWithSpan,
15561556
/// The columns to be extracted from each element of the array or object.
15571557
/// Each column must have a name and a type.
15581558
columns: Vec<JsonTableColumn>,
@@ -1573,7 +1573,7 @@ pub enum TableFactor {
15731573
json_expr: Expr,
15741574
/// The path to the array or object to be iterated over.
15751575
/// It must evaluate to a json array or object.
1576-
json_path: Option<Value>,
1576+
json_path: Option<ValueWithSpan>,
15771577
/// The columns to be extracted from each element of the array or object.
15781578
/// Each column must have a name and a type.
15791579
columns: Vec<OpenJsonTableColumn>,
@@ -1833,7 +1833,7 @@ pub struct TableSampleSeed {
18331833
/// Seed modifier (e.g. `REPEATABLE` or `SEED`).
18341834
pub modifier: TableSampleSeedModifier,
18351835
/// The seed value expression.
1836-
pub value: Value,
1836+
pub value: ValueWithSpan,
18371837
}
18381838

18391839
impl fmt::Display for TableSampleSeed {
@@ -1889,9 +1889,9 @@ impl fmt::Display for TableSampleUnit {
18891889
/// Bucket-based sampling clause: `BUCKET <bucket> OUT OF <total> [ON <expr>]`.
18901890
pub struct TableSampleBucket {
18911891
/// The bucket index expression.
1892-
pub bucket: Value,
1892+
pub bucket: ValueWithSpan,
18931893
/// The total number of buckets expression.
1894-
pub total: Value,
1894+
pub total: ValueWithSpan,
18951895
/// Optional `ON <expr>` specification.
18961896
pub on: Option<Expr>,
18971897
}
@@ -3979,7 +3979,7 @@ impl fmt::Display for JsonTableColumn {
39793979
/// A nested column in a `JSON_TABLE` column list.
39803980
pub struct JsonTableNestedColumn {
39813981
/// JSON path expression (must be a literal `Value`).
3982-
pub path: Value,
3982+
pub path: ValueWithSpan,
39833983
/// Columns extracted from the matched nested array.
39843984
pub columns: Vec<JsonTableColumn>,
39853985
}
@@ -4011,7 +4011,7 @@ pub struct JsonTableNamedColumn {
40114011
/// The type of the column to be extracted.
40124012
pub r#type: DataType,
40134013
/// The path to the column to be extracted. Must be a literal string.
4014-
pub path: Value,
4014+
pub path: ValueWithSpan,
40154015
/// true if the column is a boolean set to true if the given path exists
40164016
pub exists: bool,
40174017
/// The empty handling clause of the column
@@ -4050,7 +4050,7 @@ pub enum JsonTableColumnErrorHandling {
40504050
/// `NULL` — return NULL when the path does not match.
40514051
Null,
40524052
/// `DEFAULT <value>` — use the provided `Value` as a default.
4053-
Default(Value),
4053+
Default(ValueWithSpan),
40544054
/// `ERROR` — raise an error.
40554055
Error,
40564056
}

src/ast/spans.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ use super::{
4646
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
4747
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
4848
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins, Update,
49-
UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WhileStatement,
50-
WildcardAdditionalOptions, With, WithFill,
49+
UpdateTableFromKind, Use, Values, ViewColumnDef, WhileStatement, WildcardAdditionalOptions,
50+
With, WithFill,
5151
};
5252

5353
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -2185,13 +2185,6 @@ impl Spanned for ValueWithSpan {
21852185
}
21862186
}
21872187

2188-
/// The span is stored in the `ValueWrapper` struct
2189-
impl Spanned for Value {
2190-
fn span(&self) -> Span {
2191-
Span::empty() // # todo: Value needs to store spans before this is possible
2192-
}
2193-
}
2194-
21952188
impl Spanned for Join {
21962189
fn span(&self) -> Span {
21972190
let Join {
@@ -2565,6 +2558,7 @@ impl Spanned for comments::CommentWithSpan {
25652558

25662559
#[cfg(test)]
25672560
pub mod tests {
2561+
use crate::ast::Value;
25682562
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
25692563
use crate::parser::Parser;
25702564
use crate::tokenizer::{Location, Span};

src/ast/value.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#[cfg(not(feature = "std"))]
1919
use alloc::string::String;
2020

21-
use core::fmt;
21+
use core::{
22+
fmt,
23+
ops::{Deref, DerefMut},
24+
};
2225

2326
#[cfg(feature = "bigdecimal")]
2427
use bigdecimal::BigDecimal;
@@ -67,7 +70,11 @@ use sqlparser_derive::{Visit, VisitMut};
6770
/// A `Value` paired with its source `Span` location.
6871
#[derive(Debug, Clone, Eq)]
6972
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
70-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
73+
#[cfg_attr(
74+
feature = "visitor",
75+
derive(Visit, VisitMut),
76+
visit(with = "visit_value")
77+
)]
7178
pub struct ValueWithSpan {
7279
/// The wrapped `Value`.
7380
pub value: Value,
@@ -111,14 +118,24 @@ impl From<ValueWithSpan> for Value {
111118
}
112119
}
113120

121+
impl Deref for ValueWithSpan {
122+
type Target = Value;
123+
124+
fn deref(&self) -> &Self::Target {
125+
&self.value
126+
}
127+
}
128+
129+
impl DerefMut for ValueWithSpan {
130+
fn deref_mut(&mut self) -> &mut Self::Target {
131+
&mut self.value
132+
}
133+
}
134+
114135
/// Primitive SQL values such as number and string
115136
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
116137
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
117-
#[cfg_attr(
118-
feature = "visitor",
119-
derive(Visit, VisitMut),
120-
visit(with = "visit_value")
121-
)]
138+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
122139
pub enum Value {
123140
/// Numeric literal
124141
#[cfg(not(feature = "bigdecimal"))]

0 commit comments

Comments
 (0)