Skip to content

Commit 1f74d4d

Browse files
committed
Fix interpreting like expressions with escape
#23456 incorrectly changed the invokation of the like function to use the unescaped pattern for a call that expects the pattern to still contain the escape characters. As a result, any escaped characters were treated as though they were not escaped. This affects certain queries when all arguments to the like predicate are constants.
1 parent 8e3710b commit 1f74d4d

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

presto-main/src/main/java/com/facebook/presto/sql/planner/RowExpressionInterpreter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public RowExpressionInterpreter(RowExpression expression, Metadata metadata, Con
148148
{
149149
this(expression, metadata.getFunctionAndTypeManager(), session, optimizationLevel);
150150
}
151+
151152
public RowExpressionInterpreter(RowExpression expression, FunctionAndTypeManager functionAndTypeManager, ConnectorSession session, Level optimizationLevel)
152153
{
153154
this.expression = requireNonNull(expression, "expression is null");
@@ -922,8 +923,7 @@ private SpecialCallResult tryHandleLike(CallExpression callExpression, List<Obje
922923
// this corresponds to ExpressionInterpreter::getConstantPattern
923924
if (hasEscape) {
924925
// like_pattern(pattern, escape)
925-
Slice unescapedPattern = unescapeLiteralLikePattern((Slice) nonCompiledPattern, (Slice) escape);
926-
possibleCompiledPattern = functionInvoker.invoke(((CallExpression) possibleCompiledPattern).getFunctionHandle(), session.getSqlFunctionProperties(), unescapedPattern, escape);
926+
possibleCompiledPattern = functionInvoker.invoke(((CallExpression) possibleCompiledPattern).getFunctionHandle(), session.getSqlFunctionProperties(), nonCompiledPattern, escape);
927927
}
928928
else {
929929
// like_pattern(pattern)

presto-tests/src/main/java/com/facebook/presto/tests/TestLikeQueries.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class TestLikeQueries
2323
extends AbstractTestQueryFramework
2424
{
2525
@Override
26-
protected QueryRunner createQueryRunner() throws Exception
26+
protected QueryRunner createQueryRunner()
27+
throws Exception
2728
{
2829
Session session = testSessionBuilder()
2930
.setCatalog("test")
@@ -35,8 +36,13 @@ protected QueryRunner createQueryRunner() throws Exception
3536
@Test
3637
public void testLikeQueriesWithEscape()
3738
{
38-
assertQuerySucceeds("SELECT IF('_T' LIKE '#_T' ESCAPE '#', c0, c1) FROM (values (true, false)) t(c0, c1)");
39-
assertQuerySucceeds("SELECT IF(c2 LIKE 'T' ESCAPE '#', c0, c1) FROM (values (true, false, 'T')) t(c0, c1, c2)");
39+
assertQuery("SELECT IF('xT' LIKE '#_T' ESCAPE '#', c0, c1) FROM (values (true, false)) t(c0, c1)", "SELECT false");
40+
assertQuery("SELECT IF(c2 LIKE 'T' ESCAPE '#', c0, c1) FROM (values (true, false, 'T')) t(c0, c1, c2)", "SELECT true");
41+
}
42+
43+
@Test
44+
public void testLikeQueriesWithInvalidEscape()
45+
{
4046
assertQueryFails("SELECT IF('T' LIKE '###T' ESCAPE '##', c0, c1) FROM (values (true, false)) t(c0, c1)", ".*Escape string must be a single character$");
4147
assertQueryFails("SELECT IF(c2 LIKE '' ESCAPE '', c0, c1) FROM (values (true, false, 'T')) t(c0, c1, c2)", ".*Escape string must be a single character$");
4248
}

0 commit comments

Comments
 (0)