diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java index ac2dafd2f8..941f965a3a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java @@ -23,6 +23,7 @@ import org.apache.doris.catalog.Type; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.Pair; import org.apache.doris.common.UserException; +import org.apache.doris.qe.SessionVariable; import org.apache.doris.rewrite.ExprRewriter; import com.google.common.base.Preconditions; @@ -485,7 +486,7 @@ public class SetOperationStmt extends QueryStmt { List> selectTypeWithNullable = operands.get(0).getQueryStmt().getResultExprs().stream() .map(expr -> Pair.of(expr.getType(), expr.isNullable())).collect(Collectors.toList()); for (int i = 1; i < operands.size(); i++) { - for (int j = 1; j < selectTypeWithNullable.size(); j++) { + for (int j = 0; j < selectTypeWithNullable.size(); j++) { if (selectTypeWithNullable.get(j).first.isDecimalV2() && operands.get(i).getQueryStmt().getResultExprs().get(j).getType().isDecimalV2()) { selectTypeWithNullable.get(j).first = ScalarType.getAssignmentCompatibleDecimalV2Type( @@ -498,6 +499,13 @@ public class SetOperationStmt extends QueryStmt { (ScalarType) selectTypeWithNullable.get(j).first, (ScalarType) operands.get(i).getQueryStmt().getResultExprs().get(j).getType()); } + if (selectTypeWithNullable.get(j).first.isStringType() && operands.get(i) + .getQueryStmt().getResultExprs().get(j).getType().isStringType()) { + selectTypeWithNullable.get(j).first = ScalarType.getAssignmentCompatibleType( + (ScalarType) selectTypeWithNullable.get(j).first, + (ScalarType) operands.get(i).getQueryStmt().getResultExprs().get(j).getType(), + false, SessionVariable.getEnableDecimal256()); + } } } diff --git a/regression-test/suites/ddl_p0/test_ctas.groovy b/regression-test/suites/ddl_p0/test_ctas.groovy index d634dc91e0..2aaf56ee59 100644 --- a/regression-test/suites/ddl_p0/test_ctas.groovy +++ b/regression-test/suites/ddl_p0/test_ctas.groovy @@ -290,5 +290,92 @@ suite("test_ctas") { sql 'drop table test_ctas_of_view' sql 'drop view ctas_view' } + + try { + sql '''set enable_nereids_planner=false;''' + sql ''' + CREATE TABLE IF NOT EXISTS `ctas1` ( + `k1` varchar(5) NULL, + `k2` varchar(5) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + ''' + sql ''' + CREATE TABLE IF NOT EXISTS `ctas2` ( + `k1` varchar(10) NULL, + `k2` varchar(10) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + ''' + sql ''' + insert into ctas1 values('11111','11111'); + ''' + sql ''' + insert into ctas2 values('1111111111','1111111111'); + ''' + sql ''' + create table `ctas3`(k1, k2) + PROPERTIES("replication_num" = "1") + as select * from ctas1 + union all + select * from ctas2; + ''' + } finally { + sql '''DROP TABLE IF EXISTS ctas1''' + sql '''DROP TABLE IF EXISTS ctas2''' + sql '''DROP TABLE IF EXISTS ctas3''' + } + + try { + sql '''set enable_nereids_planner=true;''' + sql '''set enable_fallback_to_original_planner=false;''' + sql ''' + CREATE TABLE IF NOT EXISTS `ctas1` ( + `k1` varchar(5) NULL, + `k2` varchar(5) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + ''' + sql ''' + CREATE TABLE IF NOT EXISTS `ctas2` ( + `k1` varchar(10) NULL, + `k2` varchar(10) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + ''' + sql ''' + insert into ctas1 values('11111','11111'); + ''' + sql ''' + insert into ctas2 values('1111111111','1111111111'); + ''' + sql ''' + create table `ctas3`(k1, k2) + PROPERTIES("replication_num" = "1") + as select * from ctas1 + union all + select * from ctas2; + ''' + } finally { + sql '''DROP TABLE IF EXISTS ctas1''' + sql '''DROP TABLE IF EXISTS ctas2''' + sql '''DROP TABLE IF EXISTS ctas3''' + } }