Skip to content

Commit 4ef01fd

Browse files
committed
test(udf): cover multi-UDF, cross-session, and multi-row lifecycle
1 parent cf187a3 commit 4ef01fd

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,92 @@ void udfThrowingException_propagatesClassAndMessage() {
353353
"expected user message in error, got: " + msg);
354354
}
355355
}
356+
357+
@Test
358+
void twoUdfsInOneSession_bothCallable() throws Exception {
359+
try (SessionContext ctx = new SessionContext();
360+
BufferAllocator allocator = new RootAllocator()) {
361+
ctx.registerUdf(
362+
"add_one",
363+
new AddOne(),
364+
new ArrowType.Int(32, true),
365+
List.of(new ArrowType.Int(32, true)),
366+
Volatility.IMMUTABLE);
367+
ctx.registerUdf(
368+
"java_square",
369+
new Square(),
370+
new ArrowType.FloatingPoint(org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE),
371+
List.of(
372+
new ArrowType.FloatingPoint(
373+
org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE)),
374+
Volatility.IMMUTABLE);
375+
376+
try (DataFrame df =
377+
ctx.sql(
378+
"SELECT add_one(CAST(10 AS INT)) AS a, java_square(CAST(3 AS DOUBLE)) AS b");
379+
ArrowReader r = df.collect(allocator)) {
380+
assertEquals(true, r.loadNextBatch());
381+
VectorSchemaRoot root = r.getVectorSchemaRoot();
382+
IntVector a = (IntVector) root.getVector("a");
383+
org.apache.arrow.vector.Float8Vector b =
384+
(org.apache.arrow.vector.Float8Vector) root.getVector("b");
385+
assertEquals(11, a.get(0));
386+
assertEquals(9.0, b.get(0), 0.0);
387+
}
388+
}
389+
}
390+
391+
@Test
392+
void registerSameNameAfterCloseInNewSession_works() throws Exception {
393+
for (int round = 0; round < 2; round++) {
394+
try (SessionContext ctx = new SessionContext();
395+
BufferAllocator allocator = new RootAllocator()) {
396+
ctx.registerUdf(
397+
"add_one",
398+
new AddOne(),
399+
new ArrowType.Int(32, true),
400+
List.of(new ArrowType.Int(32, true)),
401+
Volatility.IMMUTABLE);
402+
try (DataFrame df = ctx.sql("SELECT add_one(CAST(7 AS INT))");
403+
ArrowReader r = df.collect(allocator)) {
404+
assertEquals(true, r.loadNextBatch());
405+
IntVector v = (IntVector) r.getVectorSchemaRoot().getVector(0);
406+
assertEquals(8, v.get(0));
407+
}
408+
}
409+
}
410+
}
411+
412+
@Test
413+
void udfAppliedToMultiRowQuery_processesAllRows() throws Exception {
414+
try (SessionContext ctx = new SessionContext();
415+
BufferAllocator allocator = new RootAllocator()) {
416+
ctx.registerUdf(
417+
"add_one",
418+
new AddOne(),
419+
new ArrowType.Int(32, true),
420+
List.of(new ArrowType.Int(32, true)),
421+
Volatility.IMMUTABLE);
422+
String values =
423+
java.util.stream.IntStream.rangeClosed(1, 100)
424+
.mapToObj(i -> "(CAST(" + i + " AS INT))")
425+
.collect(java.util.stream.Collectors.joining(", "));
426+
try (DataFrame df =
427+
ctx.sql("SELECT add_one(x) AS y FROM (VALUES " + values + ") AS t(x) ORDER BY y");
428+
ArrowReader r = df.collect(allocator)) {
429+
long total = 0;
430+
long rows = 0;
431+
while (r.loadNextBatch()) {
432+
IntVector y = (IntVector) r.getVectorSchemaRoot().getVector("y");
433+
for (int i = 0; i < y.getValueCount(); i++) {
434+
total += y.get(i);
435+
rows++;
436+
}
437+
}
438+
assertEquals(100, rows);
439+
// Sum of 2..101 = (2+101)*100/2 = 5150
440+
assertEquals(5150L, total);
441+
}
442+
}
443+
}
356444
}

0 commit comments

Comments
 (0)