Skip to content

Commit 7322971

Browse files
Merge pull request #94 from data-integrations/feature/CDAP-17386-handle-null-value-0.1
Handle null value correctly to avoid NPE
2 parents 59a2c71 + 4ef95e9 commit 7322971

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

delta-plugins-common/src/main/java/io/cdap/delta/plugin/common/Records.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static StructuredRecord convert(Struct struct) {
161161
Schema fieldSchema = field.getSchema();
162162
fieldSchema = fieldSchema.isNullable() ? fieldSchema.getNonNullable() : fieldSchema;
163163
Schema.LogicalType logicalType = fieldSchema.getLogicalType();
164-
if (logicalType == null) {
164+
if (logicalType == null || val == null) {
165165
builder.set(fieldName, val);
166166
} else {
167167
switch (logicalType) {

mysql-delta-plugins/src/test/java/io/cdap/delta/mysql/MySqlEventReaderIntegrationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ public static void setupClass() throws Exception {
122122
ps.setDate(3, Date.valueOf("1971-01-01"));
123123
ps.addBatch();
124124

125+
ps.setInt(1, 2);
126+
ps.setString(2, "tim");
127+
ps.setDate(3, null);
128+
ps.addBatch();
129+
125130
ps.executeBatch();
126131
}
127132
}
@@ -190,6 +195,18 @@ public void test() throws InterruptedException {
190195
.setDate("bday", LocalDate.ofEpochDay(365))
191196
.build();
192197
Assert.assertEquals(expected, row);
198+
199+
dmlEvent = eventEmitter.getDmlEvents().get(2);
200+
Assert.assertEquals(DMLOperation.INSERT, dmlEvent.getOperation());
201+
Assert.assertEquals(DB, dmlEvent.getDatabase());
202+
Assert.assertEquals(CUSTOMERS_TABLE, dmlEvent.getTable());
203+
row = dmlEvent.getRow();
204+
expected = StructuredRecord.builder(CUSTOMERS_SCHEMA)
205+
.set("id", 2)
206+
.set("name", "tim")
207+
.setDate("bday", null)
208+
.build();
209+
Assert.assertEquals(expected, row);
193210
}
194211

195212
@Test

sqlserver-delta-plugins/src/test/java/io.cdap.delta.sqlserver/SqlServerEventReaderIntegrationTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public static void setupClass() throws Exception {
119119
ps.setDate(3, Date.valueOf("1971-01-01"));
120120
ps.addBatch();
121121

122+
ps.setInt(1, 2);
123+
ps.setString(2, "tim");
124+
ps.setDate(3, null);
125+
ps.addBatch();
126+
122127
ps.executeBatch();
123128
}
124129

@@ -194,6 +199,18 @@ public void test() throws InterruptedException {
194199
.setDate("bday", LocalDate.ofEpochDay(365))
195200
.build();
196201
Assert.assertEquals(expected, row);
202+
203+
dmlEvent = eventEmitter.getDmlEvents().get(2);
204+
Assert.assertEquals(DMLOperation.INSERT, dmlEvent.getOperation());
205+
Assert.assertEquals(DB, dmlEvent.getDatabase());
206+
Assert.assertEquals(CUSTOMERS_TABLE, dmlEvent.getTable());
207+
row = dmlEvent.getRow();
208+
expected = StructuredRecord.builder(CUSTOMERS_SCHEMA)
209+
.set("id", 2)
210+
.set("name", "tim")
211+
.setDate("bday", null)
212+
.build();
213+
Assert.assertEquals(expected, row);
197214
}
198215

199216
@Test

0 commit comments

Comments
 (0)