@@ -42,6 +42,7 @@ use std::ops::Bound;
42
42
use std:: sync:: Arc ;
43
43
use stream_schema_provider:: collect_manifest_files;
44
44
use sysinfo:: System ;
45
+ use tokio:: runtime:: Runtime ;
45
46
46
47
use self :: error:: ExecuteError ;
47
48
use self :: stream_schema_provider:: GlobalSchemaProvider ;
@@ -60,6 +61,24 @@ use crate::utils::time::TimeRange;
60
61
pub static QUERY_SESSION : Lazy < SessionContext > =
61
62
Lazy :: new ( || Query :: create_session_context ( PARSEABLE . storage ( ) ) ) ;
62
63
64
+ /// Dedicated multi-threaded runtime to run all queries on
65
+ pub static QUERY_RUNTIME : Lazy < Runtime > =
66
+ Lazy :: new ( || Runtime :: new ( ) . expect ( "Runtime should be constructible" ) ) ;
67
+
68
+
69
+ /// This function executes a query on the dedicated runtime, ensuring that the query is not isolated to a single thread/CPU
70
+ /// at a time and has access to the entire thread pool, enabling better concurrent processing, and thus quicker results.
71
+ pub async fn execute (
72
+ query : Query ,
73
+ stream_name : & str ,
74
+ ) -> Result < ( Vec < RecordBatch > , Vec < String > ) , ExecuteError > {
75
+ let time_partition = PARSEABLE . get_stream ( stream_name) ?. get_time_partition ( ) ;
76
+ QUERY_RUNTIME
77
+ . spawn ( async move { query. execute ( time_partition. as_ref ( ) ) . await } )
78
+ . await
79
+ . expect ( "The Join should have been successful" )
80
+ }
81
+
63
82
// A query request by client
64
83
#[ derive( Debug ) ]
65
84
pub struct Query {
@@ -129,15 +148,12 @@ impl Query {
129
148
SessionContext :: new_with_state ( state)
130
149
}
131
150
132
- #[ tokio:: main( flavor = "multi_thread" ) ]
133
151
pub async fn execute (
134
152
& self ,
135
- stream_name : String ,
153
+ time_partition : Option < & String > ,
136
154
) -> Result < ( Vec < RecordBatch > , Vec < String > ) , ExecuteError > {
137
- let time_partition = PARSEABLE . get_stream ( & stream_name) ?. get_time_partition ( ) ;
138
-
139
155
let df = QUERY_SESSION
140
- . execute_logical_plan ( self . final_logical_plan ( & time_partition) )
156
+ . execute_logical_plan ( self . final_logical_plan ( time_partition) )
141
157
. await ?;
142
158
143
159
let fields = df
@@ -153,21 +169,23 @@ impl Query {
153
169
}
154
170
155
171
let results = df. collect ( ) . await ?;
172
+
156
173
Ok ( ( results, fields) )
157
174
}
158
175
159
- pub async fn get_dataframe ( & self , stream_name : String ) -> Result < DataFrame , ExecuteError > {
160
- let time_partition = PARSEABLE . get_stream ( & stream_name) ?. get_time_partition ( ) ;
161
-
176
+ pub async fn get_dataframe (
177
+ & self ,
178
+ time_partition : Option < & String > ,
179
+ ) -> Result < DataFrame , ExecuteError > {
162
180
let df = QUERY_SESSION
163
- . execute_logical_plan ( self . final_logical_plan ( & time_partition) )
181
+ . execute_logical_plan ( self . final_logical_plan ( time_partition) )
164
182
. await ?;
165
183
166
184
Ok ( df)
167
185
}
168
186
169
187
/// return logical plan with all time filters applied through
170
- fn final_logical_plan ( & self , time_partition : & Option < String > ) -> LogicalPlan {
188
+ fn final_logical_plan ( & self , time_partition : Option < & String > ) -> LogicalPlan {
171
189
// see https://github.com/apache/arrow-datafusion/pull/8400
172
190
// this can be eliminated in later version of datafusion but with slight caveat
173
191
// transform cannot modify stringified plans by itself
@@ -487,7 +505,7 @@ fn transform(
487
505
plan : LogicalPlan ,
488
506
start_time : NaiveDateTime ,
489
507
end_time : NaiveDateTime ,
490
- time_partition : & Option < String > ,
508
+ time_partition : Option < & String > ,
491
509
) -> Transformed < LogicalPlan > {
492
510
plan. transform ( & |plan| match plan {
493
511
LogicalPlan :: TableScan ( table) => {
@@ -545,7 +563,7 @@ fn transform(
545
563
546
564
fn table_contains_any_time_filters (
547
565
table : & datafusion:: logical_expr:: TableScan ,
548
- time_partition : & Option < String > ,
566
+ time_partition : Option < & String > ,
549
567
) -> bool {
550
568
table
551
569
. filters
@@ -559,8 +577,8 @@ fn table_contains_any_time_filters(
559
577
} )
560
578
. any ( |expr| {
561
579
matches ! ( & * expr. left, Expr :: Column ( Column { name, .. } )
562
- if ( ( time_partition. is_some ( ) && name == time_partition . as_ref ( ) . unwrap ( ) ) ||
563
- ( ! time_partition. is_some ( ) && name == event:: DEFAULT_TIMESTAMP_KEY ) ) )
580
+ if ( time_partition. is_some_and ( |field| field == name ) ||
581
+ ( time_partition. is_none ( ) && name == event:: DEFAULT_TIMESTAMP_KEY ) ) )
564
582
} )
565
583
}
566
584
0 commit comments