Skip to content
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

Fix like matching with newlines and no wildcard #23404

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion presto-docs/src/main/sphinx/functions/comparison.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ LIKE
----
The LIKE operator is used to match a specified character pattern in a string. Patterns can contain
regular characters as well as wildcards. Wildcard characters can be escaped using the single character
specified for the ESCAPE parameter. Matching is case sensitive.
specified for the ESCAPE parameter. Matching is case sensitive, and the pattern must match the whole string.

Syntax:

Expand Down Expand Up @@ -199,6 +199,9 @@ Examples::
WHERE name LIKE '%#%%' ESCAPE '#'
--returns 'a%c' and '%cd'

SELECT 'ab' || chr(10) || 'c' LIKE 'ab' --chr(10) is a newline character
--returns 'false'


Row comparison: IN
------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ private static Regex likePattern(String patternString, char escapeChar, boolean
regex.append('$');

byte[] bytes = regex.toString().getBytes(UTF_8);
return new Regex(bytes, 0, bytes.length, Option.MULTILINE, NonStrictUTF8Encoding.INSTANCE, SYNTAX);
// Option.MULTILINE specifies that wildcard characters (. and *) should match newlines
// Option.SINGLELINE specifies that anchors (^ and $) should match the beginning and end of
// input rather than the beginning and end of the line
return new Regex(bytes, 0, bytes.length, Option.MULTILINE | Option.SINGLELINE, NonStrictUTF8Encoding.INSTANCE, SYNTAX);
}

@SuppressWarnings("NumericCastThatLosesPrecision")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,32 @@ public void testLikeNewlineBeforeMatch()
assertTrue(likeVarchar(utf8Slice("foo\nbar"), regex));
}

@Test
public void testLikeNewlineNoWildcard()
{
Regex regex = likePattern(utf8Slice("foo\nbar"));
assertTrue(likeVarchar(utf8Slice("foo\nbar"), regex));
}

@Test
public void testLikeNoMatchAfterNewline()
{
Regex regex = likePattern(utf8Slice("foo"));
assertFalse(likeVarchar(utf8Slice("foo\nbar"), regex));
}

@Test
public void testLikeNewlineInMatch()
{
Regex regex = likePattern(utf8Slice("f%b%"));
assertTrue(likeVarchar(utf8Slice("foo\nbar"), regex));
}
@Test
public void testLikeNewlineInSingleWildcardMatch()
{
Regex regex = likePattern(utf8Slice("foo_bar"));
assertTrue(likeVarchar(utf8Slice("foo\nbar"), regex));
}

@Test(timeOut = 1000)
public void testLikeUtf8Pattern()
Expand Down
Loading