diff --git a/test/distributed/cases/function/func_datetime_interval_arithmetic.result b/test/distributed/cases/function/func_datetime_interval_arithmetic.result new file mode 100644 index 0000000000000..9e02e9e1169c6 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_interval_arithmetic.result @@ -0,0 +1,148 @@ +SELECT DATE_ADD('2026-05-07', INTERVAL 3 DAY); +DATE_ADD(2026-05-07, INTERVAL 3 day) +2026-05-10 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 HOUR); +DATE_ADD(2026-05-07, INTERVAL 3 hour) +2026-05-07 03:00:00 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 MINUTE); +DATE_ADD(2026-05-07, INTERVAL 3 minute) +2026-05-07 00:03:00 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 SECOND); +DATE_ADD(2026-05-07, INTERVAL 3 second) +2026-05-07 00:00:03 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 MONTH); +DATE_ADD(2026-05-07, INTERVAL 3 month) +2026-08-07 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 YEAR); +DATE_ADD(2026-05-07, INTERVAL 3 year) +2029-05-07 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 WEEK); +DATE_ADD(2026-05-07, INTERVAL 3 week) +2026-05-28 +SELECT DATE_ADD('2026-05-07', INTERVAL 3 QUARTER); +DATE_ADD(2026-05-07, INTERVAL 3 quarter) +2027-02-07 +SELECT DATE_SUB('2026-05-07', INTERVAL 5+1 DAY); +DATE_SUB(2026-05-07, INTERVAL 5 + 1 day) +2026-05-01 +SELECT DATE_SUB('2026-05-07', INTERVAL 5-1 DAY); +DATE_SUB(2026-05-07, INTERVAL 5 - 1 day) +2026-05-03 +SELECT DATE_SUB('2026-05-07', INTERVAL 2*3 DAY); +DATE_SUB(2026-05-07, INTERVAL 2 * 3 day) +2026-05-01 +SELECT DATE_SUB('2026-05-07', INTERVAL 10/2 DAY); +DATE_SUB(2026-05-07, INTERVAL 10 / 2 day) +2026-05-02 +SELECT DATE_SUB('2026-05-07', INTERVAL 10%3 DAY); +DATE_SUB(2026-05-07, INTERVAL 10 % 3 day) +2026-05-06 +SELECT DATE_SUB('2026-05-07', INTERVAL 10 MOD 3 DAY); +DATE_SUB(2026-05-07, INTERVAL 10 % 3 day) +2026-05-06 +SELECT DATE_SUB('2026-05-07', INTERVAL 10 DIV 3 DAY); +DATE_SUB(2026-05-07, INTERVAL 10 div 3 day) +2026-05-04 +SELECT DATE_SUB('2026-05-07', INTERVAL -3 DAY); +DATE_SUB(2026-05-07, INTERVAL -3 day) +2026-05-10 +SELECT DATE_ADD('2026-05-07', INTERVAL -3 DAY); +DATE_ADD(2026-05-07, INTERVAL -3 day) +2026-05-04 +SELECT DATE_SUB('2026-05-07', INTERVAL (1+2)%3 DAY); +DATE_SUB(2026-05-07, INTERVAL +(1 + 2) % 3 day) +2026-05-07 +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY); +DATE_SUB(2026-05-07, INTERVAL +(DAYOFWEEK(2026-05-07) + 5) % 7 day) +2026-05-04 +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY); +DATE_SUB(2026-05-07, INTERVAL +(DAYOFWEEK(2026-05-07) + 5) % 7 + 7 day) +2026-04-27 +SELECT DATE_ADD('2026-05-07', INTERVAL (3+4)%5 DAY); +DATE_ADD(2026-05-07, INTERVAL +(3 + 4) % 5 day) +2026-05-09 +SELECT DATE_ADD('2026-05-07', INTERVAL 3%2+7%3 HOUR); +DATE_ADD(2026-05-07, INTERVAL 3 % 2 + 7 % 3 hour) +2026-05-07 02:00:00 +SELECT DATE_SUB('2026-05-07', INTERVAL 10%3 HOUR); +DATE_SUB(2026-05-07, INTERVAL 10 % 3 hour) +2026-05-06 23:00:00 +SELECT DATE_SUB('2026-05-07', INTERVAL 100%7 MINUTE); +DATE_SUB(2026-05-07, INTERVAL 100 % 7 minute) +2026-05-06 23:58:00 +SELECT '2026-05-07' - INTERVAL 3 DAY; +2026-05-07 - INTERVAL 3 day +2026-05-04 +SELECT '2026-05-07' + INTERVAL 3 DAY; +2026-05-07 + INTERVAL 3 day +2026-05-10 +SELECT '2026-05-07' - INTERVAL 5+1 DAY; +2026-05-07 - INTERVAL 5 + 1 day +2026-05-01 +SELECT '2026-05-07' + INTERVAL 2*3 DAY; +2026-05-07 + INTERVAL 2 * 3 day +2026-05-13 +SELECT '2026-05-07' - INTERVAL (1+2)%3 DAY; +2026-05-07 - INTERVAL +(1 + 2) % 3 day +2026-05-07 +SELECT '2026-05-07' + INTERVAL (3+4)%5 DAY; +2026-05-07 + INTERVAL +(3 + 4) % 5 day +2026-05-09 +SELECT DATE_SUB('2026-05-07', INTERVAL ABS(-3) DAY); +DATE_SUB(2026-05-07, INTERVAL ABS(-3) day) +2026-05-04 +SELECT DATE_ADD('2026-05-07', INTERVAL ABS(-5)%3 DAY); +DATE_ADD(2026-05-07, INTERVAL ABS(-5) % 3 day) +2026-05-09 +drop table if exists t1; +create table t1(id int, d date, offset_days int); +insert into t1 values(1, '2026-05-07', 3),(2, '2026-05-07', 7),(3, '2026-05-07', 14); +select id, DATE_SUB(d, INTERVAL offset_days DAY) from t1; +id DATE_SUB(d, INTERVAL offset_days day) +1 2026-05-04 +2 2026-04-30 +3 2026-04-23 +select id, DATE_ADD(d, INTERVAL offset_days DAY) from t1; +id DATE_ADD(d, INTERVAL offset_days day) +1 2026-05-10 +2 2026-05-14 +3 2026-05-21 +drop table t1; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-10'),(4, '2026-05-14'); +select * from t1 where d >= DATE_SUB('2026-05-07', INTERVAL 3 DAY) and d <= DATE_ADD('2026-05-07', INTERVAL 3 DAY); +id d +1 2026-05-04 +2 2026-05-07 +3 2026-05-10 +drop table t1; +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) as this_week_start; +this_week_start +2026-05-04 +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY) as last_week_start; +last_week_start +2026-04-27 +drop table if exists events; +create table events(id int, event_date date, val int); +insert into events values(1,'2026-05-05',10),(2,'2026-05-06',20),(3,'2026-05-07',30); +insert into events values(4,'2026-04-28',40),(5,'2026-04-29',50),(6,'2026-04-30',60); +insert into events values(7,'2026-04-21',70); +select +sum(case when event_date >= DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) then val else 0 end) as this_week_sum, +sum(case when event_date >= DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY) +and event_date < DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) then val else 0 end) as last_week_sum +from events; +this_week_sum last_week_sum +60 150 +drop table events; +drop table if exists t1; +create table t1(id int, dt datetime, ts timestamp); +insert into t1 values(1, '2026-05-07 10:00:00', '2026-05-07 10:00:00'); +select DATE_ADD(dt, INTERVAL 2 HOUR), DATE_SUB(ts, INTERVAL 30 MINUTE) from t1; +DATE_ADD(dt, INTERVAL 2 hour) DATE_SUB(ts, INTERVAL 30 minute) +2026-05-07 12:00:00 2026-05-07 09:30:00 +select DATE_ADD(dt, INTERVAL 10%3 HOUR), DATE_SUB(ts, INTERVAL (2+1)%2 HOUR) from t1; +DATE_ADD(dt, INTERVAL 10 % 3 hour) DATE_SUB(ts, INTERVAL +(2 + 1) % 2 hour) +2026-05-07 11:00:00 2026-05-07 09:00:00 +drop table t1; diff --git a/test/distributed/cases/function/func_datetime_interval_arithmetic.test b/test/distributed/cases/function/func_datetime_interval_arithmetic.test new file mode 100644 index 0000000000000..65e71603b5531 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_interval_arithmetic.test @@ -0,0 +1,85 @@ +-- DATE_ADD/DATE_SUB with various INTERVAL units +SELECT DATE_ADD('2026-05-07', INTERVAL 3 DAY); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 HOUR); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 MINUTE); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 SECOND); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 MONTH); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 YEAR); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 WEEK); +SELECT DATE_ADD('2026-05-07', INTERVAL 3 QUARTER); + +-- DATE_SUB with simple arithmetic in INTERVAL +SELECT DATE_SUB('2026-05-07', INTERVAL 5+1 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 5-1 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 2*3 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 10/2 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 10%3 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 10 MOD 3 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL 10 DIV 3 DAY); + +-- Negative interval +SELECT DATE_SUB('2026-05-07', INTERVAL -3 DAY); +SELECT DATE_ADD('2026-05-07', INTERVAL -3 DAY); + +-- (expr) % N pattern (issue #24235 original case) +SELECT DATE_SUB('2026-05-07', INTERVAL (1+2)%3 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY); +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY); +SELECT DATE_ADD('2026-05-07', INTERVAL (3+4)%5 DAY); +SELECT DATE_ADD('2026-05-07', INTERVAL 3%2+7%3 HOUR); + +-- Mixed arithmetic with modulo in different units +SELECT DATE_SUB('2026-05-07', INTERVAL 10%3 HOUR); +SELECT DATE_SUB('2026-05-07', INTERVAL 100%7 MINUTE); + +-- date +/- INTERVAL syntax +SELECT '2026-05-07' - INTERVAL 3 DAY; +SELECT '2026-05-07' + INTERVAL 3 DAY; +SELECT '2026-05-07' - INTERVAL 5+1 DAY; +SELECT '2026-05-07' + INTERVAL 2*3 DAY; +SELECT '2026-05-07' - INTERVAL (1+2)%3 DAY; +SELECT '2026-05-07' + INTERVAL (3+4)%5 DAY; + +-- INTERVAL with function call as operand +SELECT DATE_SUB('2026-05-07', INTERVAL ABS(-3) DAY); +SELECT DATE_ADD('2026-05-07', INTERVAL ABS(-5)%3 DAY); + +-- INTERVAL with table +drop table if exists t1; +create table t1(id int, d date, offset_days int); +insert into t1 values(1, '2026-05-07', 3),(2, '2026-05-07', 7),(3, '2026-05-07', 14); +select id, DATE_SUB(d, INTERVAL offset_days DAY) from t1; +select id, DATE_ADD(d, INTERVAL offset_days DAY) from t1; +drop table t1; + +-- INTERVAL in WHERE clause +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-10'),(4, '2026-05-14'); +select * from t1 where d >= DATE_SUB('2026-05-07', INTERVAL 3 DAY) and d <= DATE_ADD('2026-05-07', INTERVAL 3 DAY); +drop table t1; + +-- NL2SQL typical pattern: current week calculation using DAYOFWEEK + modulo +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) as this_week_start; +SELECT DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY) as last_week_start; + +-- Full NL2SQL pattern: this week vs last week with table +drop table if exists events; +create table events(id int, event_date date, val int); +insert into events values(1,'2026-05-05',10),(2,'2026-05-06',20),(3,'2026-05-07',30); +insert into events values(4,'2026-04-28',40),(5,'2026-04-29',50),(6,'2026-04-30',60); +insert into events values(7,'2026-04-21',70); +select + sum(case when event_date >= DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) then val else 0 end) as this_week_sum, + sum(case when event_date >= DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7+7 DAY) + and event_date < DATE_SUB('2026-05-07', INTERVAL (DAYOFWEEK('2026-05-07')+5)%7 DAY) then val else 0 end) as last_week_sum +from events; +drop table events; + +-- INTERVAL with various date types +drop table if exists t1; +create table t1(id int, dt datetime, ts timestamp); +insert into t1 values(1, '2026-05-07 10:00:00', '2026-05-07 10:00:00'); +select DATE_ADD(dt, INTERVAL 2 HOUR), DATE_SUB(ts, INTERVAL 30 MINUTE) from t1; +select DATE_ADD(dt, INTERVAL 10%3 HOUR), DATE_SUB(ts, INTERVAL (2+1)%2 HOUR) from t1; +drop table t1; diff --git a/test/distributed/cases/function/func_datetime_week.result b/test/distributed/cases/function/func_datetime_week.result new file mode 100644 index 0000000000000..b52abb1ddf5e2 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_week.result @@ -0,0 +1,203 @@ +SELECT week('2026-05-07'); +week(2026-05-07) +18 +SELECT week('2026-05-07', 0); +week(2026-05-07, 0) +18 +SELECT week('2026-05-07', 1); +week(2026-05-07, 1) +19 +SELECT week('2026-05-07', 2); +week(2026-05-07, 2) +18 +SELECT week('2026-05-07', 3); +week(2026-05-07, 3) +19 +SELECT week('2026-05-07', 4); +week(2026-05-07, 4) +18 +SELECT week('2026-05-07', 5); +week(2026-05-07, 5) +18 +SELECT week('2026-05-07', 6); +week(2026-05-07, 6) +18 +SELECT week('2026-05-07', 7); +week(2026-05-07, 7) +18 +SELECT week(NULL); +week(null) +null +SELECT week(NULL, 3); +week(null, 3) +null +SELECT week('2026-01-01 12:00:00', 3); +week(2026-01-01 12:00:00, 3) +1 +SELECT week('2026-05-07 23:59:59', 3), week('2026-05-07 00:00:00', 3); +week(2026-05-07 23:59:59, 3) week(2026-05-07 00:00:00, 3) +19 19 +SELECT week(cast('2026-05-07' as date), 3); +week(cast(2026-05-07 as date), 3) +19 +SELECT week(cast('2026-05-07 10:00:00' as datetime), 3); +week(cast(2026-05-07 10:00:00 as datetime), 3) +19 +SELECT week('2026-05-07', cast(3 as signed)); +week(2026-05-07, cast(3 as signed)) +19 +SELECT week('2026-05-07', cast(3 as unsigned)); +week(2026-05-07, cast(3 as unsigned)) +19 +SELECT week('2026-05-07', 8), week('2026-05-07', 0); +week(2026-05-07, 8) week(2026-05-07, 0) +18 18 +SELECT week('2026-05-07', 9), week('2026-05-07', 1); +week(2026-05-07, 9) week(2026-05-07, 1) +19 19 +SELECT week('2026-05-07', -1); +week(2026-05-07, -1) +18 +SELECT week('2024-01-01', 0), week('2024-01-01', 1), week('2024-01-01', 2), week('2024-01-01', 3); +week(2024-01-01, 0) week(2024-01-01, 1) week(2024-01-01, 2) week(2024-01-01, 3) +0 1 53 1 +SELECT week('2024-12-31', 0), week('2024-12-31', 1), week('2024-12-31', 2), week('2024-12-31', 3); +week(2024-12-31, 0) week(2024-12-31, 1) week(2024-12-31, 2) week(2024-12-31, 3) +52 53 52 1 +SELECT week('2024-02-29', 0), week('2024-02-29', 1), week('2024-02-29', 2), week('2024-02-29', 3); +week(2024-02-29, 0) week(2024-02-29, 1) week(2024-02-29, 2) week(2024-02-29, 3) +8 9 8 9 +SELECT week('2023-01-01', 0), week('2023-01-01', 1), week('2023-01-01', 2), week('2023-01-01', 3); +week(2023-01-01, 0) week(2023-01-01, 1) week(2023-01-01, 2) week(2023-01-01, 3) +1 0 1 52 +SELECT week('2023-12-31', 0), week('2023-12-31', 1), week('2023-12-31', 2), week('2023-12-31', 3); +week(2023-12-31, 0) week(2023-12-31, 1) week(2023-12-31, 2) week(2023-12-31, 3) +53 52 53 52 +SELECT week('2025-01-01', 0), week('2025-01-01', 1), week('2025-01-01', 2), week('2025-01-01', 3); +week(2025-01-01, 0) week(2025-01-01, 1) week(2025-01-01, 2) week(2025-01-01, 3) +0 1 52 1 +SELECT week('2025-12-31', 0), week('2025-12-31', 1), week('2025-12-31', 2), week('2025-12-31', 3); +week(2025-12-31, 0) week(2025-12-31, 1) week(2025-12-31, 2) week(2025-12-31, 3) +52 53 52 1 +SELECT week('2023-01-01', 0), week('2023-01-01', 1), week('2023-01-01', 4), week('2023-01-01', 5); +week(2023-01-01, 0) week(2023-01-01, 1) week(2023-01-01, 4) week(2023-01-01, 5) +1 0 1 0 +SELECT week('2024-01-01', 0), week('2024-01-01', 1), week('2024-01-01', 4), week('2024-01-01', 5); +week(2024-01-01, 0) week(2024-01-01, 1) week(2024-01-01, 4) week(2024-01-01, 5) +0 1 1 1 +SELECT week('2020-12-31', 1), week('2020-12-31', 3); +week(2020-12-31, 1) week(2020-12-31, 3) +53 53 +SELECT week('2015-12-31', 1), week('2015-12-31', 3); +week(2015-12-31, 1) week(2015-12-31, 3) +53 53 +SELECT week('2026-05-07', 1+2); +week(2026-05-07, 1 + 2) +19 +SELECT week('2026-05-07', 1) - week('2026-04-30', 1); +week(2026-05-07, 1) - week(2026-04-30, 1) +1 +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2024-01-01'),(2, '2024-02-29'),(3, '2024-06-15'),(4, '2024-12-31'); +select id, week(d, 0), week(d, 1), week(d, 2), week(d, 3) from t1; +id week(d, 0) week(d, 1) week(d, 2) week(d, 3) +1 0 1 53 1 +2 8 9 8 9 +3 23 24 23 24 +4 52 53 52 1 +select id, week(d, 4), week(d, 5), week(d, 6), week(d, 7) from t1; +id week(d, 4) week(d, 5) week(d, 6) week(d, 7) +1 1 1 1 1 +2 9 9 9 9 +3 24 24 24 24 +4 53 53 1 53 +drop table t1; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-11'),(4, '2026-05-12'); +select * from t1 where week(d, 3) = 19; +id d +1 2026-05-04 +2 2026-05-07 +drop table t1; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-03-15'),(2, '2026-01-10'),(3, '2026-07-20'),(4, '2026-05-07'); +select id, d, week(d, 1) as wk from t1 order by wk; +id d wk +2 2026-01-10 2 +1 2026-03-15 11 +4 2026-05-07 19 +3 2026-07-20 30 +drop table t1; +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1, '2026-05-04', 10),(2, '2026-05-05', 20),(3, '2026-05-11', 30),(4, '2026-05-12', 40); +select week(d, 1) as wk, sum(val) from t1 group by wk order by wk; +wk sum(val) +19 30 +20 70 +drop table t1; +drop table if exists t1; +create table t1(id int, dt datetime); +insert into t1 values(1, '2026-01-01 08:00:00'),(2, '2026-05-07 12:30:00'),(3, '2026-12-31 23:59:59'); +select id, week(dt, 0), week(dt, 3) from t1; +id week(dt, 0) week(dt, 3) +1 0 1 +2 18 19 +3 52 53 +drop table t1; +drop table if exists t1; +create table t1(id int, ts timestamp); +insert into t1 values(1, '2026-01-01 08:00:00'),(2, '2026-05-07 12:30:00'),(3, '2026-12-31 23:59:59'); +select id, week(ts, 0), week(ts, 3) from t1; +id week(ts, 0) week(ts, 3) +1 0 1 +2 18 19 +3 52 53 +drop table t1; +SELECT * FROM (SELECT week('2026-05-07', 3) as wk) t WHERE t.wk = 19; +wk +19 +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1,'2026-05-04',10),(2,'2026-05-05',20),(3,'2026-05-11',30),(4,'2026-05-12',40),(5,'2026-05-06',50); +select week(d, 1) as wk, count(*) as cnt from t1 group by wk having cnt >= 2 order by wk; +wk cnt +19 3 +20 2 +drop table t1; +drop table if exists t1; +drop table if exists t2; +create table t1(id int, d date); +create table t2(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-11'); +insert into t2 values(10, '2026-05-07'),(20, '2026-05-14'); +select t1.id, t2.id from t1 join t2 on week(t1.d, 1) = week(t2.d, 1); +id id +1 10 +2 20 +drop table t1; +drop table t2; +drop table if exists t1; +drop table if exists t2; +create table t1(d date); +create table t2(wk int); +insert into t1 values('2026-01-01'),('2026-05-07'),('2026-12-31'); +insert into t2 select week(d, 3) from t1; +select * from t2 order by wk; +wk +1 +19 +53 +drop table t1; +drop table t2; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1,'2026-05-04'),(2,'2026-05-05'),(3,'2026-05-06'),(4,'2026-05-11'),(5,'2026-05-12'); +select distinct week(d, 1) as wk from t1 order by wk; +wk +19 +20 +drop table t1; diff --git a/test/distributed/cases/function/func_datetime_week.test b/test/distributed/cases/function/func_datetime_week.test new file mode 100644 index 0000000000000..0170701b92934 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_week.test @@ -0,0 +1,152 @@ +-- WEEK function: 1-argument form (uses default_week_format) +-- NOTE: MO returns 19 (mode=1 behavior) but MySQL returns 18 (mode=0) when default_week_format=0 +-- This is a known bug (#24293): WEEK(date) ignores @@default_week_format +SELECT week('2026-05-07'); + +-- WEEK function: 2-argument form with all modes (0-7) +SELECT week('2026-05-07', 0); +SELECT week('2026-05-07', 1); +SELECT week('2026-05-07', 2); +SELECT week('2026-05-07', 3); +SELECT week('2026-05-07', 4); +SELECT week('2026-05-07', 5); +SELECT week('2026-05-07', 6); +SELECT week('2026-05-07', 7); + +-- WEEK: NULL handling +SELECT week(NULL); +SELECT week(NULL, 3); + +-- WEEK: DATETIME input +SELECT week('2026-01-01 12:00:00', 3); + +-- WEEK: time portion should be ignored +SELECT week('2026-05-07 23:59:59', 3), week('2026-05-07 00:00:00', 3); + +-- WEEK: with CAST +SELECT week(cast('2026-05-07' as date), 3); +SELECT week(cast('2026-05-07 10:00:00' as datetime), 3); + +-- WEEK: BIGINT mode (original issue #24236) +SELECT week('2026-05-07', cast(3 as signed)); +SELECT week('2026-05-07', cast(3 as unsigned)); + +-- WEEK: mode > 7 (wraps around to mode % 8) +SELECT week('2026-05-07', 8), week('2026-05-07', 0); +SELECT week('2026-05-07', 9), week('2026-05-07', 1); + +-- WEEK: negative mode +SELECT week('2026-05-07', -1); + +-- WEEK: year boundary - 2024 leap year (Jan 1 is Monday) +SELECT week('2024-01-01', 0), week('2024-01-01', 1), week('2024-01-01', 2), week('2024-01-01', 3); +SELECT week('2024-12-31', 0), week('2024-12-31', 1), week('2024-12-31', 2), week('2024-12-31', 3); + +-- WEEK: leap year Feb 29 +SELECT week('2024-02-29', 0), week('2024-02-29', 1), week('2024-02-29', 2), week('2024-02-29', 3); + +-- WEEK: year boundary - 2023 (Jan 1 is Sunday) +SELECT week('2023-01-01', 0), week('2023-01-01', 1), week('2023-01-01', 2), week('2023-01-01', 3); +SELECT week('2023-12-31', 0), week('2023-12-31', 1), week('2023-12-31', 2), week('2023-12-31', 3); + +-- WEEK: year boundary - 2025 (Jan 1 is Wednesday) +SELECT week('2025-01-01', 0), week('2025-01-01', 1), week('2025-01-01', 2), week('2025-01-01', 3); +SELECT week('2025-12-31', 0), week('2025-12-31', 1), week('2025-12-31', 2), week('2025-12-31', 3); + +-- WEEK: Jan 1 is Sunday - modes 0,1,4,5 +SELECT week('2023-01-01', 0), week('2023-01-01', 1), week('2023-01-01', 4), week('2023-01-01', 5); + +-- WEEK: Jan 1 is Monday - modes 0,1,4,5 +SELECT week('2024-01-01', 0), week('2024-01-01', 1), week('2024-01-01', 4), week('2024-01-01', 5); + +-- WEEK: week 53 cases +SELECT week('2020-12-31', 1), week('2020-12-31', 3); +SELECT week('2015-12-31', 1), week('2015-12-31', 3); + +-- WEEK: mode as expression +SELECT week('2026-05-07', 1+2); + +-- WEEK: arithmetic +SELECT week('2026-05-07', 1) - week('2026-04-30', 1); + +-- WEEK: with table - all modes +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2024-01-01'),(2, '2024-02-29'),(3, '2024-06-15'),(4, '2024-12-31'); +select id, week(d, 0), week(d, 1), week(d, 2), week(d, 3) from t1; +select id, week(d, 4), week(d, 5), week(d, 6), week(d, 7) from t1; +drop table t1; + +-- WEEK: in WHERE clause +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-11'),(4, '2026-05-12'); +select * from t1 where week(d, 3) = 19; +drop table t1; + +-- WEEK: in ORDER BY +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-03-15'),(2, '2026-01-10'),(3, '2026-07-20'),(4, '2026-05-07'); +select id, d, week(d, 1) as wk from t1 order by wk; +drop table t1; + +-- WEEK: in GROUP BY +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1, '2026-05-04', 10),(2, '2026-05-05', 20),(3, '2026-05-11', 30),(4, '2026-05-12', 40); +select week(d, 1) as wk, sum(val) from t1 group by wk order by wk; +drop table t1; + +-- WEEK: with DATETIME column +drop table if exists t1; +create table t1(id int, dt datetime); +insert into t1 values(1, '2026-01-01 08:00:00'),(2, '2026-05-07 12:30:00'),(3, '2026-12-31 23:59:59'); +select id, week(dt, 0), week(dt, 3) from t1; +drop table t1; + +-- WEEK: with TIMESTAMP column +drop table if exists t1; +create table t1(id int, ts timestamp); +insert into t1 values(1, '2026-01-01 08:00:00'),(2, '2026-05-07 12:30:00'),(3, '2026-12-31 23:59:59'); +select id, week(ts, 0), week(ts, 3) from t1; +drop table t1; + +-- WEEK: subquery +SELECT * FROM (SELECT week('2026-05-07', 3) as wk) t WHERE t.wk = 19; + +-- WEEK: HAVING clause +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1,'2026-05-04',10),(2,'2026-05-05',20),(3,'2026-05-11',30),(4,'2026-05-12',40),(5,'2026-05-06',50); +select week(d, 1) as wk, count(*) as cnt from t1 group by wk having cnt >= 2 order by wk; +drop table t1; + +-- WEEK: JOIN condition +drop table if exists t1; +drop table if exists t2; +create table t1(id int, d date); +create table t2(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-11'); +insert into t2 values(10, '2026-05-07'),(20, '2026-05-14'); +select t1.id, t2.id from t1 join t2 on week(t1.d, 1) = week(t2.d, 1); +drop table t1; +drop table t2; + +-- WEEK: INSERT SELECT +drop table if exists t1; +drop table if exists t2; +create table t1(d date); +create table t2(wk int); +insert into t1 values('2026-01-01'),('2026-05-07'),('2026-12-31'); +insert into t2 select week(d, 3) from t1; +select * from t2 order by wk; +drop table t1; +drop table t2; + +-- WEEK: DISTINCT +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1,'2026-05-04'),(2,'2026-05-05'),(3,'2026-05-06'),(4,'2026-05-11'),(5,'2026-05-12'); +select distinct week(d, 1) as wk from t1 order by wk; +drop table t1; diff --git a/test/distributed/cases/function/func_datetime_yearweek.result b/test/distributed/cases/function/func_datetime_yearweek.result new file mode 100644 index 0000000000000..052ff64cf3c67 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_yearweek.result @@ -0,0 +1,197 @@ +SELECT yearweek('2026-05-07'); +yearweek(2026-05-07) +202618 +SELECT yearweek('2026-05-07', 0); +yearweek(2026-05-07, 0) +202618 +SELECT yearweek('2026-05-07', 1); +yearweek(2026-05-07, 1) +202619 +SELECT yearweek('2026-05-07', 2); +yearweek(2026-05-07, 2) +202618 +SELECT yearweek('2026-05-07', 3); +yearweek(2026-05-07, 3) +202619 +SELECT yearweek('2026-05-07', 4); +yearweek(2026-05-07, 4) +202618 +SELECT yearweek('2026-05-07', 5); +yearweek(2026-05-07, 5) +202618 +SELECT yearweek('2026-05-07', 6); +yearweek(2026-05-07, 6) +202618 +SELECT yearweek('2026-05-07', 7); +yearweek(2026-05-07, 7) +202618 +SELECT yearweek(NULL); +yearweek(null) +null +SELECT yearweek(NULL, 3); +yearweek(null, 3) +null +SELECT yearweek('2026-01-01 12:00:00', 3); +yearweek(2026-01-01 12:00:00, 3) +202601 +SELECT yearweek('2026-05-07 23:59:59', 3), yearweek('2026-05-07 00:00:00', 3); +yearweek(2026-05-07 23:59:59, 3) yearweek(2026-05-07 00:00:00, 3) +202619 202619 +SELECT yearweek(cast('2026-05-07' as date), 3); +yearweek(cast(2026-05-07 as date), 3) +202619 +SELECT yearweek(cast('2026-05-07 10:00:00' as datetime), 3); +yearweek(cast(2026-05-07 10:00:00 as datetime), 3) +202619 +SELECT yearweek('2026-05-07', cast(3 as signed)); +yearweek(2026-05-07, cast(3 as signed)) +202619 +SELECT yearweek('2026-05-07', cast(3 as unsigned)); +yearweek(2026-05-07, cast(3 as unsigned)) +202619 +SELECT yearweek('2024-01-01', 0), yearweek('2024-01-01', 1), yearweek('2024-01-01', 2), yearweek('2024-01-01', 3); +yearweek(2024-01-01, 0) yearweek(2024-01-01, 1) yearweek(2024-01-01, 2) yearweek(2024-01-01, 3) +202353 202401 202353 202401 +SELECT yearweek('2024-12-31', 0), yearweek('2024-12-31', 1), yearweek('2024-12-31', 2), yearweek('2024-12-31', 3); +yearweek(2024-12-31, 0) yearweek(2024-12-31, 1) yearweek(2024-12-31, 2) yearweek(2024-12-31, 3) +202452 202501 202452 202501 +SELECT yearweek('2023-01-01', 0), yearweek('2023-01-01', 1), yearweek('2023-01-01', 2), yearweek('2023-01-01', 3); +yearweek(2023-01-01, 0) yearweek(2023-01-01, 1) yearweek(2023-01-01, 2) yearweek(2023-01-01, 3) +202301 202252 202301 202252 +SELECT yearweek('2023-12-31', 0), yearweek('2023-12-31', 1), yearweek('2023-12-31', 2), yearweek('2023-12-31', 3); +yearweek(2023-12-31, 0) yearweek(2023-12-31, 1) yearweek(2023-12-31, 2) yearweek(2023-12-31, 3) +202353 202352 202353 202352 +SELECT yearweek('2025-01-01', 0), yearweek('2025-01-01', 1), yearweek('2025-01-01', 2), yearweek('2025-01-01', 3); +yearweek(2025-01-01, 0) yearweek(2025-01-01, 1) yearweek(2025-01-01, 2) yearweek(2025-01-01, 3) +202452 202501 202452 202501 +SELECT yearweek('2025-12-31', 0), yearweek('2025-12-31', 1), yearweek('2025-12-31', 2), yearweek('2025-12-31', 3); +yearweek(2025-12-31, 0) yearweek(2025-12-31, 1) yearweek(2025-12-31, 2) yearweek(2025-12-31, 3) +202552 202601 202552 202601 +SELECT yearweek('2020-12-31', 1), yearweek('2020-12-31', 3); +yearweek(2020-12-31, 1) yearweek(2020-12-31, 3) +202053 202053 +SELECT yearweek('2015-12-31', 1), yearweek('2015-12-31', 3); +yearweek(2015-12-31, 1) yearweek(2015-12-31, 3) +201553 201553 +SELECT yearweek('2026-05-07', 1+2); +yearweek(2026-05-07, 1 + 2) +202619 +SELECT yearweek('2026-05-07', 3) - yearweek('2026-04-30', 3); +yearweek(2026-05-07, 3) - yearweek(2026-04-30, 3) +1 +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2024-01-01'),(2, '2024-02-29'),(3, '2024-06-15'),(4, '2024-12-31'); +select id, yearweek(d, 0), yearweek(d, 1), yearweek(d, 2), yearweek(d, 3) from t1; +id yearweek(d, 0) yearweek(d, 1) yearweek(d, 2) yearweek(d, 3) +1 202353 202401 202353 202401 +2 202408 202409 202408 202409 +3 202423 202424 202423 202424 +4 202452 202501 202452 202501 +select id, yearweek(d, 4), yearweek(d, 5), yearweek(d, 6), yearweek(d, 7) from t1; +id yearweek(d, 4) yearweek(d, 5) yearweek(d, 6) yearweek(d, 7) +1 202401 202401 202401 202401 +2 202409 202409 202409 202409 +3 202424 202424 202424 202424 +4 202501 202453 202501 202453 +drop table t1; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-11'),(4, '2026-05-12'); +select * from t1 where yearweek(d, 3) = yearweek('2026-05-07', 3); +id d +1 2026-05-04 +2 2026-05-07 +drop table t1; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-03-15'),(2, '2026-01-10'),(3, '2026-07-20'),(4, '2026-05-07'); +select id, d, yearweek(d, 1) as yw from t1 order by yw; +id d yw +2 2026-01-10 202602 +1 2026-03-15 202611 +4 2026-05-07 202619 +3 2026-07-20 202630 +drop table t1; +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1, '2026-05-04', 10),(2, '2026-05-05', 20),(3, '2026-05-11', 30),(4, '2026-05-12', 40); +select yearweek(d, 1) as yw, sum(val) from t1 group by yw order by yw; +yw sum(val) +202619 30 +202620 70 +drop table t1; +drop table if exists t1; +create table t1(id int, dt datetime); +insert into t1 values(1, '2024-01-01 08:00:00'),(2, '2024-06-15 12:30:00'),(3, '2024-12-31 23:59:59'); +select id, yearweek(dt, 0), yearweek(dt, 3) from t1; +id yearweek(dt, 0) yearweek(dt, 3) +1 202353 202401 +2 202423 202424 +3 202452 202501 +drop table t1; +drop table if exists t1; +create table t1(id int, ts timestamp); +insert into t1 values(1, '2024-01-01 08:00:00'),(2, '2024-06-15 12:30:00'),(3, '2024-12-31 23:59:59'); +select id, yearweek(ts, 0), yearweek(ts, 3) from t1; +id yearweek(ts, 0) yearweek(ts, 3) +1 202353 202401 +2 202423 202424 +3 202452 202501 +drop table t1; +SELECT * FROM (SELECT yearweek('2026-05-07', 3) as yw) t WHERE t.yw = 202619; +yw +202619 +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1,'2026-05-04',10),(2,'2026-05-05',20),(3,'2026-05-11',30),(4,'2026-05-12',40),(5,'2026-05-06',50); +select yearweek(d, 1) as yw, count(*) as cnt from t1 group by yw having cnt >= 2 order by yw; +yw cnt +202619 3 +202620 2 +drop table t1; +drop table if exists t1; +drop table if exists t2; +create table t1(id int, d date); +create table t2(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-11'); +insert into t2 values(10, '2026-05-07'),(20, '2026-05-14'); +select t1.id, t2.id from t1 join t2 on yearweek(t1.d, 1) = yearweek(t2.d, 1); +id id +1 10 +2 20 +drop table t1; +drop table t2; +drop table if exists t1; +drop table if exists t2; +create table t1(d date); +create table t2(yw int); +insert into t1 values('2024-01-01'),('2024-06-15'),('2024-12-31'); +insert into t2 select yearweek(d, 3) from t1; +select * from t2 order by yw; +yw +202401 +202424 +202501 +drop table t1; +drop table t2; +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1,'2026-05-04'),(2,'2026-05-05'),(3,'2026-05-06'),(4,'2026-05-11'),(5,'2026-05-12'); +select distinct yearweek(d, 1) as yw from t1 order by yw; +yw +202619 +202620 +drop table t1; +SELECT week('2024-12-31', 1), yearweek('2024-12-31', 1); +week(2024-12-31, 1) yearweek(2024-12-31, 1) +53 202501 +SELECT week('2024-12-31', 3), yearweek('2024-12-31', 3); +week(2024-12-31, 3) yearweek(2024-12-31, 3) +1 202501 +SELECT week('2023-01-01', 1), yearweek('2023-01-01', 1); +week(2023-01-01, 1) yearweek(2023-01-01, 1) +0 202252 +SELECT week('2023-01-01', 3), yearweek('2023-01-01', 3); +week(2023-01-01, 3) yearweek(2023-01-01, 3) +52 202252 diff --git a/test/distributed/cases/function/func_datetime_yearweek.test b/test/distributed/cases/function/func_datetime_yearweek.test new file mode 100644 index 0000000000000..85a8a3a811fd7 --- /dev/null +++ b/test/distributed/cases/function/func_datetime_yearweek.test @@ -0,0 +1,143 @@ +-- @label:bvt + +-- YEARWEEK function: 1-argument form +SELECT yearweek('2026-05-07'); + +-- YEARWEEK function: 2-argument form with all modes (0-7) +SELECT yearweek('2026-05-07', 0); +SELECT yearweek('2026-05-07', 1); +SELECT yearweek('2026-05-07', 2); +SELECT yearweek('2026-05-07', 3); +SELECT yearweek('2026-05-07', 4); +SELECT yearweek('2026-05-07', 5); +SELECT yearweek('2026-05-07', 6); +SELECT yearweek('2026-05-07', 7); + +-- YEARWEEK: NULL handling +SELECT yearweek(NULL); +SELECT yearweek(NULL, 3); + +-- YEARWEEK: DATETIME input +SELECT yearweek('2026-01-01 12:00:00', 3); + +-- YEARWEEK: time portion should be ignored +SELECT yearweek('2026-05-07 23:59:59', 3), yearweek('2026-05-07 00:00:00', 3); + +-- YEARWEEK: with CAST +SELECT yearweek(cast('2026-05-07' as date), 3); +SELECT yearweek(cast('2026-05-07 10:00:00' as datetime), 3); + +-- YEARWEEK: BIGINT mode (original issue #24236 related) +SELECT yearweek('2026-05-07', cast(3 as signed)); +SELECT yearweek('2026-05-07', cast(3 as unsigned)); + +-- YEARWEEK: year boundary - 2024 leap year (Jan 1 is Monday) +-- Key: YEARWEEK can return a year different from the date's year +SELECT yearweek('2024-01-01', 0), yearweek('2024-01-01', 1), yearweek('2024-01-01', 2), yearweek('2024-01-01', 3); +SELECT yearweek('2024-12-31', 0), yearweek('2024-12-31', 1), yearweek('2024-12-31', 2), yearweek('2024-12-31', 3); + +-- YEARWEEK: year boundary - 2023 (Jan 1 is Sunday) +SELECT yearweek('2023-01-01', 0), yearweek('2023-01-01', 1), yearweek('2023-01-01', 2), yearweek('2023-01-01', 3); +SELECT yearweek('2023-12-31', 0), yearweek('2023-12-31', 1), yearweek('2023-12-31', 2), yearweek('2023-12-31', 3); + +-- YEARWEEK: year boundary - 2025 (Jan 1 is Wednesday) +SELECT yearweek('2025-01-01', 0), yearweek('2025-01-01', 1), yearweek('2025-01-01', 2), yearweek('2025-01-01', 3); +SELECT yearweek('2025-12-31', 0), yearweek('2025-12-31', 1), yearweek('2025-12-31', 2), yearweek('2025-12-31', 3); + +-- YEARWEEK: week 53 cases - year rolls over +SELECT yearweek('2020-12-31', 1), yearweek('2020-12-31', 3); +SELECT yearweek('2015-12-31', 1), yearweek('2015-12-31', 3); + +-- YEARWEEK: mode as expression +SELECT yearweek('2026-05-07', 1+2); + +-- YEARWEEK: arithmetic (this week vs last week pattern) +SELECT yearweek('2026-05-07', 3) - yearweek('2026-04-30', 3); + +-- YEARWEEK: with table - all modes +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2024-01-01'),(2, '2024-02-29'),(3, '2024-06-15'),(4, '2024-12-31'); +select id, yearweek(d, 0), yearweek(d, 1), yearweek(d, 2), yearweek(d, 3) from t1; +select id, yearweek(d, 4), yearweek(d, 5), yearweek(d, 6), yearweek(d, 7) from t1; +drop table t1; + +-- YEARWEEK: in WHERE clause (typical "this week" pattern from NL2SQL) +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-07'),(3, '2026-05-11'),(4, '2026-05-12'); +select * from t1 where yearweek(d, 3) = yearweek('2026-05-07', 3); +drop table t1; + +-- YEARWEEK: in ORDER BY +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1, '2026-03-15'),(2, '2026-01-10'),(3, '2026-07-20'),(4, '2026-05-07'); +select id, d, yearweek(d, 1) as yw from t1 order by yw; +drop table t1; + +-- YEARWEEK: in GROUP BY +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1, '2026-05-04', 10),(2, '2026-05-05', 20),(3, '2026-05-11', 30),(4, '2026-05-12', 40); +select yearweek(d, 1) as yw, sum(val) from t1 group by yw order by yw; +drop table t1; + +-- YEARWEEK: with DATETIME column +drop table if exists t1; +create table t1(id int, dt datetime); +insert into t1 values(1, '2024-01-01 08:00:00'),(2, '2024-06-15 12:30:00'),(3, '2024-12-31 23:59:59'); +select id, yearweek(dt, 0), yearweek(dt, 3) from t1; +drop table t1; + +-- YEARWEEK: with TIMESTAMP column +drop table if exists t1; +create table t1(id int, ts timestamp); +insert into t1 values(1, '2024-01-01 08:00:00'),(2, '2024-06-15 12:30:00'),(3, '2024-12-31 23:59:59'); +select id, yearweek(ts, 0), yearweek(ts, 3) from t1; +drop table t1; + +-- YEARWEEK: subquery +SELECT * FROM (SELECT yearweek('2026-05-07', 3) as yw) t WHERE t.yw = 202619; + +-- YEARWEEK: HAVING clause +drop table if exists t1; +create table t1(id int, d date, val int); +insert into t1 values(1,'2026-05-04',10),(2,'2026-05-05',20),(3,'2026-05-11',30),(4,'2026-05-12',40),(5,'2026-05-06',50); +select yearweek(d, 1) as yw, count(*) as cnt from t1 group by yw having cnt >= 2 order by yw; +drop table t1; + +-- YEARWEEK: JOIN condition +drop table if exists t1; +drop table if exists t2; +create table t1(id int, d date); +create table t2(id int, d date); +insert into t1 values(1, '2026-05-04'),(2, '2026-05-11'); +insert into t2 values(10, '2026-05-07'),(20, '2026-05-14'); +select t1.id, t2.id from t1 join t2 on yearweek(t1.d, 1) = yearweek(t2.d, 1); +drop table t1; +drop table t2; + +-- YEARWEEK: INSERT SELECT +drop table if exists t1; +drop table if exists t2; +create table t1(d date); +create table t2(yw int); +insert into t1 values('2024-01-01'),('2024-06-15'),('2024-12-31'); +insert into t2 select yearweek(d, 3) from t1; +select * from t2 order by yw; +drop table t1; +drop table t2; + +-- YEARWEEK: DISTINCT +drop table if exists t1; +create table t1(id int, d date); +insert into t1 values(1,'2026-05-04'),(2,'2026-05-05'),(3,'2026-05-06'),(4,'2026-05-11'),(5,'2026-05-12'); +select distinct yearweek(d, 1) as yw from t1 order by yw; +drop table t1; + +-- YEARWEEK: combined with WEEK for cross-check +SELECT week('2024-12-31', 1), yearweek('2024-12-31', 1); +SELECT week('2024-12-31', 3), yearweek('2024-12-31', 3); +SELECT week('2023-01-01', 1), yearweek('2023-01-01', 1); +SELECT week('2023-01-01', 3), yearweek('2023-01-01', 3);