Skip to content

Commit 5937650

Browse files
committed
fix: validate UDF result row count matches longest input
The bridge now rejects UDFs that return a vector whose length does not match the longest input. Previously a buggy UDF returning a shorter or longer vector would silently corrupt query results.
1 parent a16f336 commit 5937650

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

common/src/main/java/org/apache/comet/udf/CometUdfBridge.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ public static void evaluate(
9393
throw new RuntimeException(
9494
"CometUDF.evaluate() must return a FieldVector, got: " + result.getClass().getName());
9595
}
96+
// Result length must match the longest input. Scalar (length-1) inputs
97+
// are allowed to be shorter, but a vector input bounds the output.
98+
int expectedLen = 0;
99+
for (ValueVector v : inputs) {
100+
expectedLen = Math.max(expectedLen, v.getValueCount());
101+
}
102+
if (result.getValueCount() != expectedLen) {
103+
throw new RuntimeException(
104+
"CometUDF.evaluate() returned "
105+
+ result.getValueCount()
106+
+ " rows, expected "
107+
+ expectedLen);
108+
}
96109
ArrowArray outArr = ArrowArray.wrap(outArrayPtr);
97110
ArrowSchema outSch = ArrowSchema.wrap(outSchemaPtr);
98111
Data.exportVector(allocator, (FieldVector) result, null, outArr, outSch);

0 commit comments

Comments
 (0)