@@ -223,6 +223,7 @@ impl Planner {
223223 #[ fastrace:: trace]
224224 pub async fn plan_stmt ( & mut self , stmt : & Statement , attach_query : bool ) -> Result < Plan > {
225225 let start = Instant :: now ( ) ;
226+ let query_kind = get_query_kind ( stmt) ;
226227 let settings = self . ctx . get_settings ( ) ;
227228 // Step 3: Bind AST with catalog, and generate a pure logical SExpr
228229 let name_resolution_ctx = NameResolutionContext :: try_from ( settings. as_ref ( ) ) ?;
@@ -243,8 +244,7 @@ impl Planner {
243244 info ! ( "logical plan from cache, time used: {:?}" , start. elapsed( ) ) ;
244245 if attach_query {
245246 // update for clickhouse handler
246- self . ctx
247- . attach_query_str ( get_query_kind ( stmt) , stmt. to_mask_sql ( ) ) ;
247+ self . ctx . attach_query_str ( query_kind, stmt. to_mask_sql ( ) ) ;
248248 }
249249 return Ok ( plan. plan ) ;
250250 }
@@ -260,11 +260,14 @@ impl Planner {
260260 )
261261 . with_subquery_executor ( self . query_executor . clone ( ) ) ;
262262
263- // Indicate binder there is no need to collect column statistics for the binding table.
263+ // must attach before bind, because ParquetRSTable::create used it.
264+ if attach_query {
265+ self . ctx . attach_query_str ( query_kind, stmt. to_mask_sql ( ) ) ;
266+ }
264267 let plan = binder. bind ( stmt) . await ?;
268+ // attach again to avoid the query kind is overwritten by the subquery
265269 if attach_query {
266- self . ctx
267- . attach_query_str ( get_query_kind ( stmt) , stmt. to_mask_sql ( ) ) ;
270+ self . ctx . attach_query_str ( query_kind, stmt. to_mask_sql ( ) ) ;
268271 }
269272
270273 // Step 4: Optimize the SExpr with optimizers, and generate optimized physical SExpr
0 commit comments