diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SortInfo.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SortInfo.java index 63394db4cd..01173a2433 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SortInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SortInfo.java @@ -271,6 +271,11 @@ public class SortInfo { */ public ExprSubstitutionMap createMaterializedOrderExprs( TupleDescriptor sortTupleDesc, Analyzer analyzer) { + // the sort node exprs may come from the child outer join node + // we need change the slots to nullable from all outer join nullable side temporarily + // then the sort node expr would have correct nullable info + // after create the output tuple we need revert the change by call analyzer.changeSlotsToNotNullable(slots) + List slots = analyzer.changeSlotToNullableOfOuterJoinedTuples(); ExprSubstitutionMap substOrderBy = new ExprSubstitutionMap(); for (Expr origOrderingExpr : orderingExprs) { SlotDescriptor materializedDesc = analyzer.addSlotDescriptor(sortTupleDesc); @@ -280,6 +285,7 @@ public class SortInfo { substOrderBy.put(origOrderingExpr, materializedRef); materializedOrderingExprs.add(origOrderingExpr); } + analyzer.changeSlotsToNotNullable(slots); return substOrderBy; } diff --git a/regression-test/data/correctness_p0/test_outer_join_with_order_by.out b/regression-test/data/correctness_p0/test_outer_join_with_order_by.out new file mode 100644 index 0000000000..72d126351a --- /dev/null +++ b/regression-test/data/correctness_p0/test_outer_join_with_order_by.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +1 + diff --git a/regression-test/suites/correctness_p0/test_outer_join_with_order_by.groovy b/regression-test/suites/correctness_p0/test_outer_join_with_order_by.groovy new file mode 100644 index 0000000000..ae250e8d20 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_outer_join_with_order_by.groovy @@ -0,0 +1,69 @@ +// 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_outer_join_with_order_by") { + sql """ + drop table if exists outerjoin_A; + """ + + sql """ + drop table if exists outerjoin_B; + """ + + sql """ + create table outerjoin_A ( a int not null ) + ENGINE=OLAP + DISTRIBUTED BY HASH(a) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + + sql """ + create table outerjoin_B ( a int not null ) + ENGINE=OLAP + DISTRIBUTED BY HASH(a) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + + sql """ + insert into outerjoin_A values( 1 ); + """ + + sql """ + insert into outerjoin_B values( 1 ); + """ + + qt_select """ + select case when outerjoin_A.a <= outerjoin_A.a then outerjoin_A.a else outerjoin_A.a end as r + from outerjoin_A right join outerjoin_B on outerjoin_A.a = outerjoin_B.a order by outerjoin_A.a; + """ + + sql """ + drop table if exists outerjoin_A; + """ + + sql """ + drop table if exists outerjoin_B; + """ +} \ No newline at end of file