fix(core): unquote backtick/double-quoted table tokens in alias resolution (#82)#143
Merged
Merged
Conversation
…ution
Hibernate emits backtick- or double-quoted table identifiers under
hibernate.globally_quoted_identifiers=true (and for reserved-word table
names). MissingIndexDetector.resolveAliases used FROM_ALIAS / JOIN_ALIAS
regexes that only matched bare \w+ segments, so for SQL like
`SELECT m1_0.id FROM \`messages\` m1_0 WHERE m1_0.user_id = ?` the
alias-to-table map ended up empty.
resolveTable then hit its Hibernate-pattern fallback (m1_0 matches
[a-z]{1,3}\d+_\d+) and returned null — silently skipping legitimate
missing-index, redundant-filter, composite-index, and for-update
findings. The same issue applies whenever any FROM/JOIN segment is
quoted.
Allow quoted segments in both regexes and unquote them in registerAlias
before lowercasing, so the canonical map key matches the bare name that
the JSqlParser-driven WHERE-column extractor already produces. The
"Hibernate alias smells like a table" guard at line 709 stays in place
as the safety net for genuinely unparseable cases.
Refs #82.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MissingIndexDetector.resolveAliasesnow matches and strips backtick / double-quote wrapping on each segment of FROM/JOIN table tokens.QuotedTableAliasResolutionTestwith three failing-before regression cases that hitMissingIndexDetectorandRedundantFilterDetector(the latter delegates to the same alias map).Why
A targeted slice of #82. Hibernate emits backtick- or double-quoted table identifiers when
hibernate.globally_quoted_identifiers=trueor when the table name is a reserved word. For SQL like:FROM_ALIAS(\bFROM\s+((?:\w+\.){0,2}\w+)…) only matches bare\w+segments, so the regex didn't match at all and the alias map was empty.resolveTable("m1_0", emptyMap)then hit its Hibernate-pattern fallback ([a-z]{1,3}\d+_\d+) and returnednull— silently dropping legitimate missing-index, redundant-filter, composite-index, and for-update findings on every Hibernate-emitted query that ran with quoted identifiers.The fix:
\w+,`…`, or"…".registerAliasbefore lowercasing so the canonical key matches the bare names the JSqlParser-driven WHERE/JOIN column extractor already produces.The Hibernate-pattern guard in
resolveTablestays — it's still the right behaviour when the FROM clause genuinely can't be parsed.This is narrower than the full proposal in #82. The issue contemplates routing all detectors through
EnhancedSqlParserfor canonical alias resolution; that's a larger refactor and is intentionally out of scope here. Leaving #82 open for that follow-up.Test plan
:query-audit-core:test—QuotedTableAliasResolutionTest(3 new tests) plus the full suite green../gradlew testBUILD SUCCESSFUL on the local SB 3.4.x baseline.FalsePositiveFixTest(hibernateAlias_mapsToFromTable,aliasedSoftDelete_*) still pass — the unquote step is a no-op when no quotes are present (earlyindexOfshort-circuit).Refs #82