From bf777a0fb180af90ad088681068186604c3f9ec0 Mon Sep 17 00:00:00 2001 From: xfz <73645462+xuefengze@users.noreply.github.com> Date: Wed, 24 Jan 2024 13:38:54 +0800 Subject: [PATCH] feat: support ISO 8601 interval parsing (#14747) --- e2e_test/batch/types/interval.slt.part | 18 ++++++++++++++++++ src/common/src/types/interval.rs | 13 +++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/e2e_test/batch/types/interval.slt.part b/e2e_test/batch/types/interval.slt.part index b6eef0915ab3c..629f2c77ef8b7 100644 --- a/e2e_test/batch/types/interval.slt.part +++ b/e2e_test/batch/types/interval.slt.part @@ -185,6 +185,24 @@ select v / d from t; 00:01:19.101562 00:03:57.304688 +query T +select 'P1Y2M3DT4H5M6S'::interval; +---- +1 year 2 mons 3 days 04:05:06 + +query T +select 'P0Y0M0DT0H0M0S'::interval; +---- +00:00:00 + +query T +select 'P12Y2M30DT4H5M0.1234567S'::interval; +---- +12 years 2 mons 30 days 04:05:00.123456 + +statement error +select 'P0Y0M0DT0H0M0S1'::interval; + # The following is an overflow bug present in PostgreSQL 15.2 # Their `days` overflows to a negative value, leading to the latter smaller # than the former. We report an error in this case. diff --git a/src/common/src/types/interval.rs b/src/common/src/types/interval.rs index 9224c8955db66..44470e29acf77 100644 --- a/src/common/src/types/interval.rs +++ b/src/common/src/types/interval.rs @@ -1051,7 +1051,7 @@ impl Interval { pub fn from_iso_8601(s: &str) -> ParseResult { // ISO pattern - PnYnMnDTnHnMnS static ISO_8601_REGEX: LazyLock = LazyLock::new(|| { - Regex::new(r"P([0-9]+)Y([0-9]+)M([0-9]+)DT([0-9]+)H([0-9]+)M([0-9]+(?:\.[0-9]+)?)S") + Regex::new(r"^P([0-9]+)Y([0-9]+)M([0-9]+)DT([0-9]+)H([0-9]+)M([0-9]+(?:\.[0-9]+)?)S$") .unwrap() }); // wrap into a closure to simplify error handling @@ -1453,7 +1453,10 @@ impl Interval { if let Some(leading_field) = leading_field { Self::parse_sql_standard(s, leading_field) } else { - Self::parse_postgres(s) + match s.as_bytes().get(0) { + Some(b'P') => Self::from_iso_8601(s), + _ => Self::parse_postgres(s), + } } } } @@ -1530,6 +1533,12 @@ mod tests { interval, Interval::from_month(14) + Interval::from_days(3) + Interval::from_minutes(62) ); + + let interval = "P1Y2M3DT0H5M0S".parse::().unwrap(); + assert_eq!( + interval, + Interval::from_month(14) + Interval::from_days(3) + Interval::from_minutes(5) + ); } #[test]