Skip to content

Commit e220003

Browse files
authored
Merge pull request #111 from data-integrations/bugfix/PLUGIN-691-null-time
[PLUGIN-691] Fix NPE when working with nullable time field in MSSQL
2 parents b0c399f + 33c053f commit e220003

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

mssql-plugin/src/main/java/io/cdap/plugin/mssql/SqlServerSourceDBRecord.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import io.cdap.plugin.db.ColumnType;
2222
import io.cdap.plugin.db.DBRecord;
2323
import io.cdap.plugin.db.SchemaReader;
24-
2524
import java.sql.ResultSet;
2625
import java.sql.SQLException;
26+
import java.sql.Timestamp;
2727
import java.sql.Types;
2828
import java.util.List;
2929

3030
/**
31-
* SQL Server Source implementation {@link org.apache.hadoop.mapreduce.lib.db.DBWritable} and
32-
* {@link org.apache.hadoop.io.Writable}.
31+
* SQL Server Source implementation {@link org.apache.hadoop.mapreduce.lib.db.DBWritable} and {@link
32+
* org.apache.hadoop.io.Writable}.
3333
*/
3434
public class SqlServerSourceDBRecord extends DBRecord {
3535

@@ -42,14 +42,17 @@ public SqlServerSourceDBRecord() {
4242
}
4343

4444
@Override
45-
protected void handleField(ResultSet resultSet, StructuredRecord.Builder recordBuilder, Schema.Field field,
46-
int columnIndex, int sqlType, int sqlPrecision, int sqlScale) throws SQLException {
45+
protected void handleField(ResultSet resultSet, StructuredRecord.Builder recordBuilder,
46+
Schema.Field field,
47+
int columnIndex, int sqlType, int sqlPrecision, int sqlScale) throws SQLException {
4748
switch (sqlType) {
4849
case Types.TIME:
4950
// Handle reading SQL Server 'TIME' data type to avoid accuracy loss.
5051
// 'TIME' data type has the accuracy of 100 nanoseconds(1 millisecond in Informatica)
5152
// but reading via 'getTime' and 'getObject' will round value to second.
52-
recordBuilder.setTime(field.getName(), resultSet.getTimestamp(columnIndex).toLocalDateTime().toLocalTime());
53+
final Timestamp timestamp = resultSet.getTimestamp(columnIndex);
54+
recordBuilder.setTime(field.getName(),
55+
timestamp == null ? null : timestamp.toLocalDateTime().toLocalTime());
5356
break;
5457
case SqlServerSourceSchemaReader.DATETIME_OFFSET_TYPE:
5558
recordBuilder.set(field.getName(), resultSet.getString(columnIndex));

0 commit comments

Comments
 (0)