Skip to content

Commit 5efe089

Browse files
authored
feat(frontend): ban update statements modifying pk columns (risingwavelabs#8569)
Signed-off-by: Clearlove <[email protected]>
1 parent 25a9127 commit 5efe089

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

e2e_test/batch/basic/dml.slt.part

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,24 @@ select count(*) from t;
7070

7171
statement ok
7272
drop table t;
73+
74+
statement ok
75+
create table t (v1 int, v2 int primary key, v3 varchar);
76+
77+
statement ok
78+
insert into t values (0, 1, 'a'), (1, 2, 'b');
79+
80+
statement ok
81+
update t set (v1, v3) = (v1+v2, v3||v3);
82+
83+
query IIT
84+
select * from t order by v1;
85+
----
86+
1 1 aa
87+
3 2 bb
88+
89+
statement error QueryError: Bind error: update modifying the PK column is banned
90+
update t set (v3, v2) = (v3||v3, v1+v2);
91+
92+
statement ok
93+
drop table t;

e2e_test/ddl/invalid_operation.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ SELECT * from v limit 0;
264264
statement ok
265265
insert into t values (1);
266266

267-
statement ok
267+
statement error QueryError: Bind error: update modifying the PK column is banned
268268
update t set v = 2;
269269

270270
statement ok

e2e_test/ddl/table.slt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ create table "T2" ("V1" int);
120120
statement ok
121121
insert into "T2" ("V1") values (1);
122122

123+
statement ok
124+
drop table "T2"
125+
123126
statement error
124127
create table C1 (c1 varchar(5));
125128

src/frontend/planner_test/tests/testdata/update.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@
6060
└─BatchExchange { order: [], dist: Single }
6161
└─BatchFilter { predicate: (t.v1 <> t.v2) }
6262
└─BatchScan { table: t, columns: [t.v1, t.v2, t._row_id], distribution: UpstreamHashShard(t._row_id) }
63+
- sql: |
64+
create table t (v1 int primary key, v2 int);
65+
update t set (v2, v1) = (v1, v2);
66+
binder_error: 'Bind error: update modifying the PK column is banned'

src/frontend/src/binder/update.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ impl Binder {
9191
Self::resolve_schema_qualified_name(&self.db_name, name.clone())?;
9292

9393
let table_catalog = self.resolve_dml_table(schema_name.as_deref(), &table_name, false)?;
94+
let pk_indices = table_catalog
95+
.pk()
96+
.iter()
97+
.map(|column_order| column_order.column_index)
98+
.collect_vec();
9499
let table_id = table_catalog.id;
95100
let owner = table_catalog.owner;
96101
let table_version_id = table_catalog.version_id().expect("table must be versioned");
@@ -131,6 +136,18 @@ impl Binder {
131136

132137
for (id, value) in assignments {
133138
let id_expr = self.bind_expr(Expr::Identifier(id.clone()))?;
139+
if let Some(id_input_ref) = id_expr.clone().as_input_ref() {
140+
let id_index = id_input_ref.index;
141+
for &pk in &pk_indices {
142+
if id_index == pk {
143+
return Err(ErrorCode::BindError(
144+
"update modifying the PK column is banned".to_owned(),
145+
)
146+
.into());
147+
}
148+
}
149+
}
150+
134151
let value_expr = self.bind_expr(value)?.cast_assign(id_expr.return_type())?;
135152

136153
match assignment_exprs.entry(id_expr) {

0 commit comments

Comments
 (0)