Skip to content

logservice: qualify CREATE VIEW column references#5044

Open
3AceShowHand wants to merge 4 commits into
pingcap:masterfrom
3AceShowHand:codex/fix-create-view-table-qualified-columns
Open

logservice: qualify CREATE VIEW column references#5044
3AceShowHand wants to merge 4 commits into
pingcap:masterfrom
3AceShowHand:codex/fix-create-view-table-qualified-columns

Conversation

@3AceShowHand
Copy link
Copy Markdown
Collaborator

@3AceShowHand 3AceShowHand commented May 13, 2026

What problem does this PR solve?

Issue Number: close #5043

CREATE VIEW normalization already uses TiDB's stored View.SelectStmt to recover resolved source table schemas. However, for statements such as:

USE source_db;
CREATE VIEW other_db.v AS SELECT orders.id FROM orders;

TiDB stores the SELECT as SELECT orders.id FROM source_db.orders: the FROM table is schema-qualified, but the table-qualified column remains unqualified. If table routing later renames source_db.orders, the column qualifier can keep pointing at the old table name.

What is changed and how it works?

This PR normalizes CREATE VIEW stored SELECT statements further by qualifying unaliased table-qualified column references from the SELECT's own FROM scope.

For example, it rewrites:

SELECT `orders`.`id` FROM `source_db`.`orders`

into:

SELECT `source_db`.`orders`.`id` FROM `source_db`.`orders`

Explicit aliases are preserved, so this remains unchanged:

SELECT `orders`.`id` FROM `source_db`.`orders` AS `orders`

Same-schema CREATE VIEW statements still keep their original query unless this column-qualifier normalization is needed.

Check List

Tests

  • Unit test

Commands run:

go test ./logservice/schemastore -run TestBuildPersistedDDLEventForCreateViewQualifiesTableColumnReferences -count=1
go test ./logservice/schemastore -run 'TestBuildPersistedDDLEventForCreateView(UsesStoredSelectStmt|KeepsOriginalQueryForSameSchemaSelect|QualifiesTableColumnReferences)$' -count=1
git diff --check

Also attempted:

go test ./logservice/schemastore -count=1

This hit an existing TiDB disttask global metrics panic: duplicate metrics collector registration attempted.

Questions

Will it cause performance regression or break compatibility?

No expected performance regression. The extra AST pass only runs for CREATE VIEW persisted event normalization.

Compatibility impact is limited to normalizing persisted CREATE VIEW queries more completely. Existing queries without table-qualified column references keep the previous behavior.

Do you need to update user documentation, design documentation or monitoring documentation?

No.

Release note

Fix CREATE VIEW normalization for table-qualified column references when the source table is schema-qualified.

Summary by CodeRabbit

  • Improvements

    • Improved CREATE VIEW normalization to better qualify column references across schemas and fallback safely when normalization fails, preserving the original view query.
    • Better handling of schema qualification so views keep correct schema/table names after processing.
  • Tests

    • Added comprehensive tests validating column qualification and schema/table preservation for cross-schema view definitions.

Review Change Stack

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 13, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-triage-completed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 13, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Warning

Rate limit exceeded

@3AceShowHand has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 20 minutes and 32 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c35e1bd1-e2c0-4e91-a53b-900372772909

📥 Commits

Reviewing files that changed from the base of the PR and between 4e15e51 and 77f5202.

📒 Files selected for processing (4)
  • logservice/schemastore/utils.go
  • logservice/schemastore/utils_test.go
  • pkg/common/event/ddl_query_normalizer.go
  • pkg/common/event/ddl_query_normalizer_test.go
📝 Walkthrough

Walkthrough

Adds a shared normalizer that parses CREATE VIEW and stored SELECT, conditionally replaces the view Select with the stored Select, and qualifies unaliased table-qualified column references by resolving table sources; the persist handler now delegates to this normalizer and a new unit test verifies qualification scenarios.

Changes

CREATE VIEW Column Reference Qualification

Layer / File(s) Summary
Normalization entrypoint
pkg/common/event/ddl_query_normalizer.go
Adds NormalizeCreateViewQueryWithStoredSelect(query, storedSelectStmt, currentSchema) which parses CREATE VIEW and stored SELECT, swaps in the stored Select when eligible, restores the AST, and returns a normalized query or error.
AST visitor and scope tracking
pkg/common/event/ddl_query_normalizer.go
Defines a SELECT-traversing visitor that maintains a per-SELECT scope stack and records whether only the current schema is referenced to decide normalization eligibility.
Column qualification implementation
pkg/common/event/ddl_query_normalizer.go
Rewrites unaliased table-qualified ColumnName nodes by resolving their source table from the current SELECT scope and attaching the resolved Schema when uniquely identifiable.
Scope building for FROM/JOIN
pkg/common/event/ddl_query_normalizer.go
Collects table sources and aliases from FROM/JOIN, builds mappings from unqualified table name to schema when unambiguous, and marks ambiguous names to avoid qualification.
Persist handler integration
logservice/schemastore/persist_storage_ddl_handlers.go
Refactors normalizeCreateViewQueryWithStoredSelect to delegate normalization to commonEvent.NormalizeCreateViewQueryWithStoredSelect; it guards on missing TableInfo/View and logs/keeps the original query on normalization errors.
Unit tests
logservice/schemastore/persist_storage_test.go
Adds TestBuildPersistedDDLEventForCreateViewQualifiesTableColumnReferences — table-driven test covering cross-schema, same-schema, aliases, ambiguous names, subquery scope, and join-qualified column qualification, and asserts resulting ddl.SchemaName/TableName.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • pingcap/ticdc#5027: Prior PR that replaced view SELECT body with stored TableInfo.View.SelectStmt to normalize cross-schema table references; this PR extends normalization to qualify unaliased table-qualified columns.

Suggested labels

lgtm

Suggested reviewers

  • wk989898

🐰 I hopped through ASTs, keen and spry,
I tagged each column with its sky.
Where qualifiers wandered, now they stay,
So routed views won't slip away.
Hop, hop — the schema's fixed today!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% 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 accurately summarizes the main change: qualifying CREATE VIEW column references with schema names to enable safe downstream table routing.
Description check ✅ Passed The description includes issue reference, problem statement, solution explanation with examples, test verification, and compatibility assessment as required by the template.
Linked Issues check ✅ Passed The PR fully addresses issue #5043 by implementing CREATE VIEW SELECT normalization to qualify unaliased table-qualified column references, enabling safe downstream table routing.
Out of Scope Changes check ✅ Passed All changes are in scope: the new normalizer function, refactored persist storage handler, and new unit tests directly address the linked issue's requirements.

✏️ 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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements automatic schema qualification for table-qualified column references in CREATE VIEW statements. It introduces a visitor-based mechanism to resolve table names to schemas by analyzing the SELECT statement's scope. While the implementation includes initial tests for cross-schema and alias scenarios, the reviewer suggested adding further test cases for ambiguous table names, subqueries, and joins to ensure the scope handling is robust against complex SQL structures.

Comment thread logservice/schemastore/persist_storage_test.go
@3AceShowHand 3AceShowHand marked this pull request as ready for review May 13, 2026 14:32
@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 13, 2026
@3AceShowHand
Copy link
Copy Markdown
Collaborator Author

/test all

@3AceShowHand
Copy link
Copy Markdown
Collaborator Author

/check-issue-triage-complete

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@logservice/schemastore/persist_storage_ddl_handlers.go`:
- Around line 708-727: qualifyColumnName currently only checks the innermost
scope (scopes[len-1]) so correlated subquery references in outer SELECTs are not
resolved; update createViewColumnQualifier.qualifyColumnName to iterate scopes
from innermost to outermost (for i := len(q.scopes)-1; i >= 0; i--), and for
each scope check alias/ambiguousTables and tableByName (using
strings.ToLower(c.Table.O)) and on first successful match set c.Schema =
ast.NewCIStr(schema) and q.changed = true, then return; preserve the existing
early-return guards (nil c, empty c.Table.O, or pre-existing c.Schema.O).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1fb59976-3c8b-429f-9ed0-55b7076127ab

📥 Commits

Reviewing files that changed from the base of the PR and between 5745770 and f9b5ee2.

📒 Files selected for processing (2)
  • logservice/schemastore/persist_storage_ddl_handlers.go
  • logservice/schemastore/persist_storage_test.go

Comment thread logservice/schemastore/persist_storage_ddl_handlers.go Outdated
@3AceShowHand 3AceShowHand force-pushed the codex/fix-create-view-table-qualified-columns branch from f9b5ee2 to 3356eec Compare May 13, 2026 14:44
@3AceShowHand 3AceShowHand force-pushed the codex/fix-create-view-table-qualified-columns branch from 3356eec to 8cd9936 Compare May 13, 2026 14:51
@3AceShowHand
Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot ti-chi-bot Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 14, 2026
@3AceShowHand
Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels May 14, 2026
@3AceShowHand
Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 14, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lidezhu, wk989898

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

The pull request process is described 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

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 14, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 14, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-05-14 03:00:48.821804233 +0000 UTC m=+321017.354583552: ☑️ agreed by lidezhu.
  • 2026-05-14 03:29:09.435978044 +0000 UTC m=+322717.968757364: ☑️ agreed by wk989898.

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

Labels

approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

table router: CREATE VIEW with unaliased table-qualified columns can be routed to invalid query

3 participants