Skip to content

feat(tidb): add schema introspection tools and prebuilt config#2949

Open
qiffang wants to merge 4 commits intogoogleapis:mainfrom
qiffang:feat/tidb-prebuilt-tools
Open

feat(tidb): add schema introspection tools and prebuilt config#2949
qiffang wants to merge 4 commits intogoogleapis:mainfrom
qiffang:feat/tidb-prebuilt-tools

Conversation

@qiffang
Copy link
Copy Markdown

@qiffang qiffang commented Apr 5, 2026

Description

This PR adds 4 new TiDB-specific tools to achieve feature parity with MySQL, plus a prebuilt config for one-command startup.

New Tools

Tool Description
tidb-list-tables List table schema info with TiFlash replica count, supports simple/detailed output
tidb-get-query-plan EXPLAIN with safety checks (ANALYZE restricted to SELECT only)
tidb-list-active-queries Show running queries from processlist with safety limits
tidb-list-tiflash-replicas TiDB-specific: monitor TiFlash replica sync status

Prebuilt Config

internal/prebuiltconfigs/tools/tidb.yaml enables --prebuilt=tidb with three toolsets:

  • data: execute_sql, list_tables, get_query_plan, list_active_queries
  • monitor: get_query_plan, list_active_queries, list_tiflash_replicas
  • htap: execute_sql, list_tables, list_tiflash_replicas

Security Features

  • EXPLAIN ANALYZE safety: Restricted to SELECT/WITH statements only (prevents accidental data modification)
  • Multi-statement injection prevention: Detects and rejects semicolons outside string literals
  • SQL comment stripping: Handles -- and /* */ comments for accurate statement type detection
  • TiDB version compatibility: Uses MySQL error code 1054 to detect unsupported TiFlash columns

Known Limitations

containsMultipleStatements() does not handle:

  • Backtick-quoted identifiers containing semicolons
  • Escaped single quotes

These are tracked for follow-up improvement.

Testing

  • All unit tests pass
  • Tested with TiDB 4.0+ and TiDB Cloud
  • Security test cases cover: comment stripping, statement type detection, multi-statement injection

Related

Builds on the existing TiDB source and basic tools contributed by @Icemap in #829.

Add 4 new TiDB-specific tools to achieve feature parity with MySQL:
- tidb-list-tables: List table schema info with TiFlash replica count
- tidb-get-query-plan: EXPLAIN with safety checks (ANALYZE restricted to SELECT)
- tidb-list-active-queries: Show running queries from processlist
- tidb-list-tiflash-replicas: TiDB-specific tool for TiFlash replica status

Add prebuilt config (tidb.yaml) with toolsets: data, monitor, htap

Security features:
- EXPLAIN ANALYZE restricted to SELECT/WITH statements only
- Multi-statement injection prevention (semicolon detection)
- SQL comment stripping for accurate statement type detection
- TiDB version compatibility check using MySQL error code 1054

Known limitations (tracked for follow-up):
- containsMultipleStatements() does not handle backticks or escaped quotes

Tested with TiDB 4.0+ and TiDB Cloud.
@qiffang qiffang requested a review from a team as a code owner April 5, 2026 12:29
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 5, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

@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 TiDB support by adding tools for query planning, active query monitoring, table metadata listing, and TiFlash replica status, along with a prebuilt configuration and unit tests. The review feedback suggests enhancing the multi-statement SQL detection by stripping comments before validation to avoid false positives and simplifying parameter type assertions to align with established repository patterns.

Comment thread internal/tools/tidb/tidbgetqueryplan/tidbgetqueryplan.go
Comment thread internal/tools/tidb/tidblistactivequeries/tidblistactivequeries.go Outdated
qiffang added 3 commits April 5, 2026 21:36
1. containsMultipleStatements: strip comments before checking for
   semicolons to avoid false positives (e.g., SELECT 1 -- comment;)

2. limit parameter: simplify type assertion as framework guarantees
   type safety for typed constructors like NewIntParameterWithDefault

Added test cases for semicolons in comments.
Only my new files should have copyright year 2026.
@Yuan325 Yuan325 added the priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. label Apr 9, 2026
@Yuan325
Copy link
Copy Markdown
Contributor

Yuan325 commented Apr 9, 2026

Hi @qiffang, thank you for the contribution! We recently renamed the repository, which updated the package name. Could you please rebase your branch against the latest main before we dive into the review? It’ll help avoid any import issues. Thanks

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

Labels

priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants