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

Optimization: make the most of Hint::AcceptsSingular when call make_scalar_function to Improve performance #10054

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
6 changes: 6 additions & 0 deletions datafusion/functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ path = "src/lib.rs"

[dependencies]
arrow = { workspace = true }
bitflags = "2.5.0"
base64 = { version = "0.22", optional = true }
blake2 = { version = "^0.10.2", optional = true }
blake3 = { version = "1.0", optional = true }
Expand Down Expand Up @@ -133,3 +134,8 @@ required-features = ["string_expressions"]
harness = false
name = "upper"
required-features = ["string_expressions"]

[[bench]]
harness = false
name = "overlay"
required-features = ["string_expressions"]
80 changes: 80 additions & 0 deletions datafusion/functions/benches/overlay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.

use arrow::array::{Array, Int64Array, StringArray};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use datafusion_common::ScalarValue;
use datafusion_expr::ColumnarValue;
use datafusion_functions::string;
use std::sync::Arc;

/// Create four args, three of which are Scalars and one is a StringArray.
/// The `size` represents the length of the StringArray.
fn create_4args_with_3scalars(size: usize) -> Vec<ColumnarValue> {
let array: StringArray = std::iter::repeat(Some("Txxxxas")).take(size).collect();
assert_eq!(array.len(), size);
let characters = ScalarValue::Utf8(Some("hom".to_string()));
let pos = ScalarValue::Int64(Some(2));
let len = ScalarValue::Int64(Some(4));
vec![
ColumnarValue::Array(Arc::new(array)),
ColumnarValue::Scalar(characters),
ColumnarValue::Scalar(pos),
ColumnarValue::Scalar(len),
]
}

/// Create four args, all of which are Arrays.
/// The `size` represents the length of Array.
fn create_4args_without_scalar(size: usize) -> Vec<ColumnarValue> {
let array: StringArray = std::iter::repeat(Some("Txxxxas")).take(size).collect();
let characters: StringArray = std::iter::repeat(Some("hom")).take(size).collect();
let pos: Int64Array = std::iter::repeat(Some(2)).take(size).collect();
let len: Int64Array = std::iter::repeat(Some(4)).take(size).collect();
vec![
ColumnarValue::Array(Arc::new(array)),
ColumnarValue::Array(Arc::new(characters)),
ColumnarValue::Array(Arc::new(pos)),
ColumnarValue::Array(Arc::new(len)),
]
}

fn criterion_benchmark(c: &mut Criterion) {
let overlay = string::overlay();
let sizes: Vec<usize> = vec![1024, 4096, 8192];

for size in &sizes {
let args = create_4args_with_3scalars(*size);
let mut group = c.benchmark_group("4args_with_3scalars");
group.bench_function(BenchmarkId::new("overlay", size), |b| {
b.iter(|| criterion::black_box(overlay.invoke(&args).unwrap()))
});
group.finish();
}

for size in &sizes {
let args = create_4args_without_scalar(*size);
let mut group = c.benchmark_group("4args_without_scalar");
group.bench_function(BenchmarkId::new("overlay", size), |b| {
b.iter(|| criterion::black_box(overlay.invoke(&args).unwrap()))
});
group.finish();
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
234 changes: 234 additions & 0 deletions datafusion/functions/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,237 @@ macro_rules! make_function_inputs2 {
.collect::<$ARRAY_TYPE1>()
}};
}

macro_rules! array_iter {
($ARRAY:expr) => {{
$ARRAY.iter()
}};
}

macro_rules! scalar_iter {
($ARRAY:expr) => {{
let value = if $ARRAY.is_null(0) {
None
} else {
Some($ARRAY.value(0))
};
std::iter::repeat(value)
}};
}

macro_rules! make_function_3args {
(ScalarFlags::None, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(array_iter!($ARG0), array_iter!($ARG1), array_iter!($ARG2))
};
(ScalarFlags::A, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(scalar_iter!($ARG0), array_iter!($ARG1), array_iter!($ARG2))
};
(ScalarFlags::B, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(array_iter!($ARG0), scalar_iter!($ARG1), array_iter!($ARG2))
};
(ScalarFlags::C, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(array_iter!($ARG0), array_iter!($ARG1), scalar_iter!($ARG2))
};
(ScalarFlags::AB, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(scalar_iter!($ARG0), scalar_iter!($ARG1), array_iter!($ARG2))
};
(ScalarFlags::AC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(scalar_iter!($ARG0), array_iter!($ARG1), scalar_iter!($ARG2))
};
(ScalarFlags::BC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(array_iter!($ARG0), scalar_iter!($ARG1), scalar_iter!($ARG2))
};
(ScalarFlags::ABC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
$FUNC(array_iter!($ARG0), array_iter!($ARG1), array_iter!($ARG2))
};
}

macro_rules! make_function_4args {
(ScalarFlags::None, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
array_iter!($ARG1),
array_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::A, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
array_iter!($ARG1),
array_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::B, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
scalar_iter!($ARG1),
array_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::C, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
array_iter!($ARG1),
scalar_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::D, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
array_iter!($ARG1),
array_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::AB, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
scalar_iter!($ARG1),
array_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::AC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
array_iter!($ARG1),
scalar_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::AD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
array_iter!($ARG1),
array_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::BC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
scalar_iter!($ARG1),
scalar_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::BD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
scalar_iter!($ARG1),
array_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::CD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
array_iter!($ARG1),
scalar_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::ABC, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
scalar_iter!($ARG1),
scalar_iter!($ARG2),
array_iter!($ARG3),
)
};
(ScalarFlags::ABD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
scalar_iter!($ARG1),
array_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::ACD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
scalar_iter!($ARG0),
array_iter!($ARG1),
scalar_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::BCD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
$FUNC(
array_iter!($ARG0),
scalar_iter!($ARG1),
scalar_iter!($ARG2),
scalar_iter!($ARG3),
)
};
(ScalarFlags::ABCD, $FUNC:ident, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
// all args use array_iter
$FUNC(
array_iter!($ARG0),
array_iter!($ARG1),
array_iter!($ARG2),
array_iter!($ARG3),
)
};
}

macro_rules! invoke_function_impl {
($FLAG:expr, $FUNC:expr, $ARG0:expr, $ARG1:expr, $ARG2:expr, [$($FLAG_ITEM:tt),+]) => {
match $FLAG {
$(ScalarFlags::$FLAG_ITEM => {
let func = $FUNC;
make_function_3args!(
ScalarFlags::$FLAG_ITEM,
func,
$ARG0,
$ARG1,
$ARG2
)
}),+
_ => unreachable!("{:?}", $FLAG),
}
};
($FLAG:expr, $FUNC:expr, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr, [$($FLAG_ITEM:tt),+]) => {
match $FLAG {
$(ScalarFlags::$FLAG_ITEM => {
let func = $FUNC;
make_function_4args!(
ScalarFlags::$FLAG_ITEM,
func,
$ARG0,
$ARG1,
$ARG2,
$ARG3
)
}),+
_ => unreachable!("{:?}", $FLAG),
}
};
}

macro_rules! invoke_function {
($FLAG:expr, $FUNC:expr, $ARG0:expr, $ARG1:expr, $ARG2:expr) => {
invoke_function_impl!(
$FLAG,
$FUNC,
$ARG0,
$ARG1,
$ARG2,
[None, A, B, C, AB, AC, BC, ABC]
)
};
($FLAG:expr, $FUNC:expr, $ARG0:expr, $ARG1:expr, $ARG2:expr, $ARG3:expr) => {
invoke_function_impl!(
$FLAG,
$FUNC,
$ARG0,
$ARG1,
$ARG2,
$ARG3,
[None, A, B, C, D, AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD, ABCD]
)
};
}
Loading
Loading