Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix order by nan column overflow (#30759) (backport #37024) #51846

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<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.
Loading