-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathclickbench.py
More file actions
executable file
·654 lines (622 loc) · 27.4 KB
/
clickbench.py
File metadata and controls
executable file
·654 lines (622 loc) · 27.4 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright the Vortex contributors
#!/usr/bin/env python3
# ruff: noqa: E501
import argparse
import os.path
import timeit
from collections import defaultdict
from collections.abc import Callable
from datetime import date
from typing import Any
import polars as pl
import vortex as vx
# 0: No., 1: SQL, 2: Polars
queries: list[tuple[str, str, Callable[[pl.LazyFrame], Any]]] = [ # pyright: ignore[reportExplicitAny]
("Q0", "SELECT COUNT(*) FROM hits;", lambda x: x.select(pl.len()).collect().height),
(
"Q1",
"SELECT COUNT(*) FROM hits WHERE AdvEngineID <> 0;",
lambda x: x.select(pl.col("AdvEngineID").filter(pl.col("AdvEngineID") != 0).count()).collect().height,
),
(
"Q2",
"SELECT SUM(AdvEngineID), COUNT(*), AVG(ResolutionWidth) FROM hits;",
lambda x: (
x.select(a_sum=pl.col("AdvEngineID").sum(), count=pl.len(), a_mean=pl.col("AdvEngineID").mean())
.collect()
.rows()[0]
),
),
(
"Q3",
"SELECT AVG(UserID) FROM hits;",
lambda x: x.select(pl.col("UserID").mean()).collect().item(), # pyright: ignore[reportAny]
),
(
"Q4",
"SELECT COUNT(DISTINCT UserID) FROM hits;",
lambda x: x.select(pl.col("UserID").n_unique()).collect().item(), # pyright: ignore[reportAny]
),
(
"Q5",
"SELECT COUNT(DISTINCT SearchPhrase) FROM hits;",
lambda x: x.select(pl.col("SearchPhrase").n_unique()).collect().item(), # pyright: ignore[reportAny]
),
(
"Q6",
"SELECT MIN(EventDate), MAX(EventDate) FROM hits;",
lambda x: x.select(e_min=pl.col("EventDate").min(), e_max=pl.col("EventDate").max()).collect().rows()[0],
),
(
"Q7",
"SELECT AdvEngineID, COUNT(*) FROM hits WHERE AdvEngineID <> 0 GROUP BY AdvEngineID ORDER BY COUNT(*) DESC;",
lambda x: (
x.filter(pl.col("AdvEngineID") != 0)
.group_by("AdvEngineID")
.agg(pl.len().alias("count"))
.sort("count", descending=True)
.collect()
),
),
(
"Q8",
"SELECT RegionID, COUNT(DISTINCT UserID) AS u FROM hits GROUP BY RegionID ORDER BY u DESC LIMIT 10;",
lambda x: (
x.group_by("RegionID").agg(pl.col("UserID").n_unique()).sort("UserID", descending=True).head(10).collect()
),
),
(
"Q9",
"SELECT RegionID, SUM(AdvEngineID), COUNT(*) AS c, AVG(ResolutionWidth), COUNT(DISTINCT UserID) FROM hits GROUP BY RegionID ORDER BY c DESC LIMIT 10;",
lambda x: (
x.group_by("RegionID")
.agg(
[
pl.sum("AdvEngineID").alias("AdvEngineID_sum"),
pl.mean("ResolutionWidth").alias("ResolutionWidth_mean"),
pl.col("UserID").n_unique().alias("UserID_nunique"),
]
)
.sort("AdvEngineID_sum", descending=True)
.head(10)
.collect()
),
),
(
"Q10",
"SELECT MobilePhoneModel, COUNT(DISTINCT UserID) AS u FROM hits WHERE MobilePhoneModel <> '' GROUP BY MobilePhoneModel ORDER BY u DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("MobilePhoneModel") != "")
.group_by("MobilePhoneModel")
.agg(pl.col("UserID").n_unique())
.sort("UserID", descending=True)
.head(10)
.collect()
),
),
(
"Q11",
"SELECT MobilePhone, MobilePhoneModel, COUNT(DISTINCT UserID) AS u FROM hits WHERE MobilePhoneModel <> '' GROUP BY MobilePhone, MobilePhoneModel ORDER BY u DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("MobilePhoneModel") != "")
.group_by(["MobilePhone", "MobilePhoneModel"])
.agg(pl.col("UserID").n_unique())
.sort("UserID", descending=True)
.head(10)
.collect()
),
),
(
"Q12",
"SELECT SearchPhrase, COUNT(*) AS c FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.group_by("SearchPhrase")
.agg(pl.len().alias("count"))
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q13",
"SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.group_by("SearchPhrase")
.agg(pl.col("UserID").n_unique())
.sort("UserID", descending=True)
.head(10)
.collect()
),
),
(
"Q14",
"SELECT SearchEngineID, SearchPhrase, COUNT(*) AS c FROM hits WHERE SearchPhrase <> '' GROUP BY SearchEngineID, SearchPhrase ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.group_by(["SearchEngineID", "SearchPhrase"])
.agg(pl.len().alias("count"))
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q15",
"SELECT UserID, COUNT(*) FROM hits GROUP BY UserID ORDER BY COUNT(*) DESC LIMIT 10;",
lambda x: x.group_by("UserID").agg(pl.len().alias("count")).sort("count", descending=True).head(10).collect(),
),
(
"Q16",
"SELECT UserID, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, SearchPhrase ORDER BY COUNT(*) DESC LIMIT 10;",
lambda x: (
x.group_by(["UserID", "SearchPhrase"])
.agg(pl.len().alias("count"))
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q17",
"SELECT UserID, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, SearchPhrase LIMIT 10;",
lambda x: x.group_by(["UserID", "SearchPhrase"]).agg(pl.len()).head(10).collect(),
),
(
"Q18",
"SELECT UserID, extract(minute FROM EventTime) AS m, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, m, SearchPhrase ORDER BY COUNT(*) DESC LIMIT 10;",
lambda x: (
x.group_by([pl.col("UserID"), pl.col("EventTime").dt.minute(), "SearchPhrase"])
.agg(pl.len().alias("count"))
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q19",
"SELECT UserID FROM hits WHERE UserID = 435090932899640449;",
lambda x: x.select("UserID").filter(pl.col("UserID") == 435090932899640449).collect(),
),
(
"Q20",
"SELECT COUNT(*) FROM hits WHERE URL LIKE '%google%';",
lambda x: x.filter(pl.col("URL").str.contains("google")).select(pl.len()).collect().item(), # pyright: ignore[reportAny]
),
(
"Q21",
"SELECT SearchPhrase, MIN(URL), COUNT(*) AS c FROM hits WHERE URL LIKE '%google%' AND SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter((pl.col("URL").str.contains("google")) & (pl.col("SearchPhrase") != ""))
.group_by("SearchPhrase")
.agg([pl.col("URL").min(), pl.len().alias("count")])
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q22",
"SELECT SearchPhrase, MIN(URL), MIN(Title), COUNT(*) AS c, COUNT(DISTINCT UserID) FROM hits WHERE Title LIKE '%Google%' AND URL NOT LIKE '%.google.%' AND SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter(
(pl.col("Title").str.contains("Google"))
& (~pl.col("URL").str.contains(".google."))
& (pl.col("SearchPhrase") != "")
)
.group_by("SearchPhrase")
.agg(
[
pl.col("URL").min(),
pl.col("Title").min(),
pl.len().alias("count"),
pl.col("UserID").n_unique(),
]
)
.sort("count", descending=True)
.head(10)
.collect()
),
),
(
"Q23",
"SELECT * FROM hits WHERE URL LIKE '%google%' ORDER BY EventTime LIMIT 10;",
lambda x: x.filter(pl.col("URL").str.contains("google")).sort("EventTime").head(10).collect(),
),
(
"Q24",
"SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY EventTime LIMIT 10;",
lambda x: x.filter(pl.col("SearchPhrase") != "").sort("EventTime").select("SearchPhrase").head(10).collect(),
),
(
"Q25",
"SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY SearchPhrase LIMIT 10;",
lambda x: x.filter(pl.col("SearchPhrase") != "").sort("SearchPhrase").select("SearchPhrase").head(10).collect(),
),
(
"Q26",
"SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY EventTime, SearchPhrase LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.sort(["EventTime", "SearchPhrase"])
.select("SearchPhrase")
.head(10)
.collect()
),
),
(
"Q27",
"SELECT CounterID, AVG(STRLEN(URL)) AS l, COUNT(*) AS c FROM hits WHERE URL <> '' GROUP BY CounterID HAVING COUNT(*) > 100000 ORDER BY l DESC LIMIT 25;",
lambda x: (
x.filter(pl.col("URL") != "") # WHERE URL <> ''
.group_by("CounterID") # GROUP BY CounterID
.agg(
[
pl.col("URL").str.len_chars().mean().alias("l"), # AVG(STRLEN(URL))
pl.len().alias("c"), # COUNT(*)
]
)
.filter(pl.col("c") > 100000) # HAVING COUNT(*) > 100000
.sort("l", descending=True) # ORDER BY l DESC
.limit(25)
.collect()
), # LIMIT 25,
),
(
"Q28",
"SELECT REGEXP_REPLACE(Referer, '(?-u)^https?://(?:www\\.)?([^/]+)/.*$', '\\1') AS k, AVG(STRLEN(Referer)) AS l, COUNT(*) AS c, MIN(Referer) FROM hits WHERE Referer <> '' GROUP BY k HAVING COUNT(*) > 100000 ORDER BY l DESC LIMIT 25;",
lambda x: (
x.filter(pl.col("Referer") != "")
.with_columns(pl.col("Referer").str.extract(r"^https?://(?:www\\.)?([^/]+)/.*$").alias("k"))
.group_by("k")
.agg(
[
pl.col("Referer").str.len_chars().mean().alias("l"), # AVG(STRLEN(Referer))
pl.col("Referer").min().alias("min_referer"), # MIN(Referer)
pl.len().alias("c"), # COUNT(*)
]
)
.filter(pl.col("c") > 100000) # HAVING COUNT(*) > 100000
.sort("l", descending=True) # ORDER BY l DESC
.limit(25)
.collect() # LIMIT 25
),
),
(
"Q29",
"SELECT SUM(ResolutionWidth), SUM(ResolutionWidth + 1), SUM(ResolutionWidth + 2), SUM(ResolutionWidth + 3), SUM(ResolutionWidth + 4), SUM(ResolutionWidth + 5), SUM(ResolutionWidth + 6), SUM(ResolutionWidth + 7), SUM(ResolutionWidth + 8), SUM(ResolutionWidth + 9), SUM(ResolutionWidth + 10), SUM(ResolutionWidth + 11), SUM(ResolutionWidth + 12), SUM(ResolutionWidth + 13), SUM(ResolutionWidth + 14), SUM(ResolutionWidth + 15), SUM(ResolutionWidth + 16), SUM(ResolutionWidth + 17), SUM(ResolutionWidth + 18), SUM(ResolutionWidth + 19), SUM(ResolutionWidth + 20), SUM(ResolutionWidth + 21), SUM(ResolutionWidth + 22), SUM(ResolutionWidth + 23), SUM(ResolutionWidth + 24), SUM(ResolutionWidth + 25), SUM(ResolutionWidth + 26), SUM(ResolutionWidth + 27), SUM(ResolutionWidth + 28), SUM(ResolutionWidth + 29), SUM(ResolutionWidth + 30), SUM(ResolutionWidth + 31), SUM(ResolutionWidth + 32), SUM(ResolutionWidth + 33), SUM(ResolutionWidth + 34), SUM(ResolutionWidth + 35), SUM(ResolutionWidth + 36), SUM(ResolutionWidth + 37), SUM(ResolutionWidth + 38), SUM(ResolutionWidth + 39), SUM(ResolutionWidth + 40), SUM(ResolutionWidth + 41), SUM(ResolutionWidth + 42), SUM(ResolutionWidth + 43), SUM(ResolutionWidth + 44), SUM(ResolutionWidth + 45), SUM(ResolutionWidth + 46), SUM(ResolutionWidth + 47), SUM(ResolutionWidth + 48), SUM(ResolutionWidth + 49), SUM(ResolutionWidth + 50), SUM(ResolutionWidth + 51), SUM(ResolutionWidth + 52), SUM(ResolutionWidth + 53), SUM(ResolutionWidth + 54), SUM(ResolutionWidth + 55), SUM(ResolutionWidth + 56), SUM(ResolutionWidth + 57), SUM(ResolutionWidth + 58), SUM(ResolutionWidth + 59), SUM(ResolutionWidth + 60), SUM(ResolutionWidth + 61), SUM(ResolutionWidth + 62), SUM(ResolutionWidth + 63), SUM(ResolutionWidth + 64), SUM(ResolutionWidth + 65), SUM(ResolutionWidth + 66), SUM(ResolutionWidth + 67), SUM(ResolutionWidth + 68), SUM(ResolutionWidth + 69), SUM(ResolutionWidth + 70), SUM(ResolutionWidth + 71), SUM(ResolutionWidth + 72), SUM(ResolutionWidth + 73), SUM(ResolutionWidth + 74), SUM(ResolutionWidth + 75), SUM(ResolutionWidth + 76), SUM(ResolutionWidth + 77), SUM(ResolutionWidth + 78), SUM(ResolutionWidth + 79), SUM(ResolutionWidth + 80), SUM(ResolutionWidth + 81), SUM(ResolutionWidth + 82), SUM(ResolutionWidth + 83), SUM(ResolutionWidth + 84), SUM(ResolutionWidth + 85), SUM(ResolutionWidth + 86), SUM(ResolutionWidth + 87), SUM(ResolutionWidth + 88), SUM(ResolutionWidth + 89) FROM hits;",
lambda x: x.select(pl.sum_horizontal([pl.col("ResolutionWidth").shift(i) for i in range(1, 90)])).collect(),
),
(
"Q30",
"SELECT SearchEngineID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits WHERE SearchPhrase <> '' GROUP BY SearchEngineID, ClientIP ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.group_by(["SearchEngineID", "ClientIP"])
.agg(
[
pl.len().alias("c"),
pl.sum("IsRefresh").alias("IsRefreshSum"),
pl.mean("ResolutionWidth").alias("AvgResolutionWidth"),
]
)
.sort("c", descending=True)
.head(10)
.collect()
),
),
(
"Q31",
"SELECT WatchID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits WHERE SearchPhrase <> '' GROUP BY WatchID, ClientIP ORDER BY c DESC LIMIT 10;",
lambda x: (
x.filter(pl.col("SearchPhrase") != "")
.group_by(["WatchID", "ClientIP"])
.agg(
[
pl.len().alias("c"),
pl.sum("IsRefresh").alias("IsRefreshSum"),
pl.mean("ResolutionWidth").alias("AvgResolutionWidth"),
]
)
.sort("c", descending=True)
.head(10)
.collect()
),
),
(
"Q32",
"SELECT WatchID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits GROUP BY WatchID, ClientIP ORDER BY c DESC LIMIT 10;",
lambda x: (
x.group_by(["WatchID", "ClientIP"])
.agg(
[
pl.len().alias("c"),
pl.sum("IsRefresh").alias("IsRefreshSum"),
pl.mean("ResolutionWidth").alias("AvgResolutionWidth"),
]
)
.sort("c", descending=True)
.head(10)
.collect()
),
),
(
"Q33",
"SELECT URL, COUNT(*) AS c FROM hits GROUP BY URL ORDER BY c DESC LIMIT 10;",
lambda x: x.group_by("URL").agg(pl.len().alias("c")).sort("c", descending=True).head(10).collect(),
),
(
"Q34",
"SELECT 1, URL, COUNT(*) AS c FROM hits GROUP BY 1, URL ORDER BY c DESC LIMIT 10;",
lambda x: x.group_by("URL").agg(pl.len().alias("c")).sort("c", descending=True).head(10).collect(),
),
(
"Q35",
"SELECT ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3, COUNT(*) AS c FROM hits GROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3 ORDER BY c DESC LIMIT 10;",
lambda x: (
x.with_columns([pl.col("ClientIP")])
.group_by(["ClientIP"])
.agg(pl.len().alias("c"))
.sort("c", descending=True)
.head(10)
.collect()
),
),
(
"Q36",
"SELECT URL, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND DontCountHits = 0 AND IsRefresh = 0 AND URL <> '' GROUP BY URL ORDER BY PageViews DESC LIMIT 10;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("DontCountHits") == 0)
& (pl.col("IsRefresh") == 0)
& (pl.col("URL") != "")
)
.group_by("URL")
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.head(10)
.collect()
),
),
(
"Q37",
"SELECT Title, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND DontCountHits = 0 AND IsRefresh = 0 AND Title <> '' GROUP BY Title ORDER BY PageViews DESC LIMIT 10;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("DontCountHits") == 0)
& (pl.col("IsRefresh") == 0)
& (pl.col("Title") != "")
)
.group_by("Title")
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.head(10)
.collect()
),
),
(
"Q38",
"SELECT URL, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND IsLink <> 0 AND IsDownload = 0 GROUP BY URL ORDER BY PageViews DESC LIMIT 10 OFFSET 1000;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("IsRefresh") == 0)
& (pl.col("IsLink") != 0)
& (pl.col("IsDownload") == 0)
)
.group_by("URL")
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.slice(1000, 10)
.collect()
),
),
(
"Q39",
"SELECT TraficSourceID, SearchEngineID, AdvEngineID, CASE WHEN (SearchEngineID = 0 AND AdvEngineID = 0) THEN Referer ELSE '' END AS Src, URL AS Dst, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 GROUP BY TraficSourceID, SearchEngineID, AdvEngineID, Src, Dst ORDER BY PageViews DESC LIMIT 10 OFFSET 1000;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("IsRefresh") == 0)
)
.group_by(
[
"TraficSourceID",
"SearchEngineID",
"AdvEngineID",
pl.when(pl.col("SearchEngineID").eq(0) & pl.col("AdvEngineID").eq(0))
.then(pl.col("Referer"))
.otherwise(pl.lit(""))
.alias("Src"),
"URL",
]
)
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.slice(1000, 10)
.collect()
),
),
(
"Q40",
"SELECT URLHash, EventDate, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND TraficSourceID IN (-1, 6) AND RefererHash = 3594120000172545465 GROUP BY URLHash, EventDate ORDER BY PageViews DESC LIMIT 10 OFFSET 100;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("IsRefresh") == 0)
& (pl.col("TraficSourceID").is_in([-1, 6]))
& (pl.col("RefererHash") == 3594120000172545465)
)
.group_by(["URLHash", "EventDate"])
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.slice(100, 10)
.collect()
),
),
(
"Q41",
"SELECT WindowClientWidth, WindowClientHeight, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND DontCountHits = 0 AND URLHash = 2868770270353813622 GROUP BY WindowClientWidth, WindowClientHeight ORDER BY PageViews DESC LIMIT 10 OFFSET 10000;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 1))
& (pl.col("EventDate") <= date(2013, 7, 31))
& (pl.col("IsRefresh") == 0)
& (pl.col("DontCountHits") == 0)
& (pl.col("URLHash") == 2868770270353813622)
)
.group_by(["WindowClientWidth", "WindowClientHeight"])
.agg(pl.len().alias("PageViews"))
.sort("PageViews", descending=True)
.slice(10000, 10)
.collect()
),
),
(
"Q42",
"SELECT DATE_TRUNC('minute', EventTime) AS M, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-14' AND EventDate <= '2013-07-15' AND IsRefresh = 0 AND DontCountHits = 0 GROUP BY DATE_TRUNC('minute', EventTime) ORDER BY DATE_TRUNC('minute', EventTime) LIMIT 10 OFFSET 1000;",
lambda x: (
x.filter(
(pl.col("CounterID") == 62)
& (pl.col("EventDate") >= date(2013, 7, 14))
& (pl.col("EventDate") <= date(2013, 7, 15))
& (pl.col("IsRefresh") == 0)
& (pl.col("DontCountHits") == 0)
)
.group_by(pl.col("EventTime").dt.truncate("1m"))
.agg(pl.len().alias("PageViews"))
.slice(1000, 10)
.collect()
),
),
]
def run_timings(lf: pl.LazyFrame, name: str, src: str, load_time: int | None) -> dict[str, Any] | None: # pyright: ignore[reportExplicitAny]
queries_times: list[list[float | None]] = []
for q in queries:
# if q[0] == "Q19":
# print("Q19 fails for https://github.com/pola-rs/polars/issues/21674")
# queries_times.append([None, None, None])
# continue
print(q[0])
times: list[float | None] = []
for _ in range(3):
start = timeit.default_timer()
try:
result = q[2](lf) # pyright: ignore[reportAny]
except Exception as e:
print("Failed", e)
result = None
end = timeit.default_timer()
if result is None:
times.append(None)
else:
times.append(end - start)
queries_times.append(times)
result_json = {
"system": name,
"date": date.today().strftime("%Y-%m-%d"),
"machine": "c6a.metal, 500gb gp2",
"cluster_size": 1,
"comment": "",
"tags": [
"column-oriented",
src,
],
"load_time": float(load_time) if load_time is not None else None,
"result": queries_times,
}
print(result_json)
return result_json
# if cpuinfo contains "AMD EPYC 9654" update machine and write result into results/epyc-9654.json
# if "AMD EPYC 9654" in open("/proc/cpuinfo").read():
# result_json["machine"] = "EPYC 9654, 384G"
# with open(f"results/{src}_epyc-9654.json", "w") as f:
# f.write(json.dumps(result_json, indent=4))
# else:
# # write result into results/c6a.metal.json
# with open(f"results/{src}_c6a.metal.json", "w") as f:
# f.write(json.dumps(result_json, indent=4))
PARSER = argparse.ArgumentParser()
PARSER.add_argument( # pyright: ignore[reportUnusedCallResult]
"--path",
type=str,
default="hits.parquet",
help="Path to the parquet file",
)
PARSER.add_argument( # pyright: ignore[reportUnusedCallResult]
"--formats",
nargs="+",
choices=("vortex", "parquet"),
default=None,
help="Formats to run",
)
PARSER.add_argument( # pyright: ignore[reportUnusedCallResult]
"-q",
"--queries",
nargs="+",
type=int,
default=list(range(len(queries))),
help="Queries to run",
)
PARSER.add_argument("-i", "--iterations", type=int, default=3, help="Number of iterations to run") # pyright: ignore[reportUnusedCallResult]
def main(args: argparse.Namespace):
assert isinstance(args.path, str) # pyright: ignore[reportAny]
assert isinstance(args.queries, list) # pyright: ignore[reportAny]
assert isinstance(args.formats, list | None) # pyright: ignore[reportAny]
if not os.path.exists(args.path):
raise ValueError(f"File {args.path} does not exist")
results: defaultdict[str, list[float]] = defaultdict(list)
def run_queries(format: str, lf: pl.LazyFrame):
for q in args.queries: # pyright: ignore[reportAny]
assert isinstance(q, int)
timings = []
for _ in range(args.iterations): # pyright: ignore[reportAny]
start = timeit.default_timer()
try:
_result: Callable[[pl.LazyFrame], Any] = queries[q][2](lf) # pyright: ignore[reportExplicitAny, reportAny]
except Exception as e:
print(f"Failed Q{q}", e)
timings.append(timeit.default_timer() - start) # pyright: ignore[reportUnknownMemberType]
average = sum(timings) / len(timings) # pyright: ignore[reportUnknownArgumentType]
results[format].append(average)
print(f"{format} Q{q}", average)
if args.formats is None or "vortex" in args.formats: # pyright: ignore[reportUnknownMemberType]
vx_base, _ = os.path.splitext(args.path)
vx_path = f"{vx_base}.vortex"
if not os.path.exists(vx_path):
import pyarrow.parquet as pq
pf = pq.ParquetFile(args.path)
def _iter():
for i in range(pf.num_row_groups):
arr = pf.read_row_group(i).to_struct_array() # pyright: ignore[reportUnknownMemberType]
arr = vx.Array.from_arrow(arr)
yield arr
it = vx.ArrayIterator.from_iter(vx.DType.from_arrow(pf.schema_arrow, non_nullable=True), _iter())
vx.io.write(it, vx_path)
lf = vx.open(vx_path).to_polars()
run_queries("vortex", lf)
if args.formats is None or "parquet" in args.formats: # pyright: ignore[reportUnknownMemberType]
lf = pl.scan_parquet(args.path)
run_queries("parquet", lf)
for format in list(results):
if format == "parquet":
continue
results[f"{format}_vs_pq"] = [a / b for a, b in zip(results[format], results["parquet"])]
print(results)
if __name__ == "__main__":
main(PARSER.parse_args())