Skip to content

Conversation

zhangxu19830126
Copy link
Contributor

@zhangxu19830126 zhangxu19830126 commented Sep 28, 2025

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #16438

What this PR does / why we need it:

support alter table add/drop/truncate/redefine partitions


PR Type

Enhancement


Description

  • Add comprehensive ALTER TABLE partition support

  • Implement ADD, DROP, TRUNCATE, and REDEFINE operations

  • Add partition service methods and storage layer

  • Include comprehensive test coverage for operations


Diagram Walkthrough

flowchart LR
  A["ALTER TABLE Statement"] --> B["Partition Service"]
  B --> C["Storage Layer"]
  C --> D["Metadata Updates"]
  C --> E["Partition Tables"]
  B --> F["ADD Partitions"]
  B --> G["DROP Partitions"] 
  B --> H["TRUNCATE Partitions"]
  B --> I["REDEFINE Partitions"]
Loading

File Walkthrough

Relevant files
Enhancement
8 files
storage.go
Add partition storage operations implementation                   
+462/-1 
service.go
Implement partition service ALTER methods                               
+343/-11
alter.go
Add ALTER partition compilation support                                   
+125/-33
types.go
Define partition service interface methods                             
+75/-1   
build_ddl.go
Add ALTER partition plan building                                               
+18/-6   
service_range.go
Refactor expression handling for range partitions               
+1/-9     
service_list.go
Refactor expression handling for list partitions                 
+1/-9     
plan.proto
Add ALTER partition protobuf definitions                                 
+12/-0   
Tests
3 files
alter_parittion_test.go
Add comprehensive ALTER partition tests                                   
+342/-0 
storage_test.go
Add mock storage methods for testing                                         
+50/-0   
partition_test.go
Fix partition count assertion in tests                                     
+1/-1     
Bug fix
2 files
alter.go
Fix memory management in ALTER clauses                                     
+5/-5     
build_show_util.go
Fix context usage in partition metadata                                   
+1/-1     
Additional files
1 files
plan.pb.go +1103/-840

Copy link

You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 Security concerns

SQL construction:
Multiple places build SQL via fmt.Sprintf with identifiers and literals (e.g., DROP/TRUNCATE/RENAME, UPDATE statements). While inputs are internal metadata, partition names and table names may originate from user DDL. Consider using parameterized execution or strict identifier quoting/escaping to avoid injection or malformed SQL.

⚡ Recommended focus areas for review

Possible Issue

In DropPartitions, the constructed IN clause string ends with a trailing comma and is sliced without guarding for empty input; additionally, partition names are interpolated directly without escaping, which can break SQL if names contain quotes or special chars.

res, err := txn.Exec(
	fmt.Sprintf("delete from %s where primary_table_id = %d and partition_name in (%s)",
		catalog.MOPartitionTables,
		metadata.TableID,
		whereCause[:len(whereCause)-1],
	),
	executor.StatementOption{},
)
if err != nil {
Logic Risk

When qry.AlterPartition is nil and AlgorithmType is not COPY, the code expects RawSQL to be present or handles only rename; otherwise it panics. This can cause user-visible panics for valid alter cases lacking RawSQL. Consider graceful error handling or guaranteed RawSQL presence.

	}

	panic("missing RawSQL for alter partition tables")
}

metadata, err := ps.GetPartitionMetadata(
	c.proc.Ctx,
	qry.TableDef.TblId,
Validation Missing

AddPartitions currently lacks checks for overlapping ranges/list values or duplicate names beyond TODOs. This can allow inconsistent partition metadata. Ensure validation before persisting changes.

switch metadata.Method {
case partition.PartitionMethod_Hash,
	partition.PartitionMethod_Key,
	partition.PartitionMethod_LinearHash,
	partition.PartitionMethod_LinearKey:
	return moerr.NewNotSupportedNoCtx("add partition is not supported for hash/key partitioned table")
case partition.PartitionMethod_Range:
	// TODO: check overlapping range

case partition.PartitionMethod_List:
	// TODO: check overlapping list values
default:
	panic("BUG: unsupported partition method")
}

var values []partition.Partition
n := len(metadata.Partitions)
for i, p := range partitions {
	values = append(values,
		partition.Partition{
			Name:               p.Name.String(),
			PartitionTableName: GetPartitionTableName(def.Name, p.Name.String()),
			Position:           uint32(i + n),
			ExprStr:            getExpr(p),
			Expr:               newTestValuesInExpr2(p.Name.String()),
		},
	)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants