Skip to content

Commit

Permalink
refactor(frontend): refactor explain for CREATE TABLE (#13406)
Browse files Browse the repository at this point in the history
Co-authored-by: Bugen Zhao <[email protected]>
  • Loading branch information
kwannoel and BugenZhao authored Nov 15, 2023
1 parent e313f22 commit e156663
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 85 deletions.
90 changes: 59 additions & 31 deletions src/frontend/src/handler/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,43 +936,21 @@ fn derive_connect_properties(
Ok(connect_properties.into_iter().collect())
}

#[allow(clippy::too_many_arguments)]
pub async fn handle_create_table(
handler_args: HandlerArgs,
pub(super) async fn handle_create_table_plan(
context: OptimizerContext,
col_id_gen: ColumnIdGenerator,
source_schema: Option<ConnectorSchema>,
cdc_table_info: Option<CdcTableInfo>,
table_name: ObjectName,
column_defs: Vec<ColumnDef>,
constraints: Vec<TableConstraint>,
if_not_exists: bool,
source_schema: Option<ConnectorSchema>,
source_watermarks: Vec<SourceWatermark>,
append_only: bool,
notice: Option<String>,
cdc_table_info: Option<CdcTableInfo>,
) -> Result<RwPgResponse> {
let session = handler_args.session.clone();
// TODO(st1page): refactor it
if let Some(notice) = notice {
session.notice_to_user(notice)
}

if append_only {
session.notice_to_user("APPEND ONLY TABLE is currently an experimental feature.");
}

if let Either::Right(resp) = session.check_relation_name_duplicated(
table_name.clone(),
StatementType::CREATE_TABLE,
if_not_exists,
)? {
return Ok(resp);
}

let (graph, source, table, job_type) = {
let context = OptimizerContext::from_handler_args(handler_args);
let source_schema = check_create_table_with_source(context.with_options(), source_schema)?;
let col_id_gen = ColumnIdGenerator::new_initial();
) -> Result<(PlanRef, Option<PbSource>, PbTable, TableJobType)> {
let source_schema = check_create_table_with_source(context.with_options(), source_schema)?;

let ((plan, source, table), job_type) = match (source_schema, cdc_table_info.as_ref()) {
let ((plan, source, table), job_type) =
match (source_schema, cdc_table_info.as_ref()) {
(Some(source_schema), None) => (
gen_create_table_plan_with_source(
context,
Expand Down Expand Up @@ -1020,6 +998,56 @@ pub async fn handle_create_table(
)
.into()),
};
Ok((plan, source, table, job_type))
}

#[allow(clippy::too_many_arguments)]
pub async fn handle_create_table(
handler_args: HandlerArgs,
table_name: ObjectName,
column_defs: Vec<ColumnDef>,
constraints: Vec<TableConstraint>,
if_not_exists: bool,
source_schema: Option<ConnectorSchema>,
source_watermarks: Vec<SourceWatermark>,
append_only: bool,
notice: Option<String>,
cdc_table_info: Option<CdcTableInfo>,
) -> Result<RwPgResponse> {
let session = handler_args.session.clone();
// TODO(st1page): refactor it
if let Some(notice) = notice {
session.notice_to_user(notice)
}

if append_only {
session.notice_to_user("APPEND ONLY TABLE is currently an experimental feature.");
}

if let Either::Right(resp) = session.check_relation_name_duplicated(
table_name.clone(),
StatementType::CREATE_TABLE,
if_not_exists,
)? {
return Ok(resp);
}

let (graph, source, table, job_type) = {
let context = OptimizerContext::from_handler_args(handler_args);
let col_id_gen = ColumnIdGenerator::new_initial();
let (plan, source, table, job_type) = handle_create_table_plan(
context,
col_id_gen,
source_schema,
cdc_table_info,
table_name.clone(),
column_defs,
constraints,
source_watermarks,
append_only,
)
.await?;

let mut graph = build_graph(plan);
graph.parallelism = session
.config()
Expand Down
70 changes: 16 additions & 54 deletions src/frontend/src/handler/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ use risingwave_sqlparser::ast::{ExplainOptions, ExplainType, Statement};
use super::create_index::gen_create_index_plan;
use super::create_mv::gen_create_mv_plan;
use super::create_sink::gen_sink_plan;
use super::create_table::{
check_create_table_with_source, gen_create_table_plan, gen_create_table_plan_with_source,
ColumnIdGenerator,
};
use super::create_table::ColumnIdGenerator;
use super::query::gen_batch_plan_by_statement;
use super::RwPgResponse;
use crate::handler::create_table::gen_create_table_plan_for_cdc_source;
use crate::handler::create_table::handle_create_table_plan;
use crate::handler::HandlerArgs;
use crate::optimizer::plan_node::generic::GenericPlanRef;
use crate::optimizer::plan_node::{Convention, Explain};
Expand Down Expand Up @@ -65,63 +62,28 @@ async fn do_handle_explain(
cdc_table_info,
..
} => {
// TODO(st1page): refacor it
let col_id_gen = ColumnIdGenerator::new_initial();
// TODO(st1page): refactor it
let (source_schema, notice) = match source_schema {
Some(s) => {
let (s, notice) = s.into_source_schema_v2();
(Some(s), notice)
}
None => (None, None),
};
let with_options = context.with_options();
let source_schema = check_create_table_with_source(with_options, source_schema)?;
let plan = match (source_schema, cdc_table_info) {
(Some(s), None) => {
gen_create_table_plan_with_source(
context,
name,
columns,
constraints,
s,
source_watermarks,
ColumnIdGenerator::new_initial(),
append_only,
)
.await?
.0
}
(None, None) => {
gen_create_table_plan(
context,
name,
columns,
constraints,
ColumnIdGenerator::new_initial(),
source_watermarks,
append_only,
)?
.0
}


(None, Some(cdc_table)) => {
gen_create_table_plan_for_cdc_source(
context.into(),
cdc_table.source_name.clone(),
name.clone(),
cdc_table.external_table_name.clone(),
columns,
constraints,
ColumnIdGenerator::new_initial(),
)?.0
}
(Some(_), Some(_)) => return Err(ErrorCode::NotSupported(
"Data format and encoding format doesn't apply to table created from a CDC source"
.into(),
"Remove the FORMAT and ENCODE specification".into(),
)
.into()),
};
let (plan, _source, _table, _job_type) = handle_create_table_plan(
context,
col_id_gen,
source_schema,
cdc_table_info,
name.clone(),
columns,
constraints,
source_watermarks,
append_only,
)
.await?;
let context = plan.ctx();
if let Some(notice) = notice {
context.warn_to_user(notice);
Expand Down

0 comments on commit e156663

Please sign in to comment.