PHOENIX-2641 Implicit wildcard in LIKE predicate search pattern This closes #200
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/c02d6cb5 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/c02d6cb5 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/c02d6cb5 Branch: refs/heads/calcite Commit: c02d6cb5971f7b17bcd5e308952fa081e32adf19 Parents: 7827178 Author: kliewkliew <kliewkl...@users.noreply.github.com> Authored: Wed Aug 24 06:40:35 2016 -0700 Committer: Thomas D'Silva <tdsi...@salesforce.com> Committed: Tue Sep 6 11:11:38 2016 -0700 ---------------------------------------------------------------------- .../phoenix/end2end/LikeExpressionIT.java | 23 ++++++++++++++++++++ .../phoenix/compile/ExpressionCompiler.java | 10 +++++++-- .../phoenix/expression/LikeExpressionTest.java | 6 +++++ 3 files changed, 37 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/c02d6cb5/phoenix-core/src/it/java/org/apache/phoenix/end2end/LikeExpressionIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/LikeExpressionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LikeExpressionIT.java index 6bfa358..f97e1d7 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/LikeExpressionIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/LikeExpressionIT.java @@ -182,4 +182,27 @@ public class LikeExpressionIT extends BaseHBaseManagedTimeTableReuseIT { "SELECT * FROM " + t + " WHERE k like 'AA_'"); assertFalse(rs.next()); } + + @Test + public void testOneChar() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + String t = generateRandomString(); + String ddl = "CREATE TABLE " + t + " (k VARCHAR NOT NULL PRIMARY KEY)"; + conn.createStatement().execute(ddl); + conn.createStatement().execute("UPSERT INTO " + t + " VALUES('A')"); + conn.createStatement().execute("UPSERT INTO " + t + " VALUES('AA')"); + conn.commit(); + + ResultSet rs = conn.createStatement().executeQuery( + "SELECT * FROM " + t + " WHERE k like '_'"); + assertTrue(rs.next()); + assertEquals("A", rs.getString(1)); + assertFalse(rs.next()); + + rs = conn.createStatement().executeQuery( + "SELECT * FROM " + t + " WHERE k like '_A'"); + assertTrue(rs.next()); + assertEquals("AA", rs.getString(1)); + assertFalse(rs.next()); + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/c02d6cb5/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java index 0fd1876..aaab763 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/ExpressionCompiler.java @@ -136,6 +136,7 @@ import org.apache.phoenix.schema.types.PhoenixArray; import org.apache.phoenix.util.ExpressionUtil; import org.apache.phoenix.util.IndexUtil; import org.apache.phoenix.util.SchemaUtil; +import org.apache.phoenix.util.StringUtil; public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expression> { @@ -517,8 +518,13 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio return new ComparisonExpression(Arrays.asList(lhs,rhs), op); } } - } else if (index == 0 && pattern.length() == 1) { - return IsNullExpression.create(lhs, true, context.getTempPtr()); + } else { + byte[] nullExpressionString = new byte[pattern.length()]; + byte[] wildcard = {StringUtil.MULTI_CHAR_LIKE}; + StringUtil.fill(nullExpressionString, 0, pattern.length(), wildcard, 0, 1, false); + if (pattern.equals(new String (nullExpressionString))) { + return IsNullExpression.create(lhs, true, context.getTempPtr()); + } } } QueryServices services = context.getConnection().getQueryServices(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/c02d6cb5/phoenix-core/src/test/java/org/apache/phoenix/expression/LikeExpressionTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/LikeExpressionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/LikeExpressionTest.java index 580ac8e..2e33e7b 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/expression/LikeExpressionTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/LikeExpressionTest.java @@ -77,6 +77,12 @@ public class LikeExpressionTest { } @Test + public void testOneChar() throws Exception { + assertEquals(Boolean.TRUE, testExpression ("A", "_")); + assertEquals(Boolean.FALSE, testExpression ("AA", "_")); + } + + @Test public void testEmptySourceStr() throws Exception { assertEquals(Boolean.TRUE, testExpression ("", "%")); assertEquals(Boolean.FALSE, testExpression ("", "_"));