github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/filter/utils.go (about)

     1  // Copyright 2022 PingCAP, 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 filter
    15  
    16  import (
    17  	"fmt"
    18  
    19  	timodel "github.com/pingcap/tidb/pkg/parser/model"
    20  	tifilter "github.com/pingcap/tidb/pkg/util/filter"
    21  	tfilter "github.com/pingcap/tidb/pkg/util/table-filter"
    22  	bf "github.com/pingcap/tiflow/pkg/binlog-filter"
    23  	"github.com/pingcap/tiflow/pkg/config"
    24  	cerror "github.com/pingcap/tiflow/pkg/errors"
    25  )
    26  
    27  // isSysSchema returns true if the given schema is a system schema
    28  func isSysSchema(db string) bool {
    29  	switch db {
    30  	// TiCDCSystemSchema is used by TiCDC only.
    31  	// Tables in TiCDCSystemSchema should not be replicated by cdc.
    32  	case TiCDCSystemSchema:
    33  		return true
    34  	default:
    35  		return tifilter.IsSystemSchema(db)
    36  	}
    37  }
    38  
    39  // VerifyTableRules checks the table filter rules in the configuration
    40  // and returns an invalid rule error if the verification fails,
    41  // otherwise it will return a table filter.
    42  func VerifyTableRules(cfg *config.FilterConfig) (tfilter.Filter, error) {
    43  	var f tfilter.Filter
    44  	var err error
    45  	if len(cfg.Rules) != 0 {
    46  		rules := cfg.Rules
    47  		if len(rules) == 0 {
    48  			rules = []string{"*.*"}
    49  		}
    50  		f, err = tfilter.Parse(rules)
    51  	}
    52  	if err != nil {
    53  		return nil, cerror.WrapError(cerror.ErrFilterRuleInvalid, err, cfg)
    54  	}
    55  
    56  	return f, nil
    57  }
    58  
    59  // ddlToEventType get event type from ddl query.
    60  func ddlToEventType(jobType timodel.ActionType) bf.EventType {
    61  	evenType, ok := ddlWhiteListMap[jobType]
    62  	if ok {
    63  		return evenType
    64  	}
    65  	return bf.NullEvent
    66  }
    67  
    68  var alterTableSubType = []timodel.ActionType{
    69  	// table related DDLs
    70  	timodel.ActionRenameTable,
    71  	timodel.ActionRenameTables,
    72  	timodel.ActionModifyTableComment,
    73  	timodel.ActionModifyTableCharsetAndCollate,
    74  
    75  	// partition related DDLs
    76  	timodel.ActionAddTablePartition,
    77  	timodel.ActionDropTablePartition,
    78  	timodel.ActionTruncateTablePartition,
    79  	timodel.ActionExchangeTablePartition,
    80  	timodel.ActionReorganizePartition,
    81  	timodel.ActionAlterTablePartitioning,
    82  	timodel.ActionRemovePartitioning,
    83  
    84  	// column related DDLs
    85  	timodel.ActionAddColumn,
    86  	timodel.ActionDropColumn,
    87  	timodel.ActionModifyColumn,
    88  	timodel.ActionSetDefaultValue,
    89  
    90  	// index related DDLs
    91  	timodel.ActionRebaseAutoID,
    92  	timodel.ActionAddPrimaryKey,
    93  	timodel.ActionDropPrimaryKey,
    94  	timodel.ActionAddIndex,
    95  	timodel.ActionDropIndex,
    96  	timodel.ActionRenameIndex,
    97  	timodel.ActionAlterIndexVisibility,
    98  
    99  	// TTL related DDLs
   100  	timodel.ActionAlterTTLInfo,
   101  	timodel.ActionAlterTTLRemove,
   102  
   103  	// difficult to classify DDLs
   104  	timodel.ActionMultiSchemaChange,
   105  
   106  	// deprecated DDLs,see https://github.com/pingcap/tidb/pull/35862.
   107  	// DDL types below are deprecated in TiDB v6.2.0, but we still keep them here
   108  	// In case that some users will use TiCDC to replicate data from TiDB v6.1.x.
   109  	timodel.ActionAddColumns,
   110  	timodel.ActionDropColumns,
   111  }
   112  
   113  // isAlterTable returns true if the given job type is alter table's subtype.
   114  func isAlterTable(jobType timodel.ActionType) bool {
   115  	for _, t := range alterTableSubType {
   116  		if t == jobType {
   117  			return true
   118  		}
   119  	}
   120  	return false
   121  }
   122  
   123  // SupportedEventTypes returns the supported event types.
   124  func SupportedEventTypes() []bf.EventType {
   125  	supportedEventTypes := []bf.EventType{
   126  		bf.AllDML,
   127  		bf.AllDDL,
   128  
   129  		// dml events
   130  		bf.InsertEvent,
   131  		bf.UpdateEvent,
   132  		bf.DeleteEvent,
   133  
   134  		// ddl events
   135  		bf.AlterTable,
   136  		bf.CreateSchema,
   137  		bf.DropSchema,
   138  	}
   139  
   140  	for _, ddlType := range ddlWhiteListMap {
   141  		supportedEventTypes = append(supportedEventTypes, ddlType)
   142  	}
   143  	return supportedEventTypes
   144  }
   145  
   146  func completeExpression(suffix string) string {
   147  	if suffix == "" {
   148  		return suffix
   149  	}
   150  	return fmt.Sprintf("select * from t where %s", suffix)
   151  }