You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by al...@apache.org on 2024/08/05 18:32:19 UTC

(datafusion) branch main updated: Pass scalar to `eq` inside `nullif` (#11697)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new c6f0d3cac9 Pass scalar to `eq` inside `nullif`  (#11697)
c6f0d3cac9 is described below

commit c6f0d3cac93ef1436313160f1dba878745d693bb
Author: Simon Vandel Sillesen <si...@gmail.com>
AuthorDate: Mon Aug 5 20:32:15 2024 +0200

    Pass scalar to `eq` inside `nullif`  (#11697)
    
    * Properly specialize nullif for scalar (3x faster)
    
    * missed feature flag
    
    * fix test
    
    * extract
    
    * dodes -> does
    
    Co-authored-by: Oleks V <co...@users.noreply.github.com>
    
    ---------
    
    Co-authored-by: Oleks V <co...@users.noreply.github.com>
---
 datafusion/functions/Cargo.toml         |  5 ++++
 datafusion/functions/benches/nullif.rs  | 42 +++++++++++++++++++++++++++++++++
 datafusion/functions/src/core/nullif.rs | 10 +++++---
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/datafusion/functions/Cargo.toml b/datafusion/functions/Cargo.toml
index 0281676cab..9675d03a01 100644
--- a/datafusion/functions/Cargo.toml
+++ b/datafusion/functions/Cargo.toml
@@ -112,6 +112,11 @@ harness = false
 name = "make_date"
 required-features = ["datetime_expressions"]
 
+[[bench]]
+harness = false
+name = "nullif"
+required-features = ["core_expressions"]
+
 [[bench]]
 harness = false
 name = "date_bin"
diff --git a/datafusion/functions/benches/nullif.rs b/datafusion/functions/benches/nullif.rs
new file mode 100644
index 0000000000..dfabad3358
--- /dev/null
+++ b/datafusion/functions/benches/nullif.rs
@@ -0,0 +1,42 @@
+// 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.
+
+extern crate criterion;
+
+use arrow::util::bench_util::create_string_array_with_len;
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use datafusion_common::ScalarValue;
+use datafusion_expr::ColumnarValue;
+use datafusion_functions::core::nullif;
+use std::sync::Arc;
+
+fn criterion_benchmark(c: &mut Criterion) {
+    let nullif = nullif();
+    for size in [1024, 4096, 8192] {
+        let array = Arc::new(create_string_array_with_len::<i32>(size, 0.2, 32));
+        let args = vec![
+            ColumnarValue::Scalar(ScalarValue::Utf8(Some("abcd".to_string()))),
+            ColumnarValue::Array(array),
+        ];
+        c.bench_function(&format!("nullif scalar array: {}", size), |b| {
+            b.iter(|| black_box(nullif.invoke(&args).unwrap()))
+        });
+    }
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/datafusion/functions/src/core/nullif.rs b/datafusion/functions/src/core/nullif.rs
index e8bf2db514..6fcfbd3641 100644
--- a/datafusion/functions/src/core/nullif.rs
+++ b/datafusion/functions/src/core/nullif.rs
@@ -19,7 +19,6 @@ use arrow::datatypes::DataType;
 use datafusion_common::{exec_err, Result};
 use datafusion_expr::ColumnarValue;
 
-use arrow::array::Array;
 use arrow::compute::kernels::cmp::eq;
 use arrow::compute::kernels::nullif::nullif;
 use datafusion_common::ScalarValue;
@@ -122,8 +121,13 @@ fn nullif_func(args: &[ColumnarValue]) -> Result<ColumnarValue> {
             Ok(ColumnarValue::Array(array))
         }
         (ColumnarValue::Scalar(lhs), ColumnarValue::Array(rhs)) => {
-            let lhs = lhs.to_array_of_size(rhs.len())?;
-            let array = nullif(&lhs, &eq(&lhs, &rhs)?)?;
+            let lhs_s = lhs.to_scalar()?;
+            let lhs_a = lhs.to_array_of_size(rhs.len())?;
+            let array = nullif(
+                // nullif in arrow-select does not support Datum, so we need to convert to array
+                lhs_a.as_ref(),
+                &eq(&lhs_s, &rhs)?,
+            )?;
             Ok(ColumnarValue::Array(array))
         }
         (ColumnarValue::Scalar(lhs), ColumnarValue::Scalar(rhs)) => {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org