Skip to content

Commit ba0c2bb

Browse files
authored
feat(query): Interval kin support iw and hh24 (#18114)
`iw` is ISOWeek. `hh24` is hour. In main will return parse err: ```sql SELECT TRUNC(DATE '2024-01-01', 'IW'); select TRUNC('2025-06-09 10:11:12'::Datetime, 'HH24'); ``` In pr: ```sql query T SELECT TRUNC('2024-01-01'::Date, 'IW'); ---- 2024-01-01 query T select TRUNC('2025-06-09 10:11:12'::Datetime, 'HH24'); ---- 2025-06-09 10:00:00.000000 ```
1 parent 30c41b5 commit ba0c2bb

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed

src/query/ast/src/ast/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ pub enum IntervalKind {
896896
Second,
897897
Doy,
898898
Week,
899+
ISOWeek,
899900
Dow,
900901
Epoch,
901902
MicroSecond,
@@ -921,6 +922,7 @@ impl Display for IntervalKind {
921922
IntervalKind::YearWeek => "YEARWEEK",
922923
IntervalKind::Millennium => "MILLENNIUM",
923924
IntervalKind::Week => "WEEK",
925+
IntervalKind::ISOWeek => "ISOWEEK",
924926
IntervalKind::Epoch => "EPOCH",
925927
IntervalKind::MicroSecond => "MICROSECOND",
926928
})

src/query/ast/src/parser/expr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,7 @@ pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
18661866
let doy = value(IntervalKind::Doy, rule! { DOY });
18671867
let dow = value(IntervalKind::Dow, rule! { DOW });
18681868
let isodow = value(IntervalKind::ISODow, rule! { ISODOW });
1869+
let isoweek = value(IntervalKind::ISOWeek, rule! { ISOWEEK });
18691870
let week = value(IntervalKind::Week, rule! { WEEK });
18701871
let epoch = value(IntervalKind::Epoch, rule! { EPOCH });
18711872
let microsecond = value(IntervalKind::MicroSecond, rule! { MICROSECOND });
@@ -1925,6 +1926,7 @@ pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
19251926
rule! { #literal_string_eq_ignore_case("HOUR")
19261927
| #literal_string_eq_ignore_case("H")
19271928
| #literal_string_eq_ignore_case("HH")
1929+
| #literal_string_eq_ignore_case("HH24")
19281930
| #literal_string_eq_ignore_case("HR")
19291931
| #literal_string_eq_ignore_case("HOURS")
19301932
| #literal_string_eq_ignore_case("HRS")
@@ -1991,6 +1993,11 @@ pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
19911993
},
19921994
);
19931995

1996+
let isoweek_str = value(
1997+
IntervalKind::ISOWeek,
1998+
rule! { #literal_string_eq_ignore_case("IW") },
1999+
);
2000+
19942001
let epoch_str = value(
19952002
IntervalKind::Epoch,
19962003
rule! { #literal_string_eq_ignore_case("EPOCH")
@@ -2037,6 +2044,7 @@ pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
20372044
| #epoch
20382045
| #microsecond
20392046
| #isodow
2047+
| #isoweek
20402048
| #millennium
20412049
| #yearweek
20422050
),
@@ -2055,6 +2063,7 @@ pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
20552063
| #epoch_str
20562064
| #microsecond_str
20572065
| #isodow_str
2066+
| #isoweek_str
20582067
| #yearweek_str
20592068
| #millennium_str
20602069
),

src/query/ast/src/parser/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ pub enum TokenKind {
763763
IS,
764764
#[token("ISODOW", ignore(ascii_case))]
765765
ISODOW,
766+
#[token("ISOWEEK", ignore(ascii_case))]
767+
ISOWEEK,
766768
#[token("ISOYEAR", ignore(ascii_case))]
767769
ISOYEAR,
768770
#[token("JOIN", ignore(ascii_case))]

src/query/functions/src/scalars/timestamp/src/datetime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,7 @@ fn register_rounder_functions(registry: &mut FunctionRegistry) {
23242324
);
23252325

23262326
// date | timestamp -> date
2327+
registry.register_aliases("to_monday", &["to_start_of_iso_week"]);
23272328
rounder_functions_helper::<ToLastMonday>(registry, "to_monday");
23282329
rounder_functions_helper::<ToLastSunday>(registry, "to_start_of_week");
23292330
rounder_functions_helper::<ToStartOfMonth>(registry, "to_start_of_month");

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ substring_utf8 -> substr
7575
subtract -> minus
7676
to_char -> to_string
7777
to_datetime -> to_timestamp
78+
to_start_of_iso_week -> to_monday
7879
to_text -> to_string
7980
to_varchar -> to_string
8081
trunc -> truncate

src/query/sql/src/planner/semantic/type_check.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,10 @@ impl<'a> TypeChecker<'a> {
32263226
ASTIntervalKind::Millennium => {
32273227
self.resolve_function(span, "millennium", vec![], &[arg])
32283228
}
3229+
ASTIntervalKind::ISOWeek => Err(ErrorCode::SemanticError(
3230+
"Not support interval type ISOWeek".to_string(),
3231+
)
3232+
.set_span(span)),
32293233
}
32303234
}
32313235

@@ -3312,6 +3316,13 @@ impl<'a> TypeChecker<'a> {
33123316
}],
33133317
)
33143318
}
3319+
ASTIntervalKind::ISOWeek => {
3320+
self.resolve_function(
3321+
span,
3322+
"to_start_of_iso_week", vec![],
3323+
&[date],
3324+
)
3325+
}
33153326
ASTIntervalKind::Day => {
33163327
self.resolve_function(
33173328
span,

tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ select trunc('2025-02-05 00:01:00', week);
292292
----
293293
2025-02-03
294294

295+
296+
query T
297+
SELECT TRUNC('2025-06-11'::Date, 'IW');
298+
----
299+
2025-06-09
300+
301+
query T
302+
SELECT TRUNC('2025-06-09'::Date, 'IW');
303+
----
304+
2025-06-09
305+
306+
query T
307+
SELECT TRUNC('2023-12-31'::Date, 'IW');
308+
----
309+
2023-12-25
310+
311+
query T
312+
SELECT TRUNC('2024-01-01'::Date, 'IW');
313+
----
314+
2024-01-01
315+
316+
query T
317+
select TRUNC('2025-06-09 10:11:12'::Datetime, 'HH24');
318+
----
319+
2025-06-09 10:00:00.000000
320+
295321
query FF
296322
SELECT
297323
MONTHS_BETWEEN('2019-03-15'::DATE,

0 commit comments

Comments
 (0)