PHOENIX-1949 NPE while inserting NULL in a VARCHAR array using UPSERT stmt
(Ram)
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
index b7a2ad2..80396a6 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
@@ -870,6 +870,33 @@
}
@Test
+ public void testArrayWithVarCharArray() throws Exception {
+ Connection conn;
+ PreparedStatement stmt;
+ ResultSet rs;
+ long ts = nextTimestamp();
+ Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+ props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+ conn = DriverManager.getConnection(getUrl(), props);
+ conn.createStatement().execute("CREATE TABLE t ( k VARCHAR PRIMARY KEY, a VARCHAR ARRAY[])");
+ conn.close();
+
+ props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+ conn = DriverManager.getConnection(getUrl(), props);
+ stmt = conn.prepareStatement("UPSERT INTO t VALUES('a',ARRAY['a',null])");
+ int res = stmt.executeUpdate();
+ assertEquals(1, res);
+ conn.commit();
+ conn.close();
+ props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+ conn = DriverManager.getConnection(getUrl(), props);
+ rs = conn.createStatement().executeQuery("SELECT ARRAY_ELEM(a,2) FROM t");
+ assertTrue(rs.next());
+ assertEquals(null, rs.getString(1));
+ conn.close();
+ }
+
+ @Test
public void testArraySelectSingleArrayElemWithCast() throws Exception {
Connection conn;
PreparedStatement stmt;
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
index 6b26197..4170253 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
@@ -104,12 +104,12 @@
@Override
public Object toObject(byte[] bytes, int offset, int length, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) {
- if (!actualType.isCoercibleTo(this)) { // TODO: have isCoercibleTo that takes bytes, offset?
- throwConstraintViolationException(actualType,this);
- }
if (length == 0) {
return null;
}
+ if (!actualType.isCoercibleTo(this)) {
+ throwConstraintViolationException(actualType, this);
+ }
length = StringUtil.getUnpaddedCharLength(bytes, offset, length, sortOrder);
if (sortOrder == SortOrder.DESC) {
bytes = SortOrder.invert(bytes, offset, length);
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
index fa3dbad..2575115 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
@@ -59,12 +59,12 @@
@Override
public Object toObject(byte[] bytes, int offset, int length, PDataType actualType,
SortOrder sortOrder, Integer maxLength, Integer scale) {
- if (!actualType.isCoercibleTo(this)) {
- throwConstraintViolationException(actualType, this);
- }
if (length == 0) {
return null;
}
+ if (!actualType.isCoercibleTo(this)) {
+ throwConstraintViolationException(actualType, this);
+ }
if (sortOrder == SortOrder.DESC) {
bytes = SortOrder.invert(bytes, offset, length);
offset = 0;