Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config, changefeed (ticdc): add sql mode config for changefeed #9941

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cdc/api/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ type ReplicaConfig struct {
Consistent *ConsistentConfig `json:"consistent,omitempty"`
Scheduler *ChangefeedSchedulerConfig `json:"scheduler"`
Integrity *IntegrityConfig `json:"integrity"`
ChangefeedErrorStuckDuration *JSONDuration `json:"changefeed_error_stuck_duration,omitempty" swaggertype:"string"`
ChangefeedErrorStuckDuration *JSONDuration `json:"changefeed_error_stuck_duration,omitempty"`
SQLMode string `json:"sql_mode,omitempty"`
}

// ToInternalReplicaConfig coverts *v2.ReplicaConfig into *config.ReplicaConfig
Expand All @@ -211,6 +212,7 @@ func (c *ReplicaConfig) toInternalReplicaConfigWithOriginConfig(
res.CheckGCSafePoint = c.CheckGCSafePoint
res.EnableSyncPoint = c.EnableSyncPoint
res.IgnoreIneligibleTable = c.IgnoreIneligibleTable
res.SQLMode = c.SQLMode
if c.SyncPointInterval != nil {
res.SyncPointInterval = &c.SyncPointInterval.duration
}
Expand Down Expand Up @@ -497,6 +499,7 @@ func ToAPIReplicaConfig(c *config.ReplicaConfig) *ReplicaConfig {
CheckGCSafePoint: cloned.CheckGCSafePoint,
EnableSyncPoint: cloned.EnableSyncPoint,
BDRMode: cloned.BDRMode,
SQLMode: cloned.SQLMode,
}

if cloned.SyncPointInterval != nil {
Expand Down
1 change: 1 addition & 0 deletions cdc/api/v2/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var defaultAPIConfig = &ReplicaConfig{
},
ChangefeedErrorStuckDuration: &JSONDuration{*config.
GetDefaultReplicaConfig().ChangefeedErrorStuckDuration},
SQLMode: config.GetDefaultReplicaConfig().SQLMode,
}

func TestDefaultReplicaConfig(t *testing.T) {
Expand Down
14 changes: 13 additions & 1 deletion cdc/owner/ddl_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/format"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/cdc/sink/ddlsink"
"github.com/pingcap/tiflow/cdc/sink/ddlsink/factory"
Expand Down Expand Up @@ -426,7 +427,18 @@ func (s *ddlSinkImpl) close(ctx context.Context) (err error) {

// addSpecialComment translate tidb feature to comment
func (s *ddlSinkImpl) addSpecialComment(ddl *model.DDLEvent) (string, error) {
stms, _, err := parser.New().Parse(ddl.Query, ddl.Charset, ddl.Collate)
p := parser.New()
// We need to use the correct SQL mode to parse the DDL query.
// Otherwise, the parser may fail to parse the DDL query.
// For example, it is needed to parse the following DDL query:
// `alter table "t" add column "c" int default 1;`
// by adding `ANSI_QUOTES` to the SQL mode.
mode, err := mysql.GetSQLMode(s.info.Config.SQLMode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please give more comment for the SQLMode here, which type means can handle "" quoted names?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides "" quoted names, since we introduced SQLMode here, what other cases we can handle for other SQLMode types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add more detail in the TiCDC documentation.

if err != nil {
return "", errors.Trace(err)
}
p.SetSQLMode(mode)
stms, _, err := p.Parse(ddl.Query, ddl.Charset, ddl.Collate)
if err != nil {
return "", errors.Trace(err)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/config_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const (
"integrity-check-level": "none",
"corruption-handle-level": "warn"
},
"changefeed-error-stuck-duration": 1800000000000
"changefeed-error-stuck-duration": 1800000000000,
"sql-mode":"ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
}`

testCfgTestServerConfigMarshal = `{
Expand Down Expand Up @@ -317,7 +318,8 @@ const (
"integrity-check-level": "none",
"corruption-handle-level": "warn"
},
"changefeed-error-stuck-duration": 1800000000000
"changefeed-error-stuck-duration": 1800000000000,
"sql-mode":"ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
}`

testCfgTestReplicaConfigMarshal2 = `{
Expand Down Expand Up @@ -462,6 +464,7 @@ const (
"integrity-check-level": "none",
"corruption-handle-level": "warn"
},
"changefeed-error-stuck-duration": 1800000000000
"changefeed-error-stuck-duration": 1800000000000,
"sql-mode":"ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
}`
)
9 changes: 9 additions & 0 deletions pkg/config/replica_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tiflow/pkg/config/outdated"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/integrity"
Expand All @@ -38,6 +39,12 @@ const (
// minSyncPointRetention is the minimum of SyncPointRetention can be set.
minSyncPointRetention = time.Hour * 1
minChangeFeedErrorStuckDuration = time.Minute * 30
// The default SQL Mode of TiDB: "ONLY_FULL_GROUP_BY,
// STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
// NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
// Note: The SQL Mode of TiDB is not the same as ORACLE.
// If you want to use the same SQL Mode as ORACLE, you need to add "ORACLE" to the SQL Mode.
defaultSQLMode = mysql.DefaultSQLMode
)

var defaultReplicaConfig = &ReplicaConfig{
Expand Down Expand Up @@ -88,6 +95,7 @@ var defaultReplicaConfig = &ReplicaConfig{
CorruptionHandleLevel: integrity.CorruptionHandleLevelWarn,
},
ChangefeedErrorStuckDuration: util.AddressOf(time.Minute * 30),
SQLMode: defaultSQLMode,
}

// GetDefaultReplicaConfig returns the default replica config.
Expand Down Expand Up @@ -139,6 +147,7 @@ type replicaConfig struct {
// Integrity is only available when the downstream is MQ.
Integrity *integrity.Config `toml:"integrity" json:"integrity"`
ChangefeedErrorStuckDuration *time.Duration `toml:"changefeed-error-stuck-duration" json:"changefeed-error-stuck-duration,omitempty"`
SQLMode string `toml:"sql-mode" json:"sql-mode"`
}

// Value implements the driver.Valuer interface
Expand Down
22 changes: 17 additions & 5 deletions pkg/filter/expr_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
package filter

import (
"fmt"
"strings"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -77,10 +79,16 @@ func newExprFilterRule(

// verifyAndInitRule will verify and init the rule.
// It should only be called in dmlExprFilter's verify method.
func (r *dmlExprFilterRule) verify(tableInfos []*model.TableInfo) error {
func (r *dmlExprFilterRule) verify(tableInfos []*model.TableInfo, sqlMode string) error {
// verify expression filter rule syntax.
p := parser.New()
_, _, err := p.ParseSQL(completeExpression(r.config.IgnoreInsertValueExpr))
mode, err := mysql.GetSQLMode(sqlMode)
if err != nil {
log.Error("failed to get sql mode", zap.Error(err))
return cerror.ErrInvalidReplicaConfig.FastGenByArgs(fmt.Sprintf("invalid sqlMode %s", sqlMode))
}
p.SetSQLMode(mode)
_, _, err = p.ParseSQL(completeExpression(r.config.IgnoreInsertValueExpr))
if err != nil {
log.Error("failed to parse expression", zap.Error(err))
return cerror.ErrExpressionParseFailed.
Expand Down Expand Up @@ -347,14 +355,18 @@ func getColumnFromError(err error) string {

// dmlExprFilter is a filter that filters DML events by SQL expression.
type dmlExprFilter struct {
rules []*dmlExprFilterRule
rules []*dmlExprFilterRule
sqlMODE string
}

func newExprFilter(
timezone string,
cfg *config.FilterConfig,
sqlMODE string,
) (*dmlExprFilter, error) {
res := &dmlExprFilter{}
res := &dmlExprFilter{
sqlMODE: sqlMODE,
}
sessCtx := utils.NewSessionCtx(map[string]string{
"time_zone": timezone,
})
Expand Down Expand Up @@ -382,7 +394,7 @@ func (f *dmlExprFilter) addRule(
// verify checks if all rules in this filter is valid.
func (f *dmlExprFilter) verify(tableInfos []*model.TableInfo) error {
for _, rule := range f.rules {
err := rule.verify(tableInfos)
err := rule.verify(tableInfos, f.sqlMODE)
if err != nil {
log.Error("failed to verify expression filter rule", zap.Error(err))
return errors.Trace(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/expr_filter_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func BenchmarkSkipDML(b *testing.B) {
sessCtx := utils.NewSessionCtx(map[string]string{
"time_zone": "",
})
f, err := newExprFilter("", cfg)
f, err := newExprFilter("", cfg, config.GetDefaultReplicaConfig().SQLMode)
require.Nil(b, err)

type innerCase struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/filter/expr_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestShouldSkipDMLBasic(t *testing.T) {

for _, tc := range testCases {
tableInfo := helper.execDDL(tc.ddl)
f, err := newExprFilter("", tc.cfg)
f, err := newExprFilter("", tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.Nil(t, err)
for _, c := range tc.cases {
rowDatums, err := utils.AdjustBinaryProtocolForDatum(sessCtx, c.row, tableInfo.Columns)
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestShouldSkipDMLError(t *testing.T) {

for _, tc := range testCases {
tableInfo := helper.execDDL(tc.ddl)
f, err := newExprFilter("", tc.cfg)
f, err := newExprFilter("", tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.Nil(t, err)
for _, c := range tc.cases {
rowDatums, err := utils.AdjustBinaryProtocolForDatum(sessCtx, c.row, tableInfo.Columns)
Expand Down Expand Up @@ -634,7 +634,7 @@ func TestShouldSkipDMLTableUpdated(t *testing.T) {

for _, tc := range testCases {
tableInfo := helper.execDDL(tc.ddl)
f, err := newExprFilter("", tc.cfg)
f, err := newExprFilter("", tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.Nil(t, err)
for _, c := range tc.cases {
if c.updateDDl != "" {
Expand Down Expand Up @@ -754,7 +754,7 @@ func TestVerify(t *testing.T) {
ti := helper.execDDL(ddl)
tableInfos = append(tableInfos, ti)
}
f, err := newExprFilter("", tc.cfg)
f, err := newExprFilter("", tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.Nil(t, err)
err = f.verify(tableInfos)
require.True(t, errors.ErrorEqual(tc.err, err), "case: %+v", tc, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func NewFilter(cfg *config.ReplicaConfig, tz string) (Filter, error) {
f = tfilter.CaseInsensitive(f)
}

dmlExprFilter, err := newExprFilter(tz, cfg.Filter)
dmlExprFilter, err := newExprFilter(tz, cfg.Filter, cfg.SQLMode)
if err != nil {
return nil, err
}
sqlEventFilter, err := newSQLEventFilter(cfg.Filter)
sqlEventFilter, err := newSQLEventFilter(cfg.Filter, cfg.SQLMode)
if err != nil {
return nil, err
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/filter/sql_event_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
package filter

import (
"fmt"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/log"
bf "github.com/pingcap/tidb-tools/pkg/binlog-filter"
"github.com/pingcap/tidb/parser"
timodel "github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
tfilter "github.com/pingcap/tidb/util/table-filter"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/config"
Expand Down Expand Up @@ -107,9 +109,17 @@ type sqlEventFilter struct {
rules []*sqlEventRule
}

func newSQLEventFilter(cfg *config.FilterConfig) (*sqlEventFilter, error) {
func newSQLEventFilter(cfg *config.FilterConfig, sqlMode string) (*sqlEventFilter, error) {
p := parser.New()
mode, err := mysql.GetSQLMode(sqlMode)
if err != nil {
log.Error("failed to get sql mode", zap.Error(err))
return nil, cerror.ErrInvalidReplicaConfig.FastGenByArgs(fmt.Sprintf("invalid sqlMode %s", sqlMode))
}
p.SetSQLMode(mode)

res := &sqlEventFilter{
ddlParser: parser.New(),
ddlParser: p,
}
for _, rule := range cfg.EventFilters {
if err := res.addRule(rule); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/filter/sql_event_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestShouldSkipDDL(t *testing.T) {
}

for _, tc := range testCases {
f, err := newSQLEventFilter(tc.cfg)
f, err := newSQLEventFilter(tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.True(t, errors.ErrorEqual(err, tc.err), "case: %+s", err)
for _, c := range tc.cases {
ddl := &model.DDLEvent{
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestShouldSkipDML(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
f, err := newSQLEventFilter(tc.cfg)
f, err := newSQLEventFilter(tc.cfg, config.GetDefaultReplicaConfig().SQLMode)
require.NoError(t, err)
for _, c := range tc.cases {
event := &model.RowChangedEvent{
Expand Down
60 changes: 60 additions & 0 deletions pkg/filter/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
package filter

import (
"fmt"
"testing"

"github.com/pingcap/log"
bf "github.com/pingcap/tidb-tools/pkg/binlog-filter"
"github.com/pingcap/tidb/parser"
timodel "github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
tifilter "github.com/pingcap/tidb/util/filter"
"github.com/pingcap/tiflow/pkg/config"
cerror "github.com/pingcap/tiflow/pkg/errors"
Expand Down Expand Up @@ -174,3 +176,61 @@ func TestDDLToTypeSpecialDDL(t *testing.T) {
require.Equal(t, c.evenType, et, "case%v", c.ddl)
}
}

func TestToDDLEventWithSQLMode(t *testing.T) {
t.Parallel()
cases := []struct {
name string
query string
jobTp timodel.ActionType
sqlMode string // sql mode
expect bf.EventType
errMsg string
}{
{
name: "create table",
query: "create table t1(id int primary key)",
jobTp: timodel.ActionCreateTable,
sqlMode: config.GetDefaultReplicaConfig().SQLMode,
expect: bf.CreateTable,
},
{
name: "drop table",
query: "drop table t1",
jobTp: timodel.ActionDropTable,
sqlMode: config.GetDefaultReplicaConfig().SQLMode,
expect: bf.DropTable,
},
{ // "" in table name or column name are not supported when sqlMode is set to ANSI_QUOTES
name: "create table 2",
query: `create table "t1" ("id" int primary key)`,
jobTp: timodel.ActionCreateTable,
sqlMode: config.GetDefaultReplicaConfig().SQLMode,
expect: bf.CreateTable,
errMsg: "ErrConvertDDLToEventTypeFailed",
},
{ // "" in table name or column name are supported when sqlMode is set to ANSI_QUOTES
name: "create table 3",
query: `create table "t1" ("id" int primary key)`,
jobTp: timodel.ActionCreateTable,
sqlMode: fmt.Sprint(config.GetDefaultReplicaConfig().SQLMode + ",ANSI_QUOTES"),
expect: bf.CreateTable,
},
}
for _, c := range cases {
innerCase := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
p := parser.New()
mode, err := mysql.GetSQLMode(innerCase.sqlMode)
require.NoError(t, err)
p.SetSQLMode(mode)
tp, err := ddlToEventType(p, innerCase.query, innerCase.jobTp)
if innerCase.errMsg != "" {
require.Contains(t, err.Error(), innerCase.errMsg, innerCase.name)
} else {
require.Equal(t, innerCase.expect, tp)
}
})
}
}
1 change: 1 addition & 0 deletions scripts/check-diff-line-width.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ git --no-pager diff $BASE_HASH -U0 -- cdc pkg cmd \
-- ':(exclude)*_gen.go' \
-- ':(exclude)*_gen_test.go' \
-- ':(exclude)*_mock.go' \
-- ':(exclude)*_test_data.go' \
-- ':(exclude)*.pb.go' |
grep -E '^\+' | grep -vE '^\+\+\+' | grep -vE 'json:' | grep -vE 'toml:' |
sed 's/\t/ /g' |
Expand Down
Loading