github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/dbs_algorithm_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dbs_test
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    19  	"github.com/whtcorpsinc/milevadb/dbs"
    20  )
    21  
    22  var _ = Suite(&testDBSAlgorithmSuite{})
    23  
    24  var (
    25  	allAlgorithm = []ast.AlgorithmType{ast.AlgorithmTypeCopy,
    26  		ast.AlgorithmTypeInplace, ast.AlgorithmTypeInstant}
    27  )
    28  
    29  type testDBSAlgorithmSuite struct{}
    30  
    31  type testCase struct {
    32  	alterSpec          ast.AlterBlockSpec
    33  	supportedAlgorithm []ast.AlgorithmType
    34  	expectedAlgorithm  []ast.AlgorithmType
    35  }
    36  
    37  func (s *testDBSAlgorithmSuite) TestFindAlterAlgorithm(c *C) {
    38  	supportedInstantAlgorithms := []ast.AlgorithmType{ast.AlgorithmTypeDefault, ast.AlgorithmTypeCopy, ast.AlgorithmTypeInplace, ast.AlgorithmTypeInstant}
    39  	expectedInstantAlgorithms := []ast.AlgorithmType{ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant, ast.AlgorithmTypeInstant}
    40  
    41  	testCases := []testCase{
    42  		{
    43  			ast.AlterBlockSpec{Tp: ast.AlterBlockAddConstraint},
    44  			[]ast.AlgorithmType{ast.AlgorithmTypeDefault, ast.AlgorithmTypeCopy, ast.AlgorithmTypeInplace},
    45  			[]ast.AlgorithmType{ast.AlgorithmTypeInplace, ast.AlgorithmTypeInplace, ast.AlgorithmTypeInplace},
    46  		},
    47  
    48  		{ast.AlterBlockSpec{Tp: ast.AlterBlockAddDeferredCausets}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    49  		{ast.AlterBlockSpec{Tp: ast.AlterBlockDropDeferredCauset}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    50  		{ast.AlterBlockSpec{Tp: ast.AlterBlockDropPrimaryKey}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    51  		{ast.AlterBlockSpec{Tp: ast.AlterBlockDropIndex}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    52  		{ast.AlterBlockSpec{Tp: ast.AlterBlockDropForeignKey}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    53  		{ast.AlterBlockSpec{Tp: ast.AlterBlockRenameBlock}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    54  		{ast.AlterBlockSpec{Tp: ast.AlterBlockRenameIndex}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    55  
    56  		// Alter causet options.
    57  		{ast.AlterBlockSpec{Tp: ast.AlterBlockOption, Options: []*ast.BlockOption{{Tp: ast.BlockOptionShardRowID}}}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    58  		{ast.AlterBlockSpec{Tp: ast.AlterBlockOption, Options: []*ast.BlockOption{{Tp: ast.BlockOptionAutoIncrement}}}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    59  		{ast.AlterBlockSpec{Tp: ast.AlterBlockOption, Options: []*ast.BlockOption{{Tp: ast.BlockOptionComment}}}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    60  		{ast.AlterBlockSpec{Tp: ast.AlterBlockOption, Options: []*ast.BlockOption{{Tp: ast.TableOptionCharset}}}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    61  		{ast.AlterTableSpec{Tp: ast.AlterTableOption, Options: []*ast.TableOption{{Tp: ast.TableOptionDefCauslate}}}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    62  
    63  		// TODO: after we support migrate the data of partitions, change below cases.
    64  		{ast.AlterTableSpec{Tp: ast.AlterTableCoalescePartitions}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    65  		{ast.AlterTableSpec{Tp: ast.AlterTableAddPartitions}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    66  		{ast.AlterTableSpec{Tp: ast.AlterTableDropPartition}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    67  		{ast.AlterTableSpec{Tp: ast.AlterTableTruncatePartition}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    68  		{ast.AlterTableSpec{Tp: ast.AlterTableExchangePartition}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    69  
    70  		// TODO: after we support dagger a causet, change the below case.
    71  		{ast.AlterTableSpec{Tp: ast.AlterTableLock}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    72  		// TODO: after we support changing the defCausumn type, below cases need to change.
    73  		{ast.AlterTableSpec{Tp: ast.AlterTableModifyDeferredCauset}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    74  		{ast.AlterTableSpec{Tp: ast.AlterTableChangeDeferredCauset}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    75  		{ast.AlterTableSpec{Tp: ast.AlterTableAlterDeferredCauset}, supportedInstantAlgorithms, expectedInstantAlgorithms},
    76  	}
    77  
    78  	for _, tc := range testCases {
    79  		runAlterAlgorithmTestCases(c, &tc)
    80  	}
    81  }
    82  
    83  func runAlterAlgorithmTestCases(c *C, tc *testCase) {
    84  	unsupported := make([]ast.AlgorithmType, 0, len(allAlgorithm))
    85  Loop:
    86  	for _, alm := range allAlgorithm {
    87  		for _, almSupport := range tc.supportedAlgorithm {
    88  			if alm == almSupport {
    89  				continue Loop
    90  			}
    91  		}
    92  
    93  		unsupported = append(unsupported, alm)
    94  	}
    95  
    96  	var algorithm ast.AlgorithmType
    97  	var err error
    98  
    99  	// Test supported.
   100  	for i, alm := range tc.supportedAlgorithm {
   101  		algorithm, err = dbs.ResolveAlterAlgorithm(&tc.alterSpec, alm)
   102  		if err != nil {
   103  			c.Assert(dbs.ErrAlterOperationNotSupported.Equal(err), IsTrue)
   104  		}
   105  		c.Assert(algorithm, Equals, tc.expectedAlgorithm[i])
   106  	}
   107  
   108  	// Test unsupported.
   109  	for _, alm := range unsupported {
   110  		algorithm, err = dbs.ResolveAlterAlgorithm(&tc.alterSpec, alm)
   111  		c.Assert(algorithm, Equals, ast.AlgorithmTypeDefault)
   112  		c.Assert(err, NotNil, Commentf("Tp:%v, alm:%s", tc.alterSpec.Tp, alm))
   113  		c.Assert(dbs.ErrAlterOperationNotSupported.Equal(err), IsTrue)
   114  	}
   115  }