Skip to content

Commit 31ae6f3

Browse files
committed
Expose values through ValueWithSpan
`Deref/Mut<Target = Value>` for `ValueWithSpan`
1 parent df0d56c commit 31ae6f3

File tree

15 files changed

+171
-162
lines changed

15 files changed

+171
-162
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::{ValueWithSpan, display_comma_separated, display_separated};
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: 20 additions & 20 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 {
@@ -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: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ 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,
49+
UpdateTableFromKind, Use, Values, ViewColumnDef, WhileStatement,
5050
WildcardAdditionalOptions, With, WithFill,
5151
};
5252

@@ -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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#[cfg(not(feature = "std"))]
1919
use alloc::string::String;
2020

21-
use core::fmt;
21+
use core::{fmt, ops::{Deref, DerefMut}};
2222

2323
#[cfg(feature = "bigdecimal")]
2424
use bigdecimal::BigDecimal;
@@ -67,7 +67,7 @@ use sqlparser_derive::{Visit, VisitMut};
6767
/// A `Value` paired with its source `Span` location.
6868
#[derive(Debug, Clone, Eq)]
6969
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
70-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
70+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut), visit(with = "visit_value"))]
7171
pub struct ValueWithSpan {
7272
/// The wrapped `Value`.
7373
pub value: Value,
@@ -111,14 +111,24 @@ impl From<ValueWithSpan> for Value {
111111
}
112112
}
113113

114+
impl Deref for ValueWithSpan {
115+
type Target = Value;
116+
117+
fn deref(&self) -> &Self::Target {
118+
&self.value
119+
}
120+
}
121+
122+
impl DerefMut for ValueWithSpan {
123+
fn deref_mut(&mut self) -> &mut Self::Target {
124+
&mut self.value
125+
}
126+
}
127+
114128
/// Primitive SQL values such as number and string
115129
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
116130
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
117-
#[cfg_attr(
118-
feature = "visitor",
119-
derive(Visit, VisitMut),
120-
visit(with = "visit_value")
121-
)]
131+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
122132
pub enum Value {
123133
/// Numeric literal
124134
#[cfg(not(feature = "bigdecimal"))]

src/ast/visitor.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//! Recursive visitors for ast Nodes. See [`Visitor`] for more details.
1919
20-
use crate::ast::{Expr, ObjectName, Query, Select, Statement, TableFactor, Value};
20+
use crate::ast::{Expr, ObjectName, Query, Select, Statement, TableFactor, ValueWithSpan};
2121
use core::ops::ControlFlow;
2222

2323
/// A type that can be visited by a [`Visitor`]. See [`Visitor`] for
@@ -258,12 +258,12 @@ pub trait Visitor {
258258
}
259259

260260
/// Invoked for any Value that appear in the AST before visiting children
261-
fn pre_visit_value(&mut self, _value: &Value) -> ControlFlow<Self::Break> {
261+
fn pre_visit_value(&mut self, _value: &ValueWithSpan) -> ControlFlow<Self::Break> {
262262
ControlFlow::Continue(())
263263
}
264264

265265
/// Invoked for any Value that appear in the AST after visiting children
266-
fn post_visit_value(&mut self, _value: &Value) -> ControlFlow<Self::Break> {
266+
fn post_visit_value(&mut self, _value: &ValueWithSpan) -> ControlFlow<Self::Break> {
267267
ControlFlow::Continue(())
268268
}
269269
}
@@ -386,12 +386,12 @@ pub trait VisitorMut {
386386
}
387387

388388
/// Invoked for any value that appear in the AST before visiting children
389-
fn pre_visit_value(&mut self, _value: &mut Value) -> ControlFlow<Self::Break> {
389+
fn pre_visit_value(&mut self, _value: &mut ValueWithSpan) -> ControlFlow<Self::Break> {
390390
ControlFlow::Continue(())
391391
}
392392

393393
/// Invoked for any statements that appear in the AST after visiting children
394-
fn post_visit_value(&mut self, _value: &mut Value) -> ControlFlow<Self::Break> {
394+
fn post_visit_value(&mut self, _value: &mut ValueWithSpan) -> ControlFlow<Self::Break> {
395395
ControlFlow::Continue(())
396396
}
397397
}
@@ -1015,7 +1015,7 @@ mod tests {
10151015

10161016
#[cfg(test)]
10171017
mod visit_mut_tests {
1018-
use crate::ast::{Statement, Value, VisitMut, VisitorMut};
1018+
use crate::ast::{Statement, Value, ValueWithSpan, VisitMut, VisitorMut};
10191019
use crate::dialect::GenericDialect;
10201020
use crate::parser::Parser;
10211021
use crate::tokenizer::Tokenizer;
@@ -1029,13 +1029,13 @@ mod visit_mut_tests {
10291029
impl VisitorMut for MutatorVisitor {
10301030
type Break = ();
10311031

1032-
fn pre_visit_value(&mut self, value: &mut Value) -> ControlFlow<Self::Break> {
1032+
fn pre_visit_value(&mut self, value: &mut ValueWithSpan) -> ControlFlow<Self::Break> {
10331033
self.index += 1;
1034-
*value = Value::SingleQuotedString(format!("REDACTED_{}", self.index));
1034+
value.value = Value::SingleQuotedString(format!("REDACTED_{}", self.index));
10351035
ControlFlow::Continue(())
10361036
}
10371037

1038-
fn post_visit_value(&mut self, _value: &mut Value) -> ControlFlow<Self::Break> {
1038+
fn post_visit_value(&mut self, _value: &mut ValueWithSpan) -> ControlFlow<Self::Break> {
10391039
ControlFlow::Continue(())
10401040
}
10411041
}

0 commit comments

Comments
 (0)