From 6174268d28ff0c0638fa8db39fb1a1e943daa285 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 22 Jul 2011 20:03:31 +0000 Subject: [PATCH] MAPREDUCE-1313. Fix NPE in Sqoop when table with null fields uses escape during import. Contributed by Aaron Kimball From: Christopher Douglas git-svn-id: https://svn.apache.org/repos/asf/incubator/sqoop/trunk@1149851 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/hadoop/sqoop/lib/FieldFormatter.java | 4 ++++ .../org/apache/hadoop/sqoop/TestAllTables.java | 17 +++++++++++++++-- .../hadoop/sqoop/lib/TestFieldFormatter.java | 4 ++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/hadoop/sqoop/lib/FieldFormatter.java b/src/java/org/apache/hadoop/sqoop/lib/FieldFormatter.java index 95d40104..a1b67421 100644 --- a/src/java/org/apache/hadoop/sqoop/lib/FieldFormatter.java +++ b/src/java/org/apache/hadoop/sqoop/lib/FieldFormatter.java @@ -57,6 +57,10 @@ public static final String escapeAndEnclose(String str, String escape, String en boolean escapingLegal = (null != escape && escape.length() > 0 && !escape.equals("\000")); String withEscapes; + if (null == str) { + return null; + } + if (escapingLegal) { // escaping is legal. Escape any instances of the escape char itself withEscapes = str.replace(escape, escape + escape); diff --git a/src/test/org/apache/hadoop/sqoop/TestAllTables.java b/src/test/org/apache/hadoop/sqoop/TestAllTables.java index 8e31a547..31c5167d 100644 --- a/src/test/org/apache/hadoop/sqoop/TestAllTables.java +++ b/src/test/org/apache/hadoop/sqoop/TestAllTables.java @@ -58,6 +58,8 @@ public class TestAllTables extends ImportJobTestCase { args.add(HsqldbTestServer.getUrl()); args.add("--num-mappers"); args.add("1"); + args.add("--escaped-by"); + args.add("\\"); return args.toArray(new String[0]); } @@ -86,9 +88,18 @@ public void setUp() { // create two tables. this.expectedStrings.add("A winner"); this.expectedStrings.add("is you!"); + this.expectedStrings.add(null); + int i = 0; for (String expectedStr: this.expectedStrings) { - this.createTableForColType("VARCHAR(32) PRIMARY KEY", "'" + expectedStr + "'"); + String wrappedStr = null; + if (expectedStr != null) { + wrappedStr = "'" + expectedStr + "'"; + } + + String [] types = { "INT NOT NULL PRIMARY KEY", "VARCHAR(32)" }; + String [] vals = { Integer.toString(i++) , wrappedStr }; + this.createTableWithColTypes(types, vals); this.tableNames.add(this.getTableName()); this.removeTableDir(); incrementTableNum(); @@ -100,13 +111,15 @@ public void testMultiTableImport() throws IOException { runImport(argv); Path warehousePath = new Path(this.getWarehouseDir()); + int i = 0; for (String tableName : this.tableNames) { Path tablePath = new Path(warehousePath, tableName); Path filePath = new Path(tablePath, "part-m-00000"); // dequeue the expected value for this table. This // list has the same order as the tableNames list. - String expectedVal = this.expectedStrings.get(0); + String expectedVal = Integer.toString(i++) + "," + + this.expectedStrings.get(0); this.expectedStrings.remove(0); BufferedReader reader = new BufferedReader( diff --git a/src/test/org/apache/hadoop/sqoop/lib/TestFieldFormatter.java b/src/test/org/apache/hadoop/sqoop/lib/TestFieldFormatter.java index c29e80f3..661a9ac9 100644 --- a/src/test/org/apache/hadoop/sqoop/lib/TestFieldFormatter.java +++ b/src/test/org/apache/hadoop/sqoop/lib/TestFieldFormatter.java @@ -37,6 +37,10 @@ public void testAllEmpty() { public void testNullArgs() { String result = FieldFormatter.escapeAndEnclose("", null, null, null, false); assertEquals("", result); + + char [] encloseFor = { '\"' }; + assertNull(FieldFormatter.escapeAndEnclose(null, "\\", "\"", encloseFor, + false)); } public void testBasicStr() {