Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify no argument handling #2

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions datafusion/expr/src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ impl ScalarUDF {
pub fn short_circuits(&self) -> bool {
self.inner.short_circuits()
}

pub fn support_randomness(&self) -> bool {
self.inner.support_randomness()
}
}

impl<F> From<F> for ScalarUDF
Expand Down Expand Up @@ -348,7 +344,15 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
)
}

/// Invoke the function without `args` but number of rows, returning the appropriate result
/// Invoke the function without `args` but number of rows, returning the
/// appropriate result
///
/// Note this is different than [`Self::invoke`] in that it is called with
/// the number of rows in the batch.
///
/// For functions that return a constant such as `pi()` the number of rows
/// does not matter. However for functions such as `random()` that return a
/// batch with different values for each row, the number of rows is needed.
fn invoke_no_args(&self, _number_rows: usize) -> Result<ColumnarValue> {
not_impl_err!(
"Function {} does not implement invoke_no_args but called",
Expand Down Expand Up @@ -407,12 +411,6 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
fn short_circuits(&self) -> bool {
false
}

/// Returns true if the function supports randomness, This is useful for functions that need to generate
/// random values for each row. `invoke_no_args` can be called in this case.
fn support_randomness(&self) -> bool {
false
}
}

/// ScalarUDF that adds an alias to the underlying function. It is better to
Expand Down
4 changes: 4 additions & 0 deletions datafusion/functions-array/src/make_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl ScalarUDFImpl for MakeArray {
make_scalar_function(make_array_inner)(args)
}

fn invoke_no_args(&self, _num_rows: usize) -> Result<ColumnarValue> {
self.invoke(&[])
}

fn aliases(&self) -> &[String] {
&self.aliases
}
Expand Down
4 changes: 4 additions & 0 deletions datafusion/functions/src/math/pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl ScalarUDFImpl for PiFunc {
))))
}

fn invoke_no_args(&self, _number_rows: usize) -> Result<ColumnarValue> {
self.invoke(&[])
}

fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {
Ok(Some(vec![Some(true)]))
}
Expand Down
5 changes: 1 addition & 4 deletions datafusion/functions/src/math/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ impl ScalarUDFImpl for RandomFunc {
Ok(Float64)
}

fn support_randomness(&self) -> bool {
true
}

fn invoke_no_args(&self, num_rows: usize) -> Result<ColumnarValue> {
// Since random is volatile, return a different value each row
let mut rng = thread_rng();
let values = std::iter::repeat_with(|| rng.gen_range(0.0..1.0)).take(num_rows);
let array = Float64Array::from_iter_values(values);
Expand Down
4 changes: 0 additions & 4 deletions datafusion/functions/src/string/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ impl ScalarUDFImpl for UuidFunc {
Ok(Utf8)
}

fn support_randomness(&self) -> bool {
true
}

/// Prints random (v4) uuid values per row
/// uuid() = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
fn invoke_no_args(&self, num_rows: usize) -> Result<ColumnarValue> {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-expr/src/scalar_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl PhysicalExpr for ScalarFunctionExpr {
// evaluate the function
match self.fun {
ScalarFunctionDefinition::UDF(ref fun) => {
if fun.support_randomness() {
if inputs.is_empty() {
fun.invoke_no_args(batch.num_rows())
} else {
fun.invoke(&inputs)
Expand Down