github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/alter_partition_util.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plan
    16  
    17  import (
    18  	"context"
    19  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    20  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    22  )
    23  
    24  // AddTablePartitions will add a new partition to the table.
    25  func AddTablePartitions(ctx CompilerContext, alterTable *plan.AlterTable, spec *tree.AlterPartitionAddPartitionClause) (*plan.AlterTableAddPartition, error) {
    26  	partInfo := alterTable.TableDef.GetPartition()
    27  	if partInfo == nil {
    28  		return nil, moerr.NewErrPartitionMgmtOnNonpartitioned(ctx.GetContext())
    29  	}
    30  
    31  	switch partInfo.Type {
    32  	case plan.PartitionType_KEY, plan.PartitionType_LINEAR_KEY, plan.PartitionType_HASH, plan.PartitionType_LINEAR_HASH:
    33  		// Adding partitions to hash/key is actually a reorganization of partition operations,
    34  		// not just changing metadata! Error not supported
    35  		return nil, moerr.NewNotSupported(ctx.GetContext(), "ADD PARTITION to HASH/KEY partitioned table")
    36  	case plan.PartitionType_RANGE, plan.PartitionType_RANGE_COLUMNS:
    37  		if len(spec.Partitions) == 0 {
    38  			return nil, moerr.NewPartitionsMustBeDefined(ctx.GetContext(), "RANGE")
    39  		}
    40  	case plan.PartitionType_LIST, plan.PartitionType_LIST_COLUMNS:
    41  		if len(spec.Partitions) == 0 {
    42  			return nil, moerr.NewPartitionsMustBeDefined(ctx.GetContext(), "LIST")
    43  		}
    44  	default:
    45  		return nil, moerr.NewNotSupported(ctx.GetContext(), "Unsupported Add Partition")
    46  	}
    47  
    48  	builder := NewQueryBuilder(plan.Query_SELECT, ctx, false)
    49  	bindContext := NewBindContext(builder, nil)
    50  	nodeID := builder.appendNode(&plan.Node{
    51  		NodeType:    plan.Node_TABLE_SCAN,
    52  		Stats:       nil,
    53  		ObjRef:      nil,
    54  		TableDef:    alterTable.TableDef,
    55  		BindingTags: []int32{builder.genNewTag()},
    56  	}, bindContext)
    57  
    58  	err := builder.addBinding(nodeID, tree.AliasClause{}, bindContext)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	partitionBinder := NewPartitionBinder(builder, bindContext)
    63  	alterAddPartition, err := buildAddPartitionClause(ctx.GetContext(), partitionBinder, spec, alterTable.TableDef)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	return alterAddPartition, err
    68  }
    69  
    70  // buildAddPartitionClause build alter table add partition clause info and semantic check.
    71  func buildAddPartitionClause(ctx context.Context, partitionBinder *PartitionBinder, stmt *tree.AlterPartitionAddPartitionClause, tableDef *TableDef) (*plan.AlterTableAddPartition, error) {
    72  	//var builder partitionBuilder
    73  	switch tableDef.Partition.Type {
    74  	case plan.PartitionType_RANGE, plan.PartitionType_RANGE_COLUMNS:
    75  		//builder := &rangePartitionBuilder{}
    76  		builder2 := &rangePartitionBuilder{}
    77  		err := builder2.buildAddPartition(ctx, partitionBinder, stmt, tableDef)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		alterAddPartition := &plan.AlterTableAddPartition{
    82  			Definitions:     builder2.addPartitions,
    83  			PartitionTables: builder2.addPartitionSubTableDefs,
    84  			PartitionDef:    builder2.newPartitionDef,
    85  		}
    86  		return alterAddPartition, nil
    87  	case plan.PartitionType_LIST, plan.PartitionType_LIST_COLUMNS:
    88  		//builder = &listPartitionBuilder{}
    89  		builder2 := &listPartitionBuilder{}
    90  		err := builder2.buildAddPartition(ctx, partitionBinder, stmt, tableDef)
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  		alterAddPartition := &plan.AlterTableAddPartition{
    95  			Definitions:     builder2.addPartitions,
    96  			PartitionTables: builder2.addPartitionSubTableDefs,
    97  			PartitionDef:    builder2.newPartitionDef,
    98  		}
    99  		return alterAddPartition, nil
   100  	}
   101  	return nil, nil
   102  }