11use crate :: compile:: engine:: df:: planner:: CubeQueryPlanner ;
2- use std:: { backtrace:: Backtrace , collections:: HashMap , future:: Future , pin:: Pin , sync:: Arc } ;
2+ use std:: {
3+ backtrace:: Backtrace , collections:: HashMap , future:: Future , pin:: Pin , sync:: Arc ,
4+ time:: SystemTime ,
5+ } ;
36
47use crate :: {
58 compile:: {
@@ -21,8 +24,9 @@ use crate::{
2124 } ,
2225 config:: ConfigObj ,
2326 sql:: {
24- compiler_cache:: CompilerCache , statement:: SensitiveDataSanitizer , SessionManager ,
25- SessionState ,
27+ compiler_cache:: { CompilerCache , CompilerCacheEntry } ,
28+ statement:: SensitiveDataSanitizer ,
29+ SessionManager , SessionState ,
2630 } ,
2731 transport:: { LoadRequestMeta , MetaContext , SpanId , TransportService } ,
2832 CubeErrorCauseType ,
@@ -78,6 +82,11 @@ pub trait QueryEngine {
7882
7983 fn sanitize_statement ( & self , stmt : & Self :: AstStatementType ) -> Self :: AstStatementType ;
8084
85+ async fn get_cache_entry_and_refresh_cache_if_needed (
86+ & self ,
87+ state : Arc < SessionState > ,
88+ ) -> Result < Arc < CompilerCacheEntry > , CompilationError > ;
89+
8190 async fn plan (
8291 & self ,
8392 stmt : Self :: AstStatementType ,
@@ -86,6 +95,28 @@ pub trait QueryEngine {
8695 meta : Arc < MetaContext > ,
8796 state : Arc < SessionState > ,
8897 ) -> CompilationResult < ( QueryPlan , Self :: PlanMetadataType ) > {
98+ let cache_entry = self
99+ . get_cache_entry_and_refresh_cache_if_needed ( state. clone ( ) )
100+ . await ?;
101+
102+ let planning_start = SystemTime :: now ( ) ;
103+ if let Some ( span_id) = span_id. as_ref ( ) {
104+ if let Some ( auth_context) = state. auth_context ( ) {
105+ self . transport_ref ( )
106+ . log_load_state (
107+ Some ( span_id. clone ( ) ) ,
108+ auth_context,
109+ state. get_load_request_meta ( ) ,
110+ "SQL API Query Planning" . to_string ( ) ,
111+ serde_json:: json!( {
112+ "query" : span_id. query_key. clone( ) ,
113+ } ) ,
114+ )
115+ . await
116+ . map_err ( |e| CompilationError :: internal ( e. to_string ( ) ) ) ?;
117+ }
118+ }
119+
89120 let ctx = self . create_session_ctx ( state. clone ( ) ) ?;
90121 let cube_ctx = self . create_cube_ctx ( state. clone ( ) , meta. clone ( ) , ctx. clone ( ) ) ?;
91122
@@ -144,7 +175,7 @@ pub trait QueryEngine {
144175 let mut finalized_graph = self
145176 . compiler_cache_ref ( )
146177 . rewrite (
147- state . auth_context ( ) . unwrap ( ) ,
178+ Arc :: clone ( & cache_entry ) ,
148179 cube_ctx. clone ( ) ,
149180 converter. take_egraph ( ) ,
150181 & query_params. unwrap ( ) ,
@@ -192,6 +223,7 @@ pub trait QueryEngine {
192223 let result = rewriter
193224 . find_best_plan (
194225 root,
226+ cache_entry,
195227 state. auth_context ( ) . unwrap ( ) ,
196228 qtrace,
197229 span_id. clone ( ) ,
@@ -243,12 +275,31 @@ pub trait QueryEngine {
243275 // TODO: We should find what optimizers will be safety to use for OLAP queries
244276 guard. optimizer . rules = vec ! [ ] ;
245277 }
246- if let Some ( span_id) = span_id {
278+ if let Some ( span_id) = & span_id {
247279 span_id. set_is_data_query ( true ) . await ;
248280 }
249281 } ;
250282
251283 log:: debug!( "Rewrite: {:#?}" , rewrite_plan) ;
284+
285+ if let Some ( span_id) = span_id. as_ref ( ) {
286+ if let Some ( auth_context) = state. auth_context ( ) {
287+ self . transport_ref ( )
288+ . log_load_state (
289+ Some ( span_id. clone ( ) ) ,
290+ auth_context,
291+ state. get_load_request_meta ( ) ,
292+ "SQL API Query Planning Success" . to_string ( ) ,
293+ serde_json:: json!( {
294+ "query" : span_id. query_key. clone( ) ,
295+ "duration" : planning_start. elapsed( ) . unwrap( ) . as_millis( ) as u64 ,
296+ } ) ,
297+ )
298+ . await
299+ . map_err ( |e| CompilationError :: internal ( e. to_string ( ) ) ) ?;
300+ }
301+ }
302+
252303 let rewrite_plan = Self :: evaluate_wrapped_sql (
253304 self . transport_ref ( ) . clone ( ) ,
254305 Arc :: new ( state. get_load_request_meta ( ) ) ,
@@ -501,6 +552,21 @@ impl QueryEngine for SqlQueryEngine {
501552 fn sanitize_statement ( & self , stmt : & Self :: AstStatementType ) -> Self :: AstStatementType {
502553 SensitiveDataSanitizer :: new ( ) . replace ( stmt. clone ( ) )
503554 }
555+
556+ async fn get_cache_entry_and_refresh_cache_if_needed (
557+ & self ,
558+ state : Arc < SessionState > ,
559+ ) -> Result < Arc < CompilerCacheEntry > , CompilationError > {
560+ self . compiler_cache_ref ( )
561+ . get_cache_entry_and_refresh_if_needed (
562+ state. auth_context ( ) . ok_or_else ( || {
563+ CompilationError :: internal ( "Unable to get auth context" . to_string ( ) )
564+ } ) ?,
565+ state. protocol . clone ( ) ,
566+ )
567+ . await
568+ . map_err ( |e| CompilationError :: internal ( e. to_string ( ) ) )
569+ }
504570}
505571
506572fn is_olap_query ( parent : & LogicalPlan ) -> Result < bool , CompilationError > {
0 commit comments