File: string_to_struct_escapes.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 (263 lines) | stat: -rw-r--r-- 8,354 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
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
# name: test/sql/cast/string_to_struct_escapes.test
# group: [cast]

query I
SELECT $${name: value, age: 30}$$::STRUCT(name VARCHAR, age INT);
----
{'name': value, 'age': 30}

query I
SELECT $${name: John, city: "New York"}$$::STRUCT(name VARCHAR, city VARCHAR);
----
{'name': John, 'city': New York}

query I
SELECT $${quote_at_start: "\"test\"", age: 30}$$::STRUCT(quote_at_start VARCHAR, age INT);
----
{'quote_at_start': '"test"', 'age': 30}

query I
SELECT $${user_name: Alice, status: active}$$::STRUCT(user_name VARCHAR, status VARCHAR);
----
{'user_name': Alice, 'status': active}

query I
SELECT $${special_characters: "comma, backslash\\", age: 30}$$::STRUCT(special_characters VARCHAR, age INT);
----
{'special_characters': 'comma, backslash\\', 'age': 30}

query I
SELECT $${a: 10, b: "hello world"}$$::STRUCT(a INT, b VARCHAR);
----
{'a': 10, 'b': hello world}

query I
SELECT $${first_name: "John", last_name: "Doe", age: 28}$$::STRUCT(first_name VARCHAR, last_name VARCHAR, age INT);
----
{'first_name': John, 'last_name': Doe, 'age': 28}

query I
SELECT $${first name: John, age: 30}$$::STRUCT("first name" VARCHAR, age INT);
----
{'first name': John, 'age': 30}

# Invalid: Value contains a quote that isn't escaped
statement error
SELECT $${name: "John "Doe"}$$::STRUCT(name VARCHAR);
----
can't be cast to the destination type

# second key has no ending character (:)
statement error
SELECT $${name: John, age, 30}$$::STRUCT(name VARCHAR, age INT);
----
can't be cast to the destination type

# Name is free to contain `,`, only `:` is problematic
query I
SELECT $${user,name: Alice, age: 30}$$::STRUCT("user,name" VARCHAR, age INT);
----
{'user,name': Alice, 'age': 30}

# Invalid: Contains an unescaped closing bracket
statement error
SELECT $${name: Alice, age: 30})$$::STRUCT(name VARCHAR, age INT);
----
can't be cast to the destination type

# Invalid: Name contains a backslash
statement error
SELECT $${"backslash\name": value}$$::STRUCT("backslash\name" VARCHAR);
----
can't be cast to the destination type

# Valid: Name contains a backslash outside of quotes, interpreted as literal
query III
SELECT $${backslash\name: value}$$::STRUCT("backslash\name" VARCHAR) a, a::VARCHAR::STRUCT("backslash\name" VARCHAR) b, a == b;
----
{'backslash\\name': value}	{'backslash\\name': value}	true

# first `:` is not escaped, won't match the "name:" struct key
statement error
SELECT $${name: test, value: 30}$$::STRUCT("name:" VARCHAR, value INT);
----
can't be cast to the destination type

# Invalid: Name can contain escaped `:`, but only in quotes
statement error
SELECT $${name\:: test, value: 30}$$::STRUCT("name:" VARCHAR, value INT);
----
can't be cast to the destination type STRUCT("name:" VARCHAR, "value" INTEGER)

# Valid: Name can contain escaped `:` in quotes
query I
SELECT $${"name\:": test, value: 30}$$::STRUCT("name:" VARCHAR, value INT);
----
{'name:': test, 'value': 30}

# Name consists of `{}`, not a problem, with this syntax we expect a name, which is a plain string
# Only reserved character there is `:` (and quotes, and backslash of course)
query I
SELECT $${{name}: John, age: 3}$$::STRUCT("{name}" VARCHAR, age INT);
----
{'{name}': John, 'age': 3}

# Name has `{` which normally starts a bracket that disables interpreting escape characters
query I
SELECT $${{\"name\"}: John, age: 3}$$::STRUCT("{""name""}" VARCHAR, age INT);
----
{'{"name"}': John, 'age': 3}

# Name has `{` which normally starts a bracket that disables interpreting escape characters
query I
SELECT $${{\'name\'}: John, age: 3}$$::STRUCT("{'name'}" VARCHAR, age INT);
----
{'{\'name\'}': John, 'age': 3}

# Invalid: Unterminated string value
statement error
SELECT $${name: "John, age: 30}$$::STRUCT(name VARCHAR, age INT);
----
can't be cast to the destination type

query I
SELECT $${}$$::STRUCT(name VARCHAR, age INT);
----
{'name': NULL, 'age': NULL}

# STRUCT with whitespace around colon (escaped)
query I
SELECT $${name : John, age : 30}$$::STRUCT(name VARCHAR, age INT);
----
{'name': John, 'age': 30}

# STRUCT with escaped backslash in value
query I
SELECT $${path: "C:\\Users\\John"}$$::STRUCT(path VARCHAR);
----
{'path': 'C:\\Users\\John'}

# STRUCT with special characters in value, properly escaped
query I
SELECT $${description: "Special characters: \\, \", \', (, )"}$$::STRUCT(description VARCHAR);
----
{'description': 'Special characters: \\, ", \', (, )'}

statement error
SELECT $${first\ name: "John", age: 30}$$::STRUCT("first name" VARCHAR, age INT);
----
can't be cast to the destination type STRUCT("first name" VARCHAR, age INTEGER)

# Valid: Name with escaped space
query I
SELECT $${"first\ name": "John", age: 30}$$::STRUCT("first name" VARCHAR, age INT);
----
{'first name': John, 'age': 30}

# Valid: Name with escaped quote
query I
SELECT $${\"quote at start\": "value", age: 30}$$::STRUCT("""quote at start""" VARCHAR, age INT);
----
{'"quote at start"': value, 'age': 30}

statement error
SELECT $${backslash\\name: "John Doe", age: 30}$$::STRUCT("backslash\name" VARCHAR, age INT);
----
can't be cast to the destination type STRUCT("backslash\name" VARCHAR, age INTEGER)

# Valid: Name with escaped backslash
query I
SELECT $${"backslash\\name": "John Doe", age: 30}$$::STRUCT("backslash\name" VARCHAR, age INT);
----
{'backslash\\name': John Doe, 'age': 30}

statement error
SELECT $${user\,name: "Alice", age: 25}$$::STRUCT("user,name" VARCHAR, age INT);
----
can't be cast to the destination type STRUCT("user,name" VARCHAR, age INTEGER)

# Valid: Name with escaped comma
query I
SELECT $${"user\,name": "Alice", age: 25}$$::STRUCT("user,name" VARCHAR, age INT);
----
{'user,name': Alice, 'age': 25}

# Valid: Name with comma
query I
SELECT $${"user,name": "Alice", age: 25}$$::STRUCT("user,name" VARCHAR, age INT);
----
{'user,name': Alice, 'age': 25}

statement error
SELECT $${user\(name\): "Alice", status: "active"}$$::STRUCT("user(name)" VARCHAR, status VARCHAR);
----
can't be cast to the destination type STRUCT("user(name)" VARCHAR, status VARCHAR)

# Valid: Name with escaped parenthesis
query I
SELECT $${"user\(name\)": "Alice", status: "active"}$$::STRUCT("user(name)" VARCHAR, status VARCHAR);
----
{'user(name)': Alice, 'status': active}

# Valid: Name with unescaped parenthesis
query I
SELECT $${user(name): "Alice", status: "active"}$$::STRUCT("user(name)" VARCHAR, status VARCHAR);
----
{'user(name)': Alice, 'status': active}

# Valid: Name with escaped space at end
query I
SELECT $${"user\ name\ ": "Alice", "age ": 25}$$::STRUCT("user name " VARCHAR, "age " INT);
----
{'user name ': Alice, 'age ': 25}

statement error
SELECT $${user\ name\ : "Alice", age\ : 25}$$::STRUCT("user name " VARCHAR, "age " INT);
----
can't be cast to the destination type STRUCT("user name " VARCHAR, "age " INTEGER)

# Invalid: Name contains unescaped quote
statement error
SELECT $${"quote"start": "value", age: 30}$$::STRUCT("quote""start" VARCHAR, age INT);
----
can't be cast to the destination type

# Valid: Name contains unescaped backslash outside of quotes
query I
SELECT $${backslash\name: "John", age: 30}$$::STRUCT("backslash\name" VARCHAR, age INT);
----
{'backslash\\name': John, 'age': 30}

# Valid: Name contains (unescaped) opening parenthesis
query I
SELECT $${user(name: "Alice", age: 25}$$::STRUCT("user(name" VARCHAR, age INT);
----
{'user(name': Alice, 'age': 25}

# Name is single double quote
query I
SELECT $${\": "value", age: 30}$$::STRUCT("""" VARCHAR, age INTEGER)
----
{'"': value, 'age': 30}

statement error
SELECT $${\\: "escaped", age: 30}$$::STRUCT("\" VARCHAR, age INT);
----
can't be cast to the destination type STRUCT("\" VARCHAR, age INTEGER)

# Name with only a special character (escaped)
query I
SELECT $${"\\": "escaped", age: 30}$$::STRUCT("\" VARCHAR, age INT);
----
{'\\': escaped, 'age': 30}

# Name with only a special character (not escaped)
query I
SELECT $${@: "value", age: 30}$$::STRUCT("@" VARCHAR, age INT);
----
{'@': value, 'age': 30}

query III
select $$[{'a': test}, {'a': NULL}, {'a': 'null'}, {'a': 'nUlL'}, {'a': NULL}, {'a': NULLz}, {'a': 'NULL'}]$$::STRUCT(a VARCHAR)[] a, a::VARCHAR::STRUCT(a VARCHAR)[] b, a == b
----
[{'a': test}, {'a': NULL}, {'a': 'null'}, {'a': 'nUlL'}, {'a': NULL}, {'a': NULLz}, {'a': 'NULL'}]	[{'a': test}, {'a': NULL}, {'a': 'null'}, {'a': 'nUlL'}, {'a': NULL}, {'a': NULLz}, {'a': 'NULL'}]	true