From 46b81058c7fa0914bf5df34d82430bb77bffa6c5 Mon Sep 17 00:00:00 2001 From: Matt Burgess Date: Thu, 18 Aug 2016 19:18:39 -0400 Subject: [PATCH] NIFI-2602: Fixed NPE in SelectHiveQL when CSV output and null column value This closes #898. --- .../org/apache/nifi/util/hive/HiveJdbcCommon.java | 13 +++++++++++-- .../nifi/processors/hive/TestSelectHiveQL.java | 15 ++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/main/java/org/apache/nifi/util/hive/HiveJdbcCommon.java b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/main/java/org/apache/nifi/util/hive/HiveJdbcCommon.java index 5de36dc192..fb4ac845ff 100644 --- a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/main/java/org/apache/nifi/util/hive/HiveJdbcCommon.java +++ b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/main/java/org/apache/nifi/util/hive/HiveJdbcCommon.java @@ -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 diff --git a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/test/java/org/apache/nifi/processors/hive/TestSelectHiveQL.java b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/test/java/org/apache/nifi/processors/hive/TestSelectHiveQL.java index 5386030148..8142e3f0d8 100644 --- a/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/test/java/org/apache/nifi/processors/hive/TestSelectHiveQL.java +++ b/nifi-nar-bundles/nifi-hive-bundle/nifi-hive-processors/src/test/java/org/apache/nifi/processors/hive/TestSelectHiveQL.java @@ -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);