File: test_trunc_precision.test

package info (click to toggle)
duckdb 1.5.1-2
  • 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: 558
file content (106 lines) | stat: -rw-r--r-- 1,917 bytes parent folder | download | duplicates (3)
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
# name: test/sql/function/numeric/test_trunc_precision.test
# description: Truncation to precision tests
# group: [numeric]

statement ok
PRAGMA enable_verification

statement ok
CREATE TABLE truncme(a DOUBLE, b INTEGER, c UINTEGER)

statement ok
INSERT INTO truncme VALUES (42.123456, 37, 19), (-3.141592, -75, 5);

# Non-integral types should truncate 
foreach datatype DOUBLE FLOAT DECIMAL(10,6)

query RR
select trunc(42.12345::${datatype}, 2), trunc(42.12345::${datatype}, -1)
----
42.12	40

query RR
select trunc(-42.12345::${datatype}, 2), trunc(-42.12345::${datatype}, -1)
----
-42.12	-40

query RR
select trunc(127::${datatype}, 2), trunc(127::${datatype}, -1)
----
127.000000	120

query RR
select trunc(-127::${datatype}, 2), trunc(-127::${datatype}, -1)
----
-127.000000	-120

query RR
select trunc(0::${datatype}, 2), trunc(0::${datatype}, -1)
----
0.000000	0.000000

query II
select trunc(a::${datatype}, 2), trunc(a::${datatype}, -1)
from truncme
----
42.12	40
-3.14	0

endloop

# Signed integers should truncate too
foreach signed TINYINT SMALLINT INTEGER BIGINT HUGEINT

query RR
select trunc(54::${signed}, 2), trunc(54::${signed}, -1)
----
54	50

query RR
select trunc(-27::${signed}, 2), trunc(-27::${signed}, -1)
----
-27	-20

query II
select trunc(b::${signed}, 2), trunc(b::${signed}, -1)
from truncme
----
37	30
-75	-70

endloop

# Unsigned integers should truncate too
foreach unsigned UTINYINT USMALLINT UINTEGER UBIGINT UHUGEINT

query RR
select trunc(15::${unsigned}, 2), trunc(15::${unsigned}, -1)
----
15	10

query II
select trunc(c::${unsigned}, 2), trunc(c::${unsigned}, -1)
from truncme
----
19	10
5	0

endloop

# Test specials
foreach special DOUBLE FLOAT

foreach precision 2 0 -1

query III
select 
	trunc('NAN'::${special}, ${precision}), 
	trunc('infinity'::${special}, ${precision}), 
	trunc('-infinity'::${special}, ${precision})
;
----
NAN	infinity	-infinity

endloop

endloop