Skip to content

Commit

Permalink
fix bug: insert/update sk get incorrect value (#20550)
Browse files Browse the repository at this point in the history
fix bug: insert/update sk get incorrect value

Approved by: @aressu1985, @badboynt1
  • Loading branch information
ouyuanning authored Dec 4, 2024
1 parent 5faa2c0 commit 2611646
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/plan/bind_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ func (builder *QueryBuilder) appendNodesForInsertStmt(

skipUniqueIdx[i] = true
for _, part := range idxDef.Parts {
if !columnIsNull[part] {
if !columnIsNull[catalog.ResolveAlias(part)] {
skipUniqueIdx[i] = false
break
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/sql/plan/bind_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func (builder *QueryBuilder) bindUpdate(stmt *tree.Update, bindCtx *BindContext)
}

for _, colName := range idxDef.Parts {
pkAndUkCols[colName] = true
realColName := catalog.ResolveAlias(colName)
pkAndUkCols[realColName] = true
}
}

Expand Down Expand Up @@ -233,7 +234,8 @@ func (builder *QueryBuilder) bindUpdate(stmt *tree.Update, bindCtx *BindContext)
}

for _, colName := range idxDef.Parts {
if _, ok := updateColName2Idx[alias+"."+colName]; ok {
realColName := catalog.ResolveAlias(colName)
if _, ok := updateColName2Idx[alias+"."+realColName]; ok {
idxNeedUpdate[i][j] = true
break
}
Expand Down Expand Up @@ -426,8 +428,9 @@ func (builder *QueryBuilder) bindUpdate(stmt *tree.Update, bindCtx *BindContext)
args := make([]*plan.Expr, len(idxDef.Parts))

for k, colName := range idxDef.Parts {
colPos := int32(colName2Idx[alias+"."+colName])
if updateIdx, ok := updateColName2Idx[alias+"."+colName]; ok {
realColName := catalog.ResolveAlias(colName)
colPos := int32(colName2Idx[alias+"."+realColName])
if updateIdx, ok := updateColName2Idx[alias+"."+realColName]; ok {
colPos = int32(updateIdx)
}
args[k] = &plan.Expr{
Expand Down
8 changes: 8 additions & 0 deletions test/distributed/cases/dml/insert/not_null_check.result
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ drop table if exists t1;
create table t1(a bigint primary key, b int, c int, key(b));
insert into t1 select result,result,result from generate_series(200000) g;
update t1 set b = b + 1;
drop table if exists t1;
create table t1(a varchar(10), b varchar(10), c varchar(10), primary key(b), key(c));
insert into t1 values ('a','b','c');
update t1 set c ='ccc';
delete from t1 where b='b';
select count(*) from t1;
count(*)
0
drop database if exists test;
6 changes: 6 additions & 0 deletions test/distributed/cases/dml/insert/not_null_check.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ drop table if exists t1;
create table t1(a bigint primary key, b int, c int, key(b));
insert into t1 select result,result,result from generate_series(200000) g;
update t1 set b = b + 1;
drop table if exists t1;
create table t1(a varchar(10), b varchar(10), c varchar(10), primary key(b), key(c));
insert into t1 values ('a','b','c');
update t1 set c ='ccc';
delete from t1 where b='b';
select count(*) from t1;
drop database if exists test;

0 comments on commit 2611646

Please sign in to comment.