Skip to content

Commit 6ca2948

Browse files
committed
test: add tests for replacement materialized views
1 parent f2b2e68 commit 6ca2948

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

test/race-condition/mzcompose.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,40 @@ def verify(self) -> str:
665665
raise NotImplementedError
666666

667667

668+
class ReplacementMaterializedView(Object):
669+
def create(self) -> str:
670+
select = (
671+
"* FROM " + self.references.name
672+
if self.references
673+
else "'foo' AS a, 'bar' AS b"
674+
)
675+
return f"> CREATE MATERIALIZED VIEW {self.name} AS SELECT {select}"
676+
677+
def destroy(self) -> str:
678+
return f"> DROP MATERIALIZED VIEW {self.name} CASCADE"
679+
680+
def manipulate(self, kind: int) -> str:
681+
select = (
682+
"* FROM " + self.references.name
683+
if self.references
684+
else "'foo' AS a, 'bar' AS b"
685+
)
686+
manipulations = [
687+
lambda: "",
688+
lambda: dedent(
689+
f"""
690+
> DROP MATERIALIZED VIEW IF EXISTS {self.name}_replacement
691+
> CREATE MATERIALIZED VIEW {self.name}_replacement REPLACING {self.name} AS SELECT {select}
692+
> ALTER MATERIALIZED VIEW {self.name} APPLY REPLACEMENT {self.name}_replacement
693+
"""
694+
),
695+
]
696+
return manipulations[kind % len(manipulations)]()
697+
698+
def verify(self) -> str:
699+
raise NotImplementedError
700+
701+
668702
class DefaultIndex(Object):
669703
can_refer: bool = False
670704

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Copyright Materialize, Inc. and contributors. All rights reserved.
2+
#
3+
# Use of this software is governed by the Business Source License
4+
# included in the LICENSE file at the root of this repository.
5+
#
6+
# As of the Change Date specified in that file, in accordance with
7+
# the Business Source License, use of this software will be governed
8+
# by the Apache License, Version 2.0.
9+
10+
mode cockroach
11+
12+
# Setup
13+
14+
statement ok
15+
CREATE TABLE t (a int, b int)
16+
17+
statement ok
18+
INSERT INTO t VALUES (1, 2), (3, 4), (5, 6)
19+
20+
statement ok
21+
CREATE CLUSTER other REPLICAS (r1 (SIZE 'scale=1,workers=1'), r2 (SIZE 'scale=2,workers=2'))
22+
23+
24+
# Test: basic replacement workflow
25+
26+
statement ok
27+
CREATE MATERIALIZED VIEW mv AS SELECT a, b FROM t
28+
29+
query II
30+
SELECT * FROM mv
31+
----
32+
1 2
33+
3 4
34+
5 6
35+
36+
statement ok
37+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT a + b as a, b FROM t
38+
39+
query II
40+
SELECT * FROM mv
41+
----
42+
1 2
43+
3 4
44+
5 6
45+
46+
query TTT colnames,rowsort
47+
SHOW MATERIALIZED VIEWS
48+
----
49+
name cluster comment
50+
mv quickstart (empty)
51+
rp quickstart (empty)
52+
53+
statement ok
54+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
55+
56+
query TTT colnames,rowsort
57+
SHOW MATERIALIZED VIEWS
58+
----
59+
name cluster comment
60+
mv quickstart (empty)
61+
62+
query II
63+
SELECT * FROM mv
64+
----
65+
3 2
66+
7 4
67+
11 6
68+
69+
70+
# Test: aborted replacement workflow
71+
72+
statement ok
73+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT -a as a, b FROM t
74+
75+
query II
76+
SELECT * FROM mv
77+
----
78+
3 2
79+
7 4
80+
11 6
81+
82+
query TTT colnames,rowsort
83+
SHOW MATERIALIZED VIEWS
84+
----
85+
name cluster comment
86+
mv quickstart (empty)
87+
rp quickstart (empty)
88+
89+
statement ok
90+
DROP MATERIALIZED VIEW rp
91+
92+
query TTT colnames,rowsort
93+
SHOW MATERIALIZED VIEWS
94+
----
95+
name cluster comment
96+
mv quickstart (empty)
97+
98+
query II
99+
SELECT * FROM mv
100+
----
101+
3 2
102+
7 4
103+
11 6
104+
105+
106+
# Test: replacement can be created in another cluster
107+
108+
statement ok
109+
CREATE MATERIALIZED VIEW rp REPLACING mv IN CLUSTER other AS SELECT a, b FROM t;
110+
111+
query TTT colnames,rowsort
112+
SHOW MATERIALIZED VIEWS
113+
----
114+
name cluster comment
115+
mv quickstart (empty)
116+
rp other (empty)
117+
118+
statement ok
119+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
120+
121+
query TTT colnames,rowsort
122+
SHOW MATERIALIZED VIEWS
123+
----
124+
name cluster comment
125+
mv other (empty)
126+
127+
128+
# Test: replacement can have the same query
129+
130+
statement ok
131+
CREATE MATERIALIZED VIEW rp REPLACING mv IN CLUSTER other AS SELECT a, b FROM t;
132+
133+
query TTT colnames,rowsort
134+
SHOW MATERIALIZED VIEWS
135+
----
136+
name cluster comment
137+
mv other (empty)
138+
rp other (empty)
139+
140+
statement ok
141+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
142+
143+
query TTT colnames,rowsort
144+
SHOW MATERIALIZED VIEWS
145+
----
146+
name cluster comment
147+
mv other (empty)
148+
149+
150+
# Test: usage errors
151+
152+
statement error db error: ERROR: incompatible schemas
153+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT a, b, 1 FROM t
154+
155+
statement error db error: ERROR: cannot replace table t with materialized view rp
156+
CREATE MATERIALIZED VIEW rp REPLACING t AS SELECT a, b FROM t
157+
158+
statement error db error: ERROR: t is a table not a materialized view
159+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT t
160+
161+
statement ok
162+
CREATE MATERIALIZED VIEW mv2 AS SELECT * FROM mv
163+
164+
statement ok
165+
CREATE MATERIALIZED VIEW rp REPLACING mv2 AS SELECT a, b FROM t
166+
167+
statement error cannot replace materialized view mv with materialized view rp
168+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
169+
170+
statement ok
171+
DROP MATERIALIZED VIEW mv2 CASCADE
172+
173+
174+
# Test: replacement depends on target materialized view
175+
176+
statement ok
177+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT a, b FROM t
178+
179+
statement error db error: ERROR: cannot drop materialized view "mv": still depended upon by materialized view "rp"
180+
DROP MATERIALIZED VIEW mv;
181+
182+
statement ok
183+
DROP MATERIALIZED VIEW mv CASCADE;
184+
185+
# TODO(alter-mv): Test: replacement cannot be selected from
186+
# TODO(alter-mv): Test: not more than one replacement allowed per MV
187+
# TODO(alter-mv): Test: cannot create an object depending on a replacement
188+
# TODO(alter-mv): Test: audit log

0 commit comments

Comments
 (0)