Skip to content

Commit

Permalink
Support routing based purely on target list. (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
reshke authored Feb 5, 2025
1 parent 47a272e commit b714498
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/lib/pq v1.10.9
github.com/libp2p/go-reuseport v0.4.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/sevlyar/go-daemon v0.1.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ github.com/pg-sharding/lyx v0.0.0-20250203080453-fc228647d0af h1:NnvzvVzlzj/FHdD
github.com/pg-sharding/lyx v0.0.0-20250203080453-fc228647d0af/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55 h1:Z6yI8iwmpuinCD8NOTSBHzuZRAxhD6kGfFU+e0MzhHo=
github.com/pg-sharding/lyx v0.0.0-20250205095044-3d4593d06b55/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f h1:z4UC1xwZhRjpx6S2P+crm6nT1b33ptVxK5T0eHHSQWY=
github.com/pg-sharding/lyx v0.0.0-20250205112935-771f430d614f/go.mod h1:2dPBQAhqv/30mhzj2yBXQkXhsGJQ8GhM+oWOfbGua58=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
9 changes: 9 additions & 0 deletions router/qrouter/proxy_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,12 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, rm *rmeta.RoutingMet
if e.Name == "current_schema" || e.Name == "set_config" || e.Name == "pg_is_in_recovery" || e.Name == "version" {
return plan.RandomDispatchPlan{}, ro, nil
}
for _, innerExp := range e.Args {
switch iE := innerExp.(type) {
case *lyx.Select:
_, _ = qr.planQueryV1(ctx, iE, rm)
}
}
/* Expression like SELECT 1, SELECT 'a', SELECT 1.0, SELECT true, SELECT false */
case *lyx.AExprIConst, *lyx.AExprSConst, *lyx.AExprNConst, *lyx.AExprBConst:
return plan.RandomDispatchPlan{}, ro, nil
Expand All @@ -726,8 +732,11 @@ func (qr *ProxyQrouter) routeWithRules(ctx context.Context, rm *rmeta.RoutingMet
if e.ColName == "current_schema" {
return plan.RandomDispatchPlan{}, ro, nil
}
case *lyx.Select:
_, _ = qr.planQueryV1(ctx, e, rm)
}
}

} else if node.LArg != nil && node.RArg != nil {
/* deparse populates the FromClause info,
* so it do recurse into both branches, even if an error is encountered
Expand Down
1 change: 1 addition & 0 deletions test/regress/schedule/router
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test: copy_reference_table
test: copy_inside_singleshard_connection
test: distributed_relation_multishard_modify
test: part_table
test: target_list_routing
test: reference_table
test: ddl
test: engine_v2
2 changes: 2 additions & 0 deletions test/regress/tests/router/expected/ddl.out
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
ALTER TABLE "table_2" RENAME TO "table_1";
ALTER TABLE "tmp" RENAME TO "table_2";
COMMIT;
DROP SCHEMA sh1;
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
DROP TABLE table_1 CASCADE;
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
DROP TABLE table_2;
Expand Down
98 changes: 98 additions & 0 deletions test/regress/tests/router/expected/target_list_routing.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
\c spqr-console

SPQR router admin console
Here you can configure your routing rules
------------------------------------------------
You can find documentation here
https://github.com/pg-sharding/spqr/tree/master/docs

CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
add distribution
------------------------
distribution id -> ds1
(1 row)

CREATE KEY RANGE kridi1 from 0 route to sh1 FOR DISTRIBUTION ds1;
add key range
---------------
bound -> 0
(1 row)

CREATE KEY RANGE kridi2 from 11 route to sh2 FOR DISTRIBUTION ds1;
add key range
---------------
bound -> 11
(1 row)

ALTER DISTRIBUTION ds1 ATTACH RELATION tlt1 DISTRIBUTION KEY i;
attach table
-------------------------
relation name -> tlt1
distribution id -> ds1
(2 rows)

\c regress
CREATE SCHEMA sh2;
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
CREATE TABLE sh2.tlt1(i int, j int);
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
NOTICE: send query to shard(s) : sh1
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
NOTICE: send query to shard(s) : sh1
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
NOTICE: send query to shard(s) : sh1
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
NOTICE: send query to shard(s) : sh1
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 12);
NOTICE: send query to shard(s) : sh2
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 14);
NOTICE: send query to shard(s) : sh2
INSERT INTO sh2.tlt1 (i, j) VALUES(122, 124);
NOTICE: send query to shard(s) : sh2
INSERT INTO sh2.tlt1 (i, j) VALUES(112, 124);
NOTICE: send query to shard(s) : sh2
INSERT INTO sh2.tlt1 (i, j) VALUES(113, 125);
NOTICE: send query to shard(s) : sh2
select (select sum(j) from sh2.tlt1 where i = 112);
NOTICE: send query to shard(s) : sh2
sum
-----
124
(1 row)

select (select sum(j) from sh2.tlt1 where i = 112), (select sum(j) from sh2.tlt1 where sh2.tlt1.i = 113);
NOTICE: send query to shard(s) : sh2
sum | sum
-----+-----
124 | 125
(1 row)

select coalesce((select sum(j) from sh2.tlt1 where i = 1), 0), coalesce((select sum(j) from sh2.tlt1 where i = 2 and j not in (select 12)), 0);
NOTICE: send query to shard(s) : sh1
coalesce | coalesce
----------+----------
24 | 26
(1 row)

DROP SCHEMA sh2 CASCADE;
NOTICE: send query to shard(s) : sh1,sh2,sh3,sh4
\c spqr-console

SPQR router admin console
Here you can configure your routing rules
------------------------------------------------
You can find documentation here
https://github.com/pg-sharding/spqr/tree/master/docs

DROP DISTRIBUTION ALL CASCADE;
drop distribution
------------------------
distribution id -> ds1
(1 row)

DROP KEY RANGE ALL;
drop key range
----------------
(0 rows)

2 changes: 2 additions & 0 deletions test/regress/tests/router/sql/ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ALTER TABLE "table_2" RENAME TO "table_1";
ALTER TABLE "tmp" RENAME TO "table_2";
COMMIT;

DROP SCHEMA sh1;

DROP TABLE table_1 CASCADE;
DROP TABLE table_2;

Expand Down
32 changes: 32 additions & 0 deletions test/regress/tests/router/sql/target_list_routing.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

\c spqr-console
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
CREATE KEY RANGE kridi1 from 0 route to sh1 FOR DISTRIBUTION ds1;
CREATE KEY RANGE kridi2 from 11 route to sh2 FOR DISTRIBUTION ds1;
ALTER DISTRIBUTION ds1 ATTACH RELATION tlt1 DISTRIBUTION KEY i;

\c regress

CREATE SCHEMA sh2;
CREATE TABLE sh2.tlt1(i int, j int);

INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
INSERT INTO sh2.tlt1 (i, j) VALUES(1, 12);
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
INSERT INTO sh2.tlt1 (i, j) VALUES(2, 13);
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 12);
INSERT INTO sh2.tlt1 (i, j) VALUES(12, 14);
INSERT INTO sh2.tlt1 (i, j) VALUES(122, 124);
INSERT INTO sh2.tlt1 (i, j) VALUES(112, 124);
INSERT INTO sh2.tlt1 (i, j) VALUES(113, 125);

select (select sum(j) from sh2.tlt1 where i = 112);
select (select sum(j) from sh2.tlt1 where i = 112), (select sum(j) from sh2.tlt1 where sh2.tlt1.i = 113);
select coalesce((select sum(j) from sh2.tlt1 where i = 1), 0), coalesce((select sum(j) from sh2.tlt1 where i = 2 and j not in (select 12)), 0);

DROP SCHEMA sh2 CASCADE;

\c spqr-console
DROP DISTRIBUTION ALL CASCADE;
DROP KEY RANGE ALL;

0 comments on commit b714498

Please sign in to comment.