-
Notifications
You must be signed in to change notification settings - Fork 607
Change TIMESTAMP
and TIME
parsing so that time zone information is preserved
#641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alamb
merged 3 commits into
apache:main
from
AugustoFKL:640_timestmap-and-time-time-zone-correct-usage
Oct 3, 2022
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -528,6 +528,7 @@ define_keywords!( | |
TIME, | ||
TIMESTAMP, | ||
TIMESTAMPTZ, | ||
TIMETZ, | ||
TIMEZONE, | ||
TIMEZONE_HOUR, | ||
TIMEZONE_MINUTE, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ use core::fmt; | |
|
||
use log::debug; | ||
|
||
use IsLateral::*; | ||
use IsOptional::*; | ||
|
||
use crate::ast::*; | ||
use crate::dialect::*; | ||
use crate::keywords::{self, Keyword}; | ||
|
@@ -57,15 +60,11 @@ pub enum IsOptional { | |
Mandatory, | ||
} | ||
|
||
use IsOptional::*; | ||
|
||
pub enum IsLateral { | ||
Lateral, | ||
NotLateral, | ||
} | ||
|
||
use IsLateral::*; | ||
|
||
pub enum WildcardExpr { | ||
Expr(Expr), | ||
QualifiedWildcard(ObjectName), | ||
|
@@ -3413,22 +3412,27 @@ impl<'a> Parser<'a> { | |
Keyword::TIMESTAMP => { | ||
if self.parse_keyword(Keyword::WITH) { | ||
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?; | ||
Ok(DataType::TimestampTz) | ||
Ok(DataType::Timestamp(TimezoneInfo::WithTimeZone)) | ||
} else if self.parse_keyword(Keyword::WITHOUT) { | ||
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?; | ||
Ok(DataType::Timestamp) | ||
Ok(DataType::Timestamp(TimezoneInfo::WithoutTimeZone)) | ||
} else { | ||
Ok(DataType::Timestamp) | ||
Ok(DataType::Timestamp(TimezoneInfo::None)) | ||
} | ||
} | ||
Keyword::TIMESTAMPTZ => Ok(DataType::TimestampTz), | ||
Keyword::TIMESTAMPTZ => Ok(DataType::Timestamp(TimezoneInfo::Tz)), | ||
Keyword::TIME => { | ||
// TBD: we throw away "with/without timezone" information | ||
if self.parse_keyword(Keyword::WITH) || self.parse_keyword(Keyword::WITHOUT) { | ||
if self.parse_keyword(Keyword::WITH) { | ||
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?; | ||
Ok(DataType::Time(TimezoneInfo::WithTimeZone)) | ||
} else if self.parse_keyword(Keyword::WITHOUT) { | ||
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?; | ||
Ok(DataType::Time(TimezoneInfo::WithoutTimeZone)) | ||
} else { | ||
Ok(DataType::Time(TimezoneInfo::None)) | ||
} | ||
Ok(DataType::Time) | ||
} | ||
Keyword::TIMETZ => Ok(DataType::Time(TimezoneInfo::Tz)), | ||
// Interval types can be followed by a complicated interval | ||
// qualifier that we don't currently support. See | ||
// parse_interval for a taste. | ||
|
@@ -5197,9 +5201,10 @@ impl Word { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_utils::{all_dialects, TestedDialects}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_prev_index() { | ||
let sql = "SELECT version"; | ||
|
@@ -5256,23 +5261,78 @@ mod tests { | |
} | ||
|
||
// TODO add tests for all data types? https://github.com/sqlparser-rs/sqlparser-rs/issues/2 | ||
// TODO when we have dialect validation by data type parsing, split test | ||
#[test] | ||
fn test_parse_data_type() { | ||
test_parse_data_type("BLOB", "BLOB"); | ||
test_parse_data_type("BLOB(50)", "BLOB(50)"); | ||
test_parse_data_type("CLOB", "CLOB"); | ||
test_parse_data_type("CLOB(50)", "CLOB(50)"); | ||
test_parse_data_type("DOUBLE PRECISION", "DOUBLE PRECISION"); | ||
test_parse_data_type("DOUBLE", "DOUBLE"); | ||
test_parse_data_type("VARBINARY", "VARBINARY"); | ||
test_parse_data_type("VARBINARY(20)", "VARBINARY(20)"); | ||
test_parse_data_type("BINARY", "BINARY"); | ||
test_parse_data_type("BINARY(20)", "BINARY(20)"); | ||
|
||
fn test_parse_data_type(input: &str, expected: &str) { | ||
// BINARY data type | ||
test_parse_data_type("BINARY", DataType::Binary(None), "BINARY"); | ||
test_parse_data_type("BINARY(20)", DataType::Binary(Some(20)), "BINARY(20)"); | ||
|
||
// BLOB data type | ||
test_parse_data_type("BLOB", DataType::Blob(None), "BLOB"); | ||
test_parse_data_type("BLOB(50)", DataType::Blob(Some(50)), "BLOB(50)"); | ||
|
||
// CLOB data type | ||
test_parse_data_type("CLOB", DataType::Clob(None), "CLOB"); | ||
test_parse_data_type("CLOB(50)", DataType::Clob(Some(50)), "CLOB(50)"); | ||
|
||
// Double data type | ||
test_parse_data_type( | ||
"DOUBLE PRECISION", | ||
DataType::DoublePrecision, | ||
"DOUBLE PRECISION", | ||
); | ||
test_parse_data_type("DOUBLE", DataType::Double, "DOUBLE"); | ||
|
||
// Time data type | ||
test_parse_data_type("TIME", DataType::Time(TimezoneInfo::None), "TIME"); | ||
test_parse_data_type( | ||
"TIME WITH TIME ZONE", | ||
DataType::Time(TimezoneInfo::WithTimeZone), | ||
"TIME WITH TIME ZONE", | ||
); | ||
test_parse_data_type( | ||
"TIME WITHOUT TIME ZONE", | ||
DataType::Time(TimezoneInfo::WithoutTimeZone), | ||
"TIME WITHOUT TIME ZONE", | ||
); | ||
test_parse_data_type("TIMETZ", DataType::Time(TimezoneInfo::Tz), "TIMETZ"); | ||
|
||
// Timestamp data type | ||
test_parse_data_type( | ||
"TIMESTAMP", | ||
DataType::Timestamp(TimezoneInfo::None), | ||
"TIMESTAMP", | ||
); | ||
test_parse_data_type( | ||
"TIMESTAMP WITH TIME ZONE", | ||
DataType::Timestamp(TimezoneInfo::WithTimeZone), | ||
"TIMESTAMP WITH TIME ZONE", | ||
); | ||
test_parse_data_type( | ||
"TIMESTAMP WITHOUT TIME ZONE", | ||
DataType::Timestamp(TimezoneInfo::WithoutTimeZone), | ||
"TIMESTAMP WITHOUT TIME ZONE", | ||
); | ||
test_parse_data_type( | ||
"TIMESTAMPTZ", | ||
DataType::Timestamp(TimezoneInfo::Tz), | ||
"TIMESTAMPTZ", | ||
); | ||
|
||
// VARBINARY data type | ||
test_parse_data_type("VARBINARY", DataType::Varbinary(None), "VARBINARY"); | ||
test_parse_data_type( | ||
"VARBINARY(20)", | ||
DataType::Varbinary(Some(20)), | ||
"VARBINARY(20)", | ||
); | ||
|
||
fn test_parse_data_type(input: &str, expected_type: DataType, expected_str: &str) { | ||
all_dialects().run_parser_method(input, |parser| { | ||
let data_type = parser.parse_data_type().unwrap().to_string(); | ||
assert_eq!(data_type, expected); | ||
let data_type = parser.parse_data_type().unwrap(); | ||
assert_eq!(data_type, expected_type); | ||
assert_eq!(expected_type.to_string(), expected_str.to_string()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}); | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.