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/5fb85b5f Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/5fb85b5f Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/5fb85b5f Branch: refs/heads/4.x-cdh5.14 Commit: 5fb85b5fcd575552c4952e1d48164820e3a66c8d Parents: df2a7e6 Author: kliewkliew <kl...@apache.org> Authored: Fri Sep 1 22:28:44 2017 +0100 Committer: Pedro Boado <pbo...@apache.org> Committed: Fri May 25 09:24:04 2018 +0100 ---------------------------------------------------------------------- .../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/5fb85b5f/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/5fb85b5f/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/5fb85b5f/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()); + } }