Skip to content

Commit 5c68782

Browse files
authored
[BugFix] Fix partial update for auto increment column can not triggered by insert stmt (backport #56996) (backport #58932) (#62589)
Signed-off-by: srlch <linzichao@starrocks.com>
1 parent 6e90ade commit 5c68782

File tree

7 files changed

+165
-25
lines changed

7 files changed

+165
-25
lines changed

fe/fe-core/src/main/java/com/starrocks/sql/InsertPlanner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ public ExecPlan plan(InsertStmt insertStmt, ConnectContext session) {
380380
nullExprInAutoIncrement, enableAutomaticPartition, session.getCurrentWarehouseId());
381381
if (insertStmt.usePartialUpdate()) {
382382
((OlapTableSink) dataSink).setPartialUpdateMode(TPartialUpdateMode.AUTO_MODE);
383+
if (insertStmt.autoIncrementPartialUpdate()) {
384+
((OlapTableSink) dataSink).setMissAutoIncrementColumn();
385+
}
383386
}
384387
if (olapTable.getAutomaticBucketSize() > 0) {
385388
((OlapTableSink) dataSink).setAutomaticBucketSize(olapTable.getAutomaticBucketSize());

fe/fe-core/src/main/java/com/starrocks/sql/analyzer/InsertAnalyzer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ public static void analyzeWithDeferredLock(InsertStmt insertStmt, ConnectContext
218218
}
219219
if (targetColumns.size() < olapTable.getBaseSchemaWithoutGeneratedColumn().size()) {
220220
insertStmt.setUsePartialUpdate();
221+
// mark if partial update for auto increment column if and only if:
222+
// 1. There is auto increment defined in base schema
223+
// 2. targetColumns does not contain auto increment column
224+
// 3. auto increment column is not key column
225+
if (olapTable.hasAutoIncrementColumn() &&
226+
!targetColumns.stream().anyMatch(col -> col.isAutoIncrement())) {
227+
Column autoIncrementColumn =
228+
table.getBaseSchema().stream().filter(Column::isAutoIncrement).findFirst().get();
229+
if (!autoIncrementColumn.isKey()) {
230+
insertStmt.setAutoIncrementPartialUpdate();
231+
}
232+
}
221233
}
222234
}
223235
}

fe/fe-core/src/main/java/com/starrocks/sql/ast/InsertStmt.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class InsertStmt extends DmlStmt {
6161
private List<Long> targetPartitionIds = Lists.newArrayList();
6262
private List<String> targetColumnNames;
6363
private boolean usePartialUpdate = false;
64+
private boolean autoIncrementPartialUpdate = false;
6465
private QueryStatement queryStatement;
6566
private String label = null;
6667

@@ -250,6 +251,14 @@ public List<String> getTargetColumnNames() {
250251
return targetColumnNames;
251252
}
252253

254+
public void setAutoIncrementPartialUpdate() {
255+
this.autoIncrementPartialUpdate = true;
256+
}
257+
258+
public boolean autoIncrementPartialUpdate() {
259+
return this.autoIncrementPartialUpdate;
260+
}
261+
253262
public void setUsePartialUpdate() {
254263
this.usePartialUpdate = true;
255264
}

test/sql/test_auto_increment/R/test_auto_increment

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ SELECT * FROM t1 ORDER BY id;
183183
-- result:
184184
1 5
185185
2 6
186-
10 7
187-
20 8
186+
10 1
187+
20 2
188188
-- !result
189189
DROP TABLE t1;
190190
-- result:
@@ -309,7 +309,7 @@ ADMIN SET FRONTEND CONFIG ("auto_increment_cache_size" = "0");
309309
CREATE TABLE t ( id BIGINT NOT NULL AUTO_INCREMENT, name BIGINT NOT NULL, job1 BIGINT NOT NULL, job2 BIGINT NOT NULL) Primary KEY (id, name) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES("replication_num" = "1", "replicated_storage"="true");
310310
-- result:
311311
-- !result
312-
INSERT INTO t (name,job1,job2) VALUES (1,1,1),(2,2,2);
312+
INSERT INTO t (id, name,job1,job2) VALUES (DEFAULT, 1,1,1),(DEFAULT, 2,2,2);
313313
-- result:
314314
-- !result
315315
SELECT * FROM t ORDER BY name;
@@ -365,7 +365,7 @@ SELECT * FROM t ORDER BY job1;
365365
1 1 1 1
366366
100000 1 2 2
367367
-- !result
368-
INSERT INTO t (name,job1,job2) VALUES (1,100,100);
368+
INSERT INTO t (id, name,job1,job2) VALUES (DEFAULT, 1,100,100);
369369
-- result:
370370
-- !result
371371
SELECT * FROM t ORDER BY job1;
@@ -475,18 +475,6 @@ INSERT INTO t3 (id,name,job1,job2) VALUES (1,1,DEFAULT,1);
475475
INSERT INTO t4 (id,name,job1,job2) VALUES (1,1,1,DEFAULT);
476476
-- result:
477477
-- !result
478-
INSERT INTO t1 (name,job1,job2) VALUES (1,1,1);
479-
-- result:
480-
-- !result
481-
INSERT INTO t2 (id,job1,job2) VALUES (1,1,1);
482-
-- result:
483-
-- !result
484-
INSERT INTO t3 (id,name,job2) VALUES (1,1,1);
485-
-- result:
486-
-- !result
487-
INSERT INTO t4 (id,name,job1) VALUES (1,1,1);
488-
-- result:
489-
-- !result
490478
DROP TABLE t1;
491479
-- result:
492480
-- !result
@@ -733,4 +721,90 @@ DROP TABLE t_auto_increment_partial_update_column_upsert;
733721
-- !result
734722
DROP DATABASE test_auto_increment_partial_update_column_upsert;
735723
-- result:
724+
-- !result
725+
-- name: test_auto_increment_insert_partial_update @sequential
726+
ADMIN SET FRONTEND CONFIG ("auto_increment_cache_size" = "0");
727+
CREATE TABLE `t_auto_increment_insert_partial_update` (
728+
`k` STRING NOT NULL COMMENT "",
729+
`v1` BIGINT AUTO_INCREMENT,
730+
`created` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT ""
731+
) ENGINE=OLAP
732+
PRIMARY KEY(`k`)
733+
DISTRIBUTED BY HASH(`k`) BUCKETS 1
734+
PROPERTIES (
735+
"replication_num" = "1",
736+
"in_memory" = "false",
737+
"enable_persistent_index" = "true",
738+
"replicated_storage" = "true"
739+
);
740+
-- result:
741+
-- !result
742+
insert into t_auto_increment_insert_partial_update (k) values (1);
743+
-- result:
744+
-- !result
745+
insert into t_auto_increment_insert_partial_update (k) values (1),(2);
746+
-- result:
747+
-- !result
748+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3);
749+
-- result:
750+
-- !result
751+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
752+
-- result:
753+
-- !result
754+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
755+
-- result:
756+
-- !result
757+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
758+
-- result:
759+
-- !result
760+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
761+
-- result:
762+
-- !result
763+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
764+
-- result:
765+
-- !result
766+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(5);
767+
-- result:
768+
-- !result
769+
SELECT k, v1 from t_auto_increment_insert_partial_update ORDER BY k;
770+
-- result:
771+
1 1
772+
2 2
773+
3 3
774+
4 4
775+
5 5
776+
-- !result
777+
DROP TABLE t_auto_increment_insert_partial_update force;
778+
-- result:
779+
-- !result
780+
CREATE TABLE `t_auto_increment_insert_partial_update` (
781+
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT "",
782+
`ts` bigint(20) NOT NULL COMMENT "",
783+
`testString` String NOT NULL COMMENT ""
784+
) ENGINE=OLAP
785+
PRIMARY KEY(`id`, `ts`)
786+
DISTRIBUTED BY HASH(`id`) BUCKETS 1
787+
PROPERTIES (
788+
"replicated_storage" = "true",
789+
"replication_num" = "1"
790+
);
791+
-- result:
792+
-- !result
793+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
794+
-- result:
795+
-- !result
796+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
797+
-- result:
798+
-- !result
799+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
800+
-- result:
801+
-- !result
802+
SELECT * FROM t_auto_increment_insert_partial_update order by id;
803+
-- result:
804+
1 100 abc
805+
2 100 abc
806+
3 100 abc
807+
-- !result
808+
DROP TABLE t_auto_increment_insert_partial_update force;
809+
-- result:
736810
-- !result

test/sql/test_auto_increment/T/test_auto_increment

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ USE test_insert_auto_increment;
3838
ADMIN SET FRONTEND CONFIG ("auto_increment_cache_size" = "0");
3939

4040
CREATE TABLE t ( id BIGINT NOT NULL AUTO_INCREMENT, name BIGINT NOT NULL, job1 BIGINT NOT NULL, job2 BIGINT NOT NULL) Primary KEY (id, name) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES("replication_num" = "1", "replicated_storage"="true");
41-
INSERT INTO t (name,job1,job2) VALUES (1,1,1),(2,2,2);
41+
INSERT INTO t (id, name,job1,job2) VALUES (DEFAULT, 1,1,1),(2,2,2);
4242
SELECT * FROM t ORDER BY name;
4343

4444
INSERT INTO t (id,name,job1,job2) VALUES (DEFAULT,3,3,3),(DEFAULT,4,4,4);
@@ -57,7 +57,7 @@ CREATE TABLE t ( id BIGINT NOT NULL AUTO_INCREMENT, name BIGINT NOT NULL, job1
5757
INSERT INTO t (id,name,job1,job2) VALUES (1,1,1,1),(100000,1,2,2);
5858
SELECT * FROM t ORDER BY job1;
5959

60-
INSERT INTO t (name,job1,job2) VALUES (1,100,100);
60+
INSERT INTO t (id, name,job1,job2) VALUES (DEFAULT, 1,100,100);
6161
SELECT * FROM t ORDER BY job1;
6262

6363
INSERT INTO t (id,name,job1,job2) VALUES (100000,1,100,100);
@@ -96,11 +96,6 @@ INSERT INTO t2 (id,name,job1,job2) VALUES (1,DEFAULT,1,1);
9696
INSERT INTO t3 (id,name,job1,job2) VALUES (1,1,DEFAULT,1);
9797
INSERT INTO t4 (id,name,job1,job2) VALUES (1,1,1,DEFAULT);
9898

99-
INSERT INTO t1 (name,job1,job2) VALUES (1,1,1);
100-
INSERT INTO t2 (id,job1,job2) VALUES (1,1,1);
101-
INSERT INTO t3 (id,name,job2) VALUES (1,1,1);
102-
INSERT INTO t4 (id,name,job1) VALUES (1,1,1);
103-
10499
DROP TABLE t1;
105100
DROP TABLE t2;
106101
DROP TABLE t3;
@@ -314,3 +309,50 @@ SELECT * FROM t_auto_increment_partial_update_column_upsert;
314309

315310
DROP TABLE t_auto_increment_partial_update_column_upsert;
316311
DROP DATABASE test_auto_increment_partial_update_column_upsert;
312+
313+
-- name: test_auto_increment_insert_partial_update
314+
ADMIN SET FRONTEND CONFIG ("auto_increment_cache_size" = "0");
315+
CREATE TABLE `t_auto_increment_insert_partial_update` (
316+
`k` STRING NOT NULL COMMENT "",
317+
`v1` BIGINT AUTO_INCREMENT,
318+
`created` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT ""
319+
) ENGINE=OLAP
320+
PRIMARY KEY(`k`)
321+
DISTRIBUTED BY HASH(`k`) BUCKETS 1
322+
PROPERTIES (
323+
"replication_num" = "1",
324+
"in_memory" = "false",
325+
"enable_persistent_index" = "true",
326+
"replicated_storage" = "true"
327+
);
328+
329+
insert into t_auto_increment_insert_partial_update (k) values (1);
330+
insert into t_auto_increment_insert_partial_update (k) values (1),(2);
331+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3);
332+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
333+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
334+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
335+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
336+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(3),(4);
337+
insert into t_auto_increment_insert_partial_update (k) values (1),(2),(5);
338+
339+
SELECT k, v1 from t_auto_increment_insert_partial_update ORDER BY k;
340+
DROP TABLE t_auto_increment_insert_partial_update force;
341+
342+
CREATE TABLE `t_auto_increment_insert_partial_update` (
343+
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT "",
344+
`ts` bigint(20) NOT NULL COMMENT "",
345+
`testString` String NOT NULL COMMENT ""
346+
) ENGINE=OLAP
347+
PRIMARY KEY(`id`, `ts`)
348+
DISTRIBUTED BY HASH(`id`) BUCKETS 1
349+
PROPERTIES (
350+
"replicated_storage" = "true",
351+
"replication_num" = "1"
352+
);
353+
354+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
355+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
356+
insert into t_auto_increment_insert_partial_update (ts, testString) select 100, "abc";
357+
SELECT * FROM t_auto_increment_insert_partial_update order by id;
358+
DROP TABLE t_auto_increment_insert_partial_update force;

test/sql/test_dml/R/test_update

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PRIMARY KEY(`k1`)
9393
DISTRIBUTED BY HASH(`k1`);
9494
-- result:
9595
-- !result
96-
insert into pk_tbl1(k2, k3, k4, k5) values('2024-01-01', 1, 2, 3), ('2024-01-01', 1, 2, 3);
96+
insert into pk_tbl1(k1, k2, k3, k4, k5) values(DEFAULT, '2024-01-01', 1, 2, 3), (DEFAULT, '2024-01-01', 1, 2, 3);
9797
-- result:
9898
-- !result
9999
update pk_tbl1 set K4 = 1, K3 = 1, K5 = 1 where K1 = 1;

test/sql/test_dml/T/test_update

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CREATE TABLE `pk_tbl1` (
5151
) ENGINE=OLAP
5252
PRIMARY KEY(`k1`)
5353
DISTRIBUTED BY HASH(`k1`);
54-
insert into pk_tbl1(k2, k3, k4, k5) values('2024-01-01', 1, 2, 3), ('2024-01-01', 1, 2, 3);
54+
insert into pk_tbl1(k1, k2, k3, k4, k5) values(DEFAULT, '2024-01-01', 1, 2, 3), (DEFAULT, '2024-01-01', 1, 2, 3);
5555
update pk_tbl1 set K4 = 1, K3 = 1, K5 = 1 where K1 = 1;
5656
select * from pk_tbl1 order by k1;
5757
drop table if exists pk_tbl1;

0 commit comments

Comments
 (0)