Skip to content
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

fix: add better explain for merge into #14005

Merged
merged 8 commits into from
Dec 13, 2023
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
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());
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(),
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());
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 = [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}"))
}
46 changes: 46 additions & 0 deletions tests/sqllogictests/suites/mode/standalone/explain/merge_into.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
statement ok
set enable_experimental_merge_into = 1;

statement ok
CREATE TABLE employees2 (employee_id INT, employee_name VARCHAR(255),department VARCHAR(255));

statement ok
CREATE TABLE salaries2 (employee_id INT,salary DECIMAL(10, 2));

statement ok
INSERT INTO employees2 VALUES(1, 'Alice', 'HR'),(2, 'Bob', 'IT'),(3, 'Charlie', 'Finance'),(4, 'David', 'HR');

statement ok
INSERT INTO salaries2 VALUES(1, 50000.00),(2, 60000.00);

query TT
MERGE INTO salaries2 USING (SELECT * FROM employees2) as employees2 ON salaries2.employee_id = employees2.employee_id WHEN MATCHED AND employees2.department = 'HR' THEN UPDATE SET salaries2.salary = salaries2.salary + 1000.00 WHEN MATCHED THEN UPDATE SET salaries2.salary = salaries2.salary + 500.00 WHEN NOT MATCHED THEN INSERT (employee_id, salary) VALUES (employees2.employee_id, 55000.00);
----
2 2

query T
explain MERGE INTO salaries2 USING (SELECT * FROM employees2) as employees2 ON salaries2.employee_id = employees2.employee_id WHEN MATCHED AND employees2.department = 'HR' THEN UPDATE SET salaries2.salary = salaries2.salary + 1000.00 WHEN MATCHED THEN UPDATE SET salaries2.salary = salaries2.salary + 500.00 WHEN NOT MATCHED THEN INSERT (employee_id, salary) VALUES (employees2.employee_id, 55000.00);
----
MergeInto:
target_table: default.default.salaries2
├── matched update: [condition: eq(employees2.department (#2), 'HR'),update set salary = plus(salaries2.salary (#4), 1000.00)]
├── matched update: [condition: None,update set salary = plus(salaries2.salary (#4), 500.00)]
├── unmatched insert: [condition: None,insert into (employee_id,salary) values(CAST(employees2.employee_id (#0) AS Int32 NULL),CAST(55000.00 AS Decimal(10, 2) NULL))]
└── HashJoin: RIGHT OUTER
├── equi conditions: [eq(salaries2.employee_id (#3), employees2.employee_id (#0))]
├── non-equi conditions: []
├── LogicalGet
│ ├── table: default.default.salaries2
│ ├── filters: []
│ ├── order by: []
│ └── limit: NONE
└── EvalScalar
├── scalars: [employees2.employee_id (#0), employees2.employee_name (#1), employees2.department (#2)]
└── LogicalGet
├── table: default.default.employees2
├── filters: []
├── order by: []
└── limit: NONE

statement ok
set enable_experimental_merge_into = 0;
Loading