mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-01 20:52:16 +00:00
Move Value::Interval
to Expr::Interval
(#609)
* refactor(value): convert Value::Interval to Expr::Interval * test(sqlparser_common): modify test case * refactor(parser): rename func parse_interval * refactor(tests) rename parse_interval test func
This commit is contained in:
parent
d971a029dd
commit
3ac1bb5b80
4 changed files with 81 additions and 83 deletions
|
@ -21,8 +21,6 @@ use bigdecimal::BigDecimal;
|
|||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::Expr;
|
||||
|
||||
/// Primitive SQL values such as number and string
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
|
@ -45,25 +43,6 @@ pub enum Value {
|
|||
DoubleQuotedString(String),
|
||||
/// Boolean value true or false
|
||||
Boolean(bool),
|
||||
/// INTERVAL literals, roughly in the following format:
|
||||
/// `INTERVAL '<value>' [ <leading_field> [ (<leading_precision>) ] ]
|
||||
/// [ TO <last_field> [ (<fractional_seconds_precision>) ] ]`,
|
||||
/// e.g. `INTERVAL '123:45.67' MINUTE(3) TO SECOND(2)`.
|
||||
///
|
||||
/// The parser does not validate the `<value>`, nor does it ensure
|
||||
/// that the `<leading_field>` units >= the units in `<last_field>`,
|
||||
/// so the user will have to reject intervals like `HOUR TO YEAR`.
|
||||
Interval {
|
||||
value: Box<Expr>,
|
||||
leading_field: Option<DateTimeField>,
|
||||
leading_precision: Option<u64>,
|
||||
last_field: Option<DateTimeField>,
|
||||
/// The seconds precision can be specified in SQL source as
|
||||
/// `INTERVAL '__' SECOND(_, x)` (in which case the `leading_field`
|
||||
/// will be `Second` and the `last_field` will be `None`),
|
||||
/// or as `__ TO SECOND(x)`.
|
||||
fractional_seconds_precision: Option<u64>,
|
||||
},
|
||||
/// `NULL` value
|
||||
Null,
|
||||
/// `?` or `$` Prepared statement arg placeholder
|
||||
|
@ -80,44 +59,6 @@ impl fmt::Display for Value {
|
|||
Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
|
||||
Value::HexStringLiteral(v) => write!(f, "X'{}'", v),
|
||||
Value::Boolean(v) => write!(f, "{}", v),
|
||||
Value::Interval {
|
||||
value,
|
||||
leading_field: Some(DateTimeField::Second),
|
||||
leading_precision: Some(leading_precision),
|
||||
last_field,
|
||||
fractional_seconds_precision: Some(fractional_seconds_precision),
|
||||
} => {
|
||||
// When the leading field is SECOND, the parser guarantees that
|
||||
// the last field is None.
|
||||
assert!(last_field.is_none());
|
||||
write!(
|
||||
f,
|
||||
"INTERVAL {} SECOND ({}, {})",
|
||||
value, leading_precision, fractional_seconds_precision
|
||||
)
|
||||
}
|
||||
Value::Interval {
|
||||
value,
|
||||
leading_field,
|
||||
leading_precision,
|
||||
last_field,
|
||||
fractional_seconds_precision,
|
||||
} => {
|
||||
write!(f, "INTERVAL {}", value)?;
|
||||
if let Some(leading_field) = leading_field {
|
||||
write!(f, " {}", leading_field)?;
|
||||
}
|
||||
if let Some(leading_precision) = leading_precision {
|
||||
write!(f, " ({})", leading_precision)?;
|
||||
}
|
||||
if let Some(last_field) = last_field {
|
||||
write!(f, " TO {}", last_field)?;
|
||||
}
|
||||
if let Some(fractional_seconds_precision) = fractional_seconds_precision {
|
||||
write!(f, " ({})", fractional_seconds_precision)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Value::Null => write!(f, "NULL"),
|
||||
Value::Placeholder(v) => write!(f, "{}", v),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue