Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make FOR DISTRIBUTION optional when there is only one distribution #720

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pkg/coord/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ func NewAdapter(conn *grpc.ClientConn) *Adapter {
}
}


// QDB returns the QDB object associated with the Adapter.

//
// Parameters:
// - None.
//
Expand Down Expand Up @@ -383,7 +381,7 @@ func (a *Adapter) DropKeyRange(ctx context.Context, krid string) error {
//
// Parameters:
// - ctx (context.Context): The context for the request.
//
//
// Returns:
// - error: An error if the key range drop fails, otherwise nil.
func (a *Adapter) DropKeyRangeAll(ctx context.Context) error {
Expand Down Expand Up @@ -416,7 +414,7 @@ func (a *Adapter) RegisterRouter(ctx context.Context, r *topology.Router) error
//
// Parameters:
// - ctx (context.Context): The context for the request.
//
//
// Returns:
// - []*topology.Router: A list of router instances.
// - error: An error if listing routers fails, otherwise nil.
Expand Down Expand Up @@ -768,7 +766,6 @@ func (a *Adapter) WriteTaskGroup(ctx context.Context, taskGroup *tasks.TaskGroup
return err
}


// RemoveTaskGroup removes a task group from the system.
//
// Parameters:
Expand Down
16 changes: 16 additions & 0 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func processDrop(ctx context.Context, dstmt spqrparser.Statement, isCascade bool
func processCreate(ctx context.Context, astmt spqrparser.Statement, mngr EntityMgr, cli *clientinteractor.PSQLInteractor) error {
switch stmt := astmt.(type) {
case *spqrparser.DistributionDefinition:
if stmt.ID == "default" {
return spqrerror.New(spqrerror.SPQR_INVALID_REQUEST, "You cannot create a \"default\" distribution, \"default\" is a reserved word")
}
distribution := distributions.NewDistribution(stmt.ID, stmt.ColTypes)

distributions, err := mngr.ListDistributions(ctx)
Expand All @@ -192,6 +195,19 @@ func processCreate(ctx context.Context, astmt spqrparser.Statement, mngr EntityM
case *spqrparser.ShardingRuleDefinition:
return cli.ReportError(spqrerror.ShardingRulesRemoved)
case *spqrparser.KeyRangeDefinition:
if stmt.Distribution == "default" {
list, err := mngr.ListDistributions(ctx)
if err != nil {
return spqrerror.New(spqrerror.SPQR_NO_DISTRIBUTION, "error while selecting list of distributions")
}
if len(list) == 0 {
return spqrerror.New(spqrerror.SPQR_NO_DISTRIBUTION, "you don't have any distributions")
}
if len(list) > 1 {
return spqrerror.New(spqrerror.SPQR_NO_DISTRIBUTION, "distributions count not equal one, use FOR DISTRIBUTION syntax")
}
stmt.Distribution = list[0].Id
}
ds, err := mngr.GetDistribution(ctx, stmt.Distribution)
if err != nil {
spqrlog.Zero.Error().Err(err).Msg("Error when adding key range")
Expand Down
1 change: 1 addition & 0 deletions test/regress/schedule/console
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ test: hash
test: teardown
test: delete_distribution
test: sharding_rules
test: default_distribution
61 changes: 61 additions & 0 deletions test/regress/tests/console/expected/default_distribution.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

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 KEY RANGE krid1 FROM 1 ROUTE TO sh1;
ERROR: you don't have any distributions.
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
add distribution
------------------------
distribution id -> ds1
(1 row)

CREATE KEY RANGE krid1 FROM 1 ROUTE TO sh1;
add key range
---------------
bound -> 1
(1 row)

CREATE KEY RANGE krid2 FROM 11 ROUTE TO sh1;
add key range
---------------
bound -> 11
(1 row)

SHOW key_ranges;
Key range ID | Shard ID | Distribution ID | Lower bound
--------------+----------+-----------------+-------------
krid1 | sh1 | ds1 | 1
krid2 | sh1 | ds1 | 11
(2 rows)

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

CREATE KEY RANGE krid3 FROM 11 ROUTE TO sh2;
ERROR: distributions count not equal one, use FOR DISTRIBUTION syntax.
SHOW key_ranges;
Key range ID | Shard ID | Distribution ID | Lower bound
--------------+----------+-----------------+-------------
krid1 | sh1 | ds1 | 1
krid2 | sh1 | ds1 | 11
(2 rows)

DROP DISTRIBUTION ALL CASCADE;
drop distribution
------------------------
distribution id -> ds1
distribution id -> ds2
(2 rows)

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

15 changes: 15 additions & 0 deletions test/regress/tests/console/sql/default_distribution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE KEY RANGE krid1 FROM 1 ROUTE TO sh1;
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;
CREATE KEY RANGE krid1 FROM 1 ROUTE TO sh1;
CREATE KEY RANGE krid2 FROM 11 ROUTE TO sh1;

SHOW key_ranges;

CREATE DISTRIBUTION ds2 COLUMN TYPES integer;
CREATE KEY RANGE krid3 FROM 11 ROUTE TO sh2;

SHOW key_ranges;


DROP DISTRIBUTION ALL CASCADE;
DROP KEY RANGE ALL;
Loading
Loading