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

Improve StringView support for SUBSTR #12044

Merged
merged 27 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
44894e1
operate stringview instead of generating string in SUBSTR
Kev1n8 Aug 17, 2024
4810be9
treat Utf8View as Text in sqllogictests output
Kev1n8 Aug 17, 2024
e8854b8
add bench to see enhancement of utf8view against utf8 and large_utf8
Kev1n8 Aug 17, 2024
35e4658
fix a tiny bug
Kev1n8 Aug 17, 2024
bf6c37e
make clippy happy
Kev1n8 Aug 17, 2024
74acc7c
add tests to cover stringview larger than 12B and correct the code
Kev1n8 Aug 17, 2024
af795a4
better comments
Kev1n8 Aug 17, 2024
f8989d2
fix lint
Kev1n8 Aug 17, 2024
26a878a
correct feature setting
Kev1n8 Aug 17, 2024
2d8458e
avoid expensive utf8 and some other checks
Kev1n8 Aug 18, 2024
934516a
fix lint
Kev1n8 Aug 18, 2024
48e1643
remove unnecessary indirection
Kev1n8 Aug 18, 2024
6ff32fc
add optimized_utf8_to_str_type
Kev1n8 Aug 18, 2024
9537c47
Simplify type check
alamb Aug 19, 2024
5356b27
Use ByteView
alamb Aug 19, 2024
bef80cb
Merge pull request #1 from alamb/alamb/cleanup_substr
Kev1n8 Aug 20, 2024
118aaba
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb Aug 21, 2024
b4e1ac7
update datafusion-cli.lock
alamb Aug 21, 2024
7106ef2
Remove duration override
alamb Aug 21, 2024
28d6aca
format toml
alamb Aug 21, 2024
3be3553
refactor the code, using append_view_u128 from arrow
Kev1n8 Aug 23, 2024
b288605
manually collect the views and nulls
Kev1n8 Aug 23, 2024
e2643f9
remove bench file and fix some comments
Kev1n8 Aug 23, 2024
f462bbb
fix tiny mistake
Kev1n8 Aug 23, 2024
12e878c
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb Aug 25, 2024
9320e41
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb Sep 5, 2024
2c88a19
Update Cargo.lock
alamb Sep 5, 2024
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 @@ -156,3 +156,8 @@ required-features = ["unicode_expressions"]
harness = false
name = "repeat"
required-features = ["string_expressions"]

[[bench]]
harness = false
name = "substr"
required-features = ["unicode_expressions"]
206 changes: 206 additions & 0 deletions datafusion/functions/benches/substr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// 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::array::{ArrayRef, Int64Array, OffsetSizeTrait};
use arrow::util::bench_util::{
create_string_array_with_len, create_string_view_array_with_len,
};
use criterion::{black_box, criterion_group, criterion_main, Criterion, SamplingMode};
use datafusion_expr::ColumnarValue;
use datafusion_functions::unicode;
use std::sync::Arc;
use std::time::Duration;

fn create_args_without_count<O: OffsetSizeTrait>(
size: usize,
str_len: usize,
start_half_way: bool,
use_string_view: bool,
) -> Vec<ColumnarValue> {
let start_array = Arc::new(Int64Array::from(
(0..size)
.map(|_| {
if start_half_way {
(str_len / 2) as i64
} else {
1i64
}
})
.collect::<Vec<_>>(),
));

if use_string_view {
let string_array =
Arc::new(create_string_view_array_with_len(size, 0.1, str_len, false));
vec![
ColumnarValue::Array(string_array),
ColumnarValue::Array(start_array),
]
} else {
let string_array =
Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));

vec![
ColumnarValue::Array(string_array),
ColumnarValue::Array(Arc::clone(&start_array) as ArrayRef),
]
}
}

fn create_args_with_count<O: OffsetSizeTrait>(
size: usize,
str_len: usize,
count_max: usize,
use_string_view: bool,
) -> Vec<ColumnarValue> {
let start_array =
Arc::new(Int64Array::from((0..size).map(|_| 1).collect::<Vec<_>>()));
let count = count_max.min(str_len) as i64;
let count_array = Arc::new(Int64Array::from(
(0..size).map(|_| count).collect::<Vec<_>>(),
));

if use_string_view {
let string_array =
Arc::new(create_string_view_array_with_len(size, 0.1, str_len, false));
vec![
ColumnarValue::Array(string_array),
ColumnarValue::Array(start_array),
ColumnarValue::Array(count_array),
]
} else {
let string_array =
Arc::new(create_string_array_with_len::<O>(size, 0.1, str_len));

vec![
ColumnarValue::Array(string_array),
ColumnarValue::Array(Arc::clone(&start_array) as ArrayRef),
ColumnarValue::Array(Arc::clone(&count_array) as ArrayRef),
]
}
}

fn criterion_benchmark(c: &mut Criterion) {
let substr = unicode::substr();
for size in [1024, 4096] {
// string_len = 12, substring_len=6 (see `create_args_without_count`)
let len = 12;
let mut group = c.benchmark_group("SHORTER THAN 12");
group.sampling_mode(SamplingMode::Flat);
group.sample_size(10);
group.measurement_time(Duration::from_secs(10));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we tune the sampling parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually simply copied these settings from benches/repeat. I'm not very sure about this.


let args = create_args_without_count::<i32>(size, len, true, true);
group.bench_function(
&format!("substr_string_view [size={}, strlen={}]", size, len),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_without_count::<i32>(size, len, false, false);
group.bench_function(
&format!("substr_string [size={}, strlen={}]", size, len),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_without_count::<i64>(size, len, true, false);
group.bench_function(
&format!("substr_large_string [size={}, strlen={}]", size, len),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

group.finish();

// string_len = 128, start=1, count=64, substring_len=64
let len = 128;
let count = 64;
let mut group = c.benchmark_group("LONGER THAN 12");
group.sampling_mode(SamplingMode::Flat);
group.sample_size(10);
group.measurement_time(Duration::from_secs(10));

let args = create_args_with_count::<i32>(size, len, count, true);
group.bench_function(
&format!(
"substr_string_view [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_with_count::<i32>(size, len, count, false);
group.bench_function(
&format!(
"substr_string [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_with_count::<i64>(size, len, count, false);
group.bench_function(
&format!(
"substr_large_string [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

group.finish();

// string_len = 128, start=1, count=6, substring_len=6
let len = 128;
let count = 6;
let mut group = c.benchmark_group("SRC_LEN > 12, SUB_LEN < 12");
group.sampling_mode(SamplingMode::Flat);
group.sample_size(10);
group.measurement_time(Duration::from_secs(10));

let args = create_args_with_count::<i32>(size, len, count, true);
group.bench_function(
&format!(
"substr_string_view [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_with_count::<i32>(size, len, count, false);
group.bench_function(
&format!(
"substr_string [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

let args = create_args_with_count::<i64>(size, len, count, false);
group.bench_function(
&format!(
"substr_large_string [size={}, count={}, strlen={}]",
size, count, len,
),
|b| b.iter(|| black_box(substr.invoke(&args))),
);

group.finish();
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading