From 16b090ed5484214115741ee7123a385dae17f110 Mon Sep 17 00:00:00 2001 From: Siyang Tang Date: Wed, 26 Mar 2025 21:41:56 +0800 Subject: [PATCH] [fix](schema-change) Nested types should only support enlarging varchar length with light schema change (#49452) Introduce: #48607, #46639 Problem Summary: Currently nested types only support light schema change for internal fields, for string types, only varchar can do light schema change. --- .../org/apache/doris/catalog/ColumnType.java | 9 ++- .../test_type_length_change.groovy | 7 ++- .../test_varchar_sc_in_complex.groovy | 55 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/ColumnType.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/ColumnType.java index 302e53ec457e94..6227bea2b1b126 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/ColumnType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/ColumnType.java @@ -100,6 +100,7 @@ public abstract class ColumnType { schemaChangeMatrix[PrimitiveType.VARCHAR.ordinal()][PrimitiveType.DATEV2.ordinal()] = true; schemaChangeMatrix[PrimitiveType.VARCHAR.ordinal()][PrimitiveType.STRING.ordinal()] = true; schemaChangeMatrix[PrimitiveType.VARCHAR.ordinal()][PrimitiveType.JSONB.ordinal()] = true; + // could not change varchar to char cuz varchar max length is larger than char schemaChangeMatrix[PrimitiveType.STRING.ordinal()][PrimitiveType.JSONB.ordinal()] = true; @@ -192,7 +193,10 @@ public static void checkForTypeLengthChange(Type src, Type dst) throws DdlExcept // return true if the checkType and other are both char-type otherwise return false, // which used in checkSupportSchemaChangeForComplexType private static boolean checkSupportSchemaChangeForCharType(Type checkType, Type other) throws DdlException { - if (checkType.isStringType() && other.isStringType()) { + if (checkType.getPrimitiveType() == PrimitiveType.VARCHAR + && other.getPrimitiveType() == PrimitiveType.VARCHAR) { + // currently nested types only support light schema change for internal fields, for string types, + // only varchar can do light schema change checkForTypeLengthChange(checkType, other); return true; } else { @@ -230,7 +234,8 @@ public static void checkSupportSchemaChangeForComplexType(Type checkType, Type o // only support char-type schema change behavior for nested complex type // if nested is false, we do not check return value. if (nested && !checkSupportSchemaChangeForCharType(checkType, other)) { - throw new DdlException("Cannot change " + checkType.toSql() + " to " + other.toSql()); + throw new DdlException( + "Cannot change " + checkType.toSql() + " to " + other.toSql() + " in nested types"); } } } diff --git a/regression-test/suites/schema_change_p0/test_type_length_change.groovy b/regression-test/suites/schema_change_p0/test_type_length_change.groovy index 3a071aabf5ebf6..3fa18c3fe4c249 100644 --- a/regression-test/suites/schema_change_p0/test_type_length_change.groovy +++ b/regression-test/suites/schema_change_p0/test_type_length_change.groovy @@ -25,7 +25,7 @@ suite("test_type_length_change", "p0") { c0 CHAR(5), c1 VARCHAR(5) ) DUPLICATE KEY (k) - DISTRIBUTED BY HASH(k) BUCKETS 1 + DISTRIBUTED BY HASH(k) BUCKETS 1 PROPERTIES ( "replication_allocation" = "tag.location.default: 1"); """ @@ -75,4 +75,9 @@ suite("test_type_length_change", "p0") { sql """ INSERT INTO ${tableName} VALUES(4, "abcde", "abcde") """ qt_master_sql """ SELECT * FROM ${tableName} ORDER BY k""" qt_master_sql """ DESC ${tableName} """ + + test { + sql """ ALTER TABLE ${tableName} MODIFY COLUMN c1 CHAR(10) """ + exception "Can not change VARCHAR to CHAR" + } } diff --git a/regression-test/suites/schema_change_p0/test_varchar_sc_in_complex.groovy b/regression-test/suites/schema_change_p0/test_varchar_sc_in_complex.groovy index 584a422f2a7745..1544c82068f155 100644 --- a/regression-test/suites/schema_change_p0/test_varchar_sc_in_complex.groovy +++ b/regression-test/suites/schema_change_p0/test_varchar_sc_in_complex.groovy @@ -476,6 +476,61 @@ suite ("test_varchar_sc_in_complex") { // """ // qt_sc_after " select * from there_level_nested_type order by c0; " + // case7. do not support enlarge char length in nested types + def tableName2 = "test_enlarge_char_length_nested" + sql """ DROP TABLE IF EXISTS ${tableName2} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName2} + ( + k BIGINT NOT NULL, + c1 ARRAY, + c2 MAP, + c3 STRUCT, + c4 ARRAY, + c5 MAP, + c6 STRUCT + ) DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ( "replication_num" = "1", "light_schema_change" = "true" ); + """ + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c1 ARRAY """ + exception "Cannot change char(10) to char(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c2 MAP """ + exception "Cannot change char(10) to char(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c3 STRUCT """ + exception "Cannot change char(10) to char(20) in nested types" + } + + // case8. do not support convert from char to varchar and varchar to char in nested types + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c1 ARRAY """ + exception "Cannot change char(10) to varchar(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c2 MAP """ + exception "Cannot change char(10) to varchar(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c3 STRUCT """ + exception "Cannot change char(10) to varchar(20) in nested types" + } + + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c4 ARRAY """ + exception "Cannot change varchar(10) to char(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c5 MAP """ + exception "Cannot change varchar(10) to char(20) in nested types" + } + test { + sql """ ALTER TABLE ${tableName2} MODIFY COLUMN c6 STRUCT """ + exception "Cannot change varchar(10) to char(20) in nested types" + } } finally { try_sql("DROP TABLE IF EXISTS ${tableName}") }