@@ -400,7 +400,10 @@ func processCreate(ctx context.Context, astmt spqrparser.Statement, mngr EntityM
400400func processAlter (ctx context.Context , astmt spqrparser.Statement , mngr EntityMgr , cli * clientinteractor.PSQLInteractor ) error {
401401 switch stmt := astmt .(type ) {
402402 case * spqrparser.AlterDistribution :
403- return processAlterDistribution (ctx , stmt .Element , mngr , cli )
403+ if stmt .Distribution == nil {
404+ return fmt .Errorf ("failed to process 'ALTER DISTRIBUTION' statement: distribution ID is nil" )
405+ }
406+ return processAlterDistribution (ctx , stmt .Element , mngr , cli , stmt .Distribution .ID )
404407 default :
405408 return ErrUnknownCoordinatorCommand
406409 }
@@ -416,7 +419,7 @@ func processAlter(ctx context.Context, astmt spqrparser.Statement, mngr EntityMg
416419//
417420// Returns:
418421// - error: An error if the operation fails, otherwise nil.
419- func processAlterDistribution (ctx context.Context , astmt spqrparser.Statement , mngr EntityMgr , cli * clientinteractor.PSQLInteractor ) error {
422+ func processAlterDistribution (ctx context.Context , astmt spqrparser.Statement , mngr EntityMgr , cli * clientinteractor.PSQLInteractor , dsId string ) error {
420423 switch stmt := astmt .(type ) {
421424 case * spqrparser.AttachRelation :
422425
@@ -426,7 +429,7 @@ func processAlterDistribution(ctx context.Context, astmt spqrparser.Statement, m
426429 rels = append (rels , distributions .DistributedRelationFromSQL (drel ))
427430 }
428431
429- if stmt . Distribution . ID == "default" {
432+ if dsId == "default" {
430433 list , err := mngr .ListDistributions (ctx )
431434 if err != nil {
432435 return spqrerror .New (spqrerror .SPQR_OBJECT_NOT_EXIST , "error while selecting list of distributions" )
@@ -437,29 +440,29 @@ func processAlterDistribution(ctx context.Context, astmt spqrparser.Statement, m
437440 if len (list ) > 1 {
438441 return spqrerror .New (spqrerror .SPQR_OBJECT_NOT_EXIST , "distributions count not equal one, use FOR DISTRIBUTION syntax" )
439442 }
440- stmt . Distribution . ID = list [0 ].Id
443+ dsId = list [0 ].Id
441444 }
442445
443- selectedDistribId := stmt . Distribution . ID
446+ selectedDistribId := dsId
444447
445448 if err := mngr .AlterDistributionAttach (ctx , selectedDistribId , rels ); err != nil {
446449 return cli .ReportError (err )
447450 }
448451
449452 return cli .AlterDistributionAttach (ctx , selectedDistribId , rels )
450453 case * spqrparser.DetachRelation :
451- if err := mngr .AlterDistributionDetach (ctx , stmt . Distribution . ID , stmt .RelationName ); err != nil {
454+ if err := mngr .AlterDistributionDetach (ctx , dsId , stmt .RelationName ); err != nil {
452455 return err
453456 }
454- return cli .AlterDistributionDetach (ctx , stmt . Distribution . ID , stmt .RelationName .String ())
457+ return cli .AlterDistributionDetach (ctx , dsId , stmt .RelationName .String ())
455458 case * spqrparser.AlterRelation :
456- if err := mngr .AlterDistributedRelation (ctx , stmt . Distribution . ID , distributions .DistributedRelationFromSQL (stmt .Relation )); err != nil {
459+ if err := mngr .AlterDistributedRelation (ctx , dsId , distributions .DistributedRelationFromSQL (stmt .Relation )); err != nil {
457460 return err
458461 }
459462 qName := rfqn.RelationFQN {RelationName : stmt .Relation .Name , SchemaName : stmt .Relation .SchemaName }
460- return cli .AlterDistributedRelation (ctx , stmt . Distribution . ID , qName .String ())
463+ return cli .AlterDistributedRelation (ctx , dsId , qName .String ())
461464 case * spqrparser.DropDefaultShard :
462- if distribution , err := mngr .GetDistribution (ctx , stmt . Distribution . ID ); err != nil {
465+ if distribution , err := mngr .GetDistribution (ctx , dsId ); err != nil {
463466 return err
464467 } else {
465468 manager := NewDefaultShardManager (distribution , mngr )
@@ -470,7 +473,7 @@ func processAlterDistribution(ctx context.Context, astmt spqrparser.Statement, m
470473 }
471474 }
472475 case * spqrparser.AlterDefaultShard :
473- if distribution , err := mngr .GetDistribution (ctx , stmt . Distribution . ID ); err != nil {
476+ if distribution , err := mngr .GetDistribution (ctx , dsId ); err != nil {
474477 return err
475478 } else {
476479 manager := NewDefaultShardManager (distribution , mngr )
@@ -479,11 +482,43 @@ func processAlterDistribution(ctx context.Context, astmt spqrparser.Statement, m
479482 }
480483 return cli .MakeSimpleResponse (ctx , manager .SuccessCreateResponse (stmt .Shard ))
481484 }
485+ case * spqrparser.AlterRelationV2 :
486+ return processAlterRelation (ctx , stmt .Element , mngr , cli , dsId , stmt .RelationName )
482487 default :
483488 return ErrUnknownCoordinatorCommand
484489 }
485490}
486491
492+ // processAlterDistribution processes the given 'ALTER DISTRIBUTION ALTER RELATION' statement and performs the corresponding operation.
493+ //
494+ // Parameters:
495+ // - ctx (context.Context): The context for the operation.
496+ // - astmt (spqrparser.Statement): The alter relation statement to be processed.
497+ // - mngr (EntityMgr): The entity manager for performing the operation.
498+ // - cli (*clientinteractor.PSQLInteractor): The PSQL client interactor for interacting with the PSQL server.
499+ // - dsId (string): ID of the distribution, to which the relation belongs.
500+ // - relName (string): the name of the relation to alter.
501+ //
502+ // Returns:
503+ // - error: An error if the operation fails, otherwise nil.
504+ func processAlterRelation (ctx context.Context , astmt spqrparser.Statement , mngr EntityMgr , cli * clientinteractor.PSQLInteractor , dsId string , relName string ) error {
505+ switch stmt := astmt .(type ) {
506+ case * spqrparser.AlterRelationSchema :
507+ if err := mngr .AlterDistributedRelationSchema (ctx , dsId , relName , stmt .SchemaName ); err != nil {
508+ return err
509+ }
510+ qName := rfqn.RelationFQN {RelationName : relName , SchemaName : stmt .SchemaName }
511+ return cli .AlterDistributedRelation (ctx , dsId , qName .String ())
512+ case * spqrparser.AlterRelationDistributionKey :
513+ if err := mngr .AlterDistributedRelationDistributionKey (ctx , dsId , relName , distributions .DistributionKeyFromSQL (stmt .DistributionKey )); err != nil {
514+ return err
515+ }
516+ return cli .AlterDistributedRelation (ctx , dsId , relName )
517+ default :
518+ return fmt .Errorf ("unexpected 'ALTER RELATION' request type %T" , stmt )
519+ }
520+ }
521+
487522// TODO : unit tests
488523
489524// ProcMetadataCommand processes various coordinator commands based on the provided statement.
0 commit comments