@@ -31,6 +31,7 @@ use datafusion::{
3131} ;
3232use itertools:: Itertools ;
3333use sqlparser:: ast:: { escape_single_quote_string, ObjectName } ;
34+ use uuid:: Uuid ;
3435
3536#[ derive( Clone ) ]
3637pub struct QueryRouter {
@@ -52,6 +53,20 @@ impl QueryRouter {
5253 }
5354 }
5455
56+ async fn get_compiler_id_and_refresh_cache_if_needed ( & self ) -> CompilationResult < Uuid > {
57+ self . session_manager
58+ . server
59+ . compiler_cache
60+ . get_compiler_id_and_refresh_if_needed (
61+ self . state . auth_context ( ) . ok_or_else ( || {
62+ CompilationError :: internal ( "Unable to get auth context" . to_string ( ) )
63+ } ) ?,
64+ self . state . protocol . clone ( ) ,
65+ )
66+ . await
67+ . map_err ( |e| CompilationError :: internal ( e. to_string ( ) ) )
68+ }
69+
5570 /// Common case for both planners: meta & olap
5671 /// This method tries to detect what planner to use as earlier as possible
5772 /// and forward context to correct planner
@@ -61,6 +76,9 @@ impl QueryRouter {
6176 qtrace : & mut Option < Qtrace > ,
6277 span_id : Option < Arc < SpanId > > ,
6378 ) -> CompilationResult < QueryPlan > {
79+ self . reauthenticate_if_needed ( ) . await ?;
80+ let compiler_id = self . get_compiler_id_and_refresh_cache_if_needed ( ) . await ?;
81+
6482 let planning_start = SystemTime :: now ( ) ;
6583 if let Some ( span_id) = span_id. as_ref ( ) {
6684 if let Some ( auth_context) = self . state . auth_context ( ) {
@@ -81,7 +99,7 @@ impl QueryRouter {
8199 }
82100 }
83101 let result = self
84- . create_df_logical_plan ( stmt. clone ( ) , qtrace, span_id. clone ( ) )
102+ . create_df_logical_plan ( stmt. clone ( ) , qtrace, span_id. clone ( ) , compiler_id )
85103 . await ?;
86104
87105 if let Some ( span_id) = span_id. as_ref ( ) {
@@ -230,6 +248,7 @@ impl QueryRouter {
230248 variable : & Vec < ast:: Ident > ,
231249 span_id : Option < Arc < SpanId > > ,
232250 ) -> CompilationResult < QueryPlan > {
251+ let compiler_id = self . get_compiler_id_and_refresh_cache_if_needed ( ) . await ?;
233252 let name = variable. to_vec ( ) [ 0 ] . value . clone ( ) ;
234253 if self . state . protocol == DatabaseProtocol :: PostgreSQL {
235254 let full_variable = variable. iter ( ) . map ( |v| v. value . to_lowercase ( ) ) . join ( "_" ) ;
@@ -256,7 +275,7 @@ impl QueryRouter {
256275 ) ?
257276 } ;
258277
259- self . create_df_logical_plan ( stmt, & mut None , span_id. clone ( ) )
278+ self . create_df_logical_plan ( stmt, & mut None , span_id. clone ( ) , compiler_id )
260279 . await
261280 } else if name. eq_ignore_ascii_case ( "databases" ) || name. eq_ignore_ascii_case ( "schemas" ) {
262281 Ok ( QueryPlan :: MetaTabular (
@@ -289,7 +308,7 @@ impl QueryRouter {
289308 & mut None ,
290309 ) ?;
291310
292- self . create_df_logical_plan ( stmt, & mut None , span_id. clone ( ) )
311+ self . create_df_logical_plan ( stmt, & mut None , span_id. clone ( ) , compiler_id )
293312 . await
294313 } else if name. eq_ignore_ascii_case ( "warnings" ) {
295314 Ok ( QueryPlan :: MetaTabular (
@@ -322,6 +341,7 @@ impl QueryRouter {
322341 } ,
323342 & mut None ,
324343 span_id. clone ( ) ,
344+ compiler_id,
325345 )
326346 . await
327347 }
@@ -693,8 +713,8 @@ impl QueryRouter {
693713 stmt : ast:: Statement ,
694714 qtrace : & mut Option < Qtrace > ,
695715 span_id : Option < Arc < SpanId > > ,
716+ compiler_id : Uuid ,
696717 ) -> CompilationResult < QueryPlan > {
697- self . reauthenticate_if_needed ( ) . await ?;
698718 match & stmt {
699719 ast:: Statement :: Query ( query) => match & query. body {
700720 ast:: SetExpr :: Select ( select) if select. into . is_some ( ) => {
@@ -709,7 +729,14 @@ impl QueryRouter {
709729
710730 let sql_query_engine = SqlQueryEngine :: new ( self . session_manager . clone ( ) ) ;
711731 sql_query_engine
712- . plan ( stmt, qtrace, span_id, self . meta . clone ( ) , self . state . clone ( ) )
732+ . plan (
733+ stmt,
734+ qtrace,
735+ span_id,
736+ self . meta . clone ( ) ,
737+ self . state . clone ( ) ,
738+ compiler_id,
739+ )
713740 . await
714741 }
715742}
0 commit comments