Skip to content

Commit d235fe4

Browse files
committed
add more tests
1 parent b0b039d commit d235fe4

File tree

7 files changed

+684
-4
lines changed

7 files changed

+684
-4
lines changed

tests/compat_fuse/compat-logictest/rbac/fuse_compat_write

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,4 @@ GRANT create sequence on *.* to role 'role1';
5959
statement ok
6060
GRANT create procedure on *.* to role 'role1';
6161

62-
statement ok
63-
unset global enable_experimental_procedure;
62+

tests/sqllogictests/suites/base/15_procedure/15_0002_procedure.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,3 @@ call procedure p2('x');
192192
statement ok
193193
drop procedure p2(string);
194194

195-
statement ok
196-
unset global enable_experimental_procedure;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
statement ok
2+
set global enable_experimental_procedure=1;
3+
4+
statement ok
5+
create or replace table t_rollback_1 (str varchar);
6+
7+
statement ok
8+
drop procedure if exists test_rollback_on_error();
9+
10+
# Test procedure with automatic rollback on division by zero error
11+
statement ok
12+
CREATE PROCEDURE test_rollback_on_error() RETURNS int not null LANGUAGE SQL AS $$
13+
BEGIN
14+
insert into t_rollback_1 (str) values ('a'), ('b');
15+
select 1/0;
16+
RETURN 1;
17+
END;
18+
$$;
19+
20+
# Test error causes rollback - data should not be inserted
21+
statement error
22+
begin;
23+
call procedure test_rollback_on_error();
24+
commit;
25+
26+
query I
27+
select * from t_rollback_1;
28+
----
29+
30+
31+
statement ok
32+
create or replace table t_rollback_1 (str varchar);
33+
34+
statement ok
35+
drop procedure if exists test_rollback_on_syntax_error();
36+
37+
# Test procedure with automatic rollback on syntax error
38+
statement ok
39+
CREATE PROCEDURE test_rollback_on_syntax_error() RETURNS int not null LANGUAGE SQL AS $$
40+
BEGIN
41+
insert into t_rollback_1 (str) values ('a'), ('b');
42+
1;
43+
RETURN 1;
44+
END;
45+
$$;
46+
47+
# Test syntax error causes rollback - data should not be inserted
48+
statement error
49+
begin;
50+
call procedure test_rollback_on_syntax_error();
51+
commit;
52+
53+
query I
54+
select * from t_rollback_1;
55+
----
56+
57+
58+
statement ok
59+
create or replace table t_rollback_1 (str varchar);
60+
61+
statement ok
62+
drop procedure if exists test_rollback_on_column_error();
63+
64+
# Test procedure with automatic rollback on non-existent column error
65+
statement ok
66+
CREATE PROCEDURE test_rollback_on_column_error() RETURNS int not null LANGUAGE SQL AS $$
67+
BEGIN
68+
insert into t_rollback_1 (str) values ('a'), ('b');
69+
select nonexistent from t_rollback_1;
70+
RETURN 1;
71+
END;
72+
$$;
73+
74+
# Test column error causes rollback - data should not be inserted
75+
statement error
76+
begin;
77+
call procedure test_rollback_on_column_error();
78+
commit;
79+
80+
query I
81+
select * from t_rollback_1;
82+
----
83+
84+
85+
statement ok
86+
drop procedure if exists test_rollback_on_error();
87+
88+
statement ok
89+
drop procedure if exists test_rollback_on_syntax_error();
90+
91+
statement ok
92+
drop procedure if exists test_rollback_on_column_error();
93+
94+
statement ok
95+
drop table t_rollback_1;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
statement ok
2+
set global enable_experimental_procedure=1;
3+
4+
statement ok
5+
create or replace database test_procedure_snapshots;
6+
7+
statement ok
8+
use test_procedure_snapshots;
9+
10+
statement ok
11+
create or replace table t(c int);
12+
13+
###################################
14+
# no snapshots left if tx aborted #
15+
###################################
16+
17+
statement ok
18+
drop procedure if exists test_rollback_no_snapshots();
19+
20+
# Test procedure that will be rolled back - should leave no snapshots
21+
statement ok
22+
CREATE PROCEDURE test_rollback_no_snapshots() RETURNS int not null LANGUAGE SQL AS $$
23+
BEGIN
24+
insert into t values(1);
25+
insert into t values(1);
26+
select 1/0;
27+
RETURN 1;
28+
END;
29+
$$;
30+
31+
statement error
32+
begin;
33+
call procedure test_rollback_no_snapshots();
34+
rollback;
35+
36+
query I
37+
select count() from fuse_snapshot('test_procedure_snapshots', 't');
38+
----
39+
0
40+
41+
#####################################################
42+
# one snapshot left if table mutated multiple times #
43+
#####################################################
44+
45+
statement ok
46+
drop procedure if exists test_commit_one_snapshot();
47+
48+
# Test procedure that commits successfully - should create one snapshot
49+
statement ok
50+
CREATE PROCEDURE test_commit_one_snapshot() RETURNS int not null LANGUAGE SQL AS $$
51+
BEGIN
52+
insert into t values(1);
53+
insert into t values(1);
54+
insert into t values(1);
55+
RETURN 1;
56+
END;
57+
$$;
58+
59+
statement ok
60+
begin;
61+
62+
statement ok
63+
call procedure test_commit_one_snapshot();
64+
65+
statement ok
66+
commit;
67+
68+
query I
69+
select count() from fuse_snapshot('test_procedure_snapshots', 't');
70+
----
71+
1
72+
73+
statement ok
74+
create or replace table transaction(c int);
75+
76+
statement ok
77+
drop procedure if exists test_rollback_no_snapshots();
78+
79+
statement ok
80+
drop procedure if exists test_commit_one_snapshot();
81+
82+
statement ok
83+
drop database test_procedure_snapshots;
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
statement ok
2+
set global enable_experimental_procedure=1;
3+
4+
statement ok
5+
create or replace database test_procedure_merge_into;
6+
7+
statement ok
8+
use test_procedure_merge_into;
9+
10+
statement ok
11+
CREATE TABLE employees (
12+
employee_id INT,
13+
employee_name VARCHAR(255),
14+
department VARCHAR(255)
15+
);
16+
17+
statement ok
18+
CREATE TABLE salaries (
19+
employee_id INT,
20+
salary DECIMAL(10, 2)
21+
);
22+
23+
statement ok
24+
drop procedure if exists test_merge_operations();
25+
26+
# Test procedure performing complex merge operations within transaction
27+
statement ok
28+
CREATE PROCEDURE test_merge_operations() RETURNS string not null LANGUAGE SQL AS $$
29+
BEGIN
30+
INSERT INTO employees VALUES
31+
(1, 'Alice', 'HR'),
32+
(2, 'Bob', 'IT'),
33+
(3, 'Charlie', 'Finance'),
34+
(4, 'David', 'HR');
35+
36+
INSERT INTO salaries VALUES
37+
(1, 50000.00),
38+
(2, 60000.00);
39+
40+
MERGE INTO salaries
41+
USING (SELECT * FROM employees) AS employees
42+
ON salaries.employee_id = employees.employee_id
43+
WHEN MATCHED AND employees.department = 'HR' THEN
44+
UPDATE SET
45+
salaries.salary = salaries.salary + 1000.00
46+
WHEN MATCHED THEN
47+
UPDATE SET
48+
salaries.salary = salaries.salary + 500.00
49+
WHEN NOT MATCHED THEN
50+
INSERT (employee_id, salary)
51+
VALUES (employees.employee_id, 55000.00);
52+
53+
RETURN 'merge completed';
54+
END;
55+
$$;
56+
57+
statement ok
58+
BEGIN;
59+
60+
statement ok
61+
call procedure test_merge_operations();
62+
63+
query I
64+
select count(*) from fuse_snapshot('test_procedure_merge_into','employees');
65+
----
66+
1
67+
68+
query I
69+
select count(*) from fuse_snapshot('test_procedure_merge_into','salaries');
70+
----
71+
1
72+
73+
query IF
74+
SELECT employee_id, salary FROM salaries order by employee_id;
75+
----
76+
1 51000.00
77+
2 60500.00
78+
3 55000.00
79+
4 55000.00
80+
81+
statement ok
82+
COMMIT;
83+
84+
query I
85+
select count(*) from fuse_snapshot('test_procedure_merge_into','salaries');
86+
----
87+
1
88+
89+
query I
90+
select count(*) from fuse_snapshot('test_procedure_merge_into','employees');
91+
----
92+
1
93+
94+
query IF
95+
SELECT employee_id, salary FROM salaries order by employee_id;
96+
----
97+
1 51000.00
98+
2 60500.00
99+
3 55000.00
100+
4 55000.00
101+
102+
statement ok
103+
drop database test_procedure_merge_into;
104+
105+
# Test second scenario with BEGIN TRANSACTION
106+
statement ok
107+
create or replace database test_procedure_merge_into;
108+
109+
statement ok
110+
use test_procedure_merge_into;
111+
112+
statement ok
113+
CREATE TABLE employees (
114+
employee_id INT,
115+
employee_name VARCHAR(255),
116+
department VARCHAR(255)
117+
);
118+
119+
statement ok
120+
CREATE TABLE salaries (
121+
employee_id INT,
122+
salary DECIMAL(10, 2)
123+
);
124+
125+
statement ok
126+
drop procedure if exists test_merge_operations_v2();
127+
128+
# Test procedure performing merge operations with BEGIN TRANSACTION syntax
129+
statement ok
130+
CREATE PROCEDURE test_merge_operations_v2() RETURNS string not null LANGUAGE SQL AS $$
131+
BEGIN
132+
INSERT INTO employees VALUES
133+
(1, 'Alice', 'HR'),
134+
(2, 'Bob', 'IT'),
135+
(3, 'Charlie', 'Finance'),
136+
(4, 'David', 'HR');
137+
138+
INSERT INTO salaries VALUES
139+
(1, 50000.00),
140+
(2, 60000.00);
141+
142+
MERGE INTO salaries
143+
USING (SELECT * FROM employees) AS employees
144+
ON salaries.employee_id = employees.employee_id
145+
WHEN MATCHED AND employees.department = 'HR' THEN
146+
UPDATE SET
147+
salaries.salary = salaries.salary + 1000.00
148+
WHEN MATCHED THEN
149+
UPDATE SET
150+
salaries.salary = salaries.salary + 500.00
151+
WHEN NOT MATCHED THEN
152+
INSERT (employee_id, salary)
153+
VALUES (employees.employee_id, 55000.00);
154+
155+
RETURN 'merge completed v2';
156+
END;
157+
$$;
158+
159+
statement ok
160+
BEGIN TRANSACTION;
161+
162+
statement ok
163+
call procedure test_merge_operations_v2();
164+
165+
query IF
166+
SELECT employee_id, salary FROM salaries order by employee_id;
167+
----
168+
1 51000.00
169+
2 60500.00
170+
3 55000.00
171+
4 55000.00
172+
173+
statement ok
174+
COMMIT;
175+
176+
query IF
177+
SELECT employee_id, salary FROM salaries order by employee_id;
178+
----
179+
1 51000.00
180+
2 60500.00
181+
3 55000.00
182+
4 55000.00
183+
184+
statement ok
185+
drop procedure if exists test_merge_operations();
186+
187+
statement ok
188+
drop procedure if exists test_merge_operations_v2();
189+
190+
statement ok
191+
drop database test_procedure_merge_into;

0 commit comments

Comments
 (0)