Replace regex-based LIKE matching with a linear-time direct matcher#4235
Open
arnaud-lacurie wants to merge 4 commits into
Open
Replace regex-based LIKE matching with a linear-time direct matcher#4235arnaud-lacurie wants to merge 4 commits into
arnaud-lacurie wants to merge 4 commits into
Conversation
PatternForLikeValue previously translated SQL LIKE patterns into Java regex strings (% → .*, _ → .) and LikeOperatorValue compiled them via Pattern.compile() per evaluated row. Java's NFA engine backtracks super-polynomially on patterns like %a%a%a%, making this a CPU-exhaustion vector for any caller that can submit a LIKE query. Replace the regex pipeline with a two-pointer iterative LIKE matcher that runs in O(n·m) time with no backtracking. PatternForLikeValue now emits a normalized LIKE pattern (% and _ as wildcards, \ as escape) instead of a regex string. Pattern.compile is gone entirely.
When text contained '%' at a position where the pattern also had '%', the literal equality branch fired before the wildcard branch, causing '%' in the pattern to be consumed as a literal match. Move the '%' wildcard check before the literal equality check and add regression test cases.
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
PatternForLikeValuewas translating SQL LIKE patterns into Java regex strings (%→.*,_→.), andLikeOperatorValuewas compiling them viaPattern.compile()on every evaluated row. Java's NFA engine backtracks super-polynomially on patterns like%a%a%a%, which is a CPU-exhaustion risk for any caller that can submit a LIKE query.PatternForLikeValue.eval()now emits a normalized LIKE pattern (%/_as wildcards,\as internal escape) rather than a regex string.Pattern.compileis removed entirely.LikeOperatorValueTestcases pass unchanged.