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

feat(frontend): ban update statements modifying pk columns #8569

Merged
merged 5 commits into from
Mar 16, 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
21 changes: 21 additions & 0 deletions e2e_test/batch/basic/dml.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,24 @@ select count(*) from t;

statement ok
drop table t;

statement ok
create table t (v1 int, v2 int primary key, v3 varchar);

statement ok
insert into t values (0, 1, 'a'), (1, 2, 'b');

statement ok
update t set (v1, v3) = (v1+v2, v3||v3);

query IIT
select * from t order by v1;
----
1 1 aa
3 2 bb

statement error QueryError: Bind error: update modifying the PK column is banned
update t set (v3, v2) = (v3||v3, v1+v2);

statement ok
drop table t;
2 changes: 1 addition & 1 deletion e2e_test/ddl/invalid_operation.slt
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ SELECT * from v limit 0;
statement ok
insert into t values (1);

statement ok
statement error QueryError: Bind error: update modifying the PK column is banned
update t set v = 2;

statement ok
Expand Down
3 changes: 3 additions & 0 deletions e2e_test/ddl/table.slt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ create table "T2" ("V1" int);
statement ok
insert into "T2" ("V1") values (1);

statement ok
drop table "T2"

statement error
create table C1 (c1 varchar(5));

Expand Down
4 changes: 4 additions & 0 deletions src/frontend/planner_test/tests/testdata/update.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@
└─BatchExchange { order: [], dist: Single }
└─BatchFilter { predicate: (t.v1 <> t.v2) }
└─BatchScan { table: t, columns: [t.v1, t.v2, t._row_id], distribution: UpstreamHashShard(t._row_id) }
- sql: |
create table t (v1 int primary key, v2 int);
update t set (v2, v1) = (v1, v2);
binder_error: 'Bind error: update modifying the PK column is banned'
17 changes: 17 additions & 0 deletions src/frontend/src/binder/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ impl Binder {
Self::resolve_schema_qualified_name(&self.db_name, name.clone())?;

let table_catalog = self.resolve_dml_table(schema_name.as_deref(), &table_name, false)?;
let pk_indices = table_catalog
.pk()
.iter()
.map(|column_order| column_order.column_index)
.collect_vec();
let table_id = table_catalog.id;
let owner = table_catalog.owner;
let table_version_id = table_catalog.version_id().expect("table must be versioned");
Expand Down Expand Up @@ -131,6 +136,18 @@ impl Binder {

for (id, value) in assignments {
let id_expr = self.bind_expr(Expr::Identifier(id.clone()))?;
if let Some(id_input_ref) = id_expr.clone().as_input_ref() {
let id_index = id_input_ref.index;
for &pk in &pk_indices {
if id_index == pk {
return Err(ErrorCode::BindError(
"update modifying the PK column is banned".to_owned(),
)
.into());
}
}
}

let value_expr = self.bind_expr(value)?.cast_assign(id_expr.return_type())?;

match assignment_exprs.entry(id_expr) {
Expand Down