Skip to content

feat: filter index rows before top-k - #1111

Merged
Spockkk0225 merged 5 commits into
alibaba:mainfrom
Spockkk0225:fix/fts-index-filter
Sep 23, 2026
Merged

Spockkk0225 merged 5 commits into
alibaba:mainfrom
Spockkk0225:fix/fts-index-filter

Conversation

@Spockkk0225

Copy link
Copy Markdown
Collaborator

Summary

  • register an index_filter scalar function on the SQLite FTS connection
  • pass query-scoped filter state through SQLite pointer binding
  • apply scalar filtering or MVCC visibility checks before SQLite forms the final top-k
  • push LIMIT into the FTS query while preserving deterministic row-ID tie-breaking
  • extend the SQLite wrapper with scalar-function registration and pointer binding

Testing

The incremental FTS build completed successfully before the change was moved onto this standalone branch. Per request, it was not rebuilt after the branch split and no additional tests were added.

Fixes #1106

@Spockkk0225 Spockkk0225 changed the title feat(fts): filter index rows before top-k feat: filter index rows before top-k Sep 22, 2026
@Spockkk0225 Spockkk0225 added skip-check:tests Bypass test requirement for fix PRs skip-check:docs Bypass doc requirement for feat PRs labels Sep 22, 2026
Comment thread extension/fts/src/fts_index.cc Outdated
namespace neug::fts_ext {
namespace {

struct IndexFilter {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接传一个 const 引用吧,用指针感觉会有生命周期管理风险

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessor改用引用
6d9dc38

Comment thread extension/fts/src/fts_index.cc Outdated
std::unordered_set<index_id_t> allowed;
bool use_scalar_filter{false};

bool Accepts(index_id_t index_id) const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议这个接口定义成 bool operator()(index_id_t index_id) const {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改用括号判断
6d9dc38

}
};

void DestroyIndexFilter(void* filter) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要实现 delete 呢?这里不应该管理 IndexIDAccessor 生命周期

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里delete的是IndexFilter这个类的指针

Comment thread extension/fts/src/fts_index.cc Outdated
bool use_scalar_filter{false};

bool Accepts(index_id_t index_id) const {
if (use_scalar_filter) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉这里要不实现成两个不同的类:例如:ScalarFilter or MVCCFilter。这里混在一个接口里实现有些语义不清

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改
6d9dc38

Comment thread extension/fts/src/fts_index.cc Outdated
try {
// Convert the streaming scalar filter to a hash set for fast filtering.
std::unordered_set<index_id_t> allowed;
auto index_filter = std::make_unique<IndexFilter>();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为没有数据修改且没有标量过滤场景提供优化,避免产生 IndexFilter

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

额外增加了两组prepared statement,当index内所有数据都有效时,不再使用filtering,直接使用下推的limit。
11d090b

@longbinlai longbinlai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Spockkk0225
Spockkk0225 merged commit 4a99454 into alibaba:main Sep 23, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-check:docs Bypass doc requirement for feat PRs skip-check:tests Bypass test requirement for fix PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support injecting MVCC visibility checks into FTS index queries

3 participants