Files
zvec/src/db/sqlengine/planner/query_planner.h
T
egolearner 02bfb31cf5 feat: add fts support (#408)
Add BM25-based full-text search with CJK (jieba) tokenization, supporting
  query_string and match_string syntax, phrase queries, boolean operators
  (AND/OR/NOT/MUST), and hybrid retrieval with existing vector search.

  ## Core
  - BitPacked posting format with block-max WAND pruning
  - Tokenizer pipeline: jieba (cut/cut_for_search/hmm/full), whitespace,
    lowercase, with extensible pipeline composition
  - Query parser: boolean operators, phrase queries, field scoping,
    boost, MUST(+) modifier inside OR (ES query_string semantics)
  - AST rewriter: dedup repeated terms with linear boost aggregation,
    flatten same-type composites, canonicalize OR-with-must_not into AND
    wrapper, empty-node propagation, contradiction detection
  - FTS reduce/merge integrated into Optimize compaction
  - Multi-segment score-descending sort
  - Auto-register bundled jieba dict on SDK import

  ## Performance
  - Block-max WAND with cached block_max_info_for (single binary search)
  - AVX2/SSE bitpacked encoding with cross-arch scalar fallback
  - MultiGet for batch posting retrieval and phrase position verification
  - HashSkipList memtable for posting writes
  - PinnableSlice zero-copy reads
  - Filter pushdown into composite iterators (Disjunction/Conjunction/Phrase)
  - Candidate-driven (brute-force) evaluation for selective invert filters
  - Precomputed BM25 IDF weights, cached SIMD dispatch pointers
  - Shortest-list anchor for phrase position matching
  - Single-open per-term posting iterator

  ## Query
  - Tokenize query terms through the same pipeline as indexing
  - EmptyNode for zero-token queries (all stop-words / punctuation)
  - Backslash unescape after lexing in query parser
  - Schema allows collections without vector fields (FTS-only use case)
  - Create/Drop Index validates supported index types
  - FTS fields disallowed in SQL filter expressions

  ## Bindings
  - C API: fts query params, brute-force ratio config
  - Python SDK: FTS search, jieba dict auto-registration

  ## Internals
  - Bypass cppjieba::Jieba to drop KeywordExtractor (~12MB fewer required files)
  - Hide tokenizer pipeline from public header (Pimpl-style FtsState)
  - ListColumnFamilies to avoid double-open on segment load
  - Reorganized fts_column into tokenizer/, posting/, iterator/ subdirs
2026-06-01 15:02:54 +08:00

80 lines
2.7 KiB
C++

// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <memory>
#include <string>
#include <arrow/acero/exec_plan.h>
#include <arrow/compute/expression.h>
#include <zvec/ailego/pattern/expected.hpp>
#include <zvec/db/status.h>
#include "db/index/segment/segment.h"
#include "db/sqlengine/analyzer/query_info.h"
#include "db/sqlengine/planner/doc_filter.h"
#include "plan_info.h"
namespace zvec::sqlengine {
class QueryPlanner {
public:
QueryPlanner(CollectionSchema *schema);
Result<PlanInfo::Ptr> make_plan(
const std::vector<Segment::Ptr> &segments, const std::string &trace_id,
std::vector<sqlengine::QueryInfo::Ptr> *query_infos);
private:
Result<PlanInfo::Ptr> make_physical_plan(
const std::vector<Segment::Ptr> &segments, const std::string &trace_id,
std::vector<sqlengine::QueryInfo::Ptr> *query_infos);
Result<PlanInfo::Ptr> make_group_by_physical_plan(
const std::vector<Segment::Ptr> &segments, const std::string &trace_id,
std::vector<sqlengine::QueryInfo::Ptr> *query_infos);
private:
Result<cp::Expression> parse_filter(const QueryNode *node);
Result<cp::Expression> create_filter_node(const QueryNode *node);
Result<PlanInfo::Ptr> vector_scan(
Segment::Ptr seg, QueryInfo::Ptr query_info,
std::unique_ptr<arrow::compute::Expression> forward_filter,
bool single_stage_search);
Result<PlanInfo::Ptr> invert_scan(
Segment::Ptr seg, QueryInfo::Ptr query_info,
std::unique_ptr<arrow::compute::Expression> forward_filter);
Result<PlanInfo::Ptr> forward_scan(
Segment::Ptr seg, QueryInfo::Ptr query_info,
std::unique_ptr<arrow::compute::Expression> forward_filter);
Result<PlanInfo::Ptr> fts_scan(
Segment::Ptr seg, QueryInfo::Ptr query_info,
std::unique_ptr<arrow::compute::Expression> forward_filter,
bool single_stage_search);
static DocFilter::Ptr build_doc_filter(
const Segment::Ptr &seg, const QueryInfo::Ptr &query_info,
std::unique_ptr<arrow::compute::Expression> &forward_filter,
bool single_stage_search);
static int get_batch_size(const QueryInfo &info, bool has_later_filter);
private:
CollectionSchema *schema_{nullptr};
};
} // namespace zvec::sqlengine