From 154f6489377075a15078c28f36da29f07675b516 Mon Sep 17 00:00:00 2001 From: xudong963 Date: Thu, 11 Aug 2022 08:08:52 +0800 Subject: [PATCH] remove mutable --- query/src/sql/planner/binder/bind_context.rs | 6 ++++-- query/src/sql/planner/binder/copy.rs | 10 +++++----- query/src/sql/planner/binder/ddl/database.rs | 2 +- query/src/sql/planner/binder/ddl/table.rs | 18 ++++++++---------- query/src/sql/planner/binder/delete.rs | 2 +- query/src/sql/planner/binder/insert.rs | 2 +- query/src/sql/planner/binder/join.rs | 2 +- query/src/sql/planner/binder/mod.rs | 8 ++++---- query/src/sql/planner/binder/select.rs | 13 +++++++------ query/src/sql/planner/binder/show.rs | 4 ++-- query/src/sql/planner/binder/table.rs | 7 +++---- query/src/sql/planner/semantic/type_check.rs | 4 ++-- 12 files changed, 39 insertions(+), 39 deletions(-) diff --git a/query/src/sql/planner/binder/bind_context.rs b/query/src/sql/planner/binder/bind_context.rs index 3a4dd5f419222..6bff9091ef9f9 100644 --- a/query/src/sql/planner/binder/bind_context.rs +++ b/query/src/sql/planner/binder/bind_context.rs @@ -13,6 +13,7 @@ // limitations under the License. use std::collections::HashMap; +use std::sync::Arc; use common_ast::ast::TableAlias; use common_ast::parser::token::Token; @@ -23,6 +24,7 @@ use common_datavalues::DataSchemaRefExt; use common_datavalues::DataTypeImpl; use common_exception::ErrorCode; use common_exception::Result; +use parking_lot::RwLock; use super::AggregateInfo; use crate::sql::common::IndexType; @@ -73,7 +75,7 @@ pub struct BindContext { /// Format type of query output. pub format: Option, - pub ctes_map: HashMap, + pub ctes_map: Arc>>, } #[derive(Clone, Debug)] @@ -91,7 +93,7 @@ impl BindContext { aggregate_info: AggregateInfo::default(), in_grouping: false, format: None, - ctes_map: HashMap::new(), + ctes_map: Arc::new(RwLock::new(HashMap::new())), } } diff --git a/query/src/sql/planner/binder/copy.rs b/query/src/sql/planner/binder/copy.rs index 2c60241829649..f88a7f4811194 100644 --- a/query/src/sql/planner/binder/copy.rs +++ b/query/src/sql/planner/binder/copy.rs @@ -43,7 +43,7 @@ use crate::sql::BindContext; impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_copy( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &CopyStmt<'a>, ) -> Result { match (&stmt.src, &stmt.dst) { @@ -313,7 +313,7 @@ impl<'a> Binder { #[allow(clippy::too_many_arguments)] async fn bind_copy_from_table_into_stage( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &CopyStmt<'a>, src_catalog_name: &str, src_database_name: &str, @@ -359,7 +359,7 @@ impl<'a> Binder { #[allow(clippy::too_many_arguments)] async fn bind_copy_from_table_into_uri( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &CopyStmt<'a>, src_catalog_name: &str, src_database_name: &str, @@ -409,7 +409,7 @@ impl<'a> Binder { /// Bind COPY INFO FROM async fn bind_copy_from_query_into_stage( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &CopyStmt<'a>, src_query: &Query<'_>, dst_stage: &str, @@ -439,7 +439,7 @@ impl<'a> Binder { #[allow(clippy::too_many_arguments)] async fn bind_copy_from_query_into_uri( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &CopyStmt<'a>, src_query: &Query<'_>, dst_uri_location: &UriLocation, diff --git a/query/src/sql/planner/binder/ddl/database.rs b/query/src/sql/planner/binder/ddl/database.rs index 31b35f2118384..e5dbbf954a96d 100644 --- a/query/src/sql/planner/binder/ddl/database.rs +++ b/query/src/sql/planner/binder/ddl/database.rs @@ -48,7 +48,7 @@ use crate::sql::BindContext; impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_show_databases( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &ShowDatabasesStmt<'a>, ) -> Result { let ShowDatabasesStmt { limit } = stmt; diff --git a/query/src/sql/planner/binder/ddl/table.rs b/query/src/sql/planner/binder/ddl/table.rs index 85f07a6082322..7ad45e9d791ac 100644 --- a/query/src/sql/planner/binder/ddl/table.rs +++ b/query/src/sql/planner/binder/ddl/table.rs @@ -118,7 +118,7 @@ impl SelectBuilder { impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_show_tables( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &ShowTablesStmt<'a>, ) -> Result { let ShowTablesStmt { @@ -257,7 +257,7 @@ impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_show_tables_status( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &ShowTablesStatusStmt<'a>, ) -> Result { let ShowTablesStatusStmt { @@ -357,9 +357,8 @@ impl<'a> Binder { } (None, Some(query)) => { // `CREATE TABLE AS SELECT ...` without column definitions - let mut init_bind_context = BindContext::new(); - let (_s_expr, bind_context) = - self.bind_query(&mut init_bind_context, query).await?; + let init_bind_context = BindContext::new(); + let (_s_expr, bind_context) = self.bind_query(&init_bind_context, query).await?; let fields = bind_context .columns .iter() @@ -376,9 +375,8 @@ impl<'a> Binder { // e.g. `CREATE TABLE t (i INT) AS SELECT * from old_t` with columns speicified let (source_schema, source_coments) = self.analyze_create_table_schema(source).await?; - let mut init_bind_context = BindContext::new(); - let (_s_expr, bind_context) = - self.bind_query(&mut init_bind_context, query).await?; + let init_bind_context = BindContext::new(); + let (_s_expr, bind_context) = self.bind_query(&init_bind_context, query).await?; let query_fields: Vec = bind_context .columns .iter() @@ -445,9 +443,9 @@ impl<'a> Binder { table_meta, cluster_keys, as_select: if let Some(query) = as_query { - let mut bind_context = BindContext::new(); + let bind_context = BindContext::new(); let stmt = Statement::Query(Box::new(*query.clone())); - let select_plan = self.bind_statement(&mut bind_context, &stmt).await?; + let select_plan = self.bind_statement(&bind_context, &stmt).await?; // Don't enable distributed optimization for `CREATE TABLE ... AS SELECT ...` for now let opt_ctx = Arc::new(OptimizerContext::new(OptimizerConfig::default())); let optimized_plan = optimize(self.ctx.clone(), opt_ctx, select_plan)?; diff --git a/query/src/sql/planner/binder/delete.rs b/query/src/sql/planner/binder/delete.rs index eba25bcfe50db..94a56e897153d 100644 --- a/query/src/sql/planner/binder/delete.rs +++ b/query/src/sql/planner/binder/delete.rs @@ -49,7 +49,7 @@ impl QueryASTIRVisitor> for DeleteCollectPushDowns { impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_delete( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, table_reference: &'a TableReference<'a>, selection: &'a Option>, ) -> Result { diff --git a/query/src/sql/planner/binder/insert.rs b/query/src/sql/planner/binder/insert.rs index 2e34279e150f0..06b6a5bd420b9 100644 --- a/query/src/sql/planner/binder/insert.rs +++ b/query/src/sql/planner/binder/insert.rs @@ -62,7 +62,7 @@ use crate::sql::MetadataRef; impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_insert( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &InsertStmt<'a>, ) -> Result { let InsertStmt { diff --git a/query/src/sql/planner/binder/join.rs b/query/src/sql/planner/binder/join.rs index b173c031a5361..c0768a2ce15ad 100644 --- a/query/src/sql/planner/binder/join.rs +++ b/query/src/sql/planner/binder/join.rs @@ -45,7 +45,7 @@ impl<'a> Binder { #[async_recursion] pub(super) async fn bind_join( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, join: &Join<'a>, ) -> Result<(SExpr, BindContext)> { let (left_child, left_context) = diff --git a/query/src/sql/planner/binder/mod.rs b/query/src/sql/planner/binder/mod.rs index 523a59dff31ee..a27b2b2da2fba 100644 --- a/query/src/sql/planner/binder/mod.rs +++ b/query/src/sql/planner/binder/mod.rs @@ -96,14 +96,14 @@ impl<'a> Binder { } pub async fn bind(mut self, stmt: &Statement<'a>) -> Result { - let mut init_bind_context = BindContext::new(); - self.bind_statement(&mut init_bind_context, stmt).await + let init_bind_context = BindContext::new(); + self.bind_statement(&init_bind_context, stmt).await } #[async_recursion::async_recursion] async fn bind_statement( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &Statement<'a>, ) -> Result { let plan = match stmt { @@ -317,7 +317,7 @@ impl<'a> Binder { async fn bind_rewrite_to_query( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, query: &str, rewrite_kind_r: RewriteKind, ) -> Result { diff --git a/query/src/sql/planner/binder/select.rs b/query/src/sql/planner/binder/select.rs index 3c85cfdc626e1..89cfdc939ef0d 100644 --- a/query/src/sql/planner/binder/select.rs +++ b/query/src/sql/planner/binder/select.rs @@ -57,7 +57,7 @@ pub struct SelectItem<'a> { impl<'a> Binder { pub(super) async fn bind_select_stmt( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, stmt: &SelectStmt<'a>, order_by: &[OrderByExpr<'a>], ) -> Result<(SExpr, BindContext)> { @@ -158,7 +158,7 @@ impl<'a> Binder { #[async_recursion] pub(crate) async fn bind_set_expr( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, set_expr: &SetExpr, order_by: &[OrderByExpr], ) -> Result<(SExpr, BindContext)> { @@ -181,13 +181,13 @@ impl<'a> Binder { #[async_recursion] pub(crate) async fn bind_query( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, query: &Query<'_>, ) -> Result<(SExpr, BindContext)> { if let Some(with) = &query.with { for cte in with.ctes.iter() { let table_name = cte.alias.name.name.clone(); - if bind_context.ctes_map.contains_key(&table_name) { + if bind_context.ctes_map.read().contains_key(&table_name) { return Err(ErrorCode::SemanticError(format!( "duplicate cte {table_name}" ))); @@ -198,7 +198,8 @@ impl<'a> Binder { s_expr, bind_context: cte_bind_context.clone(), }; - bind_context.ctes_map.insert(table_name, cte_info); + let mut ctes_map = bind_context.ctes_map.write(); + ctes_map.insert(table_name, cte_info); } } let (mut s_expr, mut bind_context) = match query.body { @@ -270,7 +271,7 @@ impl<'a> Binder { pub(super) async fn bind_set_operator( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, left: &SetExpr<'_>, right: &SetExpr<'_>, op: &SetOperator, diff --git a/query/src/sql/planner/binder/show.rs b/query/src/sql/planner/binder/show.rs index 921d75b231956..43eb29e023381 100644 --- a/query/src/sql/planner/binder/show.rs +++ b/query/src/sql/planner/binder/show.rs @@ -23,7 +23,7 @@ use crate::sql::Binder; impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_show_functions( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, limit: &Option>, ) -> Result { // rewrite show functions to select * from system.functions ... @@ -51,7 +51,7 @@ impl<'a> Binder { pub(in crate::sql::planner::binder) async fn bind_show_settings( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, like: &Option, ) -> Result { let sub_query = like diff --git a/query/src/sql/planner/binder/table.rs b/query/src/sql/planner/binder/table.rs index 8d036256eed30..2ed39ef8c1e8d 100644 --- a/query/src/sql/planner/binder/table.rs +++ b/query/src/sql/planner/binder/table.rs @@ -83,7 +83,7 @@ impl<'a> Binder { pub(super) async fn bind_table_reference( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, table_ref: &TableReference<'a>, ) -> Result<(SExpr, BindContext)> { match table_ref { @@ -97,8 +97,7 @@ impl<'a> Binder { } => { let table_name = normalize_identifier(table, &self.name_resolution_ctx).name; // Check and bind common table expression - let ctes = bind_context.ctes_map.clone(); - if let Some(cte_info) = ctes.get(&table_name) { + if let Some(cte_info) = bind_context.ctes_map.read().get(&table_name) { return self.bind_cte(bind_context, &table_name, alias, cte_info); } // Get catalog name @@ -240,7 +239,7 @@ impl<'a> Binder { fn bind_cte( &mut self, - bind_context: &mut BindContext, + bind_context: &BindContext, table_name: &str, alias: &Option, cte_info: &CteInfo, diff --git a/query/src/sql/planner/semantic/type_check.rs b/query/src/sql/planner/semantic/type_check.rs index d1065821ba899..5b916a1ea7ea0 100644 --- a/query/src/sql/planner/semantic/type_check.rs +++ b/query/src/sql/planner/semantic/type_check.rs @@ -1330,8 +1330,8 @@ impl<'a> TypeChecker<'a> { ); // Create new `BindContext` with current `bind_context` as its parent, so we can resolve outer columns. - let mut bind_context = BindContext::with_parent(Box::new(self.bind_context.clone())); - let (s_expr, output_context) = binder.bind_query(&mut bind_context, subquery).await?; + let bind_context = BindContext::with_parent(Box::new(self.bind_context.clone())); + let (s_expr, output_context) = binder.bind_query(&bind_context, subquery).await?; if (typ == SubqueryType::Scalar || typ == SubqueryType::Any) && output_context.columns.len() > 1