-
Notifications
You must be signed in to change notification settings - Fork 705
/
select_column_scalar.rs
160 lines (155 loc) 路 6.74 KB
/
select_column_scalar.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2021 Datafuse Labs
//
// Licensed 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.
use databend_common_arrow::arrow::bitmap::Bitmap;
use databend_common_exception::Result;
use crate::filter::SelectStrategy;
use crate::filter::Selector;
use crate::types::ValueType;
impl<'a> Selector<'a> {
// Select indices by comparing scalar and column.
#[allow(clippy::too_many_arguments)]
pub(crate) fn select_column_scalar<
T: ValueType,
C: Fn(T::ScalarRef<'_>, T::ScalarRef<'_>) -> bool,
const FALSE: bool,
>(
&self,
cmp: C,
column: T::Column,
scalar: T::ScalarRef<'a>,
validity: Option<Bitmap>,
true_selection: &mut [u32],
false_selection: &mut [u32],
mutable_true_idx: &mut usize,
mutable_false_idx: &mut usize,
select_strategy: SelectStrategy,
count: usize,
) -> Result<usize> {
let mut true_idx = *mutable_true_idx;
let mut false_idx = *mutable_false_idx;
match select_strategy {
SelectStrategy::True => unsafe {
let start = *mutable_true_idx;
let end = *mutable_true_idx + count;
match validity {
Some(validity) => {
for i in start..end {
let idx = *true_selection.get_unchecked(i);
let ret = validity.get_bit_unchecked(idx as usize)
&& cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
None => {
for i in start..end {
let idx = *true_selection.get_unchecked(i);
let ret = cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
}
},
SelectStrategy::False => unsafe {
let start = *mutable_false_idx;
let end = *mutable_false_idx + count;
match validity {
Some(validity) => {
for i in start..end {
let idx = *false_selection.get_unchecked(i);
let ret = validity.get_bit_unchecked(idx as usize)
&& cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
None => {
for i in start..end {
let idx = *false_selection.get_unchecked(i);
let ret = cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
}
},
SelectStrategy::All => unsafe {
match validity {
Some(validity) => {
for idx in 0u32..count as u32 {
let ret = validity.get_bit_unchecked(idx as usize)
&& cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
None => {
for idx in 0u32..count as u32 {
let ret = cmp(
T::index_column_unchecked(&column, idx as usize),
scalar.clone(),
);
*true_selection.get_unchecked_mut(true_idx) = idx;
true_idx += ret as usize;
if FALSE {
*false_selection.get_unchecked_mut(false_idx) = idx;
false_idx += !ret as usize;
}
}
}
}
},
}
let true_count = true_idx - *mutable_true_idx;
*mutable_true_idx = true_idx;
*mutable_false_idx = false_idx;
Ok(true_count)
}
}