-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
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 4810be9
treat Utf8View as Text in sqllogictests output
Kev1n8 e8854b8
add bench to see enhancement of utf8view against utf8 and large_utf8
Kev1n8 35e4658
fix a tiny bug
Kev1n8 bf6c37e
make clippy happy
Kev1n8 74acc7c
add tests to cover stringview larger than 12B and correct the code
Kev1n8 af795a4
better comments
Kev1n8 f8989d2
fix lint
Kev1n8 26a878a
correct feature setting
Kev1n8 2d8458e
avoid expensive utf8 and some other checks
Kev1n8 934516a
fix lint
Kev1n8 48e1643
remove unnecessary indirection
Kev1n8 6ff32fc
add optimized_utf8_to_str_type
Kev1n8 9537c47
Simplify type check
alamb 5356b27
Use ByteView
alamb bef80cb
Merge pull request #1 from alamb/alamb/cleanup_substr
Kev1n8 118aaba
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb b4e1ac7
update datafusion-cli.lock
alamb 7106ef2
Remove duration override
alamb 28d6aca
format toml
alamb 3be3553
refactor the code, using append_view_u128 from arrow
Kev1n8 b288605
manually collect the views and nulls
Kev1n8 e2643f9
remove bench file and fix some comments
Kev1n8 f462bbb
fix tiny mistake
Kev1n8 12e878c
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb 9320e41
Merge remote-tracking branch 'apache/main' into stringview-output-for…
alamb 2c88a19
Update Cargo.lock
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.