-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: Escaped wildcard (*) not accepted in LIKE #63428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c368506
5c97c9d
f089b47
5e6bc1c
ff5ee18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,12 +270,6 @@ public LikePattern visitPattern(PatternContext ctx) { | |
if (pattern == null) { | ||
throw new ParsingException(source(ctx.value), "Pattern must not be [null]"); | ||
} | ||
int pos = pattern.indexOf('*'); | ||
if (pos >= 0) { | ||
throw new ParsingException(source(ctx.value), | ||
"Invalid char [*] found in pattern [{}] at position {}; use [%] or [_] instead", | ||
pattern, pos); | ||
} | ||
|
||
char escape = 0; | ||
PatternEscapeContext escapeCtx = ctx.patternEscape(); | ||
|
@@ -288,7 +282,7 @@ public LikePattern visitPattern(PatternContext ctx) { | |
} else if (escapeString.length() == 1) { | ||
escape = escapeString.charAt(0); | ||
// these chars already have a meaning | ||
if (escape == '*' || escape == '%' || escape == '_') { | ||
if (escape == '%' || escape == '_') { | ||
throw new ParsingException(source(escapeCtx.escape), "Char [{}] cannot be used for escaping", escape); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the same idea (which I like) of actually mentioning the wildcard chars in the error message itself (for the lazy user who doesn't read the docs and asking "why % char cannot be used for escaping?"), what about adapting this error message to include the reason? Something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, good suggestion. I'll add this cosmetic fix. |
||
} | ||
// lastly validate that escape chars (if present) are followed by special chars | ||
|
@@ -303,8 +297,8 @@ public LikePattern visitPattern(PatternContext ctx) { | |
char next = pattern.charAt(i + 1); | ||
if (next != '%' && next != '_') { | ||
throw new ParsingException(source(ctx.value), | ||
"Pattern [{}] is invalid as escape char [{}] at position {} can only escape wildcard chars; found [{}]", | ||
pattern, escape, i, next); | ||
"Pattern [{}] is invalid as escape char [{}] at position {} can only escape " | ||
+ "wildcard chars [%_]; found [{}]", pattern, escape, i, next); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@costin Did I miss anything, is there a reason to keep this check here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent folks from using
*
instead of%
. Whether that's still a concern I don't know.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have anything in the docs regarding the
*
and we list only the%
and_
*
should be interpreted as a normal char for SQL:How about adding a NOTE in the docs to explicitly specify that
*
,?
and the usual regex chars are interpreted as normal chars?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK it is only the MS Access SQL flavour that treats
*
as a wildcard in aLIKE
pattern, so SQL folks should be fairly used to*
not being a wildcard. ES customers with less SQL knowledge might not. I think adding a NOTE to the docs does not hurt.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also thinking about removing the restriction on not being able to use
*
as anESCAPE
character from here. Do you see any good reason to keep that restriction?As a user
*
would not be my first choice as anESCAPE
character, but the parser can handle it since*
is just a normal character.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On PostgreSQL, MySQL, SQLite any character can be used as ESCAPE (even wildchars) and any character can be escaped, not just wildcards. PostgreSQL treats the
'%%' ESCAPE '%'
as a non-wildcard%
character (just as another escaped character), while MySQL and SQLite treat it as a wildcard (special escaping case). I assume it was intentional to behave differently and only allow escaping of the wildchars.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see it, since only
%
and_
are wildcards forLIKE
I would only allow escaping of those, but if we go on with this change it shouldn't be part of the bugfix that would be backported, but for next minor release since it's a minor "breaking" change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree @matriv that allowing escaping any characters should be a separate PR and likely should be in the next minor release only, although technically it is less of a breaking change unless someone depends on the exceptions around escaping non-wildchars. Anyways, this is off topic for this PR.
To clarify: in this current PR I am only removing the special treatment of
*
and the restriction on using*
as anESCAPE
character (wildchars are still not allowed asESCAPE
characters). Does this sound good to you?