Skip to content

Commit 78d73e6

Browse files
b41shXuanwei Zhang
authored and
Xuanwei Zhang
committed
fix(ddl): fix modify column data type null to not null failed (#17650)
* fix(ddl): fix modify column data type null to not null failed * add tests * fix tests
1 parent 601c1fc commit 78d73e6

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/query/expression/src/values.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,23 @@ impl Column {
14361436
})
14371437
.take(len),
14381438
)),
1439-
DataType::Nullable(ty) => NullableColumn::new_column(
1440-
Column::random(ty, len, options),
1441-
Bitmap::from((0..len).map(|_| rng.gen_bool(0.5)).collect::<Vec<bool>>()),
1442-
),
1439+
DataType::Nullable(ty) => {
1440+
let column = Column::random(ty, len, options);
1441+
let bitmap =
1442+
Bitmap::from((0..len).map(|_| rng.gen_bool(0.5)).collect::<Vec<bool>>());
1443+
1444+
// If the value is NULL, insert default value in underlying column
1445+
let mut builder = ColumnBuilder::with_capacity(ty, len);
1446+
for (valid, value) in bitmap.iter().zip(column.iter()) {
1447+
if valid {
1448+
builder.push(value);
1449+
} else {
1450+
builder.push_default();
1451+
}
1452+
}
1453+
let new_column = builder.build();
1454+
NullableColumn::new_column(new_column, bitmap)
1455+
}
14431456
DataType::Array(inner_ty) => {
14441457
let mut inner_len = 0;
14451458
let mut offsets: Vec<u64> = Vec::with_capacity(len + 1);

src/query/service/src/interpreters/interpreter_table_modify_column.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl ModifyTableColumnInterpreter {
267267
// 2. default expr and computed expr not changed. Otherwise, we need fill value for
268268
// new added column.
269269
if ((table.storage_format_as_parquet() && is_alter_column_string_to_binary)
270-
|| old_field.data_type.remove_nullable() == field.data_type.remove_nullable())
270+
|| old_field.data_type == field.data_type)
271271
&& old_field.default_expr == field.default_expr
272272
&& old_field.computed_expr == field.computed_expr
273273
{
@@ -301,6 +301,8 @@ impl ModifyTableColumnInterpreter {
301301
.map(|(index, field)| {
302302
if modified_field_indices.contains(&index) {
303303
let old_field = schema.field_with_name(&field.name).unwrap();
304+
let need_remove_nullable =
305+
old_field.data_type.is_nullable() && !field.data_type.is_nullable();
304306
// If the column type is Tuple or Array(Tuple), the difference in the number of leaf columns may cause
305307
// the auto cast to fail.
306308
// We read the leaf column data, and then use build function to construct a new Tuple or Array(Tuple).
@@ -421,7 +423,11 @@ impl ModifyTableColumnInterpreter {
421423
)
422424
}
423425
(_, _) => {
424-
format!("`{}`", field.name)
426+
if need_remove_nullable {
427+
format!("remove_nullable(`{}`)", field.name)
428+
} else {
429+
format!("`{}`", field.name)
430+
}
425431
}
426432
}
427433
} else {

tests/suites/0_stateless/17_altertable/17_0005_alter_table_modify_column_type.result

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ Error: APIError: QueryFailed: [1006]null value in column `a` of table `f` violat
4141
2
4242
a VARCHAR NO ''
4343
b INT NO 0
44+
begin test modify column NULL to not NULL
45+
4
46+
a 1
47+
b NULL
48+
NULL 3
49+
d 4
50+
a 1
51+
b NULL
52+
3
53+
d 4
54+
a 1
55+
b 0
56+
3
57+
d 4
58+
a VARCHAR NO ''
59+
b INT NO 0

tests/suites/0_stateless/17_altertable/17_0005_alter_table_modify_column_type.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ echo "INSERT INTO test_modify_column_type.f (a,b) values('',2)" | $BENDSQL_CLIE
5959
echo "SELECT a,b from test_modify_column_type.f order by b" | $BENDSQL_CLIENT_CONNECT
6060
echo "DESC test_modify_column_type.f" | $BENDSQL_CLIENT_CONNECT
6161

62+
echo "begin test modify column NULL to not NULL"
63+
echo "CREATE TABLE test_modify_column_type.g(a STRING NULL, b INT NULL) STORAGE_FORMAT='native'" | $BENDSQL_CLIENT_CONNECT
64+
echo "INSERT INTO test_modify_column_type.g VALUES('a',1),('b',NULL),(NULL,3),('d',4)" | $BENDSQL_CLIENT_CONNECT
65+
echo "SELECT a,b from test_modify_column_type.g" | $BENDSQL_CLIENT_CONNECT
66+
echo "ALTER TABLE test_modify_column_type.g MODIFY COLUMN a STRING NOT NULL" | $BENDSQL_CLIENT_CONNECT
67+
echo "SELECT a,b from test_modify_column_type.g" | $BENDSQL_CLIENT_CONNECT
68+
echo "ALTER TABLE test_modify_column_type.g MODIFY COLUMN b INT NOT NULL" | $BENDSQL_CLIENT_CONNECT
69+
echo "SELECT a,b from test_modify_column_type.g" | $BENDSQL_CLIENT_CONNECT
70+
echo "DESC test_modify_column_type.g" | $BENDSQL_CLIENT_CONNECT
71+
6272
echo "DROP DATABASE IF EXISTS test_modify_column_type" | $BENDSQL_CLIENT_CONNECT

0 commit comments

Comments
 (0)