feat: adds date_format function (#3167)

* feat: adds date_format function

* fix: compile error

* chore: use system timezone for FunctionContext and EvalContext

* test: as_formatted_string

* test: sqlness test

* chore: rename function
This commit is contained in:
dennis zhuang
2024-01-17 11:24:40 +08:00
committed by GitHub
parent d020a3db23
commit 204b9433b8
18 changed files with 651 additions and 61 deletions

View File

@@ -1,3 +1,4 @@
--- date_add ---
SELECT date_add('2023-12-06 07:39:46.222'::TIMESTAMP_MS, INTERVAL '5 day');
+----------------------------------------------------------------------------------------+
@@ -30,6 +31,7 @@ SELECT date_add('2023-12-06'::DATE, '3 month 5 day');
| 2024-03-11 |
+----------------------------------------------------+
--- date_sub ---
SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, INTERVAL '5 day');
+----------------------------------------------------------------------------------------+
@@ -62,6 +64,37 @@ SELECT date_sub('2023-12-06'::DATE, '3 month 5 day');
| 2023-09-01 |
+----------------------------------------------------+
--- date_format ---
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '%Y-%m-%d %H:%M:%S:%3f');
+----------------------------------------------------------------------------+
| date_format(Utf8("2023-12-06 07:39:46.222"),Utf8("%Y-%m-%d %H:%M:%S:%3f")) |
+----------------------------------------------------------------------------+
| 2023-12-06 07:39:46:222 |
+----------------------------------------------------------------------------+
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP_S, '%Y-%m-%d %H:%M:%S:%3f');
+----------------------------------------------------------------------------+
| date_format(Utf8("2023-12-06 07:39:46.222"),Utf8("%Y-%m-%d %H:%M:%S:%3f")) |
+----------------------------------------------------------------------------+
| 2023-12-06 07:39:46:000 |
+----------------------------------------------------------------------------+
--- datetime not supported yet ---
SELECT date_format('2023-12-06 07:39:46.222'::DATETIME, '%Y-%m-%d %H:%M:%S:%3f');
Error: 3000(PlanQuery), Failed to plan SQL: This feature is not implemented: Unsupported SQL type Datetime(None)
SELECT date_format('2023-12-06'::DATE, '%m-%d');
+-----------------------------------------------+
| date_format(Utf8("2023-12-06"),Utf8("%m-%d")) |
+-----------------------------------------------+
| 12-06 |
+-----------------------------------------------+
--- test date functions with table rows ---
CREATE TABLE dates(d DATE, ts timestamp time index);
Affected Rows: 0
@@ -78,6 +111,7 @@ INSERT INTO dates VALUES ('2023-12-06'::DATE, 3);
Affected Rows: 1
--- date_add ---
SELECT date_add(d, INTERVAL '1 year 2 month 3 day') from dates;
+---------------------------------------------------------------------------+
@@ -118,6 +152,7 @@ SELECT date_add(ts, '1 year 2 month 3 day') from dates;
| 1971-03-04T00:00:00.003 |
+-------------------------------------------------+
--- date_sub ---
SELECT date_sub(d, INTERVAL '1 year 2 month 3 day') from dates;
+---------------------------------------------------------------------------+
@@ -158,6 +193,27 @@ SELECT date_sub(ts, '1 year 2 month 3 day') from dates;
| 1968-10-29T00:00:00.003 |
+-------------------------------------------------+
--- date_format ---
SELECT date_format(d, '%Y-%m-%d %H:%M:%S:%3f') from dates;
+----------------------------------------------------+
| date_format(dates.d,Utf8("%Y-%m-%d %H:%M:%S:%3f")) |
+----------------------------------------------------+
| 1992-01-01 00:00:00:000 |
| 1993-12-30 00:00:00:000 |
| 2023-12-06 00:00:00:000 |
+----------------------------------------------------+
SELECT date_format(ts, '%Y-%m-%d %H:%M:%S:%3f') from dates;
+-----------------------------------------------------+
| date_format(dates.ts,Utf8("%Y-%m-%d %H:%M:%S:%3f")) |
+-----------------------------------------------------+
| 1970-01-01 00:00:00:001 |
| 1970-01-01 00:00:00:002 |
| 1970-01-01 00:00:00:003 |
+-----------------------------------------------------+
DROP TABLE dates;
Affected Rows: 0

View File

@@ -1,3 +1,4 @@
--- date_add ---
SELECT date_add('2023-12-06 07:39:46.222'::TIMESTAMP_MS, INTERVAL '5 day');
SELECT date_add('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '5 day');
@@ -6,6 +7,7 @@ SELECT date_add('2023-12-06'::DATE, INTERVAL '3 month 5 day');
SELECT date_add('2023-12-06'::DATE, '3 month 5 day');
--- date_sub ---
SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, INTERVAL '5 day');
SELECT date_sub('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '5 day');
@@ -14,7 +16,17 @@ SELECT date_sub('2023-12-06'::DATE, INTERVAL '3 month 5 day');
SELECT date_sub('2023-12-06'::DATE, '3 month 5 day');
--- date_format ---
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP_MS, '%Y-%m-%d %H:%M:%S:%3f');
SELECT date_format('2023-12-06 07:39:46.222'::TIMESTAMP_S, '%Y-%m-%d %H:%M:%S:%3f');
--- datetime not supported yet ---
SELECT date_format('2023-12-06 07:39:46.222'::DATETIME, '%Y-%m-%d %H:%M:%S:%3f');
SELECT date_format('2023-12-06'::DATE, '%m-%d');
--- test date functions with table rows ---
CREATE TABLE dates(d DATE, ts timestamp time index);
INSERT INTO dates VALUES ('1992-01-01'::DATE, 1);
@@ -23,6 +35,7 @@ INSERT INTO dates VALUES ('1993-12-30'::DATE, 2);
INSERT INTO dates VALUES ('2023-12-06'::DATE, 3);
--- date_add ---
SELECT date_add(d, INTERVAL '1 year 2 month 3 day') from dates;
SELECT date_add(d, '1 year 2 month 3 day') from dates;
@@ -31,6 +44,7 @@ SELECT date_add(ts, INTERVAL '1 year 2 month 3 day') from dates;
SELECT date_add(ts, '1 year 2 month 3 day') from dates;
--- date_sub ---
SELECT date_sub(d, INTERVAL '1 year 2 month 3 day') from dates;
SELECT date_sub(d, '1 year 2 month 3 day') from dates;
@@ -39,4 +53,9 @@ SELECT date_sub(ts, INTERVAL '1 year 2 month 3 day') from dates;
SELECT date_sub(ts, '1 year 2 month 3 day') from dates;
--- date_format ---
SELECT date_format(d, '%Y-%m-%d %H:%M:%S:%3f') from dates;
SELECT date_format(ts, '%Y-%m-%d %H:%M:%S:%3f') from dates;
DROP TABLE dates;