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

fix(query): fix and check total_buffer_len and total_bytes_len #16854

Merged
merged 7 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions src/common/arrow/src/arrow/array/binview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ impl<T: ViewType + ?Sized> BinaryViewArrayGeneric<T> {
total_bytes_len: usize,
total_buffer_len: usize,
) -> Self {
#[cfg(debug_assertions)]
{
if total_bytes_len != UNKNOWN_LEN as usize {
let total = views.iter().map(|v| v.length as usize).sum::<usize>();
assert_eq!(total, total_bytes_len);
}

if total_buffer_len != UNKNOWN_LEN as usize {
let total = buffers.iter().map(|v| v.len()).sum::<usize>();
assert_eq!(total, total_buffer_len);
}
}
// # Safety
// The caller must ensure
// - the data is valid utf8 (if required)
Expand Down
11 changes: 11 additions & 0 deletions src/common/arrow/src/arrow/array/binview/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,23 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
// Push and pop to get the properly encoded value.
// For long string this leads to a dictionary encoding,
// as we push the string only once in the buffers

let old_bytes_len = self.total_bytes_len;
let old_buffer_len = self.total_buffer_len;

let view_value = value
.map(|v| {
self.push_value_ignore_validity(v);
self.views.pop().unwrap()
})
.unwrap_or_default();

self.total_bytes_len +=
(self.total_bytes_len - old_bytes_len) * additional.saturating_sub(1);

self.total_buffer_len +=
(self.total_buffer_len - old_buffer_len) * additional.saturating_sub(1);

self.views
.extend(std::iter::repeat(view_value).take(additional));
}
Expand Down
6 changes: 1 addition & 5 deletions src/common/arrow/src/arrow/array/growable/binview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ impl<'a, T: ViewType + ?Sized> GrowableBinaryViewArray<'a, T> {
capacity: usize,
) -> Self {
let data_type = arrays[0].data_type().clone();

// if any of the arrays has nulls, insertions from any array requires setting bits
// as there is at least one array with nulls.
if !use_validity & arrays.iter().any(|array| array.null_count() > 0) {
Expand All @@ -91,11 +90,8 @@ impl<'a, T: ViewType + ?Sized> GrowableBinaryViewArray<'a, T> {
.map(|buf| BufferKey { inner: buf })
})
.collect::<ArrowIndexSet<_>>();
let total_buffer_len = arrays
.iter()
.map(|arr| arr.data_buffers().len())
.sum::<usize>();

let total_buffer_len = buffers.iter().map(|v| v.inner.len()).sum();
Self {
arrays,
data_type,
Expand Down
2 changes: 1 addition & 1 deletion src/query/expression/src/kernels/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl<'a> FilterVisitor<'a> {
new_views,
values.data.data_buffers().clone(),
None,
Some(values.data.total_buffer_len()),
None,
)
};
StringColumn::new(new_col)
Expand Down
2 changes: 1 addition & 1 deletion src/query/expression/src/kernels/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ where I: databend_common_arrow::arrow::types::Index
new_views,
col.data.data_buffers().clone(),
None,
Some(col.data.total_buffer_len()),
None,
)
};
StringColumn::new(new_col)
Expand Down
2 changes: 1 addition & 1 deletion src/query/expression/src/kernels/take_compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl<'a> TakeCompactVisitor<'a> {
new_views,
col.data.data_buffers().clone(),
None,
Some(col.data.total_buffer_len()),
None,
)
};
StringColumn::new(new_col)
Expand Down
2 changes: 1 addition & 1 deletion src/query/expression/src/kernels/take_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'a> TakeRangeVisitor<'a> {
new_views,
col.data.data_buffers().clone(),
None,
Some(col.data.total_buffer_len()),
None,
)
};
StringColumn::new(new_col)
Expand Down
12 changes: 11 additions & 1 deletion tests/sqllogictests/suites/query/join/left_outer.test
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,18 @@ SELECT * FROM t1 LEFT JOIN t2 ON t1.a = t2.a;
4 5 NULL NULL
5 6 NULL NULL


statement ok
create or replace table t1 (a string) as select number from numbers(100);

statement ok
create or replace table t2 (a string) as select number from numbers(100);

## just check it works or not
statement ok
set max_block_size = 65536;
select * from (
select 'SN0LL' as k from t1
) as a1 left join (select * from t2) as a2 on a1.k = a2.a;

statement ok
DROP TABLE IF EXISTS t1;
Expand Down
Loading