Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 32 additions & 1 deletion logservice/schemastore/persist_storage_ddl_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,7 @@ func buildDDLEventForNewTableDDL(rawEvent *PersistedDDLEvent, tableFilter filter
InfluenceType: commonEvent.InfluenceTypeNormal,
TableIDs: []int64{common.DDLSpanTableID},
}
if rawEvent.ExtraTableID != 0 {
if shouldBlockCreateTableLikeReferTable(rawEvent, tableFilter) {
if len(rawEvent.ReferTablePartitionIDs) > 0 {
ddlEvent.BlockedTables.TableIDs = append(ddlEvent.BlockedTables.TableIDs, rawEvent.ReferTablePartitionIDs...)
} else {
Expand Down Expand Up @@ -1990,6 +1990,37 @@ func buildDDLEventForNewTableDDL(rawEvent *PersistedDDLEvent, tableFilter filter
return ddlEvent, true, err
}

func shouldBlockCreateTableLikeReferTable(rawEvent *PersistedDDLEvent, tableFilter filter.Filter) bool {
if rawEvent.ExtraTableID == 0 {
return false
}
if tableFilter == nil || rawEvent.Query == "" {
return true
}

stmt, err := parser.New().ParseOneStmt(rawEvent.Query, "", "")
if err != nil {
log.Warn("parse create table ddl failed when checking create table like reference filter",
zap.String("query", rawEvent.Query),
zap.Error(err))
return true
}
createStmt, ok := stmt.(*ast.CreateTableStmt)
if !ok || createStmt.ReferTable == nil {
return true
}

referSchema := createStmt.ReferTable.Schema.O
if referSchema == "" {
referSchema = rawEvent.SchemaName
}
referTable := createStmt.ReferTable.Name.O
if referTable == "" {
return true
}
return !tableFilter.ShouldIgnoreTable(referSchema, referTable)
}

func buildDDLEventForDropTable(rawEvent *PersistedDDLEvent, tableFilter filter.Filter, tableID int64) (commonEvent.DDLEvent, bool, error) {
ddlEvent, ok, err := buildDDLEventCommon(rawEvent, tableFilter, WithoutTiDBOnly)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions logservice/schemastore/persist_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,35 @@ func TestBuildDDLEventForNewTableDDL_CreateTableLikeBlockedTables(t *testing.T)
require.ElementsMatch(t, []int64{common.DDLSpanTableID, 111, 112}, ddlEvent.BlockedTables.TableIDs)
}

func TestBuildDDLEventForNewTableDDL_CreateTableLikeBlockedTablesRespectFilter(t *testing.T) {
rawEvent := &PersistedDDLEvent{
Type: byte(model.ActionCreateTable),
SchemaID: 1,
TableID: 2,
SchemaName: "test",
TableName: "t_new",
Query: "CREATE TABLE `t_new` LIKE `t_ref`",
TableInfo: newEligibleTableInfoForTest(2, "t_new"),
ExtraTableID: 101,
}

ddlEvent, ok, err := buildDDLEventForNewTableDDL(rawEvent, buildTableFilterByNameForTest("test", "t_new"), 0)
require.NoError(t, err)
require.True(t, ok)
require.ElementsMatch(t, []int64{common.DDLSpanTableID}, ddlEvent.BlockedTables.TableIDs)

rawEvent.ReferTablePartitionIDs = []int64{111, 112}
ddlEvent, ok, err = buildDDLEventForNewTableDDL(rawEvent, buildTableFilterByNameForTest("test", "t_new"), 0)
require.NoError(t, err)
require.True(t, ok)
require.ElementsMatch(t, []int64{common.DDLSpanTableID}, ddlEvent.BlockedTables.TableIDs)

ddlEvent, ok, err = buildDDLEventForNewTableDDL(rawEvent, buildTableFilterByNameForTest("test", "t_ref"), 0)
require.NoError(t, err)
require.False(t, ok)
require.Empty(t, ddlEvent.BlockedTables)
}

func TestUpdateDDLHistoryForAddDropTable_CreateTableLikeAddsReferTable(t *testing.T) {
args := updateDDLHistoryFuncArgs{
ddlEvent: &PersistedDDLEvent{
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/event_filter/conf/diff_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source-instances = ["tidb0"]

target-instance = "mysql1"

target-check-tables = ["event_filter.t_normal", "event_filter.t_truncate", "event_filter.t_alter", "event_filter.t_name*", "event_filter.t_rename*", "event_filter.t_create"]
target-check-tables = ["event_filter.t_normal", "event_filter.t_truncate", "event_filter.t_alter", "event_filter.t_name*", "event_filter.t_rename*", "event_filter.t_create", "event_filter.t_like_from_filtered"]

[data-sources]
[data-sources.tidb0]
Expand Down
10 changes: 10 additions & 0 deletions tests/integration_tests/event_filter/data/test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ CREATE TABLE t_rename4 (
);

CREATE TABLE t_create (id INT PRIMARY KEY, val INT);

CREATE DATABASE filtered_like_src;
CREATE TABLE filtered_like_src.t_src_like (
id INT PRIMARY KEY,
val INT
);
INSERT INTO filtered_like_src.t_src_like VALUES (1, 100);

CREATE TABLE event_filter.t_like_from_filtered LIKE filtered_like_src.t_src_like;
INSERT INTO event_filter.t_like_from_filtered VALUES (1, 200);
12 changes: 12 additions & 0 deletions tests/integration_tests/event_filter/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ function run() {
pulsar) run_pulsar_consumer --upstream-uri $SINK_URI --config "$CUR/conf/cf.toml" ;;
esac

# CDC forwards CREATE TABLE ... LIKE ... as-is, so the downstream must have the
# referenced table available for the DDL to succeed.
run_sql "CREATE DATABASE IF NOT EXISTS filtered_like_src;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
run_sql "CREATE TABLE IF NOT EXISTS filtered_like_src.t_src_like (id INT PRIMARY KEY, val INT);" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}

run_sql_file $CUR/data/test.sql ${UP_TIDB_HOST} ${UP_TIDB_PORT}

# make suer table t1 is deleted in upstream and exists in downstream
Expand All @@ -48,6 +53,7 @@ function run() {
check_table_exists "event_filter.t_name2" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_table_exists "event_filter.t_name3" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_table_exists "event_filter.t_virtual" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_table_exists "event_filter.t_like_from_filtered" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
sleep 20

# check those rows that are not filtered are synced to downstream
Expand Down Expand Up @@ -92,6 +98,12 @@ function run() {
run_sql "select count(1) from event_filter.t_virtual where quantity > 10;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_contains "count(1): 0" # All discounted items should be filtered

run_sql "select count(1) from event_filter.t_like_from_filtered;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_contains "count(1): 1"

run_sql "select count(1) from event_filter.t_like_from_filtered where id=1 and val=200;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_contains "count(1): 1"

run_sql "TRUNCATE TABLE event_filter.t_truncate;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
run_sql_file $CUR/data/test_truncate.sql ${UP_TIDB_HOST} ${UP_TIDB_PORT}
run_sql "ALTER TABLE event_filter.t_alter MODIFY t_bigint BIGINT;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
Expand Down
Loading