forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecimal.slt
368 lines (313 loc) · 8.22 KB
/
decimal.slt
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
#
# This file is derived from the logic test suite in CockroachDB. The
# original file was retrieved on June 10, 2019 from:
#
# https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/decimal
#
# The original source code is subject to the terms of the Apache
# 2.0 license, a copy of which can be found in the LICENSE file at the
# root of this repository.
mode cockroach
# The following tests have results equivalent to Postgres (differences
# in string representation and number of decimals returned, but otherwise
# the same). These do not pass using the inf package. The inf package
# (http://gopkg.in/inf.v0) is what we used to use, but it had various problems
# (for example, all the test cases below), and was replaced with apd.
# inf returns 0
query R
SELECT (1.4238790346995263e-40::DECIMAL / 6.011482313728436e+41::DECIMAL)
----
2.3685988919035999994E-82
# inf returns -108.4851126682386588
query R
SELECT ln(7.682705743584112e-48::DECIMAL)
----
-108.48511266823882051
# inf returns 0
query R
SELECT sqrt(9.789765531128956e-34::DECIMAL)
----
3.1288601009199749773E-17
# inf returns 0.1547300000000000
query R
SELECT pow(4.727998800941528e-14::DECIMAL, 0.06081860494226844::DECIMAL)
----
0.15472926640705911955
# inf returns 0, 0
query RR
SELECT pow(sqrt(1e-10::DECIMAL), 2), sqrt(pow(1e-5::DECIMAL, 2))
----
1E-10 0.00001
# inf returns 1e-16, 0, 2e-16
query RRR
SELECT 1e-16::DECIMAL / 2, 1e-16::DECIMAL / 3, 1e-16::DECIMAL / 2 * 2
----
5E-17 3.3333333333333333333E-17 1.0E-16
# inf returns 1e-8, 0, 0, 0
query RRRR
SELECT pow(1e-4::DECIMAL, 2), pow(1e-5::DECIMAL, 2), pow(1e-8::DECIMAL, 2), pow(1e-9::DECIMAL, 2)
----
1E-8 1E-10 1E-16 1E-18
# inf returns argument too large
query R
SELECT pow(1e-10::DECIMAL, 2)
----
1E-20
# inf panics (#13051)
query RR
SELECT 'NaN'::FLOAT::DECIMAL, 'NaN'::DECIMAL
----
NaN NaN
# Ensure trailing zeros are kept for decimal types with no listed scale,
# and enforced when the scale is listed.
statement ok
CREATE TABLE t (d decimal, v decimal(3, 1))
statement ok
INSERT INTO t VALUES (0.000::decimal, 0.00::decimal), (1.00::decimal, 1.00::decimal), (2.0::decimal, 2.0::decimal), (3::decimal, 3::decimal)
query RR
SELECT * FROM t ORDER BY d
----
0.000 0.0
1.00 1.0
2.0 2.0
3 3.0
# Ensure trailing zeros are kept in an index.
statement ok
CREATE TABLE t2 (d decimal, v decimal(3, 1), primary key (d, v))
statement ok
INSERT INTO t2 VALUES
(1.00::decimal, 1.00::decimal),
(2.0::decimal, 2.0::decimal),
(3::decimal, 3::decimal),
('NaN'::decimal, 'NaN'::decimal),
('Inf'::decimal, 'Inf'::decimal),
('-Inf'::decimal, '-Inf'::decimal),
('-0.0000'::decimal, '-0.0000'::decimal)
query RR
SELECT * FROM t2 ORDER BY d
----
NaN NaN
-Infinity -Infinity
0.0000 0.0
1.00 1.0
2.0 2.0
3 3.0
Infinity Infinity
# Ensure uniqueness in PK columns with +/- NaN and 0.
statement error duplicate key value
INSERT INTO t2 VALUES ('-NaN'::decimal, '-NaN'::decimal)
statement error duplicate key value
INSERT INTO t2 VALUES (0, 0)
# Ensure NaN cannot be signaling or negative.
query RRRR
SELECT 'NaN'::decimal, '-NaN'::decimal, 'sNaN'::decimal, '-sNaN'::decimal
----
NaN NaN NaN NaN
query RR
SELECT * FROM t2 WHERE d IS NaN and v IS NaN
----
NaN NaN
query RR
SELECT * FROM t2 WHERE d = 'Infinity' and v = 'Infinity'
----
Infinity Infinity
query RR
SELECT * FROM t2 WHERE d = '-Infinity' and v = '-Infinity'
----
-Infinity -Infinity
# Ensure special values are handled correctly.
statement ok
CREATE TABLE s (d decimal null, index (d))
statement ok
INSERT INTO s VALUES
(null),
('NaN'::decimal),
('-NaN'::decimal),
('Inf'::decimal),
('-Inf'::decimal),
('0'::decimal),
(1),
(-1)
statement ok
INSERT INTO s VALUES
('-0'::decimal),
('-0.0'::decimal),
('-0.00'::decimal),
('-0.00E-1'::decimal),
('-0.0E-3'::decimal)
query R rowsort
SELECT * FROM s WHERE d = 0
----
0
0
0.0
0.00
0.000
0.0000
query R
SELECT * FROM s WHERE d IS NAN
----
NaN
NaN
query R
SELECT * FROM s WHERE d = 'inf'::decimal
----
Infinity
query R
SELECT * FROM s WHERE d = 'NaN'
----
NaN
NaN
# In the following tests, the various zero values all compare equal to
# each other so we must use two ORDER BY clauses to obtain a stable result.
# Check the ordering of decimal values.
query R
SELECT d FROM s ORDER BY d, d::TEXT
----
NULL
NaN
NaN
-Infinity
-1
0
0
0.0
0.00
0.000
0.0000
1
Infinity
# Just test the NaN-ness of the values.
query RBBB
SELECT d, d IS NaN, d = 'NaN', isnan(d) FROM s@{FORCE_INDEX=primary} ORDER BY d, d::TEXT
----
NULL NULL NULL NULL
NaN true true true
NaN true true true
-Infinity false false false
-1 false false false
0 false false false
0 false false false
0.0 false false false
0.00 false false false
0.000 false false false
0.0000 false false false
1 false false false
Infinity false false false
# Just test the NaN-ness of the values in secondary index
query RBBB
SELECT d, d IS NaN, d = 'NaN', isnan(d) FROM s@{FORCE_INDEX=s_d_idx} ORDER BY d, d::TEXT
----
NULL NULL NULL NULL
NaN true true true
NaN true true true
-Infinity false false false
-1 false false false
0 false false false
0 false false false
0.0 false false false
0.00 false false false
0.000 false false false
0.0000 false false false
1 false false false
Infinity false false false
query RB
select d, d > 'NaN' from s@{FORCE_INDEX=primary} where d > 'NaN' ORDER BY d, d::TEXT
----
-Infinity true
-1 true
0 true
0 true
0.0 true
0.00 true
0.000 true
0.0000 true
1 true
Infinity true
query RB
select d, d > 'NaN' from s@{FORCE_INDEX=s_d_idx} where d > 'NaN' ORDER BY d, d::TEXT
----
-Infinity true
-1 true
0 true
0 true
0.0 true
0.00 true
0.000 true
0.0000 true
1 true
Infinity true
# Verify that decimals don't lose trailing 0s even when used for an index.
statement ok
CREATE INDEX idx ON s (d)
query R rowsort
SELECT * FROM s@idx WHERE d = 0
----
0
0
0.0
0.00
0.000
0.0000
statement ok
INSERT INTO s VALUES
('10'::decimal),
('10.0'::decimal),
('10.00'::decimal),
('10.000'::decimal),
('100000E-4'::decimal),
('1000000E-5'::decimal),
('1.0000000E+1'::decimal)
query R rowsort
SELECT * FROM s@primary WHERE d = 10
----
10
10.0
10.00
10.000
10.0000
10.00000
10.000000
query R rowsort
SELECT * FROM s@idx WHERE d = 10
----
10
10.0
10.00
10.000
10.0000
10.00000
10.000000
query R
SELECT 1.00::decimal(6,4)
----
1.0000
statement error value with precision 6, scale 4 must round to an absolute value less than 10\^2
SELECT 101.00::decimal(6,4)
statement error scale \(6\) must be between 0 and precision \(4\)
SELECT 101.00::decimal(4,6)
statement error value with precision 2, scale 2 must round to an absolute value less than 1
SELECT 1::decimal(2, 2)
# Regression test for #16081
statement ok
CREATE TABLE a (b DECIMAL)
statement ok
INSERT INTO a VALUES (142378208485490985369999605144727062141206925976498256305323716858805588894693616552055968571135475510700810219028167653516982373238641332965927953273383572708760984694356069974208844865675206339235758647159337463780100273189720943242182911961627806424621091859596571173867825568394327041453823674373002756096)
query R
SELECT * FROM a
----
142378208485490985369999605144727062141206925976498256305323716858805588894693616552055968571135475510700810219028167653516982373238641332965927953273383572708760984694356069974208844865675206339235758647159337463780100273189720943242182911961627806424621091859596571173867825568394327041453823674373002756096
# Verify that NaNs are returned instead of invalid operation.
query R
SELECT 'inf'::decimal + '-inf'::decimal
----
NaN