File: test_window_cse.test

package info (click to toggle)
duckdb 1.5.1-3
  • 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: 564
file content (77 lines) | stat: -rw-r--r-- 1,768 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
# name: test/sql/window/test_window_cse.test
# description: Test window Common Subexpression Elimination
# group: [window]

statement ok
PRAGMA enable_verification

statement ok
PRAGMA explain_output = PHYSICAL_ONLY;

set seed 0.8675309

statement ok
CREATE TABLE eventlog AS
	SELECT ts,
		CHR((RANDOM() * 3 + 65)::INTEGER) AS activity_name,
		(RANDOM() * 100)::INTEGER AS case_id
	FROM generate_series('2023-01-01'::TIMESTAMP, '2023-02-01'::TIMESTAMP, INTERVAL 1 HOUR) tbl(ts);

statement ok
CREATE VIEW cse AS 
WITH t AS (SELECT
    string_agg(activity_name, ',' order by ts asc, activity_name) as trace,
    1 as cnt
from
    eventlog
group by case_id
)
SELECT
    trace,
    sum(cnt) as cnt_trace,
    sum(cnt_trace) over () as cnt_total,
    sum(cnt) / sum(cnt_trace) over () as rel,
    sum(cnt_trace) over (
         order by cnt_trace desc 
         ROWS between UNBOUNDED PRECEDING and CURRENT ROW) 
      / sum(cnt_trace) over () 
      as rel
from t
group by trace
order by cnt_trace desc

# CSE should produce only one window operator
query II
EXPLAIN FROM cse;
----
physical_plan	<REGEX>:.*WINDOW.*

query II
EXPLAIN FROM cse;
----
physical_plan	<!REGEX>:.*WINDOW.*WINDOW.*

# CSE should produce only two computations of sum(cnt_trace)
query II
EXPLAIN FROM cse;
----
physical_plan	<REGEX>:.*sum\(cnt_trace\).*sum\(cnt_trace\).*

query II
EXPLAIN FROM cse;
----
physical_plan	<!REGEX>:.*sum\(cnt_trace\).*sum\(cnt_trace\).*sum\(cnt_trace\).*


statement ok
CREATE VIEW noncse AS
SELECT
    quantile(x, 0.3) over() as q3,
    quantile(x, 0.7) over() as q7
FROM generate_series(1, 10) as tbl(x);

# Non-CSE should not eliminate the 2nd quantile computation
query II
EXPLAIN FROM noncse;
----
physical_plan	<REGEX>:.*quantile_disc\(x\).*quantile_disc\(x\).*