alamb commented on code in PR #9216:
URL: https://github.com/apache/arrow-datafusion/pull/9216#discussion_r1487954952
##########
datafusion/expr/src/built_in_function.rs:
##########
@@ -83,8 +82,6 @@ pub enum BuiltinScalarFunction {
Gcd,
/// lcm, Least common multiple
Lcm,
- /// isnan
Review Comment:
One benefit is that the implementation of these functions are consolidated,
rather than having them spread all over
##########
datafusion/functions/src/math/nans.rs:
##########
@@ -0,0 +1,92 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Encoding expressions
+
+use arrow::{
+ datatypes::DataType,
+};
+use datafusion_common::{internal_err, Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+
+use datafusion_expr::TypeSignature::*;
+use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
+use std::any::Any;
+use std::sync::Arc;
+use arrow::array::{ArrayRef, BooleanArray, Float32Array, Float64Array};
+
+#[derive(Debug)]
+pub(super) struct IsNanFunc {
+ signature: Signature,
+}
+
+impl IsNanFunc {
+ pub fn new() -> Self {
+ use DataType::*;
+ Self {
+ signature:
+ Signature::one_of(
+ vec![Exact(vec![Float32]), Exact(vec![Float64])],
+ Volatility::Immutable,
+ )
+ }
+ }
+}
+
+impl ScalarUDFImpl for IsNanFunc {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+ fn name(&self) -> &str {
+ "isnan"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(DataType::Boolean)
+ }
+
+ fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Review Comment:
Likewise all this code is moved from elsewhere, it is not new
##########
datafusion/functions/src/core/nullif.rs:
##########
@@ -15,17 +15,89 @@
// specific language governing permissions and limitations
// under the License.
+//! Encoding expressions
+
+use arrow::{
+ datatypes::DataType,
+};
+use datafusion_common::{internal_err, Result, DataFusionError};
+use datafusion_expr::{ColumnarValue};
+
+use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
+use std::any::Any;
use arrow::array::Array;
use arrow::compute::kernels::cmp::eq;
use arrow::compute::kernels::nullif::nullif;
-use datafusion_common::{internal_err, DataFusionError, Result, ScalarValue};
-use datafusion_expr::ColumnarValue;
+use datafusion_common::{ ScalarValue};
+
+#[derive(Debug)]
+pub(super) struct NullIfFunc {
+ signature: Signature,
+}
+
+/// Currently supported types by the nullif function.
Review Comment:
this code is just moved from various other places in the codebase. There is
no new logic
##########
datafusion/functions/src/macros.rs:
##########
@@ -121,3 +121,42 @@ macro_rules! make_package {
}
};
}
+
+/// Invokes a function on each element of an array and returns the result as a
new array
+///
+/// $ARG: ArrayRef
+/// $NAME: name of the function (for error messages)
+/// $ARGS_TYPE: the type of array to cast the argument to
+/// $RETURN_TYPE: the type of array to return
+/// $FUNC: the function to apply to each element of $ARG
+///
+macro_rules! make_function_scalar_inputs_return_type {
Review Comment:
This is copied (with more documentation) from datafusion-physical_expr. Once
we move all the functions we can remove the original copy
##########
datafusion/functions/src/core/mod.rs:
##########
@@ -15,23 +15,15 @@
// specific language governing permissions and limitations
// under the License.
-use arrow::datatypes::DataType;
+//! "core" DataFusion functions
+
+mod nullif;
Review Comment:
To add new functions, we can add the appropriate module and entry in this
file
##########
datafusion/proto/src/logical_plan/from_proto.rs:
##########
@@ -58,15 +58,15 @@ use datafusion_expr::{
current_time, date_bin, date_part, date_trunc, degrees, digest, ends_with,
exp,
expr::{self, InList, Sort, WindowFunction},
factorial, find_in_set, flatten, floor, from_unixtime, gcd, gen_range,
initcap,
- instr, isnan, iszero, lcm, left, levenshtein, ln, log, log10, log2,
+ instr, iszero, lcm, left, levenshtein, ln, log, log10, log2,
Review Comment:
the expr_fn's are still available (via datafusion_functions exports)
##########
datafusion/sqllogictest/test_files/errors.slt:
##########
@@ -84,8 +84,13 @@ statement error Error during planning: No function matches
the given name and ar
SELECT concat();
# error message for wrong function signature (Uniform: t args all from some
common types)
-statement error Error during planning: No function matches the given name and
argument types 'nullif\(Int64\)'. You might need to add explicit type
casts.\n\tCandidate
functions:\n\tnullif\(Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8,
Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8\)
+statement error
SELECT nullif(1);
+----
+DataFusion error: Failed to coerce arguments for NULLIF
Review Comment:
I improved the error message slightly as well
--
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]