Skip to content

Commit 45e15cc

Browse files
authored
chore(query): improve push_column_with_selection (#14954)
chore: improve push_column_with_selection
1 parent 9cbe53d commit 45e15cc

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/query/expression/src/kernels/topk.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,31 @@ impl TopKSorter {
7676

7777
// Push the column into this sorted and update the selection
7878
// The selection could be used in filter
79-
pub fn push_column_with_selection(
79+
pub fn push_column_with_selection<const SELECT_ALL: bool>(
8080
&mut self,
8181
col: &Column,
8282
selection: &mut [u32],
8383
count: usize,
8484
) -> usize {
8585
with_number_mapped_type!(|NUM_TYPE| match col.data_type() {
8686
DataType::Number(NumberDataType::NUM_TYPE) => self
87-
.push_column_with_selection_internal::<NumberType::<NUM_TYPE>>(
87+
.push_column_with_selection_internal::<NumberType::<NUM_TYPE>, SELECT_ALL>(
8888
col, selection, count
8989
),
90-
DataType::String =>
91-
self.push_column_with_selection_internal::<StringType>(col, selection, count),
92-
DataType::Timestamp =>
93-
self.push_column_with_selection_internal::<TimestampType>(col, selection, count),
94-
DataType::Date =>
95-
self.push_column_with_selection_internal::<DateType>(col, selection, count),
90+
DataType::String => self.push_column_with_selection_internal::<StringType, SELECT_ALL>(
91+
col, selection, count
92+
),
93+
DataType::Timestamp => self
94+
.push_column_with_selection_internal::<TimestampType, SELECT_ALL>(
95+
col, selection, count
96+
),
97+
DataType::Date => self
98+
.push_column_with_selection_internal::<DateType, SELECT_ALL>(col, selection, count),
9699
_ => count,
97100
})
98101
}
99102

100-
fn push_column_with_selection_internal<T: ValueType>(
103+
fn push_column_with_selection_internal<T: ValueType, const SELECT_ALL: bool>(
101104
&mut self,
102105
col: &Column,
103106
selection: &mut [u32],
@@ -109,7 +112,7 @@ impl TopKSorter {
109112
let col = T::try_downcast_column(col).unwrap();
110113
let mut result_count = 0;
111114
for i in 0..count {
112-
let idx = selection[i];
115+
let idx = if SELECT_ALL { i as u32 } else { selection[i] };
113116
let value = unsafe { T::index_column_unchecked(&col, idx as usize) };
114117
if self.data.len() < self.limit {
115118
self.data.push(T::upcast_scalar(T::to_owned_scalar(value)));

src/query/storages/fuse/src/operations/read/native_data_source_deserializer.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -878,23 +878,25 @@ impl NativeDeserializeDataTransform {
878878
let col = Column::from_arrow(array.as_ref(), &data_type)?;
879879

880880
let filter_executor = self.filter_executor.as_mut().unwrap();
881-
882-
if let Some(count) = self.read_state.filtered_count {
883-
let cnt = sorter.push_column_with_selection(
881+
let count = if let Some(count) = self.read_state.filtered_count {
882+
sorter.push_column_with_selection::<false>(
884883
&col,
885884
filter_executor.mutable_true_selection(),
886885
count,
887-
);
888-
889-
if cnt == 0 {
890-
return Ok(false);
891-
}
892-
self.read_state.filtered_count = Some(cnt);
886+
)
893887
} else {
894-
let mut bitmap = MutableBitmap::from_len_set(col.len());
895-
sorter.push_column(&col, &mut bitmap);
896-
self.read_state.filtered_count = Some(filter_executor.from_bitmap(bitmap));
888+
// If there is no prewhere filter, initialize the true selection.
889+
sorter.push_column_with_selection::<true>(
890+
&col,
891+
filter_executor.mutable_true_selection(),
892+
col.len(),
893+
)
894+
};
895+
896+
if count == 0 {
897+
return Ok(false);
897898
}
899+
self.read_state.filtered_count = Some(count);
898900
};
899901

900902
Ok(true)

0 commit comments

Comments
 (0)