Repository: phoenix Updated Branches: refs/heads/4.x-HBase-0.98 fce9a6712 -> fa18c8f09 refs/heads/4.x-HBase-1.1 9a30bcb89 -> 327a183d0 refs/heads/4.x-HBase-1.2 278baee87 -> f312db350
PHOENIX-2314 Cannot prepare parameterized statement with a 'like' predicate Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/f312db35 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/f312db35 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/f312db35 Branch: refs/heads/4.x-HBase-1.2 Commit: f312db35076982881cb7282473b750b6aa0c18d2 Parents: 278baee Author: kliewkliew <kl...@apache.org> Authored: Fri Sep 1 14:28:44 2017 -0700 Committer: Josh Elser <els...@apache.org> Committed: Wed May 23 15:20:04 2018 -0400 ---------------------------------------------------------------------- .../phoenix/end2end/LikeExpressionIT.java | 16 +++++++++++ .../phoenix/compile/ExpressionCompiler.java | 2 +- .../phoenix/end2end/QueryServerBasicsIT.java | 28 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/f312db35/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 c2198cc..0b061d5 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 @@ -414,4 +414,20 @@ public class LikeExpressionIT extends ParallelStatsDisabledIT { assertEquals(expectedCount, i); } } + + @Test + public void testParameterizedLikeExpression() throws Exception { + final Connection conn = DriverManager.getConnection(getUrl()); + final PreparedStatement select = conn.prepareStatement( + "select k from " + tableName + " where k like ?"); + select.setString(1, "12%"); + ResultSet rs = select.executeQuery(); + assertTrue(rs.next()); + assertEquals("123n7-app-2-", rs.getString(1)); + assertFalse(rs.next()); + + select.setString(1, null); + rs = select.executeQuery(); + assertFalse(rs.next()); + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/f312db35/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 fb4c542..9daa744 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 @@ -498,7 +498,7 @@ public class ExpressionCompiler extends UnsupportedAllParseNodeVisitor<Expressio if (rhs instanceof LiteralExpression) { String pattern = (String)((LiteralExpression)rhs).getValue(); if (pattern == null || pattern.length() == 0) { - return LiteralExpression.newConstant(null, rhs.getDeterminism()); + return LiteralExpression.newConstant(null, PBoolean.INSTANCE, rhs.getDeterminism()); } // TODO: for pattern of '%' optimize to strlength(lhs) > 0 // We can't use lhs IS NOT NULL b/c if lhs is NULL we need http://git-wip-us.apache.org/repos/asf/phoenix/blob/f312db35/phoenix-queryserver/src/it/java/org/apache/phoenix/end2end/QueryServerBasicsIT.java ---------------------------------------------------------------------- diff --git a/phoenix-queryserver/src/it/java/org/apache/phoenix/end2end/QueryServerBasicsIT.java b/phoenix-queryserver/src/it/java/org/apache/phoenix/end2end/QueryServerBasicsIT.java index ca4cf0b..ceb0a78 100644 --- a/phoenix-queryserver/src/it/java/org/apache/phoenix/end2end/QueryServerBasicsIT.java +++ b/phoenix-queryserver/src/it/java/org/apache/phoenix/end2end/QueryServerBasicsIT.java @@ -315,4 +315,32 @@ public class QueryServerBasicsIT extends BaseHBaseManagedTimeIT { private int getArrayValueForOffset(int arrayOffset) { return arrayOffset * 2 + 1; } + + @Test + public void testParameterizedLikeExpression() throws Exception { + final Connection conn = DriverManager.getConnection(CONN_STRING); + final String tableName = generateUniqueName(); + conn.createStatement().execute( + "CREATE TABLE " + tableName + " (k VARCHAR NOT NULL PRIMARY KEY, i INTEGER)"); + conn.commit(); + + final PreparedStatement upsert = conn.prepareStatement( + "UPSERT INTO " + tableName + " VALUES (?, ?)"); + upsert.setString(1, "123n7-app-2-"); + upsert.setInt(2, 1); + upsert.executeUpdate(); + conn.commit(); + + final PreparedStatement select = conn.prepareStatement( + "select k from " + tableName + " where k like ?"); + select.setString(1, "12%"); + ResultSet rs = select.executeQuery(); + assertTrue(rs.next()); + assertEquals("123n7-app-2-", rs.getString(1)); + assertFalse(rs.next()); + + select.setString(1, null); + rs = select.executeQuery(); + assertFalse(rs.next()); + } }