diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/StructField.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/StructField.java index a084bfe0e42c46..20ef2cb1358e51 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/StructField.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/StructField.java @@ -123,8 +123,14 @@ public String prettyPrint(int lpad) { } public static boolean canCastTo(StructField field, StructField targetField) { - // TODO(xy): support cast field - return false; + // not support cast not null to nullable + if (targetField.containsNull != field.containsNull) { + return false; + } + if (targetField.type.isStringType() && field.type.isStringType()) { + return true; + } + return Type.canCastTo(field.type, targetField.type); } public boolean matchesField(StructField f) { diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/StructType.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/StructType.java index 881d45174311f3..be5cb287b44a08 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/StructType.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/StructType.java @@ -85,8 +85,15 @@ protected String prettyPrint(int lpad) { } public static boolean canCastTo(StructType type, StructType targetType) { - // TODO(xy) : support cast struct type - return false; + if (type.fields.size() != targetType.fields.size()) { + return false; + } + for (int i = 0; i < type.fields.size(); i++) { + if (!StructField.canCastTo(type.fields.get(i), targetType.fields.get(i))) { + return false; + } + } + return true; } @Override diff --git a/regression-test/data/query_p0/show/test_struct_show_create.out b/regression-test/data/query_p0/show/test_struct_show_create.out new file mode 100644 index 00000000000000..e06f2ba5ddcc4d --- /dev/null +++ b/regression-test/data/query_p0/show/test_struct_show_create.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +test_struct_show_create CREATE TABLE `test_struct_show_create` (\n `k1` int(11) NULL,\n `k2` STRUCT NOT NULL,\n `k3` STRUCT NOT NULL,\n `k4` STRUCT NOT NULL,\n `k5` STRUCT NOT NULL,\n `k6` STRUCT NULL,\n `k7` STRUCT NOT NULL,\n `k8` STRUCT NOT NULL,\n `k9` STRUCT NOT NULL,\n `k10` STRUCT NOT NULL,\n `k11` STRUCT NULL\n) ENGINE=OLAP\nDUPLICATE KEY(`k1`)\nCOMMENT 'OLAP'\nDISTRIBUTED BY HASH(`k1`) BUCKETS 1\nPROPERTIES (\n"replication_allocation" = "tag.location.default: 1",\n"in_memory" = "false",\n"storage_format" = "V2",\n"light_schema_change" = "true",\n"disable_auto_compaction" = "false"\n); + diff --git a/regression-test/suites/query_p0/show/test_struct_show_create.groovy b/regression-test/suites/query_p0/show/test_struct_show_create.groovy new file mode 100644 index 00000000000000..e73359b663eb2a --- /dev/null +++ b/regression-test/suites/query_p0/show/test_struct_show_create.groovy @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_struct_show_create", "query") { + // define a sql table + def testTable = "test_struct_show_create" + + sql "ADMIN SET FRONTEND CONFIG ('enable_struct_type' = 'true')" + + def create_test_table = {testTablex -> + def result1 = sql """ + CREATE TABLE IF NOT EXISTS ${testTable} ( + `k1` INT(11) NULL COMMENT "", + `k2` STRUCT NOT NULL COMMENT "", + `k3` STRUCT NOT NULL COMMENT "", + `k4` STRUCT NOT NULL COMMENT "", + `k5` STRUCT NOT NULL COMMENT "", + `k6` STRUCT NULL COMMENT "", + `k7` STRUCT NOT NULL COMMENT "", + `k8` STRUCT NOT NULL COMMENT "", + `k9` STRUCT NOT NULL COMMENT "", + `k10` STRUCT NOT NULL COMMENT "", + `k11` STRUCT NULL COMMENT "" + ) ENGINE=OLAP + DUPLICATE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "storage_format" = "V2" + ); + """ + + // DDL/DML return 1 row and 3 column, the only value is update row count + assertTrue(result1.size() == 1) + assertTrue(result1[0].size() == 1) + assertTrue(result1[0][0] == 0, "Create table should update 0 rows") + + // insert 1 row to check whether the table is ok + def result2 = sql """ INSERT INTO ${testTable} VALUES + (100, + {128}, + {128, 32768}, + {128, 32768, 2147483648}, + {128, 32768, 2147483648, 'c'}, + {128, 32768, 2147483648, 'c', "doris"}, + {128, 32768, 2147483648, 'c', "doris", '2023-02-10'}, + {128, 32768, 2147483648, 'c', "doris", '2023-02-10', '2023-02-10 12:30:00'}, + {128, 32768, 2147483648, 'c', "doris", '2023-02-10', '2023-02-10 12:30:00', 0.67}, + {128, 32768, 2147483648, 'c', "doris", '2023-02-10', '2023-02-10 12:30:00', 0.67, 0.878787878}, + {128, 32768, 2147483648, 'c', "doris", '2023-02-10', '2023-02-10 12:30:00', 0.67, 0.878787878, 6.67}) + """ + assertTrue(result2.size() == 1) + assertTrue(result2[0].size() == 1) + assertTrue(result2[0][0] == 1, "Insert should update 1 rows") + } + + try { + sql "DROP TABLE IF EXISTS ${testTable}" + create_test_table.call(testTable) + + qt_select "show create table ${testTable}" + } finally { + try_sql("DROP TABLE IF EXISTS ${testTable}") + } +}