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

Pass scalar to eq inside nullif #11697

Merged
merged 5 commits into from
Aug 5, 2024
Merged
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
5 changes: 5 additions & 0 deletions datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions datafusion/functions/benches/nullif.rs
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 7 additions & 3 deletions datafusion/functions/src/core/nullif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could update the arrow-rs nullif kernel to have a special case for a constant (arrow-rs calls this idea "Datum" rather than ScalarValue):

https://docs.rs/arrow/latest/arrow/array/trait.Datum.html

But you can get the Datum like this:https://docs.rs/datafusion/latest/datafusion/common/enum.ScalarValue.html#method.to_scalar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specializing arrow-rs's nullif kernel for a constant would be the absolute best. But if others can reproduce the small % perf increase of this PR, then perhaps it can be merged in isolation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good improvement regardless as it makes the DataFusion code follow the pattern so it can take advantage of the Datum special case if/when it is implemented.

Any chance you have a moment to file a ticket upstream in arrow-rs? If not, I will do so

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)) => {
Expand Down