Skip to content

Commit 8c97419

Browse files
committed
test: add tests for replacement materialized views
1 parent 51266fe commit 8c97419

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-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: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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: schemas must match
107+
108+
statement error db error: ERROR: incompatible schemas
109+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT a, b, 1 FROM t
110+
111+
# Test: replacement can be created in another cluster
112+
113+
statement ok
114+
CREATE MATERIALIZED VIEW rp REPLACING mv IN CLUSTER other AS SELECT a, b FROM t;
115+
116+
query TTT colnames,rowsort
117+
SHOW MATERIALIZED VIEWS
118+
----
119+
name cluster comment
120+
mv quickstart (empty)
121+
rp other (empty)
122+
123+
statement ok
124+
ALTER MATERIALIZED VIEW mv APPLY REPLACEMENT rp
125+
126+
query TTT colnames,rowsort
127+
SHOW MATERIALIZED VIEWS
128+
----
129+
name cluster comment
130+
mv other (empty)
131+
132+
# Test: replacement depends on target materialized view
133+
134+
statement ok
135+
CREATE MATERIALIZED VIEW rp REPLACING mv AS SELECT a, b FROM t
136+
137+
statement error db error: ERROR: cannot drop materialized view "mv": still depended upon by materialized view "rp"
138+
DROP MATERIALIZED VIEW mv;
139+
140+
statement ok
141+
DROP MATERIALIZED VIEW mv CASCADE;
142+
143+
# TODO: Test: replacement cannot be selected from
144+
# TODO: Test: not more than one replacement allowed per MV
145+
# TODO: Test: cannot create an object depending on a replacement
146+
# TODO: Test: replacing with the same SQL
147+
# TODO: Test: audit log
148+
# TODO: Test: error on wrong replacement type in ALTER MV
149+
# TODO: Test: error on wrong target type in CREATE MV

0 commit comments

Comments
 (0)