12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: collections:: HashSet ;
15
16
use std:: sync:: Arc ;
16
17
use std:: time:: Instant ;
17
18
@@ -32,6 +33,7 @@ use common_legacy_planners::Extras;
32
33
use common_legacy_planners:: Partitions ;
33
34
use common_legacy_planners:: Projection ;
34
35
use common_legacy_planners:: ReadDataSourcePlan ;
36
+ use common_legacy_planners:: RequireColumnsVisitor ;
35
37
use common_legacy_planners:: Statistics ;
36
38
use common_legacy_planners:: TruncateTablePlan ;
37
39
use common_meta_app:: schema:: TableInfo ;
@@ -109,19 +111,69 @@ impl HiveTable {
109
111
let max_threads = std:: cmp:: min ( parts_len, max_threads) ;
110
112
111
113
let mut source_builder = SourcePipeBuilder :: create ( ) ;
114
+ let delay_timer = if self . is_simple_select_query ( plan) {
115
+ // 0, 0, 200, 200, 400,400
116
+ |x : usize | ( x / 2 ) . min ( 10 ) * 200
117
+ } else {
118
+ |_| 0
119
+ } ;
112
120
113
- for _index in 0 ..std:: cmp:: max ( 1 , max_threads) {
121
+ for index in 0 ..std:: cmp:: max ( 1 , max_threads) {
114
122
let output = OutputPort :: create ( ) ;
115
123
source_builder. add_source (
116
124
output. clone ( ) ,
117
- HiveTableSource :: create ( ctx. clone ( ) , output, block_reader. clone ( ) ) ?,
125
+ HiveTableSource :: create (
126
+ ctx. clone ( ) ,
127
+ output,
128
+ block_reader. clone ( ) ,
129
+ delay_timer ( index) ,
130
+ ) ?,
118
131
) ;
119
132
}
120
133
121
134
pipeline. add_pipe ( source_builder. finalize ( ) ) ;
122
135
Ok ( ( ) )
123
136
}
124
137
138
+ // simple select query is the sql likes `select * from xx limit 10` or
139
+ // `select * from xx where p_date = '20220201' limit 10` where p_date is a partition column;
140
+ // we just need to read few datas from table
141
+ fn is_simple_select_query ( & self , plan : & ReadDataSourcePlan ) -> bool {
142
+ // couldn't get groupby order by info
143
+ if let Some ( Extras {
144
+ filters : f,
145
+ limit : Some ( lm) ,
146
+ ..
147
+ } ) = & plan. push_downs
148
+ {
149
+ if * lm > 100000 {
150
+ return false ;
151
+ }
152
+
153
+ // filter out the partition column related expressions
154
+ let partition_keys = self . get_partition_key_sets ( ) ;
155
+ let columns = Self :: get_columns_from_expressions ( f) ;
156
+ if columns. difference ( & partition_keys) . count ( ) == 0 {
157
+ return true ;
158
+ }
159
+ }
160
+ false
161
+ }
162
+
163
+ fn get_partition_key_sets ( & self ) -> HashSet < String > {
164
+ match & self . table_options . partition_keys {
165
+ Some ( v) => v. iter ( ) . cloned ( ) . collect :: < HashSet < _ > > ( ) ,
166
+ None => HashSet :: new ( ) ,
167
+ }
168
+ }
169
+
170
+ fn get_columns_from_expressions ( expressions : & [ Expression ] ) -> HashSet < String > {
171
+ expressions
172
+ . iter ( )
173
+ . flat_map ( |e| RequireColumnsVisitor :: collect_columns_from_expr ( e) . unwrap ( ) )
174
+ . collect :: < HashSet < _ > > ( )
175
+ }
176
+
125
177
fn create_block_reader (
126
178
& self ,
127
179
ctx : & Arc < dyn TableContext > ,
0 commit comments