Skip to content

Commit

Permalink
[BugFix] Fix order by nan column overflow (#30759) (#37024)
Browse files Browse the repository at this point in the history
Signed-off-by: stdpain <[email protected]>
  • Loading branch information
stdpain authored Dec 15, 2023
1 parent c3af3db commit ec2e1cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
31 changes: 31 additions & 0 deletions be/src/exec/sorting/sort_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <algorithm>
#include <concepts>

#include "column/nullable_column.h"
#include "column/type_traits.h"
Expand Down Expand Up @@ -75,6 +76,36 @@ struct SorterComparator<TimestampValue> {
}
};

template <>
struct SorterComparator<float> {
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<double> {
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 <class NullPred>
static inline Status sort_and_tie_helper_nullable_vertical(const std::atomic<bool>& cancel,
Expand Down
19 changes: 19 additions & 0 deletions be/test/exec/chunks_sorter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "runtime/types.h"
#include "testutil/assert.h"
#include "util/json.h"
#include "util/orlp/pdqsort.h"

namespace starrocks {

Expand Down Expand Up @@ -1148,4 +1149,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<char>(ifs)), (std::istreambuf_iterator<char>()));
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<double>(permutation, data);
auto begin = inlined.begin() + 26012;
auto end = inlined.begin() + 26047;

::pdqsort(begin, end, [&](auto lhs, auto rhs) {
return SorterComparator<double>::compare(lhs.inline_value, rhs.inline_value) > 0;
});
}

} // namespace starrocks
Binary file added be/test/exec/test_data/nan_column
Binary file not shown.

0 comments on commit ec2e1cd

Please sign in to comment.