Skip to content

Commit

Permalink
remove mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Aug 11, 2022
1 parent 5c711dd commit 154f648
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 39 deletions.
6 changes: 4 additions & 2 deletions query/src/sql/planner/binder/bind_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -73,7 +75,7 @@ pub struct BindContext {
/// Format type of query output.
pub format: Option<String>,

pub ctes_map: HashMap<String, CteInfo>,
pub ctes_map: Arc<RwLock<HashMap<String, CteInfo>>>,
}

#[derive(Clone, Debug)]
Expand All @@ -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())),
}
}

Expand Down
10 changes: 5 additions & 5 deletions query/src/sql/planner/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plan> {
match (&stmt.src, &stmt.dst) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -409,7 +409,7 @@ impl<'a> Binder {
/// Bind COPY INFO <stage_location> FROM <query>
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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion query/src/sql/planner/binder/ddl/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plan> {
let ShowDatabasesStmt { limit } = stmt;
Expand Down
18 changes: 8 additions & 10 deletions query/src/sql/planner/binder/ddl/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plan> {
let ShowTablesStmt {
Expand Down Expand Up @@ -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<Plan> {
let ShowTablesStatusStmt {
Expand Down Expand Up @@ -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()
Expand All @@ -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<DataField> = bind_context
.columns
.iter()
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion query/src/sql/planner/binder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl QueryASTIRVisitor<HashSet<String>> 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<Expr<'a>>,
) -> Result<Plan> {
Expand Down
2 changes: 1 addition & 1 deletion query/src/sql/planner/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Plan> {
let InsertStmt {
Expand Down
2 changes: 1 addition & 1 deletion query/src/sql/planner/binder/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
8 changes: 4 additions & 4 deletions query/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ impl<'a> Binder {
}

pub async fn bind(mut self, stmt: &Statement<'a>) -> Result<Plan> {
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<Plan> {
let plan = match stmt {
Expand Down Expand Up @@ -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<Plan> {
Expand Down
13 changes: 7 additions & 6 deletions query/src/sql/planner/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)> {
Expand Down Expand Up @@ -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)> {
Expand All @@ -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}"
)));
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions query/src/sql/planner/binder/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShowLimit<'a>>,
) -> Result<Plan> {
// rewrite show functions to select * from system.functions ...
Expand Down Expand Up @@ -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<String>,
) -> Result<Plan> {
let sub_query = like
Expand Down
7 changes: 3 additions & 4 deletions query/src/sql/planner/binder/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -240,7 +239,7 @@ impl<'a> Binder {

fn bind_cte(
&mut self,
bind_context: &mut BindContext,
bind_context: &BindContext,
table_name: &str,
alias: &Option<TableAlias>,
cte_info: &CteInfo,
Expand Down
4 changes: 2 additions & 2 deletions query/src/sql/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 154f648

Please sign in to comment.