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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use kvproto::coprocessor::KeyRange;
use tipb::ColumnInfo;
use tipb::FieldType;
use crate::interface::*;
use tidb_query_common::storage::scanner::{RangesScanner, RangesScannerOptions};
use tidb_query_common::storage::{IntervalRange, Range, Storage};
use tidb_query_common::Result;
use tidb_query_datatype::codec::batch::LazyBatchColumnVec;
use tidb_query_datatype::expr::EvalContext;
pub trait ScanExecutorImpl: Send {
fn schema(&self) -> &[FieldType];
fn mut_context(&mut self) -> &mut EvalContext;
fn build_column_vec(&self, scan_rows: usize) -> LazyBatchColumnVec;
fn process_kv_pair(
&mut self,
key: &[u8],
value: &[u8],
columns: &mut LazyBatchColumnVec,
) -> Result<()>;
}
pub struct ScanExecutor<S: Storage, I: ScanExecutorImpl> {
imp: I,
scanner: RangesScanner<S>,
is_ended: bool,
}
pub struct ScanExecutorOptions<S, I> {
pub imp: I,
pub storage: S,
pub key_ranges: Vec<KeyRange>,
pub is_backward: bool,
pub is_key_only: bool,
pub accept_point_range: bool,
pub is_scanned_range_aware: bool,
}
impl<S: Storage, I: ScanExecutorImpl> ScanExecutor<S, I> {
pub fn new(
ScanExecutorOptions {
imp,
storage,
mut key_ranges,
is_backward,
is_key_only,
accept_point_range,
is_scanned_range_aware,
}: ScanExecutorOptions<S, I>,
) -> Result<Self> {
tidb_query_datatype::codec::table::check_table_ranges(&key_ranges)?;
if is_backward {
key_ranges.reverse();
}
Ok(Self {
imp,
scanner: RangesScanner::new(RangesScannerOptions {
storage,
ranges: key_ranges
.into_iter()
.map(|r| Range::from_pb_range(r, accept_point_range))
.collect(),
scan_backward_in_range: is_backward,
is_key_only,
is_scanned_range_aware,
}),
is_ended: false,
})
}
fn fill_column_vec(
&mut self,
scan_rows: usize,
columns: &mut LazyBatchColumnVec,
) -> Result<bool> {
assert!(scan_rows > 0);
for _ in 0..scan_rows {
let some_row = self.scanner.next()?;
if let Some((key, value)) = some_row {
if let Err(e) = self.imp.process_kv_pair(&key, &value, columns) {
columns.truncate_into_equal_length();
return Err(e);
}
} else {
return Ok(true);
}
}
Ok(false)
}
}
pub fn field_type_from_column_info(ci: &ColumnInfo) -> FieldType {
let mut field_type = FieldType::default();
field_type.set_tp(ci.get_tp());
field_type.set_flag(ci.get_flag() as u32);
field_type.set_flen(ci.get_column_len());
field_type.set_decimal(ci.get_decimal());
field_type.set_collate(ci.get_collation());
field_type.set_elems(protobuf::RepeatedField::from(ci.get_elems()));
field_type
}
pub fn check_columns_info_supported(columns_info: &[ColumnInfo]) -> Result<()> {
use std::convert::TryFrom;
use tidb_query_datatype::EvalType;
use tidb_query_datatype::FieldTypeAccessor;
for column in columns_info {
if column.get_pk_handle() {
box_try!(EvalType::try_from(column.as_accessor().tp()));
}
}
Ok(())
}
impl<S: Storage, I: ScanExecutorImpl> BatchExecutor for ScanExecutor<S, I> {
type StorageStats = S::Statistics;
#[inline]
fn schema(&self) -> &[FieldType] {
self.imp.schema()
}
#[inline]
fn next_batch(&mut self, scan_rows: usize) -> BatchExecuteResult {
assert!(!self.is_ended);
assert!(scan_rows > 0);
let mut logical_columns = self.imp.build_column_vec(scan_rows);
let is_drained = self.fill_column_vec(scan_rows, &mut logical_columns);
logical_columns.assert_columns_equal_length();
let logical_rows = (0..logical_columns.rows_len()).collect();
match &is_drained {
Err(_) | Ok(true) => self.is_ended = true,
Ok(false) => {}
};
BatchExecuteResult {
physical_columns: logical_columns,
logical_rows,
is_drained,
warnings: self.imp.mut_context().take_warnings(),
}
}
#[inline]
fn collect_exec_stats(&mut self, dest: &mut ExecuteStats) {
self.scanner
.collect_scanned_rows_per_range(&mut dest.scanned_rows_per_range);
}
#[inline]
fn collect_storage_stats(&mut self, dest: &mut Self::StorageStats) {
self.scanner.collect_storage_stats(dest);
}
#[inline]
fn take_scanned_range(&mut self) -> IntervalRange {
self.scanner.take_scanned_range()
}
#[inline]
fn can_be_cached(&self) -> bool {
self.scanner.can_be_cached()
}
}