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 ef3bf83
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 27 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
4 changes: 2 additions & 2 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
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
4 changes: 2 additions & 2 deletions query/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'a> Binder {
#[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

0 comments on commit ef3bf83

Please sign in to comment.