Skip to content

Commit 742629c

Browse files
committed
test(udf): cover concat, square, and repeated invocations
1 parent 264bcb0 commit 742629c

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,131 @@ void addOne_overConstantTable_returnsIncrementedValues() throws Exception {
8383
}
8484
}
8585
}
86+
87+
/** Concatenates two Utf8 columns. */
88+
static final class Concat implements ScalarUdf {
89+
@Override
90+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
91+
org.apache.arrow.vector.VarCharVector left = (org.apache.arrow.vector.VarCharVector) args.get(0);
92+
org.apache.arrow.vector.VarCharVector right = (org.apache.arrow.vector.VarCharVector) args.get(1);
93+
org.apache.arrow.vector.VarCharVector out =
94+
new org.apache.arrow.vector.VarCharVector("concat_out", allocator);
95+
int n = left.getValueCount();
96+
out.allocateNew(n);
97+
for (int i = 0; i < n; i++) {
98+
if (left.isNull(i) || right.isNull(i)) {
99+
out.setNull(i);
100+
} else {
101+
byte[] l = left.get(i);
102+
byte[] r = right.get(i);
103+
byte[] both = new byte[l.length + r.length];
104+
System.arraycopy(l, 0, both, 0, l.length);
105+
System.arraycopy(r, 0, both, l.length, r.length);
106+
out.setSafe(i, both);
107+
}
108+
}
109+
out.setValueCount(n);
110+
return out;
111+
}
112+
}
113+
114+
@Test
115+
void concat_overVarCharColumns_concatenatesValues() throws Exception {
116+
try (SessionContext ctx = new SessionContext();
117+
BufferAllocator allocator = new RootAllocator()) {
118+
ctx.registerUdf(
119+
"java_concat",
120+
new Concat(),
121+
new ArrowType.Utf8(),
122+
List.of(new ArrowType.Utf8(), new ArrowType.Utf8()),
123+
Volatility.IMMUTABLE);
124+
125+
try (DataFrame df =
126+
ctx.sql(
127+
"SELECT java_concat(a, b) AS c FROM (VALUES ('foo','bar'),('hello','!')) AS t(a, b)");
128+
ArrowReader r = df.collect(allocator)) {
129+
assertEquals(true, r.loadNextBatch());
130+
VectorSchemaRoot root = r.getVectorSchemaRoot();
131+
org.apache.arrow.vector.VarCharVector c =
132+
(org.apache.arrow.vector.VarCharVector) root.getVector("c");
133+
assertEquals(2, c.getValueCount());
134+
assertEquals("foobar", new String(c.get(0)));
135+
assertEquals("hello!", new String(c.get(1)));
136+
}
137+
}
138+
}
139+
140+
/** Squares a Float64 column. */
141+
static final class Square implements ScalarUdf {
142+
@Override
143+
public FieldVector evaluate(BufferAllocator allocator, List<FieldVector> args) {
144+
org.apache.arrow.vector.Float8Vector in = (org.apache.arrow.vector.Float8Vector) args.get(0);
145+
org.apache.arrow.vector.Float8Vector out =
146+
new org.apache.arrow.vector.Float8Vector("square_out", allocator);
147+
int n = in.getValueCount();
148+
out.allocateNew(n);
149+
for (int i = 0; i < n; i++) {
150+
if (in.isNull(i)) {
151+
out.setNull(i);
152+
} else {
153+
double v = in.get(i);
154+
out.set(i, v * v);
155+
}
156+
}
157+
out.setValueCount(n);
158+
return out;
159+
}
160+
}
161+
162+
@Test
163+
void square_overFloat64Column_squaresValues() throws Exception {
164+
try (SessionContext ctx = new SessionContext();
165+
BufferAllocator allocator = new RootAllocator()) {
166+
ctx.registerUdf(
167+
"java_square",
168+
new Square(),
169+
new ArrowType.FloatingPoint(org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE),
170+
List.of(
171+
new ArrowType.FloatingPoint(
172+
org.apache.arrow.vector.types.FloatingPointPrecision.DOUBLE)),
173+
Volatility.IMMUTABLE);
174+
175+
try (DataFrame df =
176+
ctx.sql("SELECT java_square(x) AS y FROM (VALUES (2.0),(3.5)) AS t(x)");
177+
ArrowReader r = df.collect(allocator)) {
178+
assertEquals(true, r.loadNextBatch());
179+
VectorSchemaRoot root = r.getVectorSchemaRoot();
180+
org.apache.arrow.vector.Float8Vector y =
181+
(org.apache.arrow.vector.Float8Vector) root.getVector("y");
182+
assertEquals(2, y.getValueCount());
183+
assertEquals(4.0, y.get(0), 0.0);
184+
assertEquals(12.25, y.get(1), 0.0);
185+
}
186+
}
187+
}
188+
189+
@Test
190+
void addOne_invokedTwiceInOneSession_executesIndependently() throws Exception {
191+
// Re-running the same UDF query twice exercises that GlobalRefs and JNI state
192+
// don't accumulate across invocations within a session.
193+
try (SessionContext ctx = new SessionContext();
194+
BufferAllocator allocator = new RootAllocator()) {
195+
ctx.registerUdf(
196+
"add_one",
197+
new AddOne(),
198+
new ArrowType.Int(32, true),
199+
List.of(new ArrowType.Int(32, true)),
200+
Volatility.IMMUTABLE);
201+
202+
for (int run = 0; run < 2; run++) {
203+
try (DataFrame df = ctx.sql("SELECT add_one(CAST(5 AS INT)) AS y");
204+
ArrowReader r = df.collect(allocator)) {
205+
assertEquals(true, r.loadNextBatch());
206+
IntVector y = (IntVector) r.getVectorSchemaRoot().getVector("y");
207+
assertEquals(1, y.getValueCount());
208+
assertEquals(6, y.get(0));
209+
}
210+
}
211+
}
212+
}
86213
}

0 commit comments

Comments
 (0)