Skip to content

Commit

Permalink
PHOENIX-2141 ComparisonExpression should return Boolean null if eithe…
Browse files Browse the repository at this point in the history
…r operand is null (bug fix)
  • Loading branch information
maryannxue committed Aug 25, 2015
1 parent db137dc commit 8a98324
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
Expand Up @@ -251,9 +251,6 @@ private Expression orExpression(List<Expression> children) throws SQLException {
if (child.getDataType() != PBoolean.INSTANCE) { if (child.getDataType() != PBoolean.INSTANCE) {
throw TypeMismatchException.newException(PBoolean.INSTANCE, child.getDataType(), child.toString()); throw TypeMismatchException.newException(PBoolean.INSTANCE, child.getDataType(), child.toString());
} }
if (LiteralExpression.isBooleanNull(child)) {
return child;
}
if (LiteralExpression.isFalse(child)) { if (LiteralExpression.isFalse(child)) {
iterator.remove(); iterator.remove();
} }
Expand Down
Expand Up @@ -44,9 +44,6 @@ public static Expression create(List<Expression> children) throws SQLException {
if (child.getDataType() != PBoolean.INSTANCE) { if (child.getDataType() != PBoolean.INSTANCE) {
throw TypeMismatchException.newException(PBoolean.INSTANCE, child.getDataType(), child.toString()); throw TypeMismatchException.newException(PBoolean.INSTANCE, child.getDataType(), child.toString());
} }
if (LiteralExpression.isBooleanNull(child)) {
return child;
}
if (LiteralExpression.isFalse(child)) { if (LiteralExpression.isFalse(child)) {
return child; return child;
} }
Expand Down
Expand Up @@ -218,8 +218,7 @@ public void testMultiColumnEqualFilter() throws SQLException {


@Test @Test
public void testCollapseFunctionToNull() throws SQLException { public void testCollapseFunctionToNull() throws SQLException {
String tenantId = "000000000000001"; String query = "select * from atable where substr(entity_id,null) = 'foo'";
String query = "select * from atable where organization_id='" + tenantId + "' and substr(entity_id,null) = 'foo'";
PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class); PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query); PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
QueryPlan plan = pstmt.optimizeQuery(); QueryPlan plan = pstmt.optimizeQuery();
Expand Down
Expand Up @@ -38,22 +38,57 @@ public void testComparisonExpressionWithNullOperands() throws Exception {
} }


@Test @Test
public void testAndOrExpressionWithNullOperands() throws Exception { public void testAndExpressionWithNullOperands() throws Exception {
String[] query = {"SELECT 'a' >= '' or '' < 'a'", String[] query = {"SELECT 'b' >= 'a' and '' < 'b'",
"SELECT 'b' >= '' and 'a' < 'b'",
"SELECT 'a' >= 'b' and 'a' < ''",
"SELECT '' >= 'a' and 'b' < 'a'",
"SELECT 'a' >= '' and '' < 'a'"}; "SELECT 'a' >= '' and '' < 'a'"};
Boolean[] result = {null,
null,
Boolean.FALSE,
Boolean.FALSE,
null};
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(getUrl(), props); Connection conn = DriverManager.getConnection(getUrl(), props);
try { try {
for (String q : query) { for (int i = 0; i < query.length; i++) {
ResultSet rs = conn.createStatement().executeQuery(q); ResultSet rs = conn.createStatement().executeQuery(query[i]);
assertTrue(rs.next()); assertTrue(rs.next());
assertNull(rs.getObject(1)); assertEquals(result[i], rs.getObject(1));
assertEquals(false, rs.getBoolean(1)); assertEquals(false, rs.getBoolean(1));
assertFalse(rs.next()); assertFalse(rs.next());
} }
} finally { } finally {
conn.close(); conn.close();
} }
} }

@Test
public void testOrExpressionWithNullOperands() throws Exception {
String[] query = {"SELECT 'b' >= 'a' or '' < 'b'",
"SELECT 'b' >= '' or 'a' < 'b'",
"SELECT 'a' >= 'b' or 'a' < ''",
"SELECT '' >= 'a' or 'b' < 'a'",
"SELECT 'a' >= '' or '' < 'a'"};
Boolean[] result = {Boolean.TRUE,
Boolean.TRUE,
null,
null,
null};
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(getUrl(), props);
try {
for (int i = 0; i < query.length; i++) {
ResultSet rs = conn.createStatement().executeQuery(query[i]);
assertTrue(rs.next());
assertEquals(result[i], rs.getObject(1));
assertEquals(Boolean.TRUE.equals(result[i]) ? true : false, rs.getBoolean(1));
assertFalse(rs.next());
}
} finally {
conn.close();
}
}


} }

0 comments on commit 8a98324

Please sign in to comment.