Skip to content

ddl: support masking policy DDL#66035

Open
tiancaiamao wants to merge 43 commits into
pingcap:masterfrom
tiancaiamao:pr/phase5-ddl
Open

ddl: support masking policy DDL#66035
tiancaiamao wants to merge 43 commits into
pingcap:masterfrom
tiancaiamao:pr/phase5-ddl

Conversation

@tiancaiamao
Copy link
Copy Markdown
Contributor

@tiancaiamao tiancaiamao commented Feb 4, 2026

What problem does this PR solve?

Issue Number: ref #65744

Problem Summary:

  • implement masking policy DDL execution and validation

What changed and how does it work?

  • add DDL executor and worker handlers for create/alter/drop masking policies
  • add validation and persist policy metadata to meta and mysql.tidb_masking_policy

Tests:

  • go test -tags intest -run TestMaskingPolicy (pkg/ddl)

Stacked PRs

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Summary by CodeRabbit

  • New Features

    • Full masking policy DDL now supported end-to-end: CREATE, ADD/ENABLE/DISABLE, REPLACE, and DROP on table columns; executor and DDL jobs handle lifecycle.
    • Info schema exposes masking-policy lookups and lazy, thread-safe caching.
  • Database

    • System table for persisted masking policy metadata updated to new column set and indexes.
  • Tests

    • Added and extended tests covering lifecycle, IF NOT EXISTS, and expression persistence.

Review Change Stack

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Feb 4, 2026
@tiprow
Copy link
Copy Markdown

tiprow Bot commented Feb 4, 2026

Hi @tiancaiamao. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@tiancaiamao tiancaiamao mentioned this pull request Feb 4, 2026
13 tasks
tiancaiamao and others added 24 commits March 11, 2026 19:49
- Add nil guards in applyCreateMaskingPolicy and applyAlterMaskingPolicy
  to prevent panic when metadata is missing/stale
- Add read lock in MaskingPolicyByID to prevent data race with
  setMaskingPolicy/deleteMaskingPolicy
- Fix TestVersionedBootstrapSchemas to only check table IDs for duplicates,
  not database IDs (same database can be referenced in multiple versions)

Co-Authored-By: Claude <noreply@anthropic.com>
Add version255 to upgrade the bootstrap version for the masking policy
system table (mysql.tidb_masking_policy).

Co-Authored-By: Claude <noreply@anthropic.com>
The In version 254, the tidb_masking_policy table should not exist yet.
    // Skip the test if it table doesn't exist and verify upgrade creates it instead.

Co-Authored-By: Claude <noreply@anthropic.com>
Use = instead of := for exists variable which was already declared above.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Implements end-to-end masking-policy DDL: model and sys-table schema changes, DDL executor dispatch and job submission, DDL worker handlers for create/alter/drop with sys-table persistence and validation, infoschema lazy caching and loader integration, builder/diff/init updates, and tests/BUILD changes.

Changes

Masking Policy DDL Support

Layer / File(s) Summary
Data Model and System Table
pkg/meta/model/masking_policy.go, pkg/meta/metadef/system_tables_def.go, pkg/session/test/bootstraptest/*
Status type converted to byte-backed enum with aliases; masking-type constants renamed with aliases; MaskingPolicyInfo extended with DBName/TableName/ColumnName; mysql.tidb_masking_policy CREATE TABLE SQL updated and bootstrap tests adjusted.
Executor Interface and DDL Dispatch
pkg/ddl/executor.go, pkg/executor/ddl.go
Executor adds CreateMaskingPolicy; DDLExec.Next recognizes *ast.CreateMaskingPolicyStmt and dispatches to executeCreateMaskingPolicy.
Executor Job Submission Methods
pkg/ddl/executor.go
Implements CreateMaskingPolicy, AddMaskingPolicy, AlterTableMaskingPolicyState, DropMaskingPolicy, and createMaskingPolicyWithInfo with schema resolution, uniqueness checks, and OnExist semantics.
Job Worker Routing and Handlers
pkg/ddl/job_worker.go, pkg/ddl/masking_policy.go
runOneJobStep routes ActionCreateMaskingPolicy/ActionAlterMaskingPolicy/ActionDropMaskingPolicy to worker handlers; handlers validate targets via infoschema, perform sys-table insert/update/delete, update schema version, and finish jobs.
Worker Helpers: Validation & Persistence
pkg/ddl/masking_policy.go
Target validation rules (reject views/sequences/temp/system schemas/generated columns/unsupported types), AST→model construction (expression restore/validation), masking-type/status/restrict-op parsing and serialization, and parameterized sys-table CRUD helpers and row decoding.
InfoSchema Interface Extension
pkg/infoschema/context/infoschema.go
Misc interface extended with MaskingPolicyByName, MaskingPolicyByTableColumn, and AllMaskingPolicies.
InfoSchema Caching & Lazy Loader
pkg/infoschema/infoschema.go
Adds masking-policy cache keyed by (tableID,columnID) with loaded flag, single-flight load coordination, accessors (ByID/ByName/ByTableColumn/All/Clone), LoadMaskingPolicies implementation, normalization helpers, and cache reset.
Builder Diff & Init Integration
pkg/infoschema/builder.go, pkg/infoschema/builder_misc.go
Builder.ApplyDiff recognizes masking-policy diffs and calls applyMaskingPolicyChange to reset cache; InitWithOldInfoSchema clones masking-policy mapping and loaded flags; InitWithDBInfos signature extended to accept maskingPolicies and passes to initMisc (param currently unused).
Loader Integration & InfoSchemaV2
pkg/infoschema/issyncer/loader.go, pkg/infoschema/infoschema_v2.go
Loader.LoadWithTS fetches masking policies (fetchMaskingPolicies added) and forwards them to InitWithDBInfos; NewInfoSchemaV2 forwards factory to newInfoSchema.
Schema Tracker
pkg/ddl/schematracker/checker.go, pkg/ddl/schematracker/dm_tracker.go
Checker.CreateMaskingPolicy delegates to real executor and tracker then validates affected table; SchemaTracker.CreateMaskingPolicy implemented as no-op for DM.
Build & Tests
pkg/ddl/BUILD.bazel, pkg/ddl/masking_policy_test.go, pkg/infoschema/*, pkg/executor/*
pkg/ddl BUILD updated to include masking_policy.go and masking_policy_test.go; added tests for masking-policy lifecycle, CASE expression handling, and IF NOT EXISTS; many tests updated to pass maskingPolicies arg; pkg/infoschema/BUILD.bazel deps and test shard_count updated.

Sequence Diagram (high-level flow)

sequenceDiagram
  participant Client
  participant Executor
  participant DDLWorker
  participant InfoSchema
  participant MySQLMeta
  Client->>Executor: CREATE MASKING POLICY ...
  Executor->>DDLWorker: submit CreateMaskingPolicy job (policy args)
  DDLWorker->>InfoSchema: validate target table/column & load masking policies
  DDLWorker->>MySQLMeta: INSERT/UPDATE `mysql.tidb_masking_policy`
  MySQLMeta-->>DDLWorker: write result (policy_id)
  DDLWorker->>InfoSchema: reset/load masking-policy cache
  DDLWorker-->>Executor: job finished (schema version)
Loading

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly Related PRs

  • pingcap/tidb#66033: Modifies mysql.tidb_masking_policy system table schema and bootstrap wiring affecting the same schema artifact.
  • pingcap/tidb#66032: Related masking-policy metadata and job-arg types used by this PR.

Suggested labels

sig/planner, ok-to-test, approved, lgtm

Suggested reviewers

  • wjhuang2016
  • fzzf678
  • yudongusa
  • Leavrth

Poem

🐰 I hop through DDL fields bright and merry,
Policies tucked where columns tarry,
From executor's call to worker's hand,
Cached states spread across the land,
Tests and rows snug in schema's ferry.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.70% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'ddl: support masking policy DDL' is clear, concise, and directly summarizes the main change—adding DDL support for masking policies—which aligns with the core changes across executor, worker, and policy implementation files.
Description check ✅ Passed The PR description includes issue reference (#65744), problem summary, what changed, test coverage information, and dependency details. However, it lacks the 'Problem Summary' section header clarity and the description appears partially cut off or malformed in formatting.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

The struct and its initializer had  declared twice due to a bad
merge. This caused a compile error:
  vet: conflict_resolution_test.go:120:2: tbl redeclared
@tiprow
Copy link
Copy Markdown

tiprow Bot commented May 13, 2026

@tiancaiamao: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/test pull-br-integration-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/ok-to-test

@ti-chi-bot ti-chi-bot Bot added the ok-to-test Indicates a PR is ready to be tested. label May 13, 2026
@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

# Conflicts:
#	pkg/infoschema/infoschema.go
#	pkg/infoschema/infoschema_nokit_test.go
@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

2 similar comments
@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

When a table is renamed, update db_name and table_name in
mysql.tidb_masking_policy. When a column is renamed, update
column_name, column_id and rewrite the expression.
@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

…il DDL on policy cleanup error

Review fixes:
1. HIGH: Add checkMaskingPolicyOnModifyColumn in executor layer (ModifyColumn/ChangeColumn)
   to reject type changes to unsupported types (e.g. JSON) for columns with masking policy.
   Also add safety-net check in syncMaskingPolicyForModifiedColumn (DDL worker layer).
   Test: TestMaskingPolicyModifyColumnRejectUnsupportedType.

2. MEDIUM: dropMaskingPoliciesOnTable/dropMaskingPoliciesOnColumn errors now return
   errors.Wrapf instead of logging Warn, so the DDL job will retry/rollback rather
   than silently leaving stale policy rows in mysql.tidb_masking_policy.
@tiancaiamao tiancaiamao requested review from bb7133 and wjhuang2016 May 22, 2026 05:41
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 25, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign d3hunter, yudongusa for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

2 similar comments
@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

@wuhuizuo
Copy link
Copy Markdown
Contributor

/test pull-integration-realcluster-test-next-gen

@tiprow
Copy link
Copy Markdown

tiprow Bot commented May 25, 2026

@wuhuizuo: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

/test fast_test_tiprow
/test tidb_parser_test

Use /test all to run all jobs.

Details

In response to this:

/test pull-integration-realcluster-test-next-gen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

PTAL @bb7133 @wjhuang2016

@tiancaiamao
Copy link
Copy Markdown
Contributor Author

/retest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants