diff --git a/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md b/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md index 39745e4eba0..694f9ab2c45 100644 --- a/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md +++ b/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md @@ -116,11 +116,27 @@ execution reaches Comet). The short-circuit applies only to a top-level cast. Se ## String to Timestamp -Comet's native `CAST(string AS TIMESTAMP)` implementation supports all timestamp formats accepted +Comet's native `CAST(string AS TIMESTAMP)` implementation supports the timestamp formats accepted by Apache Spark, including ISO 8601 date-time strings, date-only strings, time-only strings -(`HH:MM:SS`), embedded timezone offsets (e.g. `+07:30`, `GMT-01:00`, `UTC`), named timezone -suffixes (e.g. `Europe/Moscow`), and the full Spark timestamp year range -(-290308 to 294247). +(`HH:MM:SS`), and the full Spark timestamp year range (-290308 to 294247). + +A zone suffix after the seconds is resolved the way Spark's `ZoneId.of(id, ZoneId.SHORT_IDS)` +does. The following forms are supported: + +- `Z`, and `UTC`, `GMT` or `UT` on their own or followed by an offset (e.g. `UTC`, `GMT-01:00`, + `UTC+8`). +- Offsets in the forms `+H`, `+HH`, `+HHMM`, `+HHMMSS`, `+H:M`, `+HH:MM` and `+H:MM:SS` + (e.g. `+07:30`, `-0800`, `+080000`, `+08:00:00`). +- The Java `ZoneId.SHORT_IDS` aliases `ACT`, `AET`, `AGT`, `ART`, `AST`, `BET`, `BST`, `CAT`, + `CNT`, `CST`, `CTT`, `EAT`, `ECT`, `IET`, `IST`, `JST`, `MIT`, `NET`, `NST`, `PLT`, `PNT`, + `PRT`, `PST`, `SST` and `VST`, which map to a region and follow its daylight saving time, and + `EST`, `MST` and `HST`, which are the fixed offsets `-05:00`, `-07:00` and `-10:00`. +- Time zone database region ids, with or without a `/` and with or without a space before them + (e.g. `Europe/Moscow`, `America/Los_Angeles`, `CET`, `Zulu`, `Etc/GMT+8`). + +Zone ids are case sensitive, as in Spark. Offsets with a non-zero seconds part (e.g. `+08:00:30`) +are not supported: Comet returns `NULL`, or raises `CAST_INVALID_INPUT` in ANSI mode, where Spark +returns a value. See [#7224](https://github.com/apache/datafusion-comet/issues/7224). ## String to TimestampNTZ diff --git a/native/spark-expr/src/conversion_funcs/string.rs b/native/spark-expr/src/conversion_funcs/string.rs index 5cda02ebbc4..310e4a54234 100644 --- a/native/spark-expr/src/conversion_funcs/string.rs +++ b/native/spark-expr/src/conversion_funcs/string.rs @@ -1546,6 +1546,8 @@ fn timestamp_parser( /// "+HHMM" -> +H*3600+M*60 (4 digits, no colon) /// "+H:M" -> same (with colon, any digit count 1-2 each) /// "+HH:MM" -> same +/// "+H:MM:SS" -> +H*3600+M*60+S (1-2 digit hour, two-digit minutes and seconds; Spark pads +/// a one-digit hour before calling `ZoneId.of`) /// (negative with '-' analogously) /// /// Hours must be 0–18 and minutes 0–59, with a maximum absolute offset of 18:00. @@ -1564,35 +1566,62 @@ fn parse_sign_offset(s: &str) -> Option { if rest.is_empty() || !rest.bytes().all(|b| b.is_ascii_digit() || b == b':') { return None; } - let (h, m) = if let Some(colon_pos) = rest.find(':') { + let (h, m, sec) = if let Some(colon_pos) = rest.find(':') { let h_str = &rest[..colon_pos]; - let m_str = &rest[colon_pos + 1..]; + let mut m_str = &rest[colon_pos + 1..]; + // "+HH:MM:SS": java.time.ZoneOffset accepts a seconds field with two-digit minutes and + // seconds. Spark's `getZoneId` pads a one-digit hour first, so "+8:00:00" is valid too. + let mut sec = 0; + if let Some(sec_pos) = m_str.find(':') { + let s_str = &m_str[sec_pos + 1..]; + m_str = &m_str[..sec_pos]; + if !(1..=2).contains(&h_str.len()) || m_str.len() != 2 || s_str.len() != 2 { + return None; + } + sec = s_str.parse::().ok()?; + } if !(1..=2).contains(&h_str.len()) || !(1..=2).contains(&m_str.len()) { return None; } let h: i32 = h_str.parse().ok()?; - // Note: "+HH:MM:SS" (with seconds) is not handled; Spark accepts it but it is rare. let m: i32 = m_str.parse().ok()?; - (h, m) + (h, m, sec) } else { match rest.len() { - 1 | 2 => (rest.parse::().ok()?, 0), + 1 | 2 => (rest.parse::().ok()?, 0, 0), 4 => ( rest[..2].parse::().ok()?, rest[2..].parse::().ok()?, + 0, + ), + // "+HHMMSS", as `java.time.ZoneOffset.of` accepts it ("+080000" is "+08:00"). + 6 => ( + rest[..2].parse::().ok()?, + rest[2..4].parse::().ok()?, + rest[4..].parse::().ok()?, ), _ => return None, } }; - if !(0..=18).contains(&h) || !(0..=59).contains(&m) || (h == 18 && m != 0) { + if !(0..=18).contains(&h) + || !(0..=59).contains(&m) + || !(0..=59).contains(&sec) + || (h == 18 && (m != 0 || sec != 0)) + { return None; } - Some(sign * (h * 3600 + m * 60)) + Some(sign * (h * 3600 + m * 60 + sec)) } /// Constructs a [`Tz`] from an offset measured in seconds. /// E.g. `+7*3600 + 30*60` -> `"+07:30"`. fn tz_from_offset_secs(secs: i32) -> Option { + // `Tz` offsets have minute precision, so an offset with a non-zero seconds part such as + // "+08:00:30" cannot be represented. Returning `None` leaves the value unparsed, while + // Spark accepts it: https://github.com/apache/datafusion-comet/issues/7224 + if secs % 60 != 0 { + return None; + } let abs = secs.abs(); let h = abs / 3600; let m = (abs % 3600) / 60; @@ -1600,16 +1629,82 @@ fn tz_from_offset_secs(secs: i32) -> Option { Tz::from_str(&format!("{}{:02}:{:02}", sign, h, m)).ok() } -/// Returns the last (rightmost) byte position where `needle` starts inside `haystack`. -fn rfind_str(haystack: &str, needle: &str) -> Option { - let hb = haystack.as_bytes(); - let nb = needle.as_bytes(); - if nb.len() > hb.len() { +/// `java.time.ZoneId.SHORT_IDS`, which Spark passes to `ZoneId.of` when it parses the zone +/// of a timestamp string. +const JAVA_SHORT_IDS: &[(&str, &str)] = &[ + ("ACT", "Australia/Darwin"), + ("AET", "Australia/Sydney"), + ("AGT", "America/Argentina/Buenos_Aires"), + ("ART", "Africa/Cairo"), + ("AST", "America/Anchorage"), + ("BET", "America/Sao_Paulo"), + ("BST", "Asia/Dhaka"), + ("CAT", "Africa/Harare"), + ("CNT", "America/St_Johns"), + ("CST", "America/Chicago"), + ("CTT", "Asia/Shanghai"), + ("EAT", "Africa/Addis_Ababa"), + ("ECT", "Europe/Paris"), + ("IET", "America/Indiana/Indianapolis"), + ("IST", "Asia/Kolkata"), + ("JST", "Asia/Tokyo"), + ("MIT", "Pacific/Apia"), + ("NET", "Asia/Yerevan"), + ("NST", "Pacific/Auckland"), + ("PLT", "Asia/Karachi"), + ("PNT", "America/Phoenix"), + ("PRT", "America/Puerto_Rico"), + ("PST", "America/Los_Angeles"), + ("SST", "Pacific/Guadalcanal"), + ("VST", "Asia/Ho_Chi_Minh"), + ("EST", "-05:00"), + ("MST", "-07:00"), + ("HST", "-10:00"), +]; + +/// Length of the longest tz database id (`America/Argentina/ComodRivadavia` in chrono-tz 0.10). +const MAX_ZONE_ID_LEN: usize = 32; + +/// A cheap check that runs before the `Tz` lookup, which builds an error string on every miss. +/// Every tz database id and `SHORT_IDS` alias starts with an upper-case letter and is at most +/// `MAX_ZONE_ID_LEN` bytes of `[A-Za-z0-9_+/-]`, and ids are case sensitive, so anything else +/// (for example "a timestamp" or "timestamp" in a malformed value such as "not a timestamp") +/// cannot resolve and is skipped without a lookup. +fn could_be_zone_id(id: &str) -> bool { + id.starts_with(|c: char| c.is_ascii_uppercase()) + && id.len() <= MAX_ZONE_ID_LEN + && id + .bytes() + .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'+' | b'/' | b'-')) +} + +/// Resolves a zone id that starts with a letter the way `ZoneId.of(id, SHORT_IDS)` does: +/// a `SHORT_IDS` alias first, then a tz database region id such as `CET`, `Zulu` or +/// `Etc/GMT+8`. Prefix forms such as `UTC+07:30` are not handled here. +fn zone_id_to_tz(id: &str) -> Option { + if !could_be_zone_id(id) { return None; } - (0..=(hb.len() - nb.len())) - .rev() - .find(|&i| hb[i..].starts_with(nb)) + if let Some((_, target)) = JAVA_SHORT_IDS.iter().find(|(alias, _)| *alias == id) { + return Tz::from_str(target).ok(); + } + Tz::from_str(id).ok() +} + +/// Looks for a zone id at the end of `value` that starts right after a digit or whitespace, +/// e.g. "2020-01-01 12:34:56 CET" or "2020-01-01 12:34:56America/Los_Angeles", and returns +/// the text before it together with the resolved zone. +fn extract_zone_id_suffix(value: &str) -> Option<(&str, Tz)> { + let bytes = value.as_bytes(); + (1..bytes.len()).rev().find_map(|pos| { + let prev = bytes[pos - 1]; + if !bytes[pos].is_ascii_alphabetic() + || !(prev.is_ascii_digit() || prev.is_ascii_whitespace()) + { + return None; + } + zone_id_to_tz(&value[pos..]).map(|tz| (&value[..pos], tz)) + }) } /// If `value` ends with a recognised timezone suffix, returns `(datetime_prefix, Tz)`. @@ -1620,7 +1715,9 @@ fn rfind_str(haystack: &str, needle: &str) -> Option { /// UTC / " UTC" -> UTC+0 (or UTC +/- offset, e.g. "UTC+0", " UTC+07:30") /// GMT / " GMT" -> UTC+0 (or GMT +/- offset) /// UT / " UT" -> UTC+0 (or UT +/- offset) -/// Named IANA zone -> e.g. " Europe/Moscow" +/// (not when the name follows a '/', as in "Etc/UTC" or "Etc/GMT+8") +/// Region id -> e.g. " Europe/Moscow", "America/Los_Angeles", " CET", " Zulu" +/// Java SHORT_IDS -> e.g. " PST" (America/Los_Angeles), " EST" (-05:00) /// Bare +/-offset -> e.g. "+07:30", "-1:0", "+0730" /// /// **The caller must ensure the value does not already match a base timestamp pattern.** @@ -1633,49 +1730,34 @@ fn extract_offset_suffix(value: &str) -> Option<(&str, Tz)> { // 2. Named text-prefix forms: "UTC", "GMT", "UT" (optionally space-prefixed), // each optionally followed by a bare +/-offset. - // Longest first so " UTC" is tried before " UT", etc. - for prefix in &[" UTC", "UTC", " GMT", "GMT", " UT", "UT"] { - if let Some(pos) = rfind_str(value, prefix) { - let offset_str = &value[pos + prefix.len()..]; + // These are the most common named suffixes, so they are matched before the region + // lookup below. The offset can only contain signs, digits and colons, so the name + // must end right where that trailing run starts. A name that follows a '/' is part of + // a region id such as "Etc/UTC" or "Etc/GMT+8" (UTC-8, not UTC+8) and is left to the + // region lookup. + let name_end = value + .trim_end_matches(|c: char| c.is_ascii_digit() || matches!(c, '+' | '-' | ':')) + .len(); + let (head, offset_str) = value.split_at(name_end); + if let Some(before) = ["UTC", "GMT", "UT"] + .iter() + .find_map(|name| head.strip_suffix(name)) + { + if !before.ends_with('/') { if let Some(secs) = parse_sign_offset(offset_str) { - return Some((&value[..pos], tz_from_offset_secs(secs)?)); - } - } - } - - // 3. Java SHORT_IDS fixed-offset abbreviations recognised by ZoneId.of() via SHORT_IDS map. - // Only three have purely fixed offsets (no '/'): - // EST -> -05:00 (-18 000 s) - // MST -> -07:00 (-25 200 s) - // HST -> -10:00 (-36 000 s) - // These may appear with or without a leading space; no sub-offset is allowed after them. - for (abbr, offset_secs) in &[ - (" EST", -18_000i32), - ("EST", -18_000), - (" MST", -25_200), - ("MST", -25_200), - (" HST", -36_000), - ("HST", -36_000), - ] { - if let Some(pos) = rfind_str(value, abbr) { - if pos + abbr.len() == value.len() { - return Some((&value[..pos], tz_from_offset_secs(*offset_secs)?)); + let before = before.strip_suffix(' ').unwrap_or(before); + return Some((before, tz_from_offset_secs(secs)?)); } } } - // 4. Named IANA timezone: a space followed by a slash-containing word at the end. - // e.g. "2015-03-18T12:03:17.123456 Europe/Moscow" - if let Some(space_pos) = value.rfind(' ') { - let tz_name = &value[space_pos + 1..]; - if tz_name.contains('/') { - if let Ok(tz) = Tz::from_str(tz_name) { - return Some((&value[..space_pos], tz)); - } - } + // 3. Region ids and Java `ZoneId.SHORT_IDS` aliases, with or without a leading space: + // " Europe/Moscow", "America/Los_Angeles", " CET", " Zulu", " Etc/GMT+8", " PST". + if let Some((prefix, tz)) = extract_zone_id_suffix(value) { + return Some((prefix, tz)); } - // 5. Bare +/-offset: find the rightmost '+' or '-' and try to parse everything + // 4. Bare +/-offset: find the rightmost '+' or '-' and try to parse everything // from that position to the end as a complete valid offset. let last_sign = { let p = value.rfind('+'); @@ -3491,6 +3573,144 @@ mod tests { ); } + #[test] + #[cfg_attr(miri, ignore)] + fn timestamp_parser_zone_id_suffix_test() { + let tz = &Tz::from_str("UTC").unwrap(); + // 2020-01-01T12:34:56Z + let utc: i64 = 1577882096000000; + let hour: i64 = 3600 * 1_000_000; + let cases = [ + // Java ZoneId.SHORT_IDS aliases + ("2020-01-01 12:34:56 PST", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56PST", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56 CST", Some(utc + 6 * hour)), + ("2020-01-01 12:34:56 JST", Some(utc - 9 * hour)), + ("2020-01-01 12:34:56 EST", Some(utc + 5 * hour)), + // Region ids without a '/' + ("2020-01-01 12:34:56 CET", Some(utc - hour)), + ("2020-01-01 12:34:56Zulu", Some(utc)), + ("2020-01-01 12:34:56 Zulu", Some(utc)), + // Region id with no space before it + ( + "2020-01-01 12:34:56America/Los_Angeles", + Some(utc + 8 * hour), + ), + ( + "2020-01-01 12:34:56.123America/Los_Angeles", + Some(utc + 8 * hour + 123_000), + ), + // Etc/GMT+8 is UTC-8; it must not be read as the "GMT+8" prefix form + ("2020-01-01 12:34:56 Etc/GMT+8", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56 Etc/GMT-8", Some(utc - 8 * hour)), + // Offset with a seconds field, including a one-digit hour as Spark pads it + ("2020-01-01 12:34:56 +08:00:00", Some(utc - 8 * hour)), + ("2020-01-01 12:34:56-08:00:00", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56 +8:00:00", Some(utc - 8 * hour)), + ("2020-01-01 12:34:56-8:00:00", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56 UTC+8:00:00", Some(utc - 8 * hour)), + ("2020-01-01 12:34:56 GMT-8:00:00", Some(utc + 8 * hour)), + // "+HHMMSS" without colons, bare and after UTC/GMT + ("2020-01-01 12:34:56 +080000", Some(utc - 8 * hour)), + ("2020-01-01 12:34:56-080000", Some(utc + 8 * hour)), + ("2020-01-01 12:34:56 UTC-080000", Some(utc + 8 * hour)), + ( + "2020-01-01 12:34:56 GMT+053000", + Some(utc - 5 * hour - 30 * 60 * 1_000_000), + ), + ("2020-01-01 12:34:56 +080030", None), + ("2020-01-01 12:34:56 +08000", None), + // A non-zero seconds part is not supported yet (`Tz` has minute precision), + // see https://github.com/apache/datafusion-comet/issues/7224 + ("2020-01-01 12:34:56 +08:00:30", None), + // UTC/GMT/UT names, and region ids with those names after a '/' + ("2020-01-01 12:34:56 UTC", Some(utc)), + ("2020-01-01 12:34:56UTC", Some(utc)), + ("2020-01-01 12:34:56 GMT", Some(utc)), + ("2020-01-01 12:34:56 Etc/UTC", Some(utc)), + ("2020-01-01 12:34:56 Etc/GMT", Some(utc)), + ("2020-01-01 12:34:56 UT-1", Some(utc + hour)), + ("2020-01-01 12:34:56Etc/GMT+8", Some(utc + 8 * hour)), + // Prefix forms still go through the UTC/GMT/UT rule + ("2020-01-01 12:34:56 GMT+8", Some(utc - 8 * hour)), + ("2020-01-01 12:34:56 UTC-08:00", Some(utc + 8 * hour)), + // Zone ids are case sensitive, and unknown ids are rejected + ("2020-01-01 12:34:56 pst", None), + ("2020-01-01 12:34:56 XYZ", None), + ("2020-01-01 12:34:56 +08:00:0", None), + ("2020-01-01 12:34:56 +8:0:00", None), + ("2020-01-01 12:34:56 +108:00:00", None), + ]; + for (input, expected) in cases { + assert_eq!( + timestamp_parser(input, EvalMode::Legacy, tz, true).unwrap(), + expected, + "{input}" + ); + } + } + + #[test] + #[cfg_attr(miri, ignore)] + fn timestamp_parser_short_id_summer_test() { + // In summer the SHORT_IDS aliases that map to a region follow its daylight saving + // time, while the fixed-offset ones do not: PST is America/Los_Angeles (PDT, -07:00), + // but EST is always -05:00. + let tz = &Tz::from_str("UTC").unwrap(); + // 2020-07-01T12:34:56Z + let utc: i64 = 1593606896000000; + let hour: i64 = 3600 * 1_000_000; + let cases = [ + ("2020-07-01 12:34:56 PST", Some(utc + 7 * hour)), + ("2020-07-01 12:34:56 CST", Some(utc + 5 * hour)), + ("2020-07-01 12:34:56 ECT", Some(utc - 2 * hour)), + ("2020-07-01 12:34:56 EST", Some(utc + 5 * hour)), + ("2020-07-01 12:34:56 MST", Some(utc + 7 * hour)), + ("2020-07-01 12:34:56 HST", Some(utc + 10 * hour)), + ( + "2020-07-01 12:34:56 America/Los_Angeles", + Some(utc + 7 * hour), + ), + ]; + for (input, expected) in cases { + assert_eq!( + timestamp_parser(input, EvalMode::Legacy, tz, true).unwrap(), + expected, + "{input}" + ); + } + } + + #[test] + fn java_short_ids_resolve_test() { + // Every SHORT_IDS target must resolve through `Tz`, so that a tz database rename + // cannot silently turn an alias into NULL. + for (alias, target) in JAVA_SHORT_IDS { + assert!( + Tz::from_str(target).is_ok(), + "SHORT_IDS alias {alias} maps to {target}, which Tz cannot resolve" + ); + assert!(zone_id_to_tz(alias).is_some(), "{alias}"); + } + assert_eq!(JAVA_SHORT_IDS.len(), 28); + } + + #[test] + fn could_be_zone_id_test() { + assert!(could_be_zone_id("America/Argentina/ComodRivadavia")); + assert!(could_be_zone_id("Etc/GMT+8")); + assert!(could_be_zone_id("America/Port-au-Prince")); + assert!(could_be_zone_id("PST")); + for (alias, _) in JAVA_SHORT_IDS { + assert!(could_be_zone_id(alias), "{alias}"); + } + assert!(!could_be_zone_id("a timestamp")); + assert!(!could_be_zone_id("timestamp")); + assert!(!could_be_zone_id("pst")); + assert!(!could_be_zone_id("Europe/Moscow!")); + assert!(!could_be_zone_id(&"A".repeat(MAX_ZONE_ID_LEN + 1))); + } + #[test] #[cfg_attr(miri, ignore)] fn timestamp_parser_dst_test() { diff --git a/spark/src/test/resources/sql-tests/expressions/cast/cast_string_to_timestamp_zone_ids.sql b/spark/src/test/resources/sql-tests/expressions/cast/cast_string_to_timestamp_zone_ids.sql new file mode 100644 index 00000000000..18b60c3a067 --- /dev/null +++ b/spark/src/test/resources/sql-tests/expressions/cast/cast_string_to_timestamp_zone_ids.sql @@ -0,0 +1,100 @@ +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. + +-- Zone suffixes that Spark resolves through ZoneId.of(id, ZoneId.SHORT_IDS): SHORT_IDS +-- aliases, region ids and offsets with a seconds field. Every one of the 28 SHORT_IDS aliases +-- appears with a January and a July date, so Spark is the reference for each alias target, +-- including the daylight saving time of the region ones (PST follows PDT, NST follows NZST/NZDT) +-- and the fixed-offset ones (EST stays -05:00). +-- Config: spark.sql.ansi.enabled=false +-- ConfigMatrix: spark.sql.session.timeZone=UTC,America/Los_Angeles + +statement +CREATE TABLE zone_id_valid(s string) USING parquet + +statement +INSERT INTO zone_id_valid VALUES + ('2020-01-15 12:34:56 ACT'), ('2020-07-15 12:34:56 ACT'), + ('2020-01-15 12:34:56 AET'), ('2020-07-15 12:34:56 AET'), + ('2020-01-15 12:34:56 AGT'), ('2020-07-15 12:34:56 AGT'), + ('2020-01-15 12:34:56 ART'), ('2020-07-15 12:34:56 ART'), + ('2020-01-15 12:34:56 AST'), ('2020-07-15 12:34:56 AST'), + ('2020-01-15 12:34:56 BET'), ('2020-07-15 12:34:56 BET'), + ('2020-01-15 12:34:56 BST'), ('2020-07-15 12:34:56 BST'), + ('2020-01-15 12:34:56 CAT'), ('2020-07-15 12:34:56 CAT'), + ('2020-01-15 12:34:56 CNT'), ('2020-07-15 12:34:56 CNT'), + ('2020-01-15 12:34:56 CST'), ('2020-07-15 12:34:56 CST'), + ('2020-01-15 12:34:56 CTT'), ('2020-07-15 12:34:56 CTT'), + ('2020-01-15 12:34:56 EAT'), ('2020-07-15 12:34:56 EAT'), + ('2020-01-15 12:34:56 ECT'), ('2020-07-15 12:34:56 ECT'), + ('2020-01-15 12:34:56 IET'), ('2020-07-15 12:34:56 IET'), + ('2020-01-15 12:34:56 IST'), ('2020-07-15 12:34:56 IST'), + ('2020-01-15 12:34:56 JST'), ('2020-07-15 12:34:56 JST'), + ('2020-01-15 12:34:56 MIT'), ('2020-07-15 12:34:56 MIT'), + ('2020-01-15 12:34:56 NET'), ('2020-07-15 12:34:56 NET'), + ('2020-01-15 12:34:56 NST'), ('2020-07-15 12:34:56 NST'), + ('2020-01-15 12:34:56 PLT'), ('2020-07-15 12:34:56 PLT'), + ('2020-01-15 12:34:56 PNT'), ('2020-07-15 12:34:56 PNT'), + ('2020-01-15 12:34:56 PRT'), ('2020-07-15 12:34:56 PRT'), + ('2020-01-15 12:34:56 PST'), ('2020-07-15 12:34:56 PST'), + ('2020-01-15 12:34:56 SST'), ('2020-07-15 12:34:56 SST'), + ('2020-01-15 12:34:56 VST'), ('2020-07-15 12:34:56 VST'), + ('2020-01-15 12:34:56 EST'), ('2020-07-15 12:34:56 EST'), + ('2020-01-15 12:34:56 MST'), ('2020-07-15 12:34:56 MST'), + ('2020-01-15 12:34:56 HST'), ('2020-07-15 12:34:56 HST'), + ('2020-01-01 12:34:56 CET'), ('2020-07-01 12:34:56 CET'), + ('2020-01-01 12:34:56Zulu'), ('2020-01-01 12:34:56 Zulu'), + ('2020-07-01 12:34:56America/Los_Angeles'), ('2020-07-01 12:34:56.123 America/Los_Angeles'), + ('2020-01-01 12:34:56 Etc/GMT+8'), ('2020-01-01 12:34:56 Etc/GMT-8'), + ('2020-01-01 12:34:56 Etc/UTC'), ('2020-01-01 12:34:56 UTC'), ('2020-01-01 12:34:56GMT'), + ('2020-01-01 12:34:56 +08:00:00'), ('2020-01-01 12:34:56 +8:00:00'), + ('2020-01-01 12:34:56 UTC+8:00:00'), ('2020-01-01 12:34:56-08:00:00'), + ('2020-01-01 12:34:56 +080000'), ('2020-01-01 12:34:56 UTC-080000'), + (NULL) + +statement +CREATE TABLE zone_id_invalid(s string) USING parquet + +statement +INSERT INTO zone_id_invalid VALUES + ('2020-01-01 12:34:56 pst'), ('2020-01-01 12:34:56 XYZ'), + ('2020-01-01 12:34:56 +08:00:0'), ('2020-01-01 12:34:56 +8:0:00') + +query expect_native(cast) +SELECT s, cast(s AS timestamp), cast(s AS timestamp_ntz) FROM zone_id_valid + +query expect_native(cast) +SELECT s, cast(s AS timestamp), cast(s AS timestamp_ntz) FROM zone_id_invalid + +query expect_native(try_cast) +SELECT s, try_cast(s AS timestamp), try_cast(s AS timestamp_ntz) +FROM (SELECT s FROM zone_id_valid UNION ALL SELECT s FROM zone_id_invalid) + +statement +SET spark.sql.ansi.enabled=true + +query expect_native(cast) +SELECT s, cast(s AS timestamp), cast(s AS timestamp_ntz) FROM zone_id_valid + +query expect_error(CAST_INVALID_INPUT) +SELECT cast(s AS timestamp) FROM zone_id_invalid WHERE s = '2020-01-01 12:34:56 pst' + +query expect_error(CAST_INVALID_INPUT) +SELECT cast(s AS timestamp) FROM zone_id_invalid WHERE s = '2020-01-01 12:34:56 +8:0:00' + +query expect_error(CAST_INVALID_INPUT) +SELECT cast('2020-01-01 12:34:56 XYZ' AS timestamp)