Jefffrey commented on code in PR #19871:
URL: https://github.com/apache/datafusion/pull/19871#discussion_r2703291341
##########
datafusion/functions/src/math/signum.rs:
##########
@@ -98,41 +97,58 @@ impl ScalarUDFImpl for SignumFunc {
}
fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
- make_scalar_function(signum, vec![])(&args.args)
+ let return_type = args.return_type().clone();
+ let [arg] = take_function_args(self.name(), args.args)?;
+
+ match arg {
+ ColumnarValue::Scalar(scalar) => {
+ if scalar.is_null() {
+ return ColumnarValue::Scalar(ScalarValue::Null)
+ .cast_to(&return_type, None);
+ }
+
+ match scalar {
+ ScalarValue::Float64(Some(v)) => {
+ let result = if v == 0.0 { 0.0 } else { v.signum() };
+
Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(result))))
+ }
+ ScalarValue::Float32(Some(v)) => {
+ let result = if v == 0.0 { 0.0 } else { v.signum() };
+
Ok(ColumnarValue::Scalar(ScalarValue::Float32(Some(result))))
+ }
+ _ => {
+ internal_err!(
+ "Unexpected scalar type for signum: {:?}",
+ scalar.data_type()
+ )
+ }
+ }
+ }
+ ColumnarValue::Array(array) => match array.data_type() {
+ Float64 => Ok(ColumnarValue::Array(Arc::new(
+ array.as_primitive::<Float64Type>().unary::<_,
Float64Type>(
+ |x: f64| {
+ if x == 0.0 { 0.0 } else { x.signum() }
+ },
+ ),
+ ))),
+ Float32 => Ok(ColumnarValue::Array(Arc::new(
+ array.as_primitive::<Float32Type>().unary::<_,
Float32Type>(
+ |x: f32| {
+ if x == 0.0 { 0.0 } else { x.signum() }
+ },
+ ),
+ ))),
+ other => exec_err!("Unsupported data type {other:?} for
function signum"),
Review Comment:
exec err -> things that can happen in normal execution, such as invalid
value to a function (e.g. trying to get ascii character from an integer input,
and we input a value that doesnt have a corresponding character like `99999`)
internal err -> things that shouldn't normally happen, aka occur if some
other bug in datafusion allowed this code path to occur
in this case, the signature should already guard us to only have f32/f64
inputs; therefore if at this point we find an array not of that type, then
something went wrong in type coercion/signature code and its an internal bug
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]