Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor the code, using append_view_u128 from arrow
  • Loading branch information
Kev1n8 committed Aug 23, 2024
commit 3be3553a36b012dab6df8e1a963d8869a1f94493
143 changes: 57 additions & 86 deletions datafusion/functions/src/unicode/substr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use std::any::Any;
use std::cmp::max;
use std::sync::Arc;

use crate::utils::{make_scalar_function, utf8_to_str_type};
use arrow::array::{
Array, ArrayAccessor, ArrayIter, ArrayRef, AsArray, GenericStringArray,
make_view, Array, ArrayAccessor, ArrayIter, ArrayRef, AsArray, GenericStringArray,
OffsetSizeTrait, StringViewArray, StringViewBuilder,
};
use arrow::datatypes::DataType;
Expand All @@ -30,8 +31,6 @@ use datafusion_common::{exec_err, Result};
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};

use crate::utils::{make_scalar_function, utf8_to_str_type};

#[derive(Debug)]
pub struct SubstrFunc {
signature: Signature,
Expand Down Expand Up @@ -120,7 +119,7 @@ pub fn substr(args: &[ArrayRef]) -> Result<ArrayRef> {
}

// Return the exact byte index for [start, end), set count to -1 to ignore count
fn get_true_start_count(input: &str, start: usize, count: i64) -> (usize, usize) {
fn get_true_start_end(input: &str, start: usize, count: i64) -> (usize, usize) {
let (mut st, mut ed) = (input.len(), input.len());
let mut start_counting = false;
let mut cnt = 0;
Expand All @@ -144,6 +143,34 @@ fn get_true_start_count(input: &str, start: usize, count: i64) -> (usize, usize)
(st, ed)
}

/// Make a `u128` based on the given substr, start(offset to view.offset), and
/// append to the given builder
///
/// # Safety:
/// (1) The block of the given view must be added to the builder
/// (2) The range `view.offset+start..end` must be within the bounds of the block, where
/// end = view.offset+start+substr_len
unsafe fn make_and_append_view(
builder: &mut StringViewBuilder,
raw: &u128,
substr: &str,
start: u32,
) {
let substr_len = substr.len();
if substr_len == 0 {
builder.append_null();
} else {
let sub_view = if substr_len > 12 {
let view = ByteView::from(*raw);
make_view(substr.as_bytes(), view.buffer_index, view.offset + start)
} else {
// inline value does not need block id or offset
make_view(substr.as_bytes(), 0, 0)
};
builder.append_view_u128_unchecked(sub_view);
}
}

// The decoding process refs the trait at: arrow/arrow-data/src/byte_view.rs:44
// From<u128> for ByteView
fn string_view_substr(
Expand All @@ -167,43 +194,17 @@ fn string_view_substr(
.enumerate()
{
if let Some(start) = start {
let length = *raw as u32;
let start = (start - 1).max(0);
let start = (start - 1).max(0) as usize;

// Operate according to the length of bytes
if length == 0 {
builder.append_null();
} else if length > 12 {
let view = ByteView::from(*raw);
// Safety:
// 1. idx < string_array.views.size()
// 2. builder is guaranteed to have corresponding blocks
unsafe {
let str = string_view_array.value_unchecked(idx);
let (start, end) = get_true_start_end(str, start, -1);
let substr = &str[start..end];

// Safety:
// 1. idx < string_array.views.size()
// 2. builder is guaranteed to have corresponding blocks
unsafe {
let str = string_view_array.value_unchecked(idx);
let (start, end) =
get_true_start_count(str, start as usize, -1);
builder.append_view_unchecked(
view.buffer_index,
view.offset + start as u32,
// guarantee that end-offset >= 0 for end <= str.len()
(end - start) as u32,
);
}
} else {
// Safety:
// (1) original bytes are valid utf-8,
// (2) we do not slice on utf-8 codepoint
unsafe {
let bytes =
StringViewArray::inline_value(raw, length as usize);
let str =
std::str::from_utf8_unchecked(&bytes[..length as usize]);
// Extract str[start, end) by char
let (start, end) =
get_true_start_count(str, start as usize, length as i64);
builder.append_value(&str[start..end]);
}
make_and_append_view(&mut builder, raw, substr, start as u32);
}
} else {
builder.append_null();
Expand All @@ -220,48 +221,18 @@ fn string_view_substr(
.enumerate()
{
if let (Some(start), Some(count)) = (start, count) {
let length = *raw as u32;
let start = start.saturating_sub(1) as usize;
let start = (start - 1).max(0) as usize;
if count < 0 {
return exec_err!(
"negative substring length not allowed: substr(<str>, {start}, {count})"
);
} else {
let count = (count as u32).min(length);
if length == 0 {
builder.append_null();
} else if length > 12 {
let view = ByteView::from(*raw);
unsafe {
let str = string_view_array.value_unchecked(idx);
let (start, end) = get_true_start_end(str, start, count);
let substr = &str[start..end];

// Safety:
// 1. idx < string_array.views.size()
// 2. builder is guaranteed to have corresponding blocks
unsafe {
let str = string_view_array.value_unchecked(idx);
let (start, end) =
get_true_start_count(str, start, count as i64);
builder.append_view_unchecked(
view.buffer_index,
view.offset + start as u32,
// guarantee that end-offset >= 0 for end <= str.len()
(end - start) as u32,
);
}
} else {
// Safety:
// (1) original bytes are valid utf-8,
// (2) we do not slice on utf-8 codepoint
unsafe {
let bytes =
StringViewArray::inline_value(raw, length as usize);
let str = std::str::from_utf8_unchecked(
&bytes[..length as usize],
);
// Extract str[start, end) by char
let (start, end) =
get_true_start_count(str, start, count as i64);
builder.append_value(&str[start..end]);
}
make_and_append_view(&mut builder, raw, substr, start as u32);
}
}
} else {
Expand Down Expand Up @@ -350,17 +321,17 @@ mod tests {

#[test]
fn test_functions() -> Result<()> {
test_function!(
SubstrFunc::new(),
&[
ColumnarValue::Scalar(ScalarValue::Utf8View(None)),
ColumnarValue::Scalar(ScalarValue::from(1i64)),
],
Ok(None),
&str,
Utf8View,
StringViewArray
);
// test_function!(
// SubstrFunc::new(),
// &[
// ColumnarValue::Scalar(ScalarValue::Utf8View(None)),
// ColumnarValue::Scalar(ScalarValue::from(1i64)),
// ],
// Ok(None),
// &str,
// Utf8View,
// StringViewArray
// );
test_function!(
SubstrFunc::new(),
&[
Expand Down