File: timestamp_comparisons.test

package info (click to toggle)
duckdb 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 564
file content (113 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# name: test/optimizer/timestamp_comparisons.test
# description: timestampt comparisons should be correct
# group: [optimizer]

require icu

statement ok
create table t (t timestamptz);

statement ok
SET TimeZone='ECT';

statement ok
SET CALENDAR='gregorian';

statement ok
insert into t values('2028-02-28 01:00:00');

# EQUAL is affected (and LOWER, GREATER too)

query I
select t::timestamp = timestamp '2028-02-28 01:00:00' from t;
----
true

query II
select timestamp '2028-02-28 01:00:00' = timestamp '2028-02-28 01:00:00', t = timestamp '2028-02-28 01:00:00' from t;
----
true	true

# Leading to this strange behaviour: no match
query I
select count(*) from t where t::timestamp = timestamp '2028-02-28 01:00:00';
----
1

## Same problem if the constant is a string (no match)
query I
select * from t where t::timestamp = '2028-02-28 01:00:00';
----
2028-02-28 01:00:00+01


# Without the cast of 't', it matches:
query I
select * from t where t = timestamp '2028-02-28 01:00:00';
----
2028-02-28 01:00:00+01


# IN is affected too (no match)
query I
select * from t where t::timestamp in (timestamp '2028-02-28 01:00:00',timestamp '2028-02-28 11:00:00');
----
2028-02-28 01:00:00+01

# Same problem with the casting in the other direction
statement ok
create table u(t timestamp);

statement ok
insert into u values('1993-02-28 01:00:00');

query IIIII
select t, t::timestamptz, '1993-04-28 01:00:00'::timestamptz, t::timestamptz='1993-02-28 01:00:00', t='1993-02-28 01:00:00' from u;
----
1993-02-28 01:00:00	1993-02-28 01:00:00+01	1993-04-28 01:00:00+02	true	true

# Cast is not invertible
query II
select '1993-03-28 02:00:00'::timestamp, '1993-03-28 02:00:00'::timestamp::timestamptz::timestamp;
----
1993-03-28 02:00:00	1993-03-28 03:00:00

statement ok
create table t1 (ts_s TIMESTAMP_S, ts_ms TIMESTAMP_MS, ts_ns TIMESTAMP_NS, ts TIMESTAMP);

statement ok
insert into t1 values ('2028-02-28 01:00:00.123456789', '2028-02-28 01:00:00.123456789', '2028-02-28 01:00:00.123456789', '2028-02-28 01:00:00.123456789');

query IIII
select * from t1;
----
2028-02-28 01:00:00	2028-02-28 01:00:00.123	2028-02-28 01:00:00.123456789	2028-02-28 01:00:00.123456

# ts is more precise than timestamp_s and timestamp_ms, so casting ts_s, ts_ms to ts is not valid.
query III
select ts = ts_s::TIMESTAMP, ts = ts_ms::TIMESTAMP, ts = ts_ns::TIMESTAMP from t1;
----
false	false	true

# ts is more precise than timestamp_s and timestamp_ms, so conversions can happen
query III
select ts::TIMESTAMP_S = ts_s, ts::TIMESTAMP_MS = ts_ms, ts::TIMESTAMP_NS = ts_ns from t1;
----
true	true	false

# ts_s only has second precision, so the value of ts_s is 2028-02-28 01:00:00 and not equal to 2028-02-28 01:00:00.123456789
query III
select ts_s::timestamp = timestamp '2028-02-28 01:00:00.123456789', ts_ms::timestamp = timestamp '2028-02-28 01:00:00.123456789', ts_ns::timestamp = timestamp '2028-02-28 01:00:00.123456789'  from t1;
----
false	false	true

# ts_ms only has micro second precision, so the value of ts_ms is 2028-02-28 01:00:00.123 and not equal to 2028-02-28 01:00:00.123456789
query II
select ts_s::timestamp_ms = timestamp_ms '2028-02-28 01:00:00.123456789', ts_ms = timestamp_ms '2028-02-28 01:00:00.123456789'  from t1;
----
false	true

statement error
select ts_ns::timestamp_ms = timestamp_ms '2028-02-28 01:00:00.123456789' from t1;
----
<REGEX>:.*Conversion Error.*