diff --git a/be/src/exec/sorting/sort_helper.h b/be/src/exec/sorting/sort_helper.h index 86ece39872a25..a368db00e3976 100644 --- a/be/src/exec/sorting/sort_helper.h +++ b/be/src/exec/sorting/sort_helper.h @@ -15,6 +15,7 @@ #pragma once #include +#include #include "column/nullable_column.h" #include "column/type_traits.h" @@ -75,6 +76,36 @@ struct SorterComparator { } }; +template <> +struct SorterComparator { + static int compare(float lhs, float rhs) { + lhs = std::isnan(lhs) ? 0 : lhs; + rhs = std::isnan(rhs) ? 0 : rhs; + if (lhs == rhs) { + return 0; + } else if (lhs < rhs) { + return -1; + } else { + return 1; + } + } +}; + +template <> +struct SorterComparator { + static int compare(double lhs, double rhs) { + lhs = std::isnan(lhs) ? 0 : lhs; + rhs = std::isnan(rhs) ? 0 : rhs; + if (lhs == rhs) { + return 0; + } else if (lhs < rhs) { + return -1; + } else { + return 1; + } + } +}; + // TODO: reduce duplicate code template static inline Status sort_and_tie_helper_nullable_vertical(const std::atomic& cancel, diff --git a/be/test/exec/chunks_sorter_test.cpp b/be/test/exec/chunks_sorter_test.cpp index cc6e5104b2ae4..6f075ad259cbf 100644 --- a/be/test/exec/chunks_sorter_test.cpp +++ b/be/test/exec/chunks_sorter_test.cpp @@ -39,6 +39,7 @@ #include "runtime/types.h" #include "testutil/assert.h" #include "util/json.h" +#include "util/orlp/pdqsort.h" namespace starrocks { @@ -1149,4 +1150,22 @@ TEST_F(ChunksSorterTest, test_tie) { } } +// https://github.com/StarRocks/starrocks/issues/30758 +TEST_F(ChunksSorterTest, test_nan) { + std::ifstream ifs("./be/test/exec/test_data/nan_column"); + std::string context((std::istreambuf_iterator(ifs)), (std::istreambuf_iterator())); + DoubleColumn double_column; + double_column.resize(86016); + auto& data = double_column.get_data(); + memcpy(data.data(), context.data(), context.size()); + auto permutation = create_small_permutation(double_column.size()); + auto inlined = create_inline_permutation(permutation, data); + auto begin = inlined.begin() + 26012; + auto end = inlined.begin() + 26047; + + ::pdqsort(begin, end, [&](auto lhs, auto rhs) { + return SorterComparator::compare(lhs.inline_value, rhs.inline_value) > 0; + }); +} + } // namespace starrocks diff --git a/be/test/exec/test_data/nan_column b/be/test/exec/test_data/nan_column new file mode 100755 index 0000000000000..f74150bf513df Binary files /dev/null and b/be/test/exec/test_data/nan_column differ