Skip to content

Commit b3e1310

Browse files
authored
Do not fail on schema-qualified reference relations (#1571)
1 parent eccf488 commit b3e1310

File tree

7 files changed

+204
-102
lines changed

7 files changed

+204
-102
lines changed

pkg/meta/meta.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ func processCreate(ctx context.Context, astmt spqrparser.Statement, mngr EntityM
308308
case *spqrparser.ReferenceRelationDefinition:
309309

310310
r := &rrelation.ReferenceRelation{
311-
TableName: stmt.TableName,
311+
TableName: stmt.TableName.RelationName,
312+
SchemaName: stmt.TableName.SchemaName,
312313
SchemaVersion: 1,
313314
ShardIds: stmt.ShardIds,
314315
}

test/regress/tests/router/expected/reference_table.out

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ CREATE REFERENCE TABLE test_ref_rel;
2020
shard id -> sh1,sh2,sh3,sh4
2121
(2 rows)
2222

23+
-- test schema-qualified creation
24+
CREATE REFERENCE TABLE sh1.test_ref_rel_rel;
25+
create reference table
26+
------------------------------
27+
table -> test_ref_rel_rel
28+
shard id -> sh1,sh2,sh3,sh4
29+
(2 rows)
30+
2331
-- partial ref relation test
2432
CREATE REFERENCE RELATION test_ref_rel_part ON sh1, sh3;
2533
create reference table
@@ -62,12 +70,18 @@ CREATE KEY RANGE kr1 FROM 0 ROUTE TO sh1 FOR DISTRIBUTION ds1;
6270
\c regress
6371
CREATE TABLE test_ref_rel(i int, j int);
6472
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
73+
CREATE SCHEMA sh1;
74+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
75+
CREATE TABLE sh1.test_ref_rel_rel(i int, j int);
76+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
6577
CREATE TABLE test_distr_ref_rel(id int, val int);
6678
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
6779
CREATE TABLE test_ref_rel_part(i int, j int);
6880
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
6981
COPY test_ref_rel FROM STDIN;
7082
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
83+
COPY sh1.test_ref_rel_rel FROM STDIN;
84+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
7185
COPY test_ref_rel_part FROM STDIN;
7286
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
7387
COPY test_distr_ref_rel(id, val) FROM STDIN WITH DELIMITER '|';
@@ -100,6 +114,8 @@ WITH data(x,z) AS (VALUES(1,3)) INSERT INTO test_ref_rel SELECT * FROM data;
100114
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
101115
INSERT INTO test_ref_rel SELECT 1 /* __spqr__engine_v2: true */;
102116
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
117+
INSERT INTO sh1.test_ref_rel_rel SELECT 1 /* __spqr__engine_v2: true */;
118+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
103119
SELECT * FROM test_ref_rel ORDER BY i, j /*__spqr__execute_on: sh1 */;
104120
NOTICE: send query to shard(s) : sh1
105121
i | j
@@ -152,6 +168,50 @@ NOTICE: send query to shard(s) : sh4
152168
4 | 5
153169
(7 rows)
154170

171+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh1 */;
172+
NOTICE: send query to shard(s) : sh1
173+
i | j
174+
---+---
175+
1 | 2
176+
1 |
177+
2 | 3
178+
3 | 4
179+
4 | 5
180+
(5 rows)
181+
182+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh2 */;
183+
NOTICE: send query to shard(s) : sh2
184+
i | j
185+
---+---
186+
1 | 2
187+
1 |
188+
2 | 3
189+
3 | 4
190+
4 | 5
191+
(5 rows)
192+
193+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh3 */;
194+
NOTICE: send query to shard(s) : sh3
195+
i | j
196+
---+---
197+
1 | 2
198+
1 |
199+
2 | 3
200+
3 | 4
201+
4 | 5
202+
(5 rows)
203+
204+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh4 */;
205+
NOTICE: send query to shard(s) : sh4
206+
i | j
207+
---+---
208+
1 | 2
209+
1 |
210+
2 | 3
211+
3 | 4
212+
4 | 5
213+
(5 rows)
214+
155215
UPDATE test_ref_rel SET i = i + 1 /* __spqr__engine_v2: true */;
156216
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
157217
UPDATE test_ref_rel SET i = - i WHERE i IN (3, 4) /* __spqr__engine_v2: true */;
@@ -360,6 +420,10 @@ NOTICE: send query to shard(s) : sh3
360420

361421
DROP TABLE test_ref_rel;
362422
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
423+
DROP TABLE sh1.test_ref_rel_rel;
424+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
425+
DROP SCHEMA sh1;
426+
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
363427
DROP TABLE test_ref_rel_part;
364428
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
365429
DROP TABLE test_distr_ref_rel;
@@ -377,7 +441,8 @@ SHOW reference_relations;
377441
-------------------+----------------+-------------------
378442
test_ref_rel | 1 | [sh1 sh2 sh3 sh4]
379443
test_ref_rel_part | 1 | [sh1 sh3]
380-
(2 rows)
444+
test_ref_rel_rel | 1 | [sh1 sh2 sh3 sh4]
445+
(3 rows)
381446

382447
DROP DISTRIBUTION ALL CASCADE;
383448
drop distribution

test/regress/tests/router/sql/reference_table.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
55
-- test both ways of ref relation crete syntax
66
CREATE REFERENCE TABLE test_ref_rel;
77

8+
-- test schema-qualified creation
9+
CREATE REFERENCE TABLE sh1.test_ref_rel_rel;
10+
811
-- partial ref relation test
912
CREATE REFERENCE RELATION test_ref_rel_part ON sh1, sh3;
1013

@@ -18,6 +21,9 @@ CREATE KEY RANGE kr1 FROM 0 ROUTE TO sh1 FOR DISTRIBUTION ds1;
1821

1922
CREATE TABLE test_ref_rel(i int, j int);
2023

24+
CREATE SCHEMA sh1;
25+
CREATE TABLE sh1.test_ref_rel_rel(i int, j int);
26+
2127
CREATE TABLE test_distr_ref_rel(id int, val int);
2228

2329
CREATE TABLE test_ref_rel_part(i int, j int);
@@ -29,6 +35,13 @@ COPY test_ref_rel FROM STDIN;
2935
4 5
3036
\.
3137

38+
COPY sh1.test_ref_rel_rel FROM STDIN;
39+
1 2
40+
2 3
41+
3 4
42+
4 5
43+
\.
44+
3245
COPY test_ref_rel_part FROM STDIN;
3346
1 2
3447
2 3
@@ -66,12 +79,19 @@ INSERT INTO test_ref_rel VALUES(1) /* __spqr__engine_v2: true */;
6679
WITH data(x,z) AS (VALUES(1,3)) INSERT INTO test_ref_rel SELECT * FROM data;
6780

6881
INSERT INTO test_ref_rel SELECT 1 /* __spqr__engine_v2: true */;
82+
INSERT INTO sh1.test_ref_rel_rel SELECT 1 /* __spqr__engine_v2: true */;
6983

7084
SELECT * FROM test_ref_rel ORDER BY i, j /*__spqr__execute_on: sh1 */;
7185
SELECT * FROM test_ref_rel ORDER BY i, j /*__spqr__execute_on: sh2 */;
7286
SELECT * FROM test_ref_rel ORDER BY i, j /*__spqr__execute_on: sh3 */;
7387
SELECT * FROM test_ref_rel ORDER BY i, j /*__spqr__execute_on: sh4 */;
7488

89+
90+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh1 */;
91+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh2 */;
92+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh3 */;
93+
SELECT * FROM sh1.test_ref_rel_rel ORDER BY i, j /*__spqr__execute_on: sh4 */;
94+
7595
UPDATE test_ref_rel SET i = i + 1 /* __spqr__engine_v2: true */;
7696

7797
UPDATE test_ref_rel SET i = - i WHERE i IN (3, 4) /* __spqr__engine_v2: true */;
@@ -119,6 +139,8 @@ SELECT FROM test_distr_ref_rel a, test_ref_rel_part b WHERE a.id = 33;
119139
SELECT FROM test_distr_ref_rel a, test_ref_rel_part b WHERE a.id = 233;
120140

121141
DROP TABLE test_ref_rel;
142+
DROP TABLE sh1.test_ref_rel_rel;
143+
DROP SCHEMA sh1;
122144
DROP TABLE test_ref_rel_part;
123145
DROP TABLE test_distr_ref_rel;
124146

yacc/console/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ type ShardingRuleEntry struct {
124124
}
125125

126126
type ReferenceRelationDefinition struct {
127-
TableName string
127+
TableName *rfqn.RelationFQN
128128
AutoIncrementEntries []*AutoIncrementEntry
129129
ShardIds []string
130130
}

0 commit comments

Comments
 (0)