5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-03 22:20:52 +08:00

MAPREDUCE-1313. Fix NPE in Sqoop when table with null fields uses escape

during import. Contributed by Aaron Kimball

From: Christopher Douglas <cdouglas@apache.org>

git-svn-id: https://svn.apache.org/repos/asf/incubator/sqoop/trunk@1149851 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew Bayer 2011-07-22 20:03:31 +00:00
parent c7f64e4f8c
commit 6174268d28
3 changed files with 23 additions and 2 deletions

View File

@ -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);

View File

@ -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(

View File

@ -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() {