This is an automated email from the ASF dual-hosted git repository.
hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 6719b6071 chore(search): rename TopNSort to TopN (#2930)
6719b6071 is described below
commit 6719b607141fd2b07f5d5a967971fc1d6de0f87a
Author: Jensen <[email protected]>
AuthorDate: Thu May 8 18:58:47 2025 +0800
chore(search): rename TopNSort to TopN (#2930)
Co-authored-by: hulk <[email protected]>
Co-authored-by: Twice <[email protected]>
---
.../{topn_sort_executor.h => topn_executor.h} | 18 +++++++++---------
src/search/ir_pass.h | 4 ++--
src/search/ir_plan.h | 14 ++++++--------
src/search/passes/sort_limit_fuse.h | 2 +-
src/search/plan_executor.cc | 8 ++++----
tests/cppunit/plan_executor_test.cc | 6 +++---
6 files changed, 25 insertions(+), 27 deletions(-)
diff --git a/src/search/executors/topn_sort_executor.h
b/src/search/executors/topn_executor.h
similarity index 83%
rename from src/search/executors/topn_sort_executor.h
rename to src/search/executors/topn_executor.h
index 27c94d242..7eade082e 100644
--- a/src/search/executors/topn_sort_executor.h
+++ b/src/search/executors/topn_executor.h
@@ -28,8 +28,8 @@
namespace kqir {
-struct TopNSortExecutor : ExecutorNode {
- TopNSort *sort;
+struct TopNExecutor : ExecutorNode {
+ TopN *topn;
struct ComparedRow {
RowType row;
@@ -44,20 +44,20 @@ struct TopNSortExecutor : ExecutorNode {
decltype(rows)::iterator rows_iter;
bool initialized = false;
- TopNSortExecutor(ExecutorContext *ctx, TopNSort *sort) : ExecutorNode(ctx),
sort(sort) {}
+ TopNExecutor(ExecutorContext *ctx, TopN *topn) : ExecutorNode(ctx),
topn(topn) {}
StatusOr<Result> Next() override {
if (!initialized) {
- auto total = sort->limit->offset + sort->limit->count;
+ auto total = topn->limit->offset + topn->limit->count;
if (total == 0) return end;
- auto v = GET_OR_RET(ctx->Get(sort->op)->Next());
+ auto v = GET_OR_RET(ctx->Get(topn->op)->Next());
while (!std::holds_alternative<End>(v)) {
auto &row = std::get<RowType>(v);
auto get_order = [this](RowType &row) -> StatusOr<double> {
- auto order_val = GET_OR_RET(ctx->Retrieve(ctx->db_ctx, row,
sort->order->field->info));
+ auto order_val = GET_OR_RET(ctx->Retrieve(ctx->db_ctx, row,
topn->order->field->info));
CHECK(order_val.Is<kqir::Numeric>());
return order_val.Get<kqir::Numeric>();
};
@@ -79,15 +79,15 @@ struct TopNSortExecutor : ExecutorNode {
}
}
- v = GET_OR_RET(ctx->Get(sort->op)->Next());
+ v = GET_OR_RET(ctx->Get(topn->op)->Next());
}
- if (rows.size() <= sort->limit->offset) {
+ if (rows.size() <= topn->limit->offset) {
return end;
}
std::sort(rows.begin(), rows.end());
- rows_iter = rows.begin() +
static_cast<std::ptrdiff_t>(sort->limit->offset);
+ rows_iter = rows.begin() +
static_cast<std::ptrdiff_t>(topn->limit->offset);
initialized = true;
}
diff --git a/src/search/ir_pass.h b/src/search/ir_pass.h
index aa5150a1b..312095415 100644
--- a/src/search/ir_pass.h
+++ b/src/search/ir_pass.h
@@ -90,7 +90,7 @@ struct Visitor : Pass {
return Visit(std::move(v));
} else if (auto v = Node::As<Sort>(std::move(node))) {
return Visit(std::move(v));
- } else if (auto v = Node::As<TopNSort>(std::move(node))) {
+ } else if (auto v = Node::As<TopN>(std::move(node))) {
return Visit(std::move(v));
} else if (auto v = Node::As<Projection>(std::move(node))) {
return Visit(std::move(v));
@@ -223,7 +223,7 @@ struct Visitor : Pass {
return node;
}
- virtual std::unique_ptr<Node> Visit(std::unique_ptr<TopNSort> node) {
+ virtual std::unique_ptr<Node> Visit(std::unique_ptr<TopN> node) {
node->op = TransformAs<PlanOperator>(std::move(node->op));
node->limit = VisitAs<LimitClause>(std::move(node->limit));
node->order = VisitAs<SortByClause>(std::move(node->order));
diff --git a/src/search/ir_plan.h b/src/search/ir_plan.h
index 8743a8273..8542bd6e6 100644
--- a/src/search/ir_plan.h
+++ b/src/search/ir_plan.h
@@ -226,31 +226,29 @@ struct Sort : PlanOperator {
};
// operator fusion: Sort + Limit
-struct TopNSort : PlanOperator {
+struct TopN : PlanOperator {
std::unique_ptr<PlanOperator> op;
std::unique_ptr<SortByClause> order;
std::unique_ptr<LimitClause> limit;
- TopNSort(std::unique_ptr<PlanOperator> &&op, std::unique_ptr<SortByClause>
&&order,
- std::unique_ptr<LimitClause> &&limit)
+ TopN(std::unique_ptr<PlanOperator> &&op, std::unique_ptr<SortByClause>
&&order, std::unique_ptr<LimitClause> &&limit)
: op(std::move(op)), order(std::move(order)), limit(std::move(limit)) {}
- std::string_view Name() const override { return "TopNSort"; };
+ std::string_view Name() const override { return "TopN"; };
std::string Dump() const override {
return fmt::format("(top-n sort {}, {}, {}, {}: {})",
order->field->Dump(), order->OrderToString(order->order),
limit->offset, limit->count, op->Dump());
}
static inline const std::vector<std::function<Node *(Node *)>> ChildMap = {
- NodeIterator::MemFn<&TopNSort::op>,
NodeIterator::MemFn<&TopNSort::order>, NodeIterator::MemFn<&TopNSort::limit>};
+ NodeIterator::MemFn<&TopN::op>, NodeIterator::MemFn<&TopN::order>,
NodeIterator::MemFn<&TopN::limit>};
NodeIterator ChildBegin() override { return NodeIterator(this,
ChildMap.begin()); }
NodeIterator ChildEnd() override { return NodeIterator(this,
ChildMap.end()); }
std::unique_ptr<Node> Clone() const override {
- return std::make_unique<TopNSort>(Node::MustAs<PlanOperator>(op->Clone()),
-
Node::MustAs<SortByClause>(order->Clone()),
-
Node::MustAs<LimitClause>(limit->Clone()));
+ return std::make_unique<TopN>(Node::MustAs<PlanOperator>(op->Clone()),
Node::MustAs<SortByClause>(order->Clone()),
+ Node::MustAs<LimitClause>(limit->Clone()));
}
};
diff --git a/src/search/passes/sort_limit_fuse.h
b/src/search/passes/sort_limit_fuse.h
index d7d247f30..664de6547 100644
--- a/src/search/passes/sort_limit_fuse.h
+++ b/src/search/passes/sort_limit_fuse.h
@@ -35,7 +35,7 @@ struct SortLimitFuse : Visitor {
node = Node::MustAs<Limit>(Visitor::Visit(std::move(node)));
if (auto sort = Node::As<Sort>(std::move(node->op))) {
- return std::make_unique<TopNSort>(std::move(sort->op),
std::move(sort->order), std::move(node->limit));
+ return std::make_unique<TopN>(std::move(sort->op),
std::move(sort->order), std::move(node->limit));
}
return node;
diff --git a/src/search/plan_executor.cc b/src/search/plan_executor.cc
index 4ff379edd..6fdd375eb 100644
--- a/src/search/plan_executor.cc
+++ b/src/search/plan_executor.cc
@@ -34,7 +34,7 @@
#include "search/executors/projection_executor.h"
#include "search/executors/sort_executor.h"
#include "search/executors/tag_field_scan_executor.h"
-#include "search/executors/topn_sort_executor.h"
+#include "search/executors/topn_executor.h"
#include "search/indexer.h"
#include "search/ir_plan.h"
@@ -70,7 +70,7 @@ struct ExecutorContextVisitor {
return Visit(v);
}
- if (auto v = dynamic_cast<TopNSort *>(op)) {
+ if (auto v = dynamic_cast<TopN *>(op)) {
return Visit(v);
}
@@ -128,8 +128,8 @@ struct ExecutorContextVisitor {
Transform(op->source.get());
}
- void Visit(TopNSort *op) {
- ctx->nodes[op] = std::make_unique<TopNSortExecutor>(ctx, op);
+ void Visit(TopN *op) {
+ ctx->nodes[op] = std::make_unique<TopNExecutor>(ctx, op);
Transform(op->op.get());
}
diff --git a/tests/cppunit/plan_executor_test.cc
b/tests/cppunit/plan_executor_test.cc
index ecff4bb17..638fb8340 100644
--- a/tests/cppunit/plan_executor_test.cc
+++ b/tests/cppunit/plan_executor_test.cc
@@ -95,7 +95,7 @@ static auto N(double n) { return MakeValue<Numeric>(n); }
static auto T(const std::string& v) { return
MakeValue<StringArray>(util::Split(v, ",")); }
static auto V(const std::vector<double>& vals) { return
MakeValue<NumericArray>(vals); }
-TEST(PlanExecutorTest, TopNSort) {
+TEST(PlanExecutorTest, TopN) {
std::vector<ExecutorNode::RowType> data{
{"a", {{FieldI("f3"), N(4)}}, IndexI()}, {"b", {{FieldI("f3"), N(2)}},
IndexI()},
{"c", {{FieldI("f3"), N(7)}}, IndexI()}, {"d", {{FieldI("f3"), N(3)}},
IndexI()},
@@ -103,7 +103,7 @@ TEST(PlanExecutorTest, TopNSort) {
{"g", {{FieldI("f3"), N(8)}}, IndexI()},
};
{
- auto op = std::make_unique<TopNSort>(
+ auto op = std::make_unique<TopN>(
std::make_unique<Mock>(data),
std::make_unique<SortByClause>(SortByClause::ASC,
std::make_unique<FieldRef>("f3", FieldI("f3"))),
std::make_unique<LimitClause>(0, 4));
@@ -116,7 +116,7 @@ TEST(PlanExecutorTest, TopNSort) {
ASSERT_EQ(ctx.Next().GetValue(), exe_end);
}
{
- auto op = std::make_unique<TopNSort>(
+ auto op = std::make_unique<TopN>(
std::make_unique<Mock>(data),
std::make_unique<SortByClause>(SortByClause::ASC,
std::make_unique<FieldRef>("f3", FieldI("f3"))),
std::make_unique<LimitClause>(1, 4));