Skip to content

Commit b0e3e45

Browse files
committed
make StructInternalRow null safe
1 parent 7cfe754 commit b0e3e45

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,29 @@ public double getDouble(int ordinal) {
135135

136136
@Override
137137
public Decimal getDecimal(int ordinal, int precision, int scale) {
138+
return isNullAt(ordinal) ? null : getDecimalInternal(ordinal, precision, scale);
139+
}
140+
141+
private Decimal getDecimalInternal(int ordinal, int precision, int scale) {
138142
return Decimal.apply(struct.get(ordinal, BigDecimal.class));
139143
}
140144

141145
@Override
142146
public UTF8String getUTF8String(int ordinal) {
147+
return isNullAt(ordinal) ? null : getUTF8StringInternal(ordinal);
148+
}
149+
150+
private UTF8String getUTF8StringInternal(int ordinal) {
143151
CharSequence seq = struct.get(ordinal, CharSequence.class);
144152
return UTF8String.fromString(seq.toString());
145153
}
146154

147155
@Override
148156
public byte[] getBinary(int ordinal) {
157+
return isNullAt(ordinal) ? null : getBinaryInternal(ordinal);
158+
}
159+
160+
private byte[] getBinaryInternal(int ordinal) {
149161
Object bytes = struct.get(ordinal, Object.class);
150162

151163
// should only be either ByteBuffer or byte[]
@@ -165,20 +177,32 @@ public CalendarInterval getInterval(int ordinal) {
165177

166178
@Override
167179
public InternalRow getStruct(int ordinal, int numFields) {
180+
return isNullAt(ordinal) ? null : getStructInternal(ordinal, numFields);
181+
}
182+
183+
private InternalRow getStructInternal(int ordinal, int numFields) {
168184
return new StructInternalRow(
169185
type.fields().get(ordinal).type().asStructType(),
170186
struct.get(ordinal, StructLike.class));
171187
}
172188

173189
@Override
174190
public ArrayData getArray(int ordinal) {
191+
return isNullAt(ordinal) ? null : getArrayInternal(ordinal);
192+
}
193+
194+
private ArrayData getArrayInternal(int ordinal) {
175195
return collectionToArrayData(
176196
type.fields().get(ordinal).type().asListType().elementType(),
177197
struct.get(ordinal, Collection.class));
178198
}
179199

180200
@Override
181201
public MapData getMap(int ordinal) {
202+
return isNullAt(ordinal) ? null : getMapInternal(ordinal);
203+
}
204+
205+
private MapData getMapInternal(int ordinal) {
182206
return mapToMapData(type.fields().get(ordinal).type().asMapType(), struct.get(ordinal, Map.class));
183207
}
184208

@@ -194,22 +218,22 @@ public Object get(int ordinal, DataType dataType) {
194218
} else if (dataType instanceof LongType) {
195219
return getLong(ordinal);
196220
} else if (dataType instanceof StringType) {
197-
return getUTF8String(ordinal);
221+
return getUTF8StringInternal(ordinal);
198222
} else if (dataType instanceof FloatType) {
199223
return getFloat(ordinal);
200224
} else if (dataType instanceof DoubleType) {
201225
return getDouble(ordinal);
202226
} else if (dataType instanceof DecimalType) {
203227
DecimalType decimalType = (DecimalType) dataType;
204-
return getDecimal(ordinal, decimalType.precision(), decimalType.scale());
228+
return getDecimalInternal(ordinal, decimalType.precision(), decimalType.scale());
205229
} else if (dataType instanceof BinaryType) {
206-
return getBinary(ordinal);
230+
return getBinaryInternal(ordinal);
207231
} else if (dataType instanceof StructType) {
208-
return getStruct(ordinal, ((StructType) dataType).size());
232+
return getStructInternal(ordinal, ((StructType) dataType).size());
209233
} else if (dataType instanceof ArrayType) {
210-
return getArray(ordinal);
234+
return getArrayInternal(ordinal);
211235
} else if (dataType instanceof MapType) {
212-
return getMap(ordinal);
236+
return getMapInternal(ordinal);
213237
} else if (dataType instanceof BooleanType) {
214238
return getBoolean(ordinal);
215239
} else if (dataType instanceof ByteType) {

0 commit comments

Comments
 (0)