Skip to content

Commit 8a871da

Browse files
committed
auto increment column should not be nullable
1 parent 5eba86f commit 8a871da

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

fluss-client/src/main/java/org/apache/fluss/client/table/writer/UpsertWriterImpl.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,23 @@ private static void sanityCheck(
118118
pkColumnSet.set(pkIndex);
119119
}
120120

121+
BitSet autoIncrementColumnSet = new BitSet();
122+
// explicitly specifying values for an auto increment column is not allowed
123+
for (String autoIncrementColumnName : autoIncrementColumnNames) {
124+
int autoIncrementColumnIndex = rowType.getFieldIndex(autoIncrementColumnName);
125+
if (targetColumnsSet.get(autoIncrementColumnIndex)) {
126+
throw new IllegalArgumentException(
127+
String.format(
128+
"Explicitly specifying values for the auto increment column %s is not allowed.",
129+
autoIncrementColumnName));
130+
}
131+
autoIncrementColumnSet.set(autoIncrementColumnIndex);
132+
}
133+
121134
// check the columns not in targetColumns should be nullable
122135
for (int i = 0; i < rowType.getFieldCount(); i++) {
123-
// column not in primary key
124-
if (!pkColumnSet.get(i)) {
136+
// column not in primary key and not in auto increment column
137+
if (!pkColumnSet.get(i) && !autoIncrementColumnSet.get(i)) {
125138
// the column should be nullable
126139
if (!rowType.getTypeAt(i).isNullable()) {
127140
throw new IllegalArgumentException(
@@ -131,16 +144,6 @@ private static void sanityCheck(
131144
}
132145
}
133146
}
134-
135-
// explicitly specifying values for an auto increment column is not allowed
136-
for (String autoIncrementColumnName : autoIncrementColumnNames) {
137-
if (targetColumnsSet.get(rowType.getFieldIndex(autoIncrementColumnName))) {
138-
throw new IllegalArgumentException(
139-
String.format(
140-
"Explicitly specifying values for the auto increment column %s is not allowed.",
141-
autoIncrementColumnName));
142-
}
143-
}
144147
}
145148

146149
/**

fluss-client/src/test/java/org/apache/fluss/client/table/FlussTableITCase.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,9 @@ void testInvalidPartialUpdate() throws Exception {
668668
Schema.newBuilder()
669669
.column("a", DataTypes.INT())
670670
.column("b", DataTypes.INT())
671+
.column("c", DataTypes.INT())
671672
.primaryKey("a")
672-
.enableAutoIncrement("b")
673+
.enableAutoIncrement("c")
673674
.build();
674675
tableDescriptor = TableDescriptor.builder().schema(schema).distributedBy(3, "a").build();
675676
TablePath tablePath =
@@ -678,11 +679,11 @@ void testInvalidPartialUpdate() throws Exception {
678679
try (Table table = conn.getTable(tablePath)) {
679680
assertThatThrownBy(() -> table.newUpsert().createWriter())
680681
.hasMessage(
681-
"This table has auto increment column [b]. Explicitly specifying values for an auto increment column is not allowed. Please specify non-auto-increment columns as target columns using partialUpdate first.");
682+
"This table has auto increment column [c]. Explicitly specifying values for an auto increment column is not allowed. Please specify non-auto-increment columns as target columns using partialUpdate first.");
682683

683-
assertThatThrownBy(() -> table.newUpsert().partialUpdate("a", "b").createWriter())
684+
assertThatThrownBy(() -> table.newUpsert().partialUpdate("a", "c").createWriter())
684685
.hasMessage(
685-
"Explicitly specifying values for the auto increment column b is not allowed.");
686+
"Explicitly specifying values for the auto increment column c is not allowed.");
686687
}
687688
}
688689

fluss-common/src/main/java/org/apache/fluss/metadata/Schema.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,10 @@ private static List<Column> normalizeColumns(
607607
"The data type of auto increment column must be INT or BIGINT.");
608608
}
609609

610-
// primary key should not nullable
611-
if (pkSet.contains(column.getName()) && column.getDataType().isNullable()) {
610+
// primary key and auto increment column should not nullable
611+
if ((pkSet.contains(column.getName())
612+
|| autoIncrementColumnNames.contains(column.getName()))
613+
&& column.getDataType().isNullable()) {
612614
newColumns.add(
613615
new Column(
614616
column.getName(),

0 commit comments

Comments
 (0)