Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ast/helpers/key_value_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

use crate::ast::{display_comma_separated, display_separated, Value};
use crate::ast::{display_comma_separated, display_separated, ValueWithSpan};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -75,9 +75,9 @@ pub struct KeyValueOption {
/// The kind of value for a key-value option.
pub enum KeyValueOptionKind {
/// A single value.
Single(Value),
Single(ValueWithSpan),
/// Multiple values.
Multi(Vec<Value>),
Multi(Vec<ValueWithSpan>),
/// A nested list of key-value options.
KeyValueOptions(Box<KeyValueOptions>),
}
Expand Down
40 changes: 20 additions & 20 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ impl fmt::Display for MapEntry {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CastFormat {
/// A simple cast format specified by a `Value`.
Value(Value),
Value(ValueWithSpan),
/// A cast format with an explicit time zone: `(format, timezone)`.
ValueAtTimeZone(Value, Value),
ValueAtTimeZone(ValueWithSpan, ValueWithSpan),
}

/// An element of a JSON path.
Expand Down Expand Up @@ -778,7 +778,7 @@ pub enum CeilFloorKind {
/// `CEIL( <expr> TO <DateTimeField>)`
DateTimeField(DateTimeField),
/// `CEIL( <expr> [, <scale>])`
Scale(Value),
Scale(ValueWithSpan),
}

/// A WHEN clause in a CASE expression containing both
Expand Down Expand Up @@ -956,7 +956,7 @@ pub enum Expr {
/// Pattern expression.
pattern: Box<Expr>,
/// Optional escape character.
escape_char: Option<Value>,
escape_char: Option<ValueWithSpan>,
},
/// `ILIKE` (case-insensitive `LIKE`)
ILike {
Expand All @@ -970,7 +970,7 @@ pub enum Expr {
/// Pattern expression.
pattern: Box<Expr>,
/// Optional escape character.
escape_char: Option<Value>,
escape_char: Option<ValueWithSpan>,
},
/// `SIMILAR TO` regex
SimilarTo {
Expand All @@ -981,7 +981,7 @@ pub enum Expr {
/// Pattern expression.
pattern: Box<Expr>,
/// Optional escape character.
escape_char: Option<Value>,
escape_char: Option<ValueWithSpan>,
},
/// MySQL: `RLIKE` regex or `REGEXP` regex
RLike {
Expand Down Expand Up @@ -1292,7 +1292,7 @@ pub enum Expr {
/// `(<col>, <col>, ...)`.
columns: Vec<ObjectName>,
/// `<expr>`.
match_value: Value,
match_value: ValueWithSpan,
/// `<search modifier>`
opt_search_modifier: Option<SearchModifier>,
},
Expand Down Expand Up @@ -3295,7 +3295,7 @@ pub enum Set {
/// Transaction modes (e.g., ISOLATION LEVEL, READ ONLY).
modes: Vec<TransactionMode>,
/// Optional snapshot value for transaction snapshot control.
snapshot: Option<Value>,
snapshot: Option<ValueWithSpan>,
/// `true` when the `SESSION` keyword was used.
session: bool,
},
Expand Down Expand Up @@ -4630,7 +4630,7 @@ pub enum Statement {
/// Pragma name (possibly qualified).
name: ObjectName,
/// Optional pragma value.
value: Option<Value>,
value: Option<ValueWithSpan>,
/// Whether the pragma used `=`.
is_eq: bool,
},
Expand Down Expand Up @@ -6752,7 +6752,7 @@ pub enum FetchDirection {
/// Fetch a specific count of rows.
Count {
/// The limit value for the count.
limit: Value,
limit: ValueWithSpan,
},
/// Fetch the next row.
Next,
Expand All @@ -6765,12 +6765,12 @@ pub enum FetchDirection {
/// Fetch an absolute row by index.
Absolute {
/// The absolute index value.
limit: Value,
limit: ValueWithSpan,
},
/// Fetch a row relative to the current position.
Relative {
/// The relative offset value.
limit: Value,
limit: ValueWithSpan,
},
/// Fetch all rows.
All,
Expand All @@ -6779,7 +6779,7 @@ pub enum FetchDirection {
/// Fetch forward by an optional limit.
Forward {
/// Optional forward limit.
limit: Option<Value>,
limit: Option<ValueWithSpan>,
},
/// Fetch all forward rows.
ForwardAll,
Expand All @@ -6788,7 +6788,7 @@ pub enum FetchDirection {
/// Fetch backward by an optional limit.
Backward {
/// Optional backward limit.
limit: Option<Value>,
limit: Option<ValueWithSpan>,
},
/// Fetch all backward rows.
BackwardAll,
Expand Down Expand Up @@ -8116,7 +8116,7 @@ pub enum FunctionArgumentClause {
/// The `SEPARATOR` clause to the [`GROUP_CONCAT`] function in MySQL.
///
/// [`GROUP_CONCAT`]: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
Separator(Value),
Separator(ValueWithSpan),
/// The `ON NULL` clause for some JSON functions.
///
/// [MSSQL `JSON_ARRAY`](https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver16)
Expand Down Expand Up @@ -9465,7 +9465,7 @@ impl fmt::Display for CopyLegacyOption {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct FileSize {
/// Numeric size value.
pub size: Value,
pub size: ValueWithSpan,
/// Optional unit for the size (MB or GB).
pub unit: Option<FileSizeUnit>,
}
Expand Down Expand Up @@ -10654,11 +10654,11 @@ pub struct ShowStatementOptions {
/// Optional scope to show in (for example: TABLE, SCHEMA).
pub show_in: Option<ShowStatementIn>,
/// Optional `STARTS WITH` filter value.
pub starts_with: Option<Value>,
pub starts_with: Option<ValueWithSpan>,
/// Optional `LIMIT` expression.
pub limit: Option<Expr>,
/// Optional `FROM` value used with `LIMIT`.
pub limit_from: Option<Value>,
pub limit_from: Option<ValueWithSpan>,
/// Optional filter position (infix or suffix) for `LIKE`/`FILTER`.
pub filter_position: Option<ShowStatementFilterPosition>,
}
Expand Down Expand Up @@ -11474,7 +11474,7 @@ pub struct AlterUserRemoveRoleDelegation {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AlterUserAddMfaMethodOtp {
/// Optional OTP count parameter.
pub count: Option<Value>,
pub count: Option<ValueWithSpan>,
}

/// ```sql
Expand Down Expand Up @@ -11795,7 +11795,7 @@ pub struct VacuumStatement {
/// Optional table to run `VACUUM` on.
pub table_name: Option<ObjectName>,
/// Optional threshold value (percent) for `TO threshold PERCENT`.
pub threshold: Option<Value>,
pub threshold: Option<ValueWithSpan>,
/// Whether `BOOST` was specified.
pub boost: bool,
}
Expand Down
16 changes: 8 additions & 8 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ pub enum TableFactor {
json_expr: Expr,
/// The path to the array or object to be iterated over.
/// It must evaluate to a json array or object.
json_path: Value,
json_path: ValueWithSpan,
/// The columns to be extracted from each element of the array or object.
/// Each column must have a name and a type.
columns: Vec<JsonTableColumn>,
Expand All @@ -1573,7 +1573,7 @@ pub enum TableFactor {
json_expr: Expr,
/// The path to the array or object to be iterated over.
/// It must evaluate to a json array or object.
json_path: Option<Value>,
json_path: Option<ValueWithSpan>,
/// The columns to be extracted from each element of the array or object.
/// Each column must have a name and a type.
columns: Vec<OpenJsonTableColumn>,
Expand Down Expand Up @@ -1833,7 +1833,7 @@ pub struct TableSampleSeed {
/// Seed modifier (e.g. `REPEATABLE` or `SEED`).
pub modifier: TableSampleSeedModifier,
/// The seed value expression.
pub value: Value,
pub value: ValueWithSpan,
}

impl fmt::Display for TableSampleSeed {
Expand Down Expand Up @@ -1889,9 +1889,9 @@ impl fmt::Display for TableSampleUnit {
/// Bucket-based sampling clause: `BUCKET <bucket> OUT OF <total> [ON <expr>]`.
pub struct TableSampleBucket {
/// The bucket index expression.
pub bucket: Value,
pub bucket: ValueWithSpan,
/// The total number of buckets expression.
pub total: Value,
pub total: ValueWithSpan,
/// Optional `ON <expr>` specification.
pub on: Option<Expr>,
}
Expand Down Expand Up @@ -3979,7 +3979,7 @@ impl fmt::Display for JsonTableColumn {
/// A nested column in a `JSON_TABLE` column list.
pub struct JsonTableNestedColumn {
/// JSON path expression (must be a literal `Value`).
pub path: Value,
pub path: ValueWithSpan,
/// Columns extracted from the matched nested array.
pub columns: Vec<JsonTableColumn>,
}
Expand Down Expand Up @@ -4011,7 +4011,7 @@ pub struct JsonTableNamedColumn {
/// The type of the column to be extracted.
pub r#type: DataType,
/// The path to the column to be extracted. Must be a literal string.
pub path: Value,
pub path: ValueWithSpan,
/// true if the column is a boolean set to true if the given path exists
pub exists: bool,
/// The empty handling clause of the column
Expand Down Expand Up @@ -4050,7 +4050,7 @@ pub enum JsonTableColumnErrorHandling {
/// `NULL` — return NULL when the path does not match.
Null,
/// `DEFAULT <value>` — use the provided `Value` as a default.
Default(Value),
Default(ValueWithSpan),
/// `ERROR` — raise an error.
Error,
}
Expand Down
12 changes: 3 additions & 9 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ use super::{
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins, Update,
UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WhileStatement,
WildcardAdditionalOptions, With, WithFill,
UpdateTableFromKind, Use, Values, ViewColumnDef, WhileStatement, WildcardAdditionalOptions,
With, WithFill,
};

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

/// The span is stored in the `ValueWrapper` struct
impl Spanned for Value {
fn span(&self) -> Span {
Span::empty() // # todo: Value needs to store spans before this is possible
}
}

impl Spanned for Join {
fn span(&self) -> Span {
let Join {
Expand Down Expand Up @@ -2565,6 +2558,7 @@ impl Spanned for comments::CommentWithSpan {

#[cfg(test)]
pub mod tests {
use crate::ast::Value;
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
use crate::parser::Parser;
use crate::tokenizer::{Location, Span};
Expand Down
31 changes: 24 additions & 7 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#[cfg(not(feature = "std"))]
use alloc::string::String;

use core::fmt;
use core::{
fmt,
ops::{Deref, DerefMut},
};

#[cfg(feature = "bigdecimal")]
use bigdecimal::BigDecimal;
Expand Down Expand Up @@ -67,7 +70,11 @@ use sqlparser_derive::{Visit, VisitMut};
/// A `Value` paired with its source `Span` location.
#[derive(Debug, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
#[cfg_attr(
feature = "visitor",
derive(Visit, VisitMut),
visit(with = "visit_value")
)]
pub struct ValueWithSpan {
/// The wrapped `Value`.
pub value: Value,
Expand Down Expand Up @@ -111,14 +118,24 @@ impl From<ValueWithSpan> for Value {
}
}

impl Deref for ValueWithSpan {
type Target = Value;

fn deref(&self) -> &Self::Target {
&self.value
}
}

impl DerefMut for ValueWithSpan {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}

/// Primitive SQL values such as number and string
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "visitor",
derive(Visit, VisitMut),
visit(with = "visit_value")
)]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum Value {
/// Numeric literal
#[cfg(not(feature = "bigdecimal"))]
Expand Down
Loading
Loading