diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java index 60f855423ff19b..a8df1d005fbe70 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java @@ -513,29 +513,28 @@ public boolean isDecimalV3OrContainsDecimalV3() { // This method defines the char type or complex type nested char type // to support the schema-change behavior of length growth. - public boolean isSupportSchemaChangeForCharType(Type other) { + public boolean isSupportSchemaChangeForCharType(Type other) throws TypeException { if ((this.getPrimitiveType() == PrimitiveType.VARCHAR && other.getPrimitiveType() == PrimitiveType.VARCHAR) || ( this.getPrimitiveType() == PrimitiveType.CHAR && other.getPrimitiveType() == PrimitiveType.VARCHAR) || ( this.getPrimitiveType() == PrimitiveType.CHAR && other.getPrimitiveType() == PrimitiveType.CHAR)) { if (this.getLength() > other.getLength()) { - return false; + throw new TypeException("Cannot shorten string length for type " + + this.toSql() + " to " + other.toSql()); } else { return true; } - } - if (this.isStructType() && other.isStructType()) { + } else if (this.isStructType() && other.isStructType()) { StructType thisStructType = (StructType) this; StructType otherStructType = (StructType) other; if (thisStructType.getFields().size() != otherStructType.getFields().size()) { - return false; + throw new TypeException("Cannot change struct type with different field size"); } for (int i = 0; i < thisStructType.getFields().size(); i++) { if (!thisStructType.getFields().get(i).getType().isSupportSchemaChangeForCharType( otherStructType.getFields().get(i).getType())) { - return false; + throw new TypeException("Cannot change struct type with different field type"); } } - return true; } else if (this.isArrayType() && other.isArrayType()) { return ((ArrayType) this).getItemType().isSupportSchemaChangeForCharType(((ArrayType) other).getItemType()); } else if (this.isMapType() && other.isMapType()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java index e0880b2e149a5e..f5b9eda29bc378 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java @@ -882,8 +882,12 @@ public void checkSchemaChangeAllowed(Column other) throws DdlException { // Nested types only support changing the order and increasing the length of the nested char type // Char-type only support length growing - if (!type.isSupportSchemaChangeForCharType(other.type)) { - throw new DdlException("Cannot shorten string length for type " + type.toSql() + " to " + other.toSql()); + try { + if (!type.isSupportSchemaChangeForCharType(other.type)) { + throw new DdlException("Can not change " + type.toSql() + " to " + other.type.toSql()); + } + } catch (TypeException e) { + throw new DdlException(e.getMessage()); } // now we support convert decimal to varchar type 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 8c65509b91c848..1ce39b9867b5fc 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 @@ -77,6 +77,44 @@ suite ("test_varchar_sc_in_complex") { exception "Cannot shorten string length" } + // add case alter modify array/map/struct to other type + // test array to struct + test { + sql """ alter table ${tableName} modify column c_a struct + """ + exception "Cannot chang" + } + // test array to map + test { + sql """ alter table ${tableName} modify column c_a map + """ + exception "Cannot chang" + } + // test map to array + test { + sql """ alter table ${tableName} modify column c_m array + """ + exception "Cannot chang" + } + // test map to struct + test { + sql """ alter table ${tableName} modify column c_m struct + """ + exception "Cannot chang" + } + // test struct to array + test { + sql """ alter table ${tableName} modify column c_s array + """ + exception "Cannot chang" + } + // test struct to map + test { + sql """ alter table ${tableName} modify column c_s map + """ + exception "Cannot chang" + } + sql """ alter table ${tableName} modify column c_a array """ int max_try_secs = 300