Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Convert LIKE patterns to specific Go regexes #817

Merged
merged 1 commit into from
Sep 13, 2019

Conversation

agarciamontoro
Copy link
Contributor

Fixes #814

The problem here was that we converted the LIKE pattern into a regex and used one of the regex engines (the standard go regexp package, or oniguruma) to try to match the string from the input row. The regex was built by enclosing the LIKE pattern between ^ and $ (so if the pattern was text, the regexp would be ^text$). But, at least in the standard go regexp package, ^ and $ match the beginning and the end of the text, not of the line, and the dot . matches any character that is not a new line character. That is, if the LIKE pattern is %text%, then the resulting regex, ^.*text.*$, will match anything that contains the substring text and that does not contain any new line characters. I considered several solutions to fix this:

  • to modify the default behaviour of our go regexp manager internal/regex_go.go, either by:
    • adding the flag (?s), which makes . match also newline characters
    • adding the flag (?m), which makes ^ and $ match beginning and end, respectively, of a new line, not of the whole text.
  • to refactor the patternToRegex function in sql/expression/like.go so it depends on the regex engine being used.
  • to make the patternToRegex specific to the go engine, always adding the (?s) flag.

I finally implemented the last solution, as it was the least intrusive. The other ones are not generic or mix two different problems in one single place.

Note: We should block the merge of this PR until @Hydrocharged opens one with the tests.

Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
@ajnavarro ajnavarro merged commit 77d8c27 into src-d:master Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LIKE fails when new lines are present
3 participants