jupyter-duckdb 1.2.0.6__py3-none-any.whl → 1.2.100__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,454 @@
1
+ from duckdb_kernel.parser import DCParser
2
+ from . import Connection
3
+
4
+
5
+ def test_case_insensitivity():
6
+ for query in (
7
+ '{ username | users(id, username) }',
8
+ '{ username | Users(id, username) }',
9
+ '{ username | USERS(id, username) }',
10
+ '{ username | uSers(id, username) }',
11
+ ):
12
+ root = DCParser.parse_query(query)
13
+
14
+ # execute to test case insensitivity
15
+ with Connection() as con:
16
+ cols, rows = con.execute_dc_return_cols(root)
17
+
18
+ assert [c.lower() for c in cols] == [
19
+ 'username'
20
+ ]
21
+ assert rows == [
22
+ ('Alice',),
23
+ ('Bob',),
24
+ ('Charlie',)
25
+ ]
26
+
27
+ for query in (
28
+ '{ username | users(id, username) }',
29
+ '{ Username | users(id, username) }',
30
+ '{ USERNAME | users(id, username) }',
31
+ '{ userName | users(id, username) }',
32
+ ):
33
+ root = DCParser.parse_query(query)
34
+
35
+ # execute to test case insensitivity
36
+ with Connection() as con:
37
+ cols, rows = con.execute_dc_return_cols(root)
38
+
39
+ assert [c.lower() for c in cols] == [
40
+ 'username'
41
+ ]
42
+ assert rows == [
43
+ ('Alice',),
44
+ ('Bob',),
45
+ ('Charlie',)
46
+ ]
47
+
48
+
49
+ def test_simple_queries():
50
+ with Connection() as con:
51
+ for query in [
52
+ '{ id | Users(id, _) }',
53
+ '{ id | Users (id, _) }',
54
+ '{ id | Users id,_ }',
55
+ '{id|Users(id,_)}',
56
+ 'id | Users (id ,_)',
57
+ ]:
58
+ root = DCParser.parse_query(query)
59
+ cols, rows = con.execute_dc_return_cols(root)
60
+
61
+ assert [c.lower() for c in cols] == [
62
+ 'id'
63
+ ]
64
+ assert rows == [
65
+ (1,),
66
+ (2,),
67
+ (3,)
68
+ ]
69
+
70
+ for query in [
71
+ '{ id, name | Users(id, name) }',
72
+ '{ id,name | Users(id, name) }'
73
+ ]:
74
+ root = DCParser.parse_query(query)
75
+ cols, rows = con.execute_dc_return_cols(root)
76
+
77
+ assert [c.lower() for c in cols] == [
78
+ 'id',
79
+ 'name'
80
+ ]
81
+ assert rows == [
82
+ (1, 'Alice'),
83
+ (2, 'Bob'),
84
+ (3, 'Charlie')
85
+ ]
86
+
87
+
88
+ def test_asterisk_projection():
89
+ with Connection() as con:
90
+ root = DCParser.parse_query('{ * | Users(id, _) }')
91
+ cols, rows = con.execute_dc_return_cols(root)
92
+
93
+ assert [c.lower() for c in cols] == [
94
+ 'id'
95
+ ]
96
+ assert rows == [
97
+ (1,),
98
+ (2,),
99
+ (3,)
100
+ ]
101
+
102
+ root = DCParser.parse_query('{ * | Users(id, name) }')
103
+ cols, rows = con.execute_dc_return_cols(root)
104
+
105
+ assert [c.lower() for c in cols] == [
106
+ 'id',
107
+ 'name'
108
+ ]
109
+ assert rows == [
110
+ (1, 'Alice'),
111
+ (2, 'Bob'),
112
+ (3, 'Charlie')
113
+ ]
114
+
115
+
116
+ def test_conditions():
117
+ with Connection() as con:
118
+ for query in [
119
+ '{ name | Users(id, name) ∧ id > 1 }',
120
+ '{ name | Users(id, name) ∧ id ≠ 1 }',
121
+ '{ name | Users(id, name) ∧ (id = 2 ∨ id = 3) }'
122
+ ]:
123
+ root = DCParser.parse_query(query)
124
+ cols, rows = con.execute_dc_return_cols(root)
125
+
126
+ assert [c.lower() for c in cols] == [
127
+ 'name'
128
+ ]
129
+ assert rows == [
130
+ ('Bob',),
131
+ ('Charlie',)
132
+ ]
133
+
134
+ for query in [
135
+ '{ id | Users(id, name) ∧ name = "Bob" }',
136
+ '{ id | Users(id, name) ∧ name > "B" ∧ name < "C" }',
137
+ ]:
138
+ root = DCParser.parse_query(query)
139
+ cols, rows = con.execute_dc_return_cols(root)
140
+
141
+ assert [c.lower() for c in cols] == [
142
+ 'id'
143
+ ]
144
+ assert rows == [
145
+ (2,)
146
+ ]
147
+
148
+
149
+ def test_shortcut_conditions():
150
+ with Connection() as con:
151
+ # single shortcut conditions
152
+ for query in [
153
+ '{ name | Users(1, name) }'
154
+ ]:
155
+ root = DCParser.parse_query(query)
156
+ cols, rows = con.execute_dc_return_cols(root)
157
+
158
+ assert [c.lower() for c in cols] == [
159
+ 'name'
160
+ ]
161
+ assert rows == [
162
+ ('Alice',)
163
+ ]
164
+
165
+ for query in [
166
+ '{ season_name | Seasons(1, 1, season_name) }'
167
+ ]:
168
+ root = DCParser.parse_query(query)
169
+ cols, rows = con.execute_dc_return_cols(root)
170
+
171
+ assert [c.lower() for c in cols] == [
172
+ 'season_name'
173
+ ]
174
+ assert rows == [
175
+ ('Show 1 / Season 1',)
176
+ ]
177
+
178
+ # multiple shortcut conditions
179
+ for query in [
180
+ '{ sname, ename | Seasons(snum, 2, sname) ∧ Episodes(enum, snum, 2, ename) }'
181
+ ]:
182
+ root = DCParser.parse_query(query)
183
+ cols, rows = con.execute_dc_return_cols(root)
184
+
185
+ assert [c.lower() for c in cols] == [
186
+ 'sname',
187
+ 'ename'
188
+ ]
189
+ assert rows == [
190
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 1'),
191
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 2'),
192
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 3'),
193
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 1'),
194
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 2'),
195
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 3'),
196
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 4')
197
+ ]
198
+
199
+
200
+ def test_joins():
201
+ with Connection() as con:
202
+ # with one attribute
203
+ for query in [
204
+ '{ sename | Shows(shid, shname) ∧ Seasons(senum, shid, sename) }'
205
+ ]:
206
+ root = DCParser.parse_query(query)
207
+ cols, rows = con.execute_dc_return_cols(root)
208
+
209
+ assert [c.lower() for c in cols] == [
210
+ 'sename'
211
+ ]
212
+ assert rows == [
213
+ ('Show 1 / Season 1',),
214
+ ('Show 1 / Season 2',),
215
+ ('Show 2 / Season 1',),
216
+ ('Show 2 / Season 2',)
217
+ ]
218
+
219
+ for query in [
220
+ '{ sename | Shows(shid, shname) ∧ Seasons(senum, shid, sename) ∧ shname = "Show 1" }',
221
+ '{ sename | Seasons(senum, shid, sename) ∧ Shows(shid, "Show 1") }'
222
+ ]:
223
+ root = DCParser.parse_query(query)
224
+ cols, rows = con.execute_dc_return_cols(root)
225
+
226
+ assert [c.lower() for c in cols] == [
227
+ 'sename'
228
+ ]
229
+ assert rows == [
230
+ ('Show 1 / Season 1',),
231
+ ('Show 1 / Season 2',)
232
+ ]
233
+
234
+ # with multiple attributes
235
+ for query in [
236
+ '{ sname, ename | Seasons(snum, shid, sname) ∧ Episodes(enum, snum, shid, ename) ∧ shid = 2 }'
237
+ ]:
238
+ root = DCParser.parse_query(query)
239
+ cols, rows = con.execute_dc_return_cols(root)
240
+
241
+ assert [c.lower() for c in cols] == [
242
+ 'sname',
243
+ 'ename'
244
+ ]
245
+ assert rows == [
246
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 1'),
247
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 2'),
248
+ ('Show 2 / Season 1', 'Show 2 / Season 1 / Episode 3'),
249
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 1'),
250
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 2'),
251
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 3'),
252
+ ('Show 2 / Season 2', 'Show 2 / Season 2 / Episode 4')
253
+ ]
254
+
255
+ # join three relations
256
+ for query in [
257
+ '{ s2,c5 | Shows(s1,s2) ∧ Episodes(e1,e2,s1,e4) ∧ Characters(c1,e1,c3,s1,c5) ∧ s1=2 ∧ e4="Show 2 / Season 1 / Episode 2" }'
258
+ ]:
259
+ root = DCParser.parse_query(query)
260
+ cols, rows = con.execute_dc_return_cols(root)
261
+
262
+ assert [c.lower() for c in cols] == [
263
+ 's2',
264
+ 'c5'
265
+ ]
266
+ assert rows == [
267
+ ('Show 2', 'Actor F')
268
+ ]
269
+
270
+ # cross join
271
+ root = DCParser.parse_query('{ sename | Shows(shid1, shname) ∧ Seasons(senum, shid2, sename) ∧ shid1 = shid2 }')
272
+ cols, rows = con.execute_dc_return_cols(root)
273
+
274
+ assert [c.lower() for c in cols] == [
275
+ 'sename'
276
+ ]
277
+ assert rows == [
278
+ ('Show 1 / Season 1',),
279
+ ('Show 1 / Season 2',),
280
+ ('Show 2 / Season 1',),
281
+ ('Show 2 / Season 2',)
282
+ ]
283
+
284
+ for query in [
285
+ '{ s2,c5 | Shows(sa1,s2) ∧ Episodes(e1,e2,sb1,e4) ∧ Characters(c1,e1,c3,sb1,c5) ∧ sa1=2 ∧ sa1 = sb1 ∧ e4="Show 2 / Season 1 / Episode 2" }',
286
+ '{ s2,c5 | Shows(sa1,s2) ∧ Episodes(e1,e2,sb1,e4) ∧ Characters(c1,e1,c3,sc1,c5) ∧ sa1=2 ∧ sa1 = sb1 ∧ sb1 = sc1 ∧ e4="Show 2 / Season 1 / Episode 2" }'
287
+ ]:
288
+ root = DCParser.parse_query(query)
289
+ cols, rows = con.execute_dc_return_cols(root)
290
+
291
+ assert [c.lower() for c in cols] == [
292
+ 's2',
293
+ 'c5'
294
+ ]
295
+ assert rows == [
296
+ ('Show 2', 'Actor F')
297
+ ]
298
+
299
+
300
+ def test_disjunction_joins():
301
+ with Connection() as con:
302
+ for query in [
303
+ "{ enum, snum, sid | Episodes(enum, snum, sid, ename) ∧ (Characters('Character B', enum, snum, sid, _) ∨ Characters('Character D', enum, snum, sid, _)) }",
304
+ "{ enum, snum, sid | Episodes(enum, snum, sid, ename) ∧ (Characters(cname1, enum, snum, sid, _) ∧ cname1 = 'Character B' ∨ Characters(cname2, enum, snum, sid, _) ∧ cname2 = 'Character D') }",
305
+ ]:
306
+ root = DCParser.parse_query(query)
307
+ cols, rows = con.execute_dc_return_cols(root)
308
+
309
+ assert [c.lower() for c in cols] == [
310
+ 'enum',
311
+ 'snum',
312
+ 'sid'
313
+ ]
314
+ assert rows == [
315
+ (1, 1, 1),
316
+ (2, 1, 1)
317
+ ]
318
+
319
+
320
+ def test_cross_join():
321
+ with Connection() as con:
322
+ for query in [
323
+ "{ cname1, cname2 | Characters(cname1, _, _, 2, _) ∧ Characters(cname2, _, _, 2, _) }",
324
+ ]:
325
+ root = DCParser.parse_query(query)
326
+ cols, rows = con.execute_dc_return_cols(root)
327
+
328
+ assert [c.lower() for c in cols] == [
329
+ 'cname1',
330
+ 'cname2'
331
+ ]
332
+ assert rows == [
333
+ ('Character E', 'Character E'),
334
+ ('Character E', 'Character F'),
335
+ ('Character F', 'Character E'),
336
+ ('Character F', 'Character F'),
337
+ ]
338
+
339
+
340
+ def test_underscores():
341
+ with Connection() as con:
342
+ # distinct underscores
343
+ for query in [
344
+ '{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, snum, shid, ename) ∧ shid = 2 }',
345
+ '{ ename | Seasons(snum, shid, sname) ∧ Episodes(enum, _, shid, ename) ∧ shid = 2 }',
346
+ '{ ename | Seasons(snum, shid, sname) ∧ Episodes(__, snum, shid, ename) ∧ shid = 2 }',
347
+ '{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, __, shid, ename) ∧ shid = 2 }'
348
+ ]:
349
+ root = DCParser.parse_query(query)
350
+ cols, rows = con.execute_dc_return_cols(root)
351
+
352
+ assert [c.lower() for c in cols] == [
353
+ 'ename'
354
+ ]
355
+ assert rows == [
356
+ ('Show 2 / Season 1 / Episode 1',),
357
+ ('Show 2 / Season 1 / Episode 2',),
358
+ ('Show 2 / Season 1 / Episode 3',),
359
+ ('Show 2 / Season 2 / Episode 1',),
360
+ ('Show 2 / Season 2 / Episode 2',),
361
+ ('Show 2 / Season 2 / Episode 3',),
362
+ ('Show 2 / Season 2 / Episode 4',)
363
+ ]
364
+
365
+ # reused underscores in a single relation
366
+ for query in [
367
+ '{ ename | Seasons(snum, shid, sname) ∧ Episodes(_, _, shid, ename) ∧ shid = 2 }'
368
+ ]:
369
+ root = DCParser.parse_query(query)
370
+ cols, rows = con.execute_dc_return_cols(root)
371
+
372
+ assert [c.lower() for c in cols] == [
373
+ 'ename'
374
+ ]
375
+ assert rows == [
376
+ ('Show 2 / Season 1 / Episode 1',),
377
+ ('Show 2 / Season 1 / Episode 2',),
378
+ ('Show 2 / Season 1 / Episode 3',),
379
+ ('Show 2 / Season 2 / Episode 1',),
380
+ ('Show 2 / Season 2 / Episode 2',),
381
+ ('Show 2 / Season 2 / Episode 3',),
382
+ ('Show 2 / Season 2 / Episode 4',)
383
+ ]
384
+
385
+ # reused underscores in two different relations
386
+ for query in [
387
+ '{ ename | Seasons(_, shid, _) ∧ Episodes(_, _, shid, ename) ∧ shid = 2 }',
388
+ '{ ename | Seasons(_, shid, __) ∧ Episodes(_, __, shid, ename) ∧ shid = 2 }'
389
+ ]:
390
+ root = DCParser.parse_query(query)
391
+ cols, rows = con.execute_dc_return_cols(root)
392
+
393
+ assert [c.lower() for c in cols] == [
394
+ 'ename'
395
+ ]
396
+ assert rows == [
397
+ ('Show 2 / Season 1 / Episode 1',),
398
+ ('Show 2 / Season 1 / Episode 2',),
399
+ ('Show 2 / Season 1 / Episode 3',),
400
+ ('Show 2 / Season 2 / Episode 1',),
401
+ ('Show 2 / Season 2 / Episode 2',),
402
+ ('Show 2 / Season 2 / Episode 3',),
403
+ ('Show 2 / Season 2 / Episode 4',)
404
+ ]
405
+
406
+ def test_anonymous_column_names():
407
+ with Connection() as con:
408
+ for query in [
409
+ '{ * | Episodes(_, _, 2, ename) }',
410
+ ]:
411
+ root = DCParser.parse_query(query)
412
+ cols, _ = con.execute_dc_return_cols(root)
413
+
414
+ assert cols == ['2', 'ename']
415
+
416
+ for query in [
417
+ "{ * | Episodes(_, _, '2', ename) }",
418
+ ]:
419
+ root = DCParser.parse_query(query)
420
+ cols, _ = con.execute_dc_return_cols(root)
421
+
422
+ assert cols == ["'2'", 'ename']
423
+
424
+ for query in [
425
+ "{ * | Episodes(_, _, sid, 'ename') }",
426
+ ]:
427
+ root = DCParser.parse_query(query)
428
+ cols, _ = con.execute_dc_return_cols(root)
429
+
430
+ assert cols == ['sid', "'ename'"]
431
+
432
+ for query in [
433
+ '{ * | Episodes(_, 1, 2, ename) }',
434
+ ]:
435
+ root = DCParser.parse_query(query)
436
+ cols, _ = con.execute_dc_return_cols(root)
437
+
438
+ assert cols == ['1', '2', 'ename']
439
+
440
+ for query in [
441
+ '{ * | Episodes(_, 2, 2, ename) }',
442
+ ]:
443
+ root = DCParser.parse_query(query)
444
+ cols, _ = con.execute_dc_return_cols(root)
445
+
446
+ assert cols == ['2', '2', 'ename']
447
+
448
+ for query in [
449
+ '{ * | Episodes(_, _, sid, ename) ∧ sid = 2 }',
450
+ ]:
451
+ root = DCParser.parse_query(query)
452
+ cols, _ = con.execute_dc_return_cols(root)
453
+
454
+ assert cols == ['sid', 'ename']