|
| 1 | +-- Migrated from DuckDB test: test/sql/types/date/test_date.test |
| 2 | +-- Test basic DATE functionality |
| 3 | +-- Create and insert into table |
| 4 | +CREATE TABLE dates(i DATE, ts TIMESTAMP TIME INDEX); |
| 5 | + |
| 6 | +Affected Rows: 0 |
| 7 | + |
| 8 | +INSERT INTO dates VALUES ('1993-08-14', 1000), (NULL, 2000); |
| 9 | + |
| 10 | +Affected Rows: 2 |
| 11 | + |
| 12 | +-- Check that we can select dates |
| 13 | +SELECT * FROM dates ORDER BY ts; |
| 14 | + |
| 15 | ++------------+---------------------+ |
| 16 | +| i | ts | |
| 17 | ++------------+---------------------+ |
| 18 | +| 1993-08-14 | 1970-01-01T00:00:01 | |
| 19 | +| | 1970-01-01T00:00:02 | |
| 20 | ++------------+---------------------+ |
| 21 | + |
| 22 | +-- extract function |
| 23 | +SELECT extract(year FROM i) FROM dates ORDER BY ts; |
| 24 | + |
| 25 | ++---------------------------------+ |
| 26 | +| date_part(Utf8("YEAR"),dates.i) | |
| 27 | ++---------------------------------+ |
| 28 | +| 1993 | |
| 29 | +| | |
| 30 | ++---------------------------------+ |
| 31 | + |
| 32 | +-- Check that we can convert dates to string |
| 33 | +SELECT CAST(i AS VARCHAR) FROM dates ORDER BY ts; |
| 34 | + |
| 35 | ++------------+ |
| 36 | +| dates.i | |
| 37 | ++------------+ |
| 38 | +| 1993-08-14 | |
| 39 | +| | |
| 40 | ++------------+ |
| 41 | + |
| 42 | +-- Check that we can add days to a date |
| 43 | +SELECT i + INTERVAL '5 days' FROM dates ORDER BY ts; |
| 44 | + |
| 45 | ++-----------------------------------------------------------------------------------------------+ |
| 46 | +| dates.i + IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5, nanoseconds: 0 }") | |
| 47 | ++-----------------------------------------------------------------------------------------------+ |
| 48 | +| 1993-08-19 | |
| 49 | +| | |
| 50 | ++-----------------------------------------------------------------------------------------------+ |
| 51 | + |
| 52 | +-- Check that we can subtract days from a date |
| 53 | +SELECT i - INTERVAL '5 days' FROM dates ORDER BY ts; |
| 54 | + |
| 55 | ++-----------------------------------------------------------------------------------------------+ |
| 56 | +| dates.i - IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5, nanoseconds: 0 }") | |
| 57 | ++-----------------------------------------------------------------------------------------------+ |
| 58 | +| 1993-08-09 | |
| 59 | +| | |
| 60 | ++-----------------------------------------------------------------------------------------------+ |
| 61 | + |
| 62 | +-- Test date subtraction resulting in interval |
| 63 | +SELECT i - DATE '1993-08-14' FROM dates ORDER BY ts; |
| 64 | + |
| 65 | ++------------------------------+ |
| 66 | +| dates.i - Utf8("1993-08-14") | |
| 67 | ++------------------------------+ |
| 68 | +| P0D | |
| 69 | +| | |
| 70 | ++------------------------------+ |
| 71 | + |
| 72 | +-- Test various date formats |
| 73 | +CREATE TABLE date_formats(d DATE, ts TIMESTAMP TIME INDEX); |
| 74 | + |
| 75 | +Affected Rows: 0 |
| 76 | + |
| 77 | +INSERT INTO date_formats VALUES |
| 78 | + ('2021-03-01', 1000), |
| 79 | + ('2021-12-31', 2000), |
| 80 | + ('2000-01-01', 3000), |
| 81 | + ('1970-01-01', 4000); |
| 82 | + |
| 83 | +Affected Rows: 4 |
| 84 | + |
| 85 | +SELECT d, extract(year FROM d), extract(month FROM d), extract(day FROM d) FROM date_formats ORDER BY d; |
| 86 | + |
| 87 | ++------------+----------------------------------------+-----------------------------------------+---------------------------------------+ |
| 88 | +| d | date_part(Utf8("YEAR"),date_formats.d) | date_part(Utf8("MONTH"),date_formats.d) | date_part(Utf8("DAY"),date_formats.d) | |
| 89 | ++------------+----------------------------------------+-----------------------------------------+---------------------------------------+ |
| 90 | +| 1970-01-01 | 1970 | 1 | 1 | |
| 91 | +| 2000-01-01 | 2000 | 1 | 1 | |
| 92 | +| 2021-03-01 | 2021 | 3 | 1 | |
| 93 | +| 2021-12-31 | 2021 | 12 | 31 | |
| 94 | ++------------+----------------------------------------+-----------------------------------------+---------------------------------------+ |
| 95 | + |
| 96 | +-- Test date comparison |
| 97 | +SELECT d FROM date_formats WHERE d > '2000-01-01' ORDER BY d; |
| 98 | + |
| 99 | ++------------+ |
| 100 | +| d | |
| 101 | ++------------+ |
| 102 | +| 2021-03-01 | |
| 103 | +| 2021-12-31 | |
| 104 | ++------------+ |
| 105 | + |
| 106 | +SELECT d FROM date_formats WHERE d BETWEEN '2000-01-01' AND '2021-06-01' ORDER BY d; |
| 107 | + |
| 108 | ++------------+ |
| 109 | +| d | |
| 110 | ++------------+ |
| 111 | +| 2000-01-01 | |
| 112 | +| 2021-03-01 | |
| 113 | ++------------+ |
| 114 | + |
| 115 | +-- Test NULL handling |
| 116 | +INSERT INTO date_formats VALUES (NULL, 5000); |
| 117 | + |
| 118 | +Affected Rows: 1 |
| 119 | + |
| 120 | +SELECT COUNT(*), COUNT(d) FROM date_formats; |
| 121 | + |
| 122 | ++----------+-----------------------+ |
| 123 | +| count(*) | count(date_formats.d) | |
| 124 | ++----------+-----------------------+ |
| 125 | +| 5 | 4 | |
| 126 | ++----------+-----------------------+ |
| 127 | + |
| 128 | +DROP TABLE dates; |
| 129 | + |
| 130 | +Affected Rows: 0 |
| 131 | + |
| 132 | +DROP TABLE date_formats; |
| 133 | + |
| 134 | +Affected Rows: 0 |
| 135 | + |
0 commit comments