GitHub user finlaydotb closed a discussion: How to implement a custom unnest
function without getting "UDF returned a different number of rows than
expected" error
An unnest function by definition takes one value and has the possibility to
generate multiple rows.
```
> select unnest(make_array(1, 2, 3, 4, 5)) as unnested;
+----------+
| unnested |
+----------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----------+
```
For some usecase I am trying to implement a custom unnest but I keep running
into this error
```
called `Result::unwrap()` on an `Err` value: Internal("UDF returned a different
number of rows than expected. Expected: 3, Got: 6")
```
The implementation looks something like this. This is not the custom logic I
need but just to illustrate the issue
```
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
let args = ColumnarValue::values_to_arrays(args)?;
let values = datafusion::common::cast::as_int32_array(&args[0])?;
let mut builder = Int32Array::builder(args[0].len());
values.into_iter().for_each(|value| {
match value {
Some(value) => builder.append_values(&vec![value, value],
&vec![true, true]),
None => builder.append_null(),
}
});
Ok(ColumnarValue::Array(
Arc::new(builder.finish()) as ArrayRef,
))
}
```
GitHub link: https://github.com/apache/datafusion/discussions/13234
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]