Skip to content

Commit d393d99

Browse files
committed
Support parsing dates, times, and timestamps as strings
1 parent 8ea702b commit d393d99

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/sqlast/value.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ pub enum Value {
1111
NationalStringLiteral(String),
1212
/// Boolean value true or false
1313
Boolean(bool),
14+
/// Date literals
15+
Date(String),
16+
/// Time literals
17+
Time(String),
18+
/// Timestamp literals, which include both a date and time
19+
Timestamp(String),
1420
/// NULL value in insert statements,
1521
Null,
1622
}
@@ -23,6 +29,9 @@ impl ToString for Value {
2329
Value::SingleQuotedString(v) => format!("'{}'", escape_single_quote_string(v)),
2430
Value::NationalStringLiteral(v) => format!("N'{}'", v),
2531
Value::Boolean(v) => v.to_string(),
32+
Value::Date(v) => format!("date '{}'", escape_single_quote_string(v)),
33+
Value::Time(v) => format!("time '{}'", escape_single_quote_string(v)),
34+
Value::Timestamp(v) => format!("timestamp '{}'", escape_single_quote_string(v)),
2635
Value::Null => "NULL".to_string(),
2736
}
2837
}

src/sqlparser.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,17 @@ impl Parser {
191191
}
192192
"CASE" => self.parse_case_expression(),
193193
"CAST" => self.parse_cast_expression(),
194+
"DATE" => Ok(ASTNode::SQLValue(Value::Date(self.parse_literal_string()?))),
194195
"EXISTS" => self.parse_exists_expression(),
195196
"EXTRACT" => self.parse_extract_expression(),
196197
"NOT" => Ok(ASTNode::SQLUnary {
197198
operator: SQLOperator::Not,
198199
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
199200
}),
201+
"TIME" => Ok(ASTNode::SQLValue(Value::Time(self.parse_literal_string()?))),
202+
"TIMESTAMP" => Ok(ASTNode::SQLValue(Value::Timestamp(
203+
self.parse_literal_string()?,
204+
))),
200205
// Here `w` is a word, check if it's a part of a multi-part
201206
// identifier, a function call, or a simple identifier:
202207
_ => match self.peek_token() {

tests/sqlparser_common.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,36 @@ fn parse_literal_string() {
965965
);
966966
}
967967

968+
#[test]
969+
fn parse_literal_date() {
970+
let sql = "SELECT date '1999-01-01'";
971+
let select = verified_only_select(sql);
972+
assert_eq!(
973+
&ASTNode::SQLValue(Value::Date("1999-01-01".into())),
974+
expr_from_projection(only(&select.projection)),
975+
);
976+
}
977+
978+
#[test]
979+
fn parse_literal_time() {
980+
let sql = "SELECT time '01:23:34'";
981+
let select = verified_only_select(sql);
982+
assert_eq!(
983+
&ASTNode::SQLValue(Value::Time("01:23:34".into())),
984+
expr_from_projection(only(&select.projection)),
985+
);
986+
}
987+
988+
#[test]
989+
fn parse_literal_timestamp() {
990+
let sql = "SELECT timestamp '1999-01-01 01:23:34'";
991+
let select = verified_only_select(sql);
992+
assert_eq!(
993+
&ASTNode::SQLValue(Value::Timestamp("1999-01-01 01:23:34".into())),
994+
expr_from_projection(only(&select.projection)),
995+
);
996+
}
997+
968998
#[test]
969999
fn parse_simple_math_expr_plus() {
9701000
let sql = "SELECT a + b, 2 + a, 2.5 + a, a_f + b_f, 2 + a_f, 2.5 + a_f FROM c";

0 commit comments

Comments
 (0)