Skip to content

Commit

Permalink
Fix crash in sort by eval destructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
happy-san committed Apr 23, 2024
1 parent f1e6403 commit cd1ca59
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/field.h
Expand Up @@ -578,7 +578,7 @@ struct sort_by {
};

struct eval_t {
filter_node_t* filter_trees = nullptr;
filter_node_t** filter_trees = nullptr; // Array of filter_node_t pointers.
std::vector<uint32_t*> eval_ids_vec;
std::vector<uint32_t> eval_ids_count_vec;
std::vector<int64_t> scores;
Expand Down
4 changes: 2 additions & 2 deletions include/filter.h
Expand Up @@ -2,8 +2,8 @@

#include <string>
#include <map>
#include <tsl/htrie_map.h>
#include <json.hpp>
#include "tsl/htrie_map.h"
#include "json.hpp"
#include "store.h"

enum NUM_COMPARATOR {
Expand Down
36 changes: 9 additions & 27 deletions src/collection.cpp
Expand Up @@ -36,6 +36,11 @@ struct sort_fields_guard_t {
for (auto& eval_ids: sort_by_clause.eval.eval_ids_vec) {
delete [] eval_ids;
}

for (uint32_t i = 0; i < sort_by_clause.eval_expressions.size(); i++) {
delete sort_by_clause.eval.filter_trees[i];
}

delete [] sort_by_clause.eval.filter_trees;
}
}
Expand Down Expand Up @@ -1195,35 +1200,28 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<

continue;
} else if (_sort_field.name == sort_field_const::eval) {
sort_by sort_field_std(sort_field_const::eval, _sort_field.order);
sort_fields_std.emplace_back(sort_field_const::eval, _sort_field.order);
auto& sort_field_std = sort_fields_std.back();

auto const& count = _sort_field.eval_expressions.size();
sort_field_std.eval.filter_trees = new filter_node_t[count];
std::unique_ptr<filter_node_t []> filter_trees_guard(sort_field_std.eval.filter_trees);
sort_field_std.eval.filter_trees = new filter_node_t*[count]{nullptr};

for (uint32_t j = 0; j < count; j++) {
auto const& filter_exp = _sort_field.eval_expressions[j];
if (filter_exp.empty()) {
return Option<bool>(400, "The eval expression in sort_by is empty.");
}

filter_node_t* filter_tree_root = nullptr;
Option<bool> parse_filter_op = filter::parse_filter_query(filter_exp, search_schema,
store, "", filter_tree_root);
std::unique_ptr<filter_node_t> filter_tree_root_guard(filter_tree_root);

store, "", sort_field_std.eval.filter_trees[j]);
if (!parse_filter_op.ok()) {
return Option<bool>(parse_filter_op.code(), "Error parsing eval expression in sort_by clause.");
}

sort_field_std.eval.filter_trees[j] = std::move(*filter_tree_root);
}

eval_sort_count++;
sort_field_std.eval_expressions = _sort_field.eval_expressions;
sort_field_std.eval.scores = _sort_field.eval.scores;
sort_fields_std.emplace_back(sort_field_std);
filter_trees_guard.release();
continue;
}

Expand Down Expand Up @@ -1254,22 +1252,6 @@ Option<bool> Collection::validate_and_standardize_sort_fields(const std::vector<
sort_field_std.name = actual_field_name;
sort_field_std.text_match_buckets = std::stoll(match_parts[1]);

} else if(actual_field_name == sort_field_const::eval) {
const std::string& filter_exp = sort_field_std.name.substr(paran_start + 1,
sort_field_std.name.size() - paran_start -
2);
if(filter_exp.empty()) {
return Option<bool>(400, "The eval expression in sort_by is empty.");
}

Option<bool> parse_filter_op = filter::parse_filter_query(filter_exp, search_schema,
store, "", sort_field_std.eval.filter_trees);
if(!parse_filter_op.ok()) {
return Option<bool>(parse_filter_op.code(), "Error parsing eval expression in sort_by clause.");
}

sort_field_std.name = actual_field_name;
num_sort_expressions++;
} else if(actual_field_name == sort_field_const::vector_query) {
const std::string& vector_query_str = sort_field_std.name.substr(paran_start + 1,
sort_field_std.name.size() - paran_start -
Expand Down
2 changes: 1 addition & 1 deletion src/index.cpp
Expand Up @@ -6149,7 +6149,7 @@ void Index::populate_sort_mapping(int* sort_order, std::vector<size_t>& geopoint
auto& eval_exp = sort_fields_std[i].eval;
auto count = sort_fields_std[i].eval_expressions.size();
for (uint32_t j = 0; j < count; j++) {
auto filter_result_iterator = filter_result_iterator_t("", this, &eval_exp.filter_trees[j],
auto filter_result_iterator = filter_result_iterator_t("", this, eval_exp.filter_trees[j],
search_begin_us, search_stop_us);
auto filter_init_op = filter_result_iterator.init_status();
if (!filter_init_op.ok()) {
Expand Down

0 comments on commit cd1ca59

Please sign in to comment.