Skip to content

Commit

Permalink
feat: change to sort using strings for output column
Browse files Browse the repository at this point in the history
  • Loading branch information
oh2024 committed Jun 4, 2024
1 parent 6325a9b commit 2d89b55
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
10 changes: 5 additions & 5 deletions hybridse/src/codegen/udf_ir_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1493,11 +1493,11 @@ TEST_F(UdfIRBuilderTest, JsonArraySortAsInt) {
}

TEST_F(UdfIRBuilderTest, JsonArraySortRepeatedKey) {
openmldb::base::StringRef json = R"([{"a": "6", "b": "2"}, {"a": "11", "b": "9"}, {"a": "6", "b": "10"}])";
CheckUdf<StringRef, StringRef, StringRef, StringRef, int32_t, bool>("json_array_sort", "9,10,2", json, "a", "b", 10,
true);
CheckUdf<StringRef, StringRef, StringRef, StringRef, int32_t, bool>("json_array_sort", "2,10,9", json, "a", "b", 10,
false);
openmldb::base::StringRef json = R"([{"a": "6", "b": "a"}, {"a": "11", "b": "aaa"}, {"a": "6", "b": "aa"}])";
CheckUdf<StringRef, StringRef, StringRef, StringRef, int32_t, bool>("json_array_sort", "aaa,aa,a", json, "a", "b",
10, true);
CheckUdf<StringRef, StringRef, StringRef, StringRef, int32_t, bool>("json_array_sort", "a,aa,aaa", json, "a", "b",
10, false);
}

TEST_F(UdfIRBuilderTest, JsonArraySortInvalidKey) {
Expand Down
11 changes: 2 additions & 9 deletions hybridse/src/udf/default_defs/feature_zero_def.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ void json_array_sort(::openmldb::base::StringRef *json_array,

std::string_view order_ref(order->data_, order->size_);
std::string_view column_ref(column->data_, column->size_);
std::vector<std::pair<int, int>> container;
std::vector<std::pair<int, std::string>> container;

for (auto ele : arr) {
simdjson::ondemand::object obj;
Expand All @@ -677,14 +677,7 @@ void json_array_sort(::openmldb::base::StringRef *json_array,
if (ec_order != std::errc()) {
return;
}

int column_val_int;
auto [ptr_column, ec_column] =
std::from_chars(column_val.data(), column_val.data() + column_val.size(), column_val_int);
if (ec_column != std::errc()) {
return;
}
container.emplace_back(static_cast<int>(order_int), static_cast<int>(column_val_int));
container.emplace_back(static_cast<int>(order_int), std::string(column_val));
}

std::sort(container.begin(), container.end(), [desc](const auto& a, const auto& b) {
Expand Down

0 comments on commit 2d89b55

Please sign in to comment.