NIFI-2602: Fixed NPE in SelectHiveQL when CSV output and null column value

This closes #898.
This commit is contained in:
Matt Burgess 2016-08-18 19:18:39 -04:00 committed by Pierre Villard
parent 6b5950647a
commit 46b81058c7
2 changed files with 21 additions and 7 deletions

View File

@ -320,10 +320,19 @@ public class HiveJdbcCommon {
case NCHAR:
case NVARCHAR:
case VARCHAR:
rowValues.add("\"" + StringEscapeUtils.escapeCsv(rs.getString(i)) + "\"");
String valueString = rs.getString(i);
if (valueString != null) {
rowValues.add("\"" + StringEscapeUtils.escapeCsv(valueString) + "\"");
} else {
rowValues.add("");
}
break;
default:
rowValues.add(value.toString());
if (value != null) {
rowValues.add(value.toString());
} else {
rowValues.add("");
}
}
}
// Write row values

View File

@ -205,9 +205,11 @@ public class TestSelectHiveQL {
Random rng = new Random(53496);
final int nrOfRows = 100;
stmt.executeUpdate("insert into persons values (1, 'Joe Smith', " + rng.nextInt(469947) + ")");
for (int i = 2; i <= nrOfRows; i++) {
for (int i = 2; i < nrOfRows; i++) {
stmt.executeUpdate("insert into persons values (" + i + ", 'Someone Else', " + rng.nextInt(469947) + ")");
}
stmt.executeUpdate("insert into persons values (" + nrOfRows + ", 'Last Person', NULL)");
LOGGER.info("test data loaded");
runner.setProperty(SelectHiveQL.HIVEQL_SELECT_QUERY, query);
@ -254,10 +256,13 @@ public class TestSelectHiveQL {
while ((line = br.readLine()) != null) {
recordsFromStream++;
String[] values = line.split(",");
assertEquals(3, values.length);
// Assert the name has been quoted
assertTrue(values[1].startsWith("\""));
assertTrue(values[1].endsWith("\""));
if(recordsFromStream < (nrOfRows - 10)) {
assertEquals(3, values.length);
assertTrue(values[1].startsWith("\""));
assertTrue(values[1].endsWith("\""));
} else {
assertEquals(2, values.length); // Middle value is null
}
}
}
assertEquals(nrOfRows - 10, recordsFromStream);