Skip to content

[BugFix] fix information_schema.tables not escaping special characters in equality predicates#71273

Merged
dirtysalt merged 3 commits into
StarRocks:mainfrom
dontknow9179:fix-information-schema-table-name-escape
Apr 8, 2026
Merged

[BugFix] fix information_schema.tables not escaping special characters in equality predicates#71273
dirtysalt merged 3 commits into
StarRocks:mainfrom
dontknow9179:fix-information-schema-table-name-escape

Conversation

@dontknow9179
Copy link
Copy Markdown
Contributor

Why I'm doing:

When querying information_schema.tables with an equality predicate like:

SELECT * FROM information_schema.tables
WHERE table_schema = 'my_db' AND table_name = 'a_a';

The value a_a was passed directly to PatternMatcher.createMysqlPattern() as a LIKE pattern, where _ is treated as a single-character wildcard. This caused the query to return unrelated tables like aba, a1a, etc., instead of only the exact table a_a.

Similarly, table names containing backslashes (e.g. a\_a) were not handled correctly — the backslash and underscore interacted with LIKE pattern escaping rules in unexpected ways.

Fixes #67447

What I'm doing

Fix information_schema.tables queries with equality predicates (=) on table_name or table_schema incorrectly treating LIKE-special characters (_, %, \) as wildcards instead of literal characters.

Changes

  1. PatternMatcher.convertMysqlPattern: Rewrite the two-pass conversion to a single-pass approach that correctly escapes regex metacharacters (e.g. (, ), +, [, ]) in table/database names, and properly handles a trailing backslash as a literal character.

  2. PatternMatcher.escapeLikeValue: Add a new utility method that escapes a literal string so it can be safely used as a MySQL LIKE pattern for exact matching (prefixing \, %, and _ with a backslash).

  3. TablesSystemTable.evaluate: Call escapeLikeValue() on the value extracted from = predicates before passing it to the Thrift request. This is the primary fix — the SchemaTableEvaluateRule optimizer rule short-circuits simple equality queries on information_schema.tables and evaluates them directly in the FE without going through PlanFragmentBuilder.visitPhysicalSchemaScan.

  4. PlanFragmentBuilder.visitPhysicalSchemaScan: Also call escapeLikeValue() for = predicates on TABLE_NAME/TABLE_SCHEMA/DATABASE_NAME in the SchemaScan path, which is used when SchemaTableEvaluateRule is disabled or when the predicate is not a simple equality.

  5. InformationSchemaDataSource.generateTablesInfoResponse: Remove the fallback OR-logic in matchPattern and use matcher.match() directly, preventing false-positive matches when the pattern matcher rejects a name but the raw string comparison accidentally matches.

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
    • This pr needs auto generate documentation
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 4.1
    • 4.0
    • 3.5
    • 3.4

@CelerData-Reviewer
Copy link
Copy Markdown

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de34f44b20

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread fe/fe-core/src/main/java/com/starrocks/sql/plan/PlanFragmentBuilder.java Outdated
Signed-off-by: dontknow9179 <clin56322@gmail.com>
Signed-off-by: dontknow9179 <clin56322@gmail.com>
Signed-off-by: dontknow9179 <clin56322@gmail.com>
@dontknow9179 dontknow9179 force-pushed the fix-information-schema-table-name-escape branch from 1422338 to aa65d6f Compare April 7, 2026 08:36
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 7, 2026

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 7, 2026

[FE Incremental Coverage Report]

pass : 29 / 29 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/common/PatternMatcher.java 17 17 100.00% []
🔵 com/starrocks/catalog/system/information/TablesSystemTable.java 2 2 100.00% []
🔵 com/starrocks/sql/plan/PlanFragmentBuilder.java 9 9 100.00% []
🔵 com/starrocks/service/InformationSchemaDataSource.java 1 1 100.00% []

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 7, 2026

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@dirtysalt dirtysalt enabled auto-merge (squash) April 7, 2026 10:19
@dirtysalt dirtysalt merged commit 1224bae into StarRocks:main Apr 8, 2026
54 of 55 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 8, 2026

@Mergifyio backport branch-4.0

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 8, 2026

@Mergifyio backport branch-4.1

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 8, 2026

@Mergifyio backport branch-3.5

@github-actions github-actions Bot removed the 3.5 label Apr 8, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 8, 2026

backport branch-4.0

✅ Backports have been created

Details

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 8, 2026

backport branch-4.1

✅ Backports have been created

Details

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 8, 2026

backport branch-3.5

✅ Backports have been created

Details

Cherry-pick of 1224bae has failed:

On branch mergify/bp/branch-3.5/pr-71273
Your branch is up to date with 'origin/branch-3.5'.

You are currently cherry-picking commit 1224bae098.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   fe/fe-core/src/main/java/com/starrocks/common/PatternMatcher.java
	modified:   fe/fe-core/src/test/java/com/starrocks/common/PatternMatcherTest.java
	modified:   fe/fe-core/src/test/java/com/starrocks/sql/plan/ScanTest.java
	modified:   test/sql/test_information_schema/R/test_tables_like_escape
	modified:   test/sql/test_information_schema/T/test_tables_like_escape

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   fe/fe-core/src/main/java/com/starrocks/catalog/system/information/TablesSystemTable.java
	both modified:   fe/fe-core/src/main/java/com/starrocks/service/InformationSchemaDataSource.java
	both modified:   fe/fe-core/src/main/java/com/starrocks/sql/plan/PlanFragmentBuilder.java

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

mergify Bot pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (#71273)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
(cherry picked from commit 1224bae)
mergify Bot pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (#71273)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
(cherry picked from commit 1224bae)
mergify Bot pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (#71273)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
(cherry picked from commit 1224bae)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/catalog/system/information/TablesSystemTable.java
#	fe/fe-core/src/main/java/com/starrocks/service/InformationSchemaDataSource.java
#	fe/fe-core/src/main/java/com/starrocks/sql/plan/PlanFragmentBuilder.java
wanpengfei-git pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (backport #71273) (#71403)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
Co-authored-by: Alex <32681448+dontknow9179@users.noreply.github.com>
wanpengfei-git pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (backport #71273) (#71402)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
Co-authored-by: Alex <32681448+dontknow9179@users.noreply.github.com>
gengjun-git pushed a commit that referenced this pull request Apr 8, 2026
…s in equality predicates (backport #71273) (#71407)

## Why I'm doing:

When querying `information_schema.tables` with an equality predicate like:

```sql
SELECT * FROM information_schema.tables
WHERE table_schema = 'my_db' AND table_name = 'a_a';
```

The value `a_a` was passed directly to `PatternMatcher.createMysqlPattern()` as a LIKE pattern, where `_` is treated as a single-character wildcard. This caused the query to return unrelated tables like `aba`, `a1a`, etc., instead of only the exact table `a_a`.

Similarly, table names containing backslashes (e.g. `a\_a`) were not handled correctly — the backslash and underscore interacted with LIKE pattern escaping rules in unexpected ways.

Fixes #67447

## What I'm doing

Fix `information_schema.tables` queries with equality predicates (`=`) on `table_name` or `table_schema` incorrectly treating LIKE-special characters (`_`, `%`, `\`) as wildcards instead of literal characters.

### Changes

1. **`PatternMatcher.convertMysqlPattern`**: Rewrite the two-pass conversion to a single-pass approach that correctly escapes regex metacharacters (e.g. `(`, `)`, `+`, `[`, `]`) in table/database names, and properly handles a trailing backslash as a literal character.

2. **`PatternMatcher.escapeLikeValue`**: Add a new utility method that escapes a literal string so it can be safely used as a MySQL LIKE pattern for exact matching (prefixing `\`, `%`, and `_` with a backslash).

3. **`TablesSystemTable.evaluate`**: Call `escapeLikeValue()` on the value extracted from `=` predicates before passing it to the Thrift request. This is the **primary fix** — the `SchemaTableEvaluateRule` optimizer rule short-circuits simple equality queries on `information_schema.tables` and evaluates them directly in the FE without going through `PlanFragmentBuilder.visitPhysicalSchemaScan`.

4. **`PlanFragmentBuilder.visitPhysicalSchemaScan`**: Also call `escapeLikeValue()` for `=` predicates on `TABLE_NAME`/`TABLE_SCHEMA`/`DATABASE_NAME` in the SchemaScan path, which is used when `SchemaTableEvaluateRule` is disabled or when the predicate is not a simple equality.

5. **`InformationSchemaDataSource.generateTablesInfoResponse`**: Remove the fallback OR-logic in `matchPattern` and use `matcher.match()` directly, preventing false-positive matches when the pattern matcher rejects a name but the raw string comparison accidentally matches.

Signed-off-by: dontknow9179 <clin56322@gmail.com>
wanpengfei-git pushed a commit that referenced this pull request May 5, 2026
…s in equality predicates (backport #71273) (backport #71407) (#72708)

Signed-off-by: dontknow9179 <clin56322@gmail.com>
Co-authored-by: Alex <32681448+dontknow9179@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

information table query does not handle escape character correctly

5 participants