~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/test/regress/sql/interval.sql

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--
 
2
-- INTERVAL
 
3
--
 
4
 
 
5
SET DATESTYLE = 'ISO';
 
6
SET IntervalStyle to postgres;
 
7
 
 
8
-- check acceptance of "time zone style"
 
9
SELECT INTERVAL '01:00' AS "One hour";
 
10
SELECT INTERVAL '+02:00' AS "Two hours";
 
11
SELECT INTERVAL '-08:00' AS "Eight hours";
 
12
SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
 
13
SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
 
14
SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
 
15
SELECT INTERVAL '1.5 months' AS "One month 15 days";
 
16
SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
 
17
 
 
18
CREATE TABLE INTERVAL_TBL (f1 interval);
 
19
 
 
20
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
 
21
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
 
22
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
 
23
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
 
24
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
 
25
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
 
26
INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
 
27
INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
 
28
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
 
29
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
 
30
 
 
31
-- badly formatted interval
 
32
INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
 
33
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
 
34
 
 
35
-- test interval operators
 
36
 
 
37
SELECT '' AS ten, * FROM INTERVAL_TBL;
 
38
 
 
39
SELECT '' AS nine, * FROM INTERVAL_TBL
 
40
   WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
 
41
 
 
42
SELECT '' AS three, * FROM INTERVAL_TBL
 
43
   WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
 
44
 
 
45
SELECT '' AS three, * FROM INTERVAL_TBL
 
46
   WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
 
47
 
 
48
SELECT '' AS one, * FROM INTERVAL_TBL
 
49
   WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
 
50
 
 
51
SELECT '' AS five, * FROM INTERVAL_TBL
 
52
   WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
 
53
 
 
54
SELECT '' AS nine, * FROM INTERVAL_TBL
 
55
   WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
 
56
 
 
57
SELECT '' AS fortyfive, r1.*, r2.*
 
58
   FROM INTERVAL_TBL r1, INTERVAL_TBL r2
 
59
   WHERE r1.f1 > r2.f1
 
60
   ORDER BY r1.f1, r2.f1;
 
61
 
 
62
 
 
63
-- Test multiplication and division with intervals.
 
64
-- Floating point arithmetic rounding errors can lead to unexpected results,
 
65
-- though the code attempts to do the right thing and round up to days and
 
66
-- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
 
67
-- Note that it is expected for some day components to be greater than 29 and
 
68
-- some time components be greater than 23:59:59 due to how intervals are
 
69
-- stored internally.
 
70
 
 
71
CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
 
72
COPY INTERVAL_MULDIV_TBL FROM STDIN;
 
73
41 mon 12 days 360:00
 
74
-41 mon -12 days +360:00
 
75
-12 days
 
76
9 mon -27 days 12:34:56
 
77
-3 years 482 days 76:54:32.189
 
78
4 mon
 
79
14 mon
 
80
999 mon 999 days
 
81
\.
 
82
 
 
83
SELECT span * 0.3 AS product
 
84
FROM INTERVAL_MULDIV_TBL;
 
85
 
 
86
SELECT span * 8.2 AS product
 
87
FROM INTERVAL_MULDIV_TBL;
 
88
 
 
89
SELECT span / 10 AS quotient
 
90
FROM INTERVAL_MULDIV_TBL;
 
91
 
 
92
SELECT span / 100 AS quotient
 
93
FROM INTERVAL_MULDIV_TBL;
 
94
 
 
95
DROP TABLE INTERVAL_MULDIV_TBL;
 
96
 
 
97
SET DATESTYLE = 'postgres';
 
98
SET IntervalStyle to postgres_verbose;
 
99
 
 
100
SELECT '' AS ten, * FROM INTERVAL_TBL;
 
101
 
 
102
-- test avg(interval), which is somewhat fragile since people have been
 
103
-- known to change the allowed input syntax for type interval without
 
104
-- updating pg_aggregate.agginitval
 
105
 
 
106
select avg(f1) from interval_tbl;
 
107
 
 
108
-- test long interval input
 
109
select '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'::interval;
 
110
 
 
111
 
 
112
-- test justify_hours() and justify_days()
 
113
 
 
114
SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as "6 mons 5 days 4 hours 3 mins 2 seconds";
 
115
SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as "7 mons 6 days 5 hours 4 mins 3 seconds";
 
116
 
 
117
-- test justify_interval()
 
118
 
 
119
SELECT justify_interval(interval '1 month -1 hour') as "1 month -1 hour";
 
120
 
 
121
-- test fractional second input, and detection of duplicate units
 
122
SET DATESTYLE = 'ISO';
 
123
SET IntervalStyle TO postgres;
 
124
 
 
125
SELECT '1 millisecond'::interval, '1 microsecond'::interval,
 
126
       '500 seconds 99 milliseconds 51 microseconds'::interval;
 
127
SELECT '3 days 5 milliseconds'::interval;
 
128
 
 
129
SELECT '1 second 2 seconds'::interval;              -- error
 
130
SELECT '10 milliseconds 20 milliseconds'::interval; -- error
 
131
SELECT '5.5 seconds 3 milliseconds'::interval;      -- error
 
132
SELECT '1:20:05 5 microseconds'::interval;          -- error
 
133
SELECT '1 day 1 day'::interval;                     -- error
 
134
SELECT interval '1-2';  -- SQL year-month literal
 
135
SELECT interval '999' second;  -- oversize leading field is ok
 
136
SELECT interval '999' minute;
 
137
SELECT interval '999' hour;
 
138
SELECT interval '999' day;
 
139
SELECT interval '999' month;
 
140
 
 
141
-- test SQL-spec syntaxes for restricted field sets
 
142
SELECT interval '1' year;
 
143
SELECT interval '2' month;
 
144
SELECT interval '3' day;
 
145
SELECT interval '4' hour;
 
146
SELECT interval '5' minute;
 
147
SELECT interval '6' second;
 
148
SELECT interval '1' year to month;
 
149
SELECT interval '1-2' year to month;
 
150
SELECT interval '1 2' day to hour;
 
151
SELECT interval '1 2:03' day to hour;
 
152
SELECT interval '1 2:03:04' day to hour;
 
153
SELECT interval '1 2' day to minute;
 
154
SELECT interval '1 2:03' day to minute;
 
155
SELECT interval '1 2:03:04' day to minute;
 
156
SELECT interval '1 2' day to second;
 
157
SELECT interval '1 2:03' day to second;
 
158
SELECT interval '1 2:03:04' day to second;
 
159
SELECT interval '1 2' hour to minute;
 
160
SELECT interval '1 2:03' hour to minute;
 
161
SELECT interval '1 2:03:04' hour to minute;
 
162
SELECT interval '1 2' hour to second;
 
163
SELECT interval '1 2:03' hour to second;
 
164
SELECT interval '1 2:03:04' hour to second;
 
165
SELECT interval '1 2' minute to second;
 
166
SELECT interval '1 2:03' minute to second;
 
167
SELECT interval '1 2:03:04' minute to second;
 
168
SELECT interval '123 11' day to hour; -- ok
 
169
SELECT interval '123 11' day; -- not ok
 
170
SELECT interval '123 11'; -- not ok, too ambiguous
 
171
 
 
172
-- test syntaxes for restricted precision
 
173
SELECT interval(0) '1 day 01:23:45.6789';
 
174
SELECT interval(2) '1 day 01:23:45.6789';
 
175
SELECT interval '12:34.5678' minute to second(2);  -- per SQL spec
 
176
SELECT interval(2) '12:34.5678' minute to second;  -- historical PG
 
177
SELECT interval(2) '12:34.5678' minute to second(2);  -- syntax error
 
178
SELECT interval '1.234' second;
 
179
SELECT interval '1.234' second(2);
 
180
SELECT interval '1 2.345' day to second(2);
 
181
SELECT interval '1 2:03' day to second(2);
 
182
SELECT interval '1 2:03.4567' day to second(2);
 
183
SELECT interval '1 2:03:04.5678' day to second(2);
 
184
SELECT interval '1 2.345' hour to second(2);
 
185
SELECT interval '1 2:03.45678' hour to second(2);
 
186
SELECT interval '1 2:03:04.5678' hour to second(2);
 
187
SELECT interval '1 2.3456' minute to second(2);
 
188
SELECT interval '1 2:03.5678' minute to second(2);
 
189
SELECT interval '1 2:03:04.5678' minute to second(2);
 
190
 
 
191
-- test inputting and outputting SQL standard interval literals
 
192
SET IntervalStyle TO sql_standard;
 
193
SELECT  interval '0'                       AS "zero",
 
194
        interval '1-2' year to month       AS "year-month",
 
195
        interval '1 2:03:04' day to second AS "day-time",
 
196
        - interval '1-2'                   AS "negative year-month",
 
197
        - interval '1 2:03:04'             AS "negative day-time";
 
198
 
 
199
-- test input of some not-quite-standard interval values in the sql style
 
200
SET IntervalStyle TO postgres;
 
201
SELECT  interval '+1 -1:00:00',
 
202
        interval '-1 +1:00:00',
 
203
        interval '+1-2 -3 +4:05:06.789',
 
204
        interval '-1-2 +3 -4:05:06.789';
 
205
 
 
206
-- test output of couple non-standard interval values in the sql style
 
207
SET IntervalStyle TO sql_standard;
 
208
SELECT  interval '1 day -1 hours',
 
209
        interval '-1 days +1 hours',
 
210
        interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
 
211
        - interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
 
212
 
 
213
-- test outputting iso8601 intervals
 
214
SET IntervalStyle to iso_8601;
 
215
select  interval '0'                                AS "zero",
 
216
        interval '1-2'                              AS "a year 2 months",
 
217
        interval '1 2:03:04'                        AS "a bit over a day",
 
218
        interval '2:03:04.45679'                    AS "a bit over 2 hours",
 
219
        (interval '1-2' + interval '3 4:05:06.7')   AS "all fields",
 
220
        (interval '1-2' - interval '3 4:05:06.7')   AS "mixed sign",
 
221
        (- interval '1-2' + interval '3 4:05:06.7') AS "negative";
 
222
 
 
223
-- test inputting ISO 8601 4.4.2.1 "Format With Time Unit Designators"
 
224
SET IntervalStyle to sql_standard;
 
225
select  interval 'P0Y'                    AS "zero",
 
226
        interval 'P1Y2M'                  AS "a year 2 months",
 
227
        interval 'P1W'                    AS "a week",
 
228
        interval 'P1DT2H3M4S'             AS "a bit over a day",
 
229
        interval 'P1Y2M3DT4H5M6.7S'       AS "all fields",
 
230
        interval 'P-1Y-2M-3DT-4H-5M-6.7S' AS "negative",
 
231
        interval 'PT-0.1S'                AS "fractional second";
 
232
 
 
233
-- test inputting ISO 8601 4.4.2.2 "Alternative Format"
 
234
SET IntervalStyle to postgres;
 
235
select  interval 'P00021015T103020'       AS "ISO8601 Basic Format",
 
236
        interval 'P0002-10-15T10:30:20'   AS "ISO8601 Extended Format";
 
237
 
 
238
-- Make sure optional ISO8601 alternative format fields are optional.
 
239
select  interval 'P0002'                  AS "year only",
 
240
        interval 'P0002-10'               AS "year month",
 
241
        interval 'P0002-10-15'            AS "year month day",
 
242
        interval 'P0002T1S'               AS "year only plus time",
 
243
        interval 'P0002-10T1S'            AS "year month plus time",
 
244
        interval 'P0002-10-15T1S'         AS "year month day plus time",
 
245
        interval 'PT10'                   AS "hour only",
 
246
        interval 'PT10:30'                AS "hour minute";
 
247
 
 
248
-- test a couple rounding cases that changed since 8.3 w/ HAVE_INT64_TIMESTAMP.
 
249
SET IntervalStyle to postgres_verbose;
 
250
select interval '-10 mons -3 days +03:55:06.70';
 
251
select interval '1 year 2 mons 3 days 04:05:06.699999';
 
252
select interval '0:0:0.7', interval '@ 0.70 secs', interval '0.7 seconds';
 
253
 
 
254
-- check that '30 days' equals '1 month' according to the hash function
 
255
select '30 days'::interval = '1 month'::interval as t;
 
256
select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;