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

chore(query): add more asserts #16536

Merged
merged 4 commits into from
Sep 28, 2024
Merged
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
5 changes: 1 addition & 4 deletions src/query/catalog/src/plan/stream_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ impl StreamColumnMeta {
}

pub fn build_origin_block_row_num(num_rows: usize) -> BlockEntry {
let mut row_ids = Vec::with_capacity(num_rows);
for i in 0..num_rows {
row_ids.push(i as u64);
}
let row_ids = (0..num_rows as u64).collect();
let column = Value::Column(UInt64Type::from_data(row_ids));

BlockEntry::new(
Expand Down
14 changes: 12 additions & 2 deletions src/query/expression/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl DataBlock {
num_rows: usize,
meta: Option<BlockMetaInfoPtr>,
) -> Self {
#[cfg(debug_assertions)]
Self::check_columns_valid(&columns, num_rows).unwrap();

Self {
Expand All @@ -130,6 +129,7 @@ impl DataBlock {
fn check_columns_valid(columns: &[BlockEntry], num_rows: usize) -> Result<()> {
for entry in columns.iter() {
if let Value::Column(c) = &entry.value {
#[cfg(debug_assertions)]
c.check_valid()?;
if c.len() != num_rows {
return Err(ErrorCode::Internal(format!(
Expand Down Expand Up @@ -264,6 +264,12 @@ impl DataBlock {
}

pub fn slice(&self, range: Range<usize>) -> Self {
assert!(
range.end <= self.num_rows(),
"range {:?} out of len {}",
range,
self.num_rows()
);
let columns = self
.columns()
.iter()
Expand All @@ -279,7 +285,11 @@ impl DataBlock {
.collect();
Self {
columns,
num_rows: range.end - range.start,
num_rows: if range.is_empty() {
0
} else {
range.end - range.start
},
meta: self.meta.clone(),
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/query/expression/src/kernels/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ struct FilterVisitor<'a> {
filter: &'a Bitmap,
result: Option<Value<AnyType>>,
num_rows: usize,
original_rows: usize,
}

impl<'a> FilterVisitor<'a> {
Expand All @@ -121,6 +122,7 @@ impl<'a> FilterVisitor<'a> {
filter,
result: None,
num_rows,
original_rows: filter.len(),
}
}
}
Expand All @@ -130,6 +132,8 @@ impl<'a> ValueVisitor for FilterVisitor<'a> {
match value {
Value::Scalar(c) => self.visit_scalar(c),
Value::Column(c) => {
assert!(c.len() == self.original_rows);

if c.len() == self.num_rows || c.len() == 0 {
self.result = Some(Value::Column(c));
} else if self.num_rows == 0 {
Expand Down Expand Up @@ -255,7 +259,14 @@ impl<'a> ValueVisitor for FilterVisitor<'a> {
Ok(())
}

fn visit_boolean(&mut self, bitmap: Bitmap) -> Result<()> {
fn visit_boolean(&mut self, mut bitmap: Bitmap) -> Result<()> {
// faster path for all bits set
if bitmap.unset_bits() == 0 {
bitmap.slice(0, self.num_rows);
self.result = Some(Value::Column(BooleanType::upcast_column(bitmap)));
return Ok(());
}

let capacity = self.num_rows.saturating_add(7) / 8;
let mut builder: Vec<u8> = Vec::with_capacity(capacity);
let mut builder_ptr = builder.as_mut_ptr();
Expand Down
2 changes: 2 additions & 0 deletions src/query/expression/src/kernels/sort_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl ValueVisitor for SortCompare {
// faster path for numeric
fn visit_number<T: Number>(&mut self, column: Buffer<T>) -> Result<()> {
let values = column.as_slice();
assert!(values.len() == self.rows);
self.generic_sort(values, |c, idx| c[idx as usize], |a: T, b: T| a.cmp(&b));
Ok(())
}
Expand All @@ -276,6 +277,7 @@ impl ValueVisitor for SortCompare {
}

fn visit_typed_column<T: ValueType>(&mut self, col: T::Column) -> Result<()> {
assert!(T::column_len(&col) == self.rows);
self.generic_sort(
&col,
|c, idx| -> T::ScalarRef<'_> { unsafe { T::index_column_unchecked(c, idx as _) } },
Expand Down
Loading