Skip to content

Commit

Permalink
add explian for merge into
Browse files Browse the repository at this point in the history
  • Loading branch information
JackTan25 committed Dec 13, 2023
1 parent 193ed56 commit 7abd6ea
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion src/query/sql/src/planner/format/display_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ use common_exception::Result;
use common_expression::types::DataType;
use common_expression::types::NumberDataType;
use common_expression::ROW_ID_COL_NAME;
use itertools::Itertools;

use crate::binder::ColumnBindingBuilder;
use crate::format_scalar;
use crate::optimizer::SExpr;
use crate::planner::format::display_rel_operator::FormatContext;
use crate::plans::BoundColumnRef;
use crate::plans::CreateTablePlan;
use crate::plans::DeletePlan;
use crate::plans::EvalScalar;
use crate::plans::Filter;
use crate::plans::MergeInto;
use crate::plans::Plan;
use crate::plans::RelOperator;
use crate::plans::ScalarItem;
Expand Down Expand Up @@ -110,7 +113,7 @@ impl Plan {
// Insert
Plan::Insert(_) => Ok("Insert".to_string()),
Plan::Replace(_) => Ok("Replace".to_string()),
Plan::MergeInto(_) => Ok("MergeInto".to_string()),
Plan::MergeInto(merge_into) => format_merge_into(merge_into),
Plan::Delete(delete) => format_delete(delete),
Plan::Update(_) => Ok("Update".to_string()),

Expand Down Expand Up @@ -264,3 +267,88 @@ fn format_create_table(create_table: &CreateTablePlan) -> Result<String> {
None => Ok("CreateTable".to_string()),
}
}

fn format_merge_into(merge_into: &MergeInto) -> Result<String> {
// add merge into target_table
let table_index = merge_into
.meta_data
.read()
.get_table_index(
Some(merge_into.database.as_str()),
merge_into.table.as_str(),
)
.unwrap();

let table_entry = merge_into.meta_data.read().table(table_index).clone();
let target_table_format = FormatContext::Text(format!(
"target_table: {}.{}.{}",
table_entry.catalog(),
table_entry.database(),
table_entry.name(),
));

// add macthed clauses
let mut matched_children = Vec::with_capacity(merge_into.matched_evaluators.len() as usize);
let taregt_schema = table_entry.table().schema();
for evaluator in &merge_into.matched_evaluators {
let condition_format = evaluator.condition.as_ref().map_or_else(
|| "condition: None".to_string(),
|predicate| format!("condition: {}", format_scalar(predicate)),
);
if evaluator.update.is_none() {
matched_children.push(FormatTreeNode::new(FormatContext::Text(format!(
"matched delete: [{}]",
condition_format
))));
} else {
let update_format = evaluator
.update
.as_ref()
.unwrap()
.iter()
.map(|(field_idx, expr)| {
format!(
"{} = {}",
taregt_schema.field(*field_idx).name().to_string(),
format_scalar(expr)
)
})
.join(",");
matched_children.push(FormatTreeNode::new(FormatContext::Text(format!(
"matched update: [{},update set {}]",
condition_format, update_format
))));
}
}
// add unmacthed clauses
let mut unmatched_children = Vec::with_capacity(merge_into.unmatched_evaluators.len() as usize);
for evaluator in &merge_into.unmatched_evaluators {
let condition_format = evaluator.condition.as_ref().map_or_else(
|| "condition: None".to_string(),
|predicate| format!("condition: {}", format_scalar(predicate)),
);
let insert_schema_format = evaluator
.source_schema
.fields
.iter()
.map(|field| field.name())
.join(",");
let values_format = evaluator.values.iter().map(format_scalar).join(",");
let unmatched_format = format!(
"insert into ({}) values({})",
insert_schema_format, values_format
);
unmatched_children.push(FormatTreeNode::new(FormatContext::Text(format!(
"unmatched insert: [{},{}]",
condition_format, unmatched_format
))));
}
let s_expr = merge_into.input.as_ref();
let input_format_child = s_expr.to_format_tree(&merge_into.meta_data);
let all_children = vec![matched_children, unmatched_children, vec![
input_format_child,
]]
.concat();
let res = FormatTreeNode::with_children(target_table_format, all_children).format_pretty()?;
Ok(format!("MergeInto:\n{res}"))
}

0 comments on commit 7abd6ea

Please sign in to comment.