File: parse_formatted_bytes.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 (216 lines) | stat: -rw-r--r-- 3,810 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
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
# name: test/sql/function/string/parse_formatted_bytes.test
# description: Test the parse_formatted_bytes function
# group: [string]

# Basic bytes and aliases
query I
SELECT parse_formatted_bytes('0 B');
----
0

query I
SELECT parse_formatted_bytes('1 byte');
----
1

query I
SELECT parse_formatted_bytes('2 bytes');
----
2

query I
SELECT parse_formatted_bytes('1 b');
----
1

# Decimal (1000^i) units
query I
SELECT parse_formatted_bytes('1 KB');
----
1000

query I
SELECT parse_formatted_bytes('1.5 KB');
----
1500

query I
SELECT parse_formatted_bytes('2 MB');
----
2000000

query I
SELECT parse_formatted_bytes('1 GB');
----
1000000000

query I
SELECT parse_formatted_bytes('1 TB');
----
1000000000000

# Binary (1024^i) units
query I
SELECT parse_formatted_bytes('1 KiB');
----
1024

query I
SELECT parse_formatted_bytes('1.5 KiB');
----
1536

query I
SELECT parse_formatted_bytes('2 MiB');
----
2097152

query I
SELECT parse_formatted_bytes('1 GiB');
----
1073741824

# No-space and mixed casing should work
query I
SELECT parse_formatted_bytes('5MB');
----
5000000

query I
SELECT parse_formatted_bytes('  2  mib  ');
----
2097152

query I
SELECT parse_formatted_bytes('1e3 b');
----
1000

# NULL input propagates to NULL
query I
SELECT parse_formatted_bytes(NULL);
----
NULL

# running the function on a table column
query II
WITH data(value) AS (
	VALUES
		('1 KB'),
		('2 MB'),
		('3 GiB'),
		('4 TB'),
		(NULL)
)
SELECT value, parse_formatted_bytes(value) AS bytes FROM data;
----
1 KB	1000
2 MB	2000000
3 GiB	3221225472
4 TB	4000000000000
NULL	NULL

# Missing number cases
statement error
SELECT parse_formatted_bytes('null');
----
Invalid Input Error: Memory must have a number (e.g. 1GB)

statement error
SELECT parse_formatted_bytes('none');
----
Invalid Input Error: Memory must have a number (e.g. 1GB)

# Error cases
statement error
SELECT parse_formatted_bytes('5');
----
Invalid Input Error: Unknown unit for memory: '' (expected: KB, MB, GB, TB for 1000^i units or KiB, MiB, GiB, TiB for 1024^i units)

statement error
SELECT parse_formatted_bytes('abc');
----
Invalid Input Error: Memory must have a number (e.g. 1GB)

statement error
SELECT parse_formatted_bytes('1 Ki');
----
Invalid Input Error: Unknown unit for memory: 'ki' (expected: KB, MB, GB, TB for 1000^i units or KiB, MiB, GiB, TiB for 1024^i units)

statement error
SELECT parse_formatted_bytes(1933);
----
Binder Error: No function matches the given name and argument types 'parse_formatted_bytes(INTEGER_LITERAL)'. You might need to add explicit type casts.

statement error
SELECT parse_formatted_bytes('10000000000 TiB');
----
Invalid Input Error: Memory value out of range: value is too large

# not a number
statement error
SELECT parse_formatted_bytes('1.5.3 GB');
----
Invalid Input Error: Invalid memory limit: '1.5.3'

statement error
SELECT parse_formatted_bytes('inf GiB');
----
Invalid Input Error: Memory must have a number (e.g. 1GB)

# Negative values are mapped to max uint64
statement error
SELECT parse_formatted_bytes('-5 MB');
----
Invalid Input Error: Memory cannot be negative

statement error
SELECT parse_formatted_bytes(' -1 KiB');
----
Invalid Input Error: Memory cannot be negative

# Using try() to return NULL on failure
query I
SELECT try(parse_formatted_bytes('abc'));
----
NULL

query I
SELECT try(parse_formatted_bytes('-5 MB'));
----
NULL

query I
SELECT try(parse_formatted_bytes('10000000000 TiB'));
----
NULL

query I
SELECT try(parse_formatted_bytes('1.5.3 GB'));
----
NULL

query I
SELECT try(parse_formatted_bytes('inf GiB'));
----
NULL

query I
SELECT try(parse_formatted_bytes('1 Ki'));
----
NULL

query I
SELECT try(parse_formatted_bytes('5'));
----
NULL

query I
SELECT try(parse_formatted_bytes('none'));
----
NULL

query I
SELECT try(parse_formatted_bytes('null'));
----
NULL