Skip to content

Commit cf187a3

Browse files
committed
test(udf): cover null/wrong-rows/wrong-type/exception contract violations
1 parent 742629c commit cf187a3

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

‎core/src/test/java/org/apache/datafusion/ScalarUdfTest.java‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,147 @@ void addOne_invokedTwiceInOneSession_executesIndependently() throws Exception {
210210
}
211211
}
212212
}
213+
214+
static final class ReturnsNull implements ScalarUdf {
215+
@Override
216+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
217+
return null;
218+
}
219+
}
220+
221+
@Test
222+
void udfReturningNull_surfacesIllegalStateException() {
223+
try (SessionContext ctx = new SessionContext();
224+
BufferAllocator allocator = new RootAllocator()) {
225+
ctx.registerUdf(
226+
"bad_null",
227+
new ReturnsNull(),
228+
new ArrowType.Int(32, true),
229+
List.of(new ArrowType.Int(32, true)),
230+
Volatility.IMMUTABLE);
231+
RuntimeException ex =
232+
org.junit.jupiter.api.Assertions.assertThrows(
233+
RuntimeException.class,
234+
() -> {
235+
try (DataFrame df = ctx.sql("SELECT bad_null(CAST(1 AS INT))");
236+
ArrowReader r = df.collect(allocator)) {
237+
while (r.loadNextBatch()) {}
238+
}
239+
});
240+
org.junit.jupiter.api.Assertions.assertTrue(
241+
ex.getMessage().contains("returned null"),
242+
"expected error to mention 'returned null', got: " + ex.getMessage());
243+
}
244+
}
245+
246+
static final class WrongRowCount implements ScalarUdf {
247+
@Override
248+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
249+
IntVector in = (IntVector) args.get(0);
250+
IntVector out = new IntVector("out", allocator);
251+
out.allocateNew(in.getValueCount() + 1); // off by one
252+
for (int i = 0; i < in.getValueCount() + 1; i++) out.set(i, 0);
253+
out.setValueCount(in.getValueCount() + 1);
254+
return out;
255+
}
256+
}
257+
258+
@Test
259+
void udfReturningWrongRowCount_surfacesIllegalStateException() {
260+
try (SessionContext ctx = new SessionContext();
261+
BufferAllocator allocator = new RootAllocator()) {
262+
ctx.registerUdf(
263+
"bad_rows",
264+
new WrongRowCount(),
265+
new ArrowType.Int(32, true),
266+
List.of(new ArrowType.Int(32, true)),
267+
Volatility.IMMUTABLE);
268+
RuntimeException ex =
269+
org.junit.jupiter.api.Assertions.assertThrows(
270+
RuntimeException.class,
271+
() -> {
272+
try (DataFrame df = ctx.sql("SELECT bad_rows(CAST(1 AS INT))");
273+
ArrowReader r = df.collect(allocator)) {
274+
while (r.loadNextBatch()) {}
275+
}
276+
});
277+
org.junit.jupiter.api.Assertions.assertTrue(
278+
ex.getMessage().contains("expected") && ex.getMessage().contains("rows"),
279+
"expected error to mention row mismatch, got: " + ex.getMessage());
280+
}
281+
}
282+
283+
static final class WrongType implements ScalarUdf {
284+
@Override
285+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
286+
// Declared return type is Int32; return Float64.
287+
org.apache.arrow.vector.Float8Vector out =
288+
new org.apache.arrow.vector.Float8Vector("out", allocator);
289+
out.allocateNew(args.get(0).getValueCount());
290+
for (int i = 0; i < args.get(0).getValueCount(); i++) out.set(i, 0.0);
291+
out.setValueCount(args.get(0).getValueCount());
292+
return out;
293+
}
294+
}
295+
296+
@Test
297+
void udfReturningWrongType_surfacesTypeMismatch() {
298+
try (SessionContext ctx = new SessionContext();
299+
BufferAllocator allocator = new RootAllocator()) {
300+
ctx.registerUdf(
301+
"bad_type",
302+
new WrongType(),
303+
new ArrowType.Int(32, true),
304+
List.of(new ArrowType.Int(32, true)),
305+
Volatility.IMMUTABLE);
306+
RuntimeException ex =
307+
org.junit.jupiter.api.Assertions.assertThrows(
308+
RuntimeException.class,
309+
() -> {
310+
try (DataFrame df = ctx.sql("SELECT bad_type(CAST(1 AS INT))");
311+
ArrowReader r = df.collect(allocator)) {
312+
while (r.loadNextBatch()) {}
313+
}
314+
});
315+
org.junit.jupiter.api.Assertions.assertTrue(
316+
ex.getMessage().toLowerCase().contains("type"),
317+
"expected error to mention type mismatch, got: " + ex.getMessage());
318+
}
319+
}
320+
321+
static final class ThrowsIAE implements ScalarUdf {
322+
@Override
323+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
324+
throw new IllegalArgumentException("custom boom from UDF");
325+
}
326+
}
327+
328+
@Test
329+
void udfThrowingException_propagatesClassAndMessage() {
330+
try (SessionContext ctx = new SessionContext();
331+
BufferAllocator allocator = new RootAllocator()) {
332+
ctx.registerUdf(
333+
"boom",
334+
new ThrowsIAE(),
335+
new ArrowType.Int(32, true),
336+
List.of(new ArrowType.Int(32, true)),
337+
Volatility.IMMUTABLE);
338+
RuntimeException ex =
339+
org.junit.jupiter.api.Assertions.assertThrows(
340+
RuntimeException.class,
341+
() -> {
342+
try (DataFrame df = ctx.sql("SELECT boom(CAST(1 AS INT))");
343+
ArrowReader r = df.collect(allocator)) {
344+
while (r.loadNextBatch()) {}
345+
}
346+
});
347+
String msg = ex.getMessage();
348+
org.junit.jupiter.api.Assertions.assertTrue(
349+
msg.contains("IllegalArgumentException"),
350+
"expected class name in error, got: " + msg);
351+
org.junit.jupiter.api.Assertions.assertTrue(
352+
msg.contains("custom boom from UDF"),
353+
"expected user message in error, got: " + msg);
354+
}
355+
}
213356
}

0 commit comments

Comments
 (0)