Skip to content

refactor: Make planner depends on TableContext trait #7600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/query/catalog/src/table_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,7 @@ pub trait TableContext: Send + Sync {
// Get user manager api.
fn get_user_manager(&self) -> Arc<UserApiProvider>;
fn get_cluster(&self) -> Arc<Cluster>;
async fn get_table(&self, catalog: &str, database: &str, table: &str)
-> Result<Arc<dyn Table>>;
async fn get_processes_info(&self) -> Vec<ProcessInfo>;
}
1 change: 1 addition & 0 deletions src/query/service/src/api/http/v1/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tokio_stream::StreamExt;
use crate::sessions::QueryContext;
use crate::sessions::SessionManager;
use crate::sessions::SessionType;
use crate::sessions::TableContext;
use crate::storages::TableStreamReadWrap;
use crate::storages::ToReadDataSourcePlan;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::executor::PhysicalScalar;
use crate::sql::PlanParser;
use crate::storages::view::view_table::QUERY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;

pub struct ExistsTableInterpreter {
ctx: Arc<QueryContext>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use common_streams::SendableDataBlockStream;

use crate::interpreters::Interpreter;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;

pub struct TruncateTableInterpreter {
ctx: Arc<QueryContext>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use common_legacy_planners::PlanRewriter;

use crate::optimizers::Optimizer;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::storages::ToReadDataSourcePlan;

struct StatisticsExactImpl<'a> {
Expand Down
32 changes: 16 additions & 16 deletions src/query/service/src/sessions/query_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,6 @@ impl QueryContext {
StageTable::try_create(table_info.clone())
}

/// Fetch a Table by db and table name.
///
/// It guaranteed to return a consistent result for multiple calls, in a same query.
/// E.g.:
/// ```sql
/// SELECT * FROM (SELECT * FROM db.table_name) as subquery_1, (SELECT * FROM db.table_name) AS subquery_2
/// ```
pub async fn get_table(
&self,
catalog: &str,
database: &str,
table: &str,
) -> Result<Arc<dyn Table>> {
self.shared.get_table(catalog, database, table).await
}

pub async fn set_current_database(&self, new_database_name: String) -> Result<()> {
let tenant_id = self.get_tenant();
let catalog = self.get_catalog(self.get_current_catalog().as_str())?;
Expand Down Expand Up @@ -418,6 +402,22 @@ impl TableContext for QueryContext {
self.shared.get_cluster()
}

/// Fetch a Table by db and table name.
///
/// It guaranteed to return a consistent result for multiple calls, in a same query.
/// E.g.:
/// ```sql
/// SELECT * FROM (SELECT * FROM db.table_name) as subquery_1, (SELECT * FROM db.table_name) AS subquery_2
/// ```
async fn get_table(
&self,
catalog: &str,
database: &str,
table: &str,
) -> Result<Arc<dyn Table>> {
self.shared.get_table(catalog, database, table).await
}

// Get all the processes list info.
async fn get_processes_info(&self) -> Vec<ProcessInfo> {
SessionManager::instance().processes_info().await
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/src/sql/optimizer/heuristic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use once_cell::sync::Lazy;

use super::rule::RuleID;
use super::ColumnSet;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::optimizer::heuristic::decorrelate::decorrelate_subquery;
use crate::sql::optimizer::heuristic::implement::HeuristicImplementor;
pub use crate::sql::optimizer::heuristic::rule_list::RuleList;
Expand Down Expand Up @@ -65,14 +65,14 @@ pub struct HeuristicOptimizer {
rules: RuleList,
implementor: HeuristicImplementor,

_ctx: Arc<QueryContext>,
_ctx: Arc<dyn TableContext>,
bind_context: Box<BindContext>,
metadata: MetadataRef,
}

impl HeuristicOptimizer {
pub fn new(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
bind_context: Box<BindContext>,
metadata: MetadataRef,
rules: RuleList,
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/src/sql/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use self::util::contains_local_table_scan;
use self::util::validate_distributed_query;
use super::plans::Plan;
use super::BindContext;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
pub use crate::sql::optimizer::heuristic::RuleList;
pub use crate::sql::optimizer::rule::RuleID;
use crate::sql::optimizer::rule::RuleSet;
Expand All @@ -74,7 +74,7 @@ impl OptimizerContext {
}

pub fn optimize(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
opt_ctx: Arc<OptimizerContext>,
plan: Plan,
) -> Result<Plan> {
Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn optimize(
}

pub fn optimize_query(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
opt_ctx: Arc<OptimizerContext>,
metadata: MetadataRef,
bind_context: Box<BindContext>,
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use common_meta_types::UserStageInfo;
use common_storage::parse_uri_location;
use common_storage::UriLocation;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::normalize_identifier;
use crate::sql::plans::CopyPlanV2;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use common_meta_types::GrantObject;
use common_meta_types::UserOption;
use common_meta_types::UserPrivilegeSet;

use crate::sessions::TableContext;
use crate::sql::plans::Plan;
use crate::sql::Binder;

Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use common_legacy_planners::UndropDatabasePlan;
use common_meta_app::schema::DatabaseMeta;
use common_meta_app::share::ShareNameIdent;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::planner::semantic::normalize_identifier;
use crate::sql::plans::Plan;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use common_ast::ast::*;
use common_exception::Result;
use itertools::Itertools;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::normalize_identifier;
use crate::sql::plans::AlterShareTenantsPlan;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use common_meta_types::UserStageInfo;
use common_storage::parse_uri_location;
use common_storage::UriLocation;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::plans::Plan;
use crate::sql::statements::parse_copy_file_format_options;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use common_meta_app::schema::TableMeta;
use tracing::debug;

use crate::catalogs::DatabaseCatalog;
use crate::sessions::TableContext;
use crate::sql::binder::scalar::ScalarBinder;
use crate::sql::binder::Binder;
use crate::sql::binder::Visibility;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/ddl/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use common_legacy_planners::AlterViewPlan;
use common_legacy_planners::CreateViewPlan;
use common_legacy_planners::DropViewPlan;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::planner::semantic::normalize_identifier;
use crate::sql::plans::Plan;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use common_legacy_planners::DeletePlan;
use common_legacy_planners::Expression;
use common_legacy_planners::Projection;

use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::binder::ScalarBinder;
use crate::sql::executor::ExpressionBuilderWithoutRenaming;
Expand Down
7 changes: 3 additions & 4 deletions src/query/service/src/sql/planner/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use crate::clusters::ClusterHelper;
use crate::evaluator::EvalNode;
use crate::evaluator::Evaluator;
use crate::pipelines::processors::transforms::ExpressionTransformV2;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::binder::Binder;
use crate::sql::binder::ScalarBinder;
Expand Down Expand Up @@ -198,7 +197,7 @@ impl<'a> Binder {
}

pub struct ValueSourceV2<'a> {
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
bind_context: &'a BindContext,
schema: DataSchemaRef,
Expand All @@ -207,7 +206,7 @@ pub struct ValueSourceV2<'a> {

impl<'a> ValueSourceV2<'a> {
pub fn new(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
bind_context: &'a BindContext,
schema: DataSchemaRef,
Expand Down Expand Up @@ -446,7 +445,7 @@ fn fill_default_value(
async fn exprs_to_datavalue<'a>(
exprs: Vec<Expr<'a>>,
schema: &DataSchemaRef,
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &NameResolutionContext,
bind_context: &BindContext,
metadata: MetadataRef,
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/src/sql/planner/binder/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use common_datavalues::wrap_nullable;
use common_exception::ErrorCode;
use common_exception::Result;

use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::binder::scalar_common::split_conjunctions;
use crate::sql::binder::scalar_common::split_equivalent_predicate;
use crate::sql::binder::scalar_common::wrap_cast_if_needed;
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn check_duplicate_join_tables(
}

struct JoinConditionResolver<'a> {
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,

Expand All @@ -247,7 +247,7 @@ struct JoinConditionResolver<'a> {

impl<'a> JoinConditionResolver<'a> {
pub fn new(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,
left_context: &'a BindContext,
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/src/sql/planner/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use super::plans::Plan;
use super::plans::RewriteKind;
use super::semantic::NameResolutionContext;
use crate::catalogs::CatalogManager;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::planner::metadata::MetadataRef;

mod aggregate;
Expand Down Expand Up @@ -75,15 +75,15 @@ mod table;
/// - Validate expressions
/// - Build `Metadata`
pub struct Binder {
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
catalogs: Arc<CatalogManager>,
name_resolution_ctx: NameResolutionContext,
metadata: MetadataRef,
}

impl<'a> Binder {
pub fn new(
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
catalogs: Arc<CatalogManager>,
name_resolution_ctx: NameResolutionContext,
metadata: MetadataRef,
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/src/sql/planner/binder/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use common_ast::ast::Expr;
use common_datavalues::DataTypeImpl;
use common_exception::Result;

use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::planner::binder::BindContext;
use crate::sql::planner::metadata::MetadataRef;
use crate::sql::planner::semantic::NameResolutionContext;
Expand All @@ -28,7 +28,7 @@ use crate::sql::plans::Scalar;
/// Helper for binding scalar expression with `BindContext`.
pub struct ScalarBinder<'a> {
bind_context: &'a BindContext,
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,
aliases: &'a [(String, Scalar)],
Expand All @@ -37,7 +37,7 @@ pub struct ScalarBinder<'a> {
impl<'a> ScalarBinder<'a> {
pub fn new(
bind_context: &'a BindContext,
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,
aliases: &'a [(String, Scalar)],
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use common_exception::ErrorCode;
use common_exception::Result;

use super::bind_context::NameResolutionResult;
use crate::sessions::TableContext;
use crate::sql::binder::scalar::ScalarBinder;
use crate::sql::binder::select::SelectList;
use crate::sql::binder::Binder;
Expand Down
1 change: 0 additions & 1 deletion src/query/service/src/sql/planner/binder/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use common_exception::ErrorCode;
use common_exception::Result;
use common_legacy_planners::Expression;

use crate::sessions::TableContext;
use crate::sql::binder::scalar::ScalarBinder;
use crate::sql::binder::Binder;
use crate::sql::binder::ColumnBinding;
Expand Down
5 changes: 2 additions & 3 deletions src/query/service/src/sql/planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use parking_lot::RwLock;
pub use plans::ScalarExpr;

use crate::clusters::ClusterHelper;
use crate::sessions::QueryContext;
use crate::sql::optimizer::optimize;
pub use crate::sql::planner::binder::BindContext;

Expand Down Expand Up @@ -56,11 +55,11 @@ const PROBE_INSERT_INITIAL_TOKENS: usize = 128;
const PROBE_INSERT_MAX_TOKENS: usize = 128 * 8;

pub struct Planner {
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
}

impl Planner {
pub fn new(ctx: Arc<QueryContext>) -> Self {
pub fn new(ctx: Arc<dyn TableContext>) -> Self {
Planner { ctx }
}

Expand Down
5 changes: 2 additions & 3 deletions src/query/service/src/sql/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ use common_legacy_planners::validate_function_arg;
use super::name_resolution::NameResolutionContext;
use super::normalize_identifier;
use crate::evaluator::Evaluator;
use crate::sessions::QueryContext;
use crate::sessions::TableContext;
use crate::sql::binder::wrap_cast_if_needed;
use crate::sql::binder::Binder;
Expand Down Expand Up @@ -90,7 +89,7 @@ use crate::sql::ScalarExpr;
/// argument types of expressions, or unresolvable columns.
pub struct TypeChecker<'a> {
bind_context: &'a BindContext,
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,

Expand All @@ -104,7 +103,7 @@ pub struct TypeChecker<'a> {
impl<'a> TypeChecker<'a> {
pub fn new(
bind_context: &'a BindContext,
ctx: Arc<QueryContext>,
ctx: Arc<dyn TableContext>,
name_resolution_ctx: &'a NameResolutionContext,
metadata: MetadataRef,
aliases: &'a [(String, Scalar)],
Expand Down
Loading