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

Import ddl logic from Planner V1 into engine V2 #971

Merged
merged 1 commit into from
Jan 27, 2025
Merged
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
82 changes: 80 additions & 2 deletions router/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/pg-sharding/lyx/lyx"
"github.com/pg-sharding/spqr/pkg/models/distributions"
"github.com/pg-sharding/spqr/pkg/models/spqrerror"
"github.com/pg-sharding/spqr/router/plan"
"github.com/pg-sharding/spqr/router/rerrors"
"github.com/pg-sharding/spqr/router/rfqn"
Expand All @@ -14,6 +15,83 @@ import (
func PlanDistributedQuery(ctx context.Context, rm *rmeta.RoutingMetadataContext, stmt lyx.Node) (plan.Plan, error) {

switch v := stmt.(type) {
/* TDB: comments? */

case *lyx.VariableSetStmt:
/* TBD: maybe skip all set stmts? */
/*
* SET x = y etc., do not dispatch any statement to shards, just process this in router
*/
return plan.RandomMatchState{}, nil

case *lyx.VariableShowStmt:
/*
if we want to reroute to execute this stmt, route to random shard
XXX: support intelegent show support, without direct query dispatch
*/
return plan.RandomMatchState{}, nil

// XXX: need alter table which renames sharding column to non-sharding column check
case *lyx.CreateTable:
if val := rm.SPH.AutoDistribution(); val != "" {

switch q := v.TableRv.(type) {
case *lyx.RangeVar:

/* pre-attach relation to its distribution
* sic! this is not transactional not abortable
*/
if err := rm.Mgr.AlterDistributionAttach(ctx, val, []*distributions.DistributedRelation{
{
Name: q.RelationName,
DistributionKey: []distributions.DistributionKeyEntry{
{
Column: rm.SPH.DistributionKey(),
/* support hash function here */
},
},
},
}); err != nil {
return nil, err
}
}
}

/* TODO: support */
// /*
// * Disallow to create table which does not contain any sharding column
// */
// if err := qr.CheckTableIsRoutable(ctx, node); err != nil {
// return nil, false, err
// }
return plan.DDLState{}, nil
case *lyx.Vacuum:
/* Send vacuum to each shard */
return plan.DDLState{}, nil
case *lyx.Analyze:
/* Send vacuum to each shard */
return plan.DDLState{}, nil
case *lyx.Cluster:
/* Send vacuum to each shard */
return plan.DDLState{}, nil
case *lyx.Index:
/*
* Disallow to index on table which does not contain any sharding column
*/
// XXX: do it
return plan.DDLState{}, nil

case *lyx.Alter, *lyx.Drop, *lyx.Truncate:
// support simple ddl commands, route them to every chard
// this is not fully ACID (not atomic at least)
return plan.DDLState{}, nil

case *lyx.CreateRole, *lyx.CreateDatabase:
/* XXX: should we forbid under separate setting? */
return plan.DDLState{}, nil
case *lyx.Copy:
return plan.CopyState{}, nil

case *lyx.ValueClause:
return plan.ScatterPlan{}, nil
case *lyx.Select:
Expand Down Expand Up @@ -79,7 +157,7 @@ func PlanDistributedQuery(ctx context.Context, rm *rmeta.RoutingMetadataContext,
return plan.ScatterPlan{
SubPlan: plan.ModifyTable{},
}, nil
default:
return nil, spqrerror.NewByCode(spqrerror.SPQR_NOT_IMPLEMENTED)
}

return nil, rerrors.ErrComplexQuery
}
Loading