Skip to content

Commit 49ee26a

Browse files
zhyassdantengsky
andauthored
fix(storage): change tracking with alias update row_version bug in merge into (#15412)
* fix change tracking with alias update row_version bug * Update src/query/sql/src/planner/binder/merge_into.rs * add test * fix test --------- Co-authored-by: dantengsky <dantengsky@gmail.com>
1 parent 06d7ed3 commit 49ee26a

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/query/sql/src/planner/binder/merge_into.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,28 @@ impl Binder {
365365
columns_set = columns_set.union(&join_column_set).cloned().collect();
366366
}
367367

368+
// If the table alias is not None, after the binding phase, the bound columns will have
369+
// a database of 'None' and the table named as the alias.
370+
// Thus, we adjust them accordingly.
371+
let target_name = if let Some(target_identify) = target_alias {
372+
normalize_identifier(&target_identify.name, &self.name_resolution_ctx)
373+
.name
374+
.clone()
375+
} else {
376+
table_name.clone()
377+
};
378+
368379
let has_update = self.has_update(&matched_clauses);
369380
let update_row_version = if table.change_tracking_enabled() && has_update {
370381
Some(Self::update_row_version(
371382
table.schema_with_stream(),
372383
&bind_ctx.columns,
373-
Some(&database_name),
374-
Some(&table_name),
384+
if target_alias.is_none() {
385+
Some(&database_name)
386+
} else {
387+
None
388+
},
389+
Some(&target_name),
375390
)?)
376391
} else {
377392
None
@@ -400,14 +415,6 @@ impl Binder {
400415
}
401416
}
402417

403-
let target_name = if let Some(target_identify) = target_alias {
404-
normalize_identifier(&target_identify.name, &self.name_resolution_ctx)
405-
.name
406-
.clone()
407-
} else {
408-
table_name.clone()
409-
};
410-
411418
// bind matched clause columns and add update fields and exprs
412419
for clause in &matched_clauses {
413420
matched_evaluators.push(

tests/sqllogictests/suites/base/09_fuse_engine/09_0011_change_tracking.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,33 @@ drop table t3 all
195195
# end of issue 14955 #
196196
######################
197197

198+
###############
199+
# issue 15412 #
200+
###############
201+
202+
statement ok
203+
CREATE TABLE test_select (id Int64, name STRING) change_tracking = true
204+
205+
statement ok
206+
insert into test_select values(1, 'Wu')
207+
208+
query I
209+
merge into db1.test_select t1 using (select * from db1.test_select) t2 on t1.id = t2.id when matched then update set t1.id=t2.id, t1.name='{}'
210+
----
211+
1
212+
213+
query ITI
214+
select id, name, _row_version from test_select
215+
----
216+
1 {} 1
217+
218+
statement ok
219+
drop table test_select all
220+
221+
######################
222+
# end of issue 15412 #
223+
######################
224+
198225
statement ok
199226
set enable_experimental_merge_into = 0
200227

0 commit comments

Comments
 (0)