diff --git a/include/paimon/file_index/file_index_reader.h b/include/paimon/file_index/file_index_reader.h index 677617bbf..1524876c9 100644 --- a/include/paimon/file_index/file_index_reader.h +++ b/include/paimon/file_index/file_index_reader.h @@ -20,11 +20,11 @@ #include #include -#include #include #include "paimon/file_index/file_index_result.h" #include "paimon/file_index/scored_file_index_result.h" +#include "paimon/index/index_reader_capabilities.h" #include "paimon/predicate/full_text_search.h" #include "paimon/predicate/function_visitor.h" #include "paimon/predicate/vector_search.h" @@ -32,7 +32,6 @@ #include "paimon/visibility.h" namespace paimon { -class LeafPredicate; struct FullTextMatch; /// Evaluates filter predicates against a file-level index to determine file eligibility. /// @@ -42,23 +41,11 @@ struct FullTextMatch; /// whether a given data file may contain rows matching a specific predicate. /// @note Callers of `Visit*` for DECIMAL fields must ensure each literal's scale matches the scale /// of the indexed data; otherwise, index filtering results may be incorrect. -class PAIMON_EXPORT FileIndexReader : public FunctionVisitor> { +class PAIMON_EXPORT FileIndexReader : public IndexReaderCapabilities, + public FunctionVisitor> { public: - enum class SearchType { FULL_TEXT_MATCH, FULL_TEXT_SEARCH, VECTOR_SEARCH }; - ~FileIndexReader() override = default; - /// Whether this reader can execute the requested search without probing it with a query. - virtual bool SupportsSearch(SearchType search_type) const; - - /// Estimates the fraction of rows in the data file expected to match the predicate. - /// Returns a value in [0, 1], or std::nullopt if no estimate is available. - /// Values outside [0, 1] are also treated as unknown. - /// The estimate only guides predicate execution order; it is not used to filter rows. - /// TODO(lisizhuo.lsz): Add selectivity estimates for scalar indexes such as bitmap indexes. - virtual std::optional EstimateSelectivity( - const std::shared_ptr& predicate) const; - Result> VisitIsNotNull() override; Result> VisitIsNull() override; diff --git a/include/paimon/index/index_reader_capabilities.h b/include/paimon/index/index_reader_capabilities.h new file mode 100644 index 000000000..2de2ee8b3 --- /dev/null +++ b/include/paimon/index/index_reader_capabilities.h @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include + +#include "paimon/visibility.h" + +namespace paimon { +class LeafPredicate; + +/// Search capabilities and predicate scheduling hints shared by index readers, independently of +/// their result types and row ID domains. +class PAIMON_EXPORT IndexReaderCapabilities { + public: + enum class SearchType { FULL_TEXT_MATCH, FULL_TEXT_SEARCH, VECTOR_SEARCH }; + + virtual ~IndexReaderCapabilities() = default; + + /// Whether this reader can execute the requested search without probing it with a query. + /// The default implementation returns false. + virtual bool SupportsSearch(SearchType search_type) const; + + /// Estimates the fraction of this reader's indexed rows expected to match the predicate. + /// Returns a value in [0, 1], or std::nullopt if no estimate is available. + /// Values outside [0, 1] are also treated as unknown by evaluators. + /// Estimates only guide predicate execution order and must never exclude rows. + /// The default implementation returns std::nullopt. + /// TODO(lisizhuo.lsz): Add selectivity estimates for scalar indexes such as bitmap indexes. + virtual std::optional EstimateSelectivity( + const std::shared_ptr& predicate) const; +}; + +} // namespace paimon diff --git a/include/paimon/predicate/full_text_match.h b/include/paimon/predicate/full_text_match.h index 51c37536a..50e9924ae 100644 --- a/include/paimon/predicate/full_text_match.h +++ b/include/paimon/predicate/full_text_match.h @@ -44,6 +44,7 @@ struct PAIMON_EXPORT FullTextMatch { /// JSON DSL query with the same syntax and analyzer semantics as `FullTextSearch::query`. std::string query; /// Optional eligible row ids. File readers use file-local physical row positions; + /// real-time index readers use real-time offsets. /// An empty set matches no rows. If not set, all rows are searched. std::optional include_row_ids; }; diff --git a/include/paimon/predicate/full_text_search.h b/include/paimon/predicate/full_text_search.h index 46e8024db..20aa01eb3 100644 --- a/include/paimon/predicate/full_text_search.h +++ b/include/paimon/predicate/full_text_search.h @@ -103,8 +103,8 @@ struct PAIMON_EXPORT FullTextSearch { int32_t limit; /// The row ids to search within, aligned with Java `includeRowIds`. The row id domain is /// determined by the reader: global row ids for GlobalIndexReader and physical file-local row - /// ids for FileIndexReader. Only these rows are ranked, so an empty set matches no rows. If not - /// set, all rows are searched. + /// ids for FileIndexReader, and real-time offsets for RealtimeIndexReader. Only these rows are + /// ranked, so an empty set matches no rows. If not set, all rows are searched. std::optional include_row_ids; }; } // namespace paimon diff --git a/include/paimon/predicate/vector_search.h b/include/paimon/predicate/vector_search.h index 7d01e03f4..c28cf95f5 100644 --- a/include/paimon/predicate/vector_search.h +++ b/include/paimon/predicate/vector_search.h @@ -36,9 +36,10 @@ struct PAIMON_EXPORT VectorSearch { /// `PreFilter`: A lightweight pre-filtering function applied **before** similarity scoring. /// The row id domain is determined by the reader executing the search: `GlobalIndexReader` /// supplies global row ids, while `FileIndexReader` supplies physical row ids local to the - /// current data file. This filter enables early pruning of irrelevant candidates (e.g., "only - /// consider rows with label X"), significantly reducing the search space. Returns true to - /// include the row in vector search process; false to exclude it. + /// current data file. Real-time index readers supply real-time offsets. This filter enables + /// early pruning of irrelevant candidates (e.g., "only consider rows with label X"), + /// significantly reducing the search space. Returns true to include the row in vector search + /// process; false to exclude it. /// /// @note Must be thread-safe. using PreFilter = std::function; @@ -71,7 +72,7 @@ struct PAIMON_EXPORT VectorSearch { /// The query vector (must match the dimensionality of the indexed vectors). std::vector query; /// A pre-filter whose row id domain is determined by the reader: global row ids for Global - /// Index and file-local physical row ids for File Index. + /// Index, file-local physical row ids for File Index, and offsets for Real-time Index. std::function pre_filter; /// A runtime filtering condition that may involve graph traversal of /// structured attributes. **Using this parameter often yields better diff --git a/include/paimon/read_context.h b/include/paimon/read_context.h index 95aafb8f0..d9db6169f 100644 --- a/include/paimon/read_context.h +++ b/include/paimon/read_context.h @@ -401,6 +401,8 @@ class PAIMON_EXPORT ReadContextBuilder { /// data to compute exact matching rows. Only scalar predicates may be evaluated without an /// index. Score output must use named projections such as __INDEX_SCORE; /// projecting _INDEX_SCORE by name, field ID or read schema is not supported in this mode. + /// Real-time append and deduplicate reads use pinned offsets and exact scalar indexes, + /// scanning predicates that have no exact index. Real-time primary-key merge is not supported. /// The default is false. /// @param enabled Whether to compute exact result row positions during file-index evaluation. /// @return Reference to this builder for method chaining. diff --git a/include/paimon/realtime/index/realtime_index_read_source.h b/include/paimon/realtime/index/realtime_index_read_source.h new file mode 100644 index 000000000..d78b9c59a --- /dev/null +++ b/include/paimon/realtime/index/realtime_index_read_source.h @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include + +#include "paimon/realtime/index/realtime_index_reader.h" +#include "paimon/realtime/realtime_store.h" + +namespace paimon { + +/// One immutable query source. Readers and index readers must share its pinned data snapshot. +class PAIMON_EXPORT RealtimeIndexReadSource { + public: + virtual ~RealtimeIndexReadSource() = default; + + virtual OffsetRange GetOffsetRange() const = 0; + + /// Builds a candidate superset from offset bounds and pinned deletion metadata without + /// reading data. Gaps may be included; index and data readers only return existing rows. + /// Deleted offsets must be excluded before full-text or vector TopK executes. + virtual Result CreateCandidates() const = 0; + + /// Returns readers bound to this source's offset range. A missing field has no readers. + /// Readers must not expose writes published after the pinned snapshot. + virtual Result>> CreateIndexReaders( + const std::string& field_name) const = 0; +}; + +/// Optional capability for precise real-time filtering and search. Ordinary query reads do not +/// require it. Each visible row in the requested range of the pinned snapshot +/// must belong to exactly one source. Source offset ranges must not overlap, so data readers +/// can use the same pinned view with source-local offset bounds. +class PAIMON_EXPORT RealtimeIndexedReadView : public RealtimeReadView { + public: + ~RealtimeIndexedReadView() override = default; + + /// Creates sources restricted to the intersection of each segment's range and the query range. + /// @param offset_range Query bounds, which may be narrower than GetOffsetRange() to exclude + /// offsets already covered by the disk snapshot selected after this view was pinned. + virtual Result>> CreateIndexReadSources( + const OffsetRange& offset_range) const = 0; +}; + +} // namespace paimon diff --git a/include/paimon/realtime/index/realtime_index_reader.h b/include/paimon/realtime/index/realtime_index_reader.h index 78e1a3681..35666898e 100644 --- a/include/paimon/realtime/index/realtime_index_reader.h +++ b/include/paimon/realtime/index/realtime_index_reader.h @@ -23,19 +23,43 @@ #include #include +#include "paimon/index/index_reader_capabilities.h" #include "paimon/predicate/function_visitor.h" +#include "paimon/realtime/index/realtime_index_result.h" +#include "paimon/realtime/index/scored_realtime_index_result.h" #include "paimon/utils/roaring_bitmap64.h" #include "paimon/visibility.h" namespace paimon { +class LeafPredicate; +struct VectorSearch; +struct FullTextSearch; +struct FullTextMatch; /// Visits predicates on one indexed field, returning realtime offsets. /// nullopt means no pruning; an empty bitmap means no candidates. False negatives are not allowed. /// Unsupported predicates default to no pruning. Plugins override only the supported visits. -class PAIMON_EXPORT RealtimeIndexReader : public FunctionVisitor> { +class PAIMON_EXPORT RealtimeIndexReader : public IndexReaderCapabilities, + public FunctionVisitor> { public: ~RealtimeIndexReader() override = default; + /// Search pre-filters and result IDs use real-time offsets, not file-local row positions. + /// Apply filters and pinned visibility before TopK; filtering a truncated result is + /// insufficient. Search results must be stable for the pinned snapshot even while later writes + /// proceed. + virtual Result> VisitVectorSearch( + const std::shared_ptr& search); + + virtual Result> VisitFullTextSearch( + const std::shared_ptr& search); + + /// Returns all matching offsets without TopK truncation. When needs_scores is true, + /// returns ScoredRealtimeIndexResult, including for an empty result; otherwise scores + /// need not be computed. + virtual Result> VisitFullTextMatch( + const std::shared_ptr& match, bool needs_scores); + Result> VisitIsNotNull() override; Result> VisitIsNull() override; Result> VisitEqual(const Literal& literal) override; diff --git a/include/paimon/realtime/index/realtime_index_result.h b/include/paimon/realtime/index/realtime_index_result.h new file mode 100644 index 000000000..ae19075d9 --- /dev/null +++ b/include/paimon/realtime/index/realtime_index_result.h @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include + +#include "paimon/macros.h" +#include "paimon/result.h" +#include "paimon/utils/roaring_bitmap64.h" +#include "paimon/visibility.h" + +namespace paimon { + +/// Matching real-time offsets. ScoredRealtimeIndexResult additionally carries scores. +class PAIMON_EXPORT RealtimeIndexResult { + public: + virtual ~RealtimeIndexResult() = default; + + static Result> Create(RoaringBitmap64 rows) { + PAIMON_RETURN_NOT_OK(ValidateRowPositions(rows)); + return std::shared_ptr(new RealtimeIndexResult(std::move(rows))); + } + + const RoaringBitmap64& GetRowPositions() const { + return rows_; + } + + protected: + explicit RealtimeIndexResult(RoaringBitmap64 rows) : rows_(std::move(rows)) {} + + static Status ValidateRowPositions(const RoaringBitmap64& rows) { + for (auto iter = rows.Begin(); iter != rows.End(); ++iter) { + if (*iter < 0) { + return Status::Invalid("Real-time search returned a negative offset"); + } + } + return Status::OK(); + } + + private: + RoaringBitmap64 rows_; +}; + +} // namespace paimon diff --git a/include/paimon/realtime/index/scored_realtime_index_result.h b/include/paimon/realtime/index/scored_realtime_index_result.h new file mode 100644 index 000000000..7d3a6fd9d --- /dev/null +++ b/include/paimon/realtime/index/scored_realtime_index_result.h @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include + +#include "paimon/realtime/index/realtime_index_result.h" +#include "paimon/utils/roaring_bitmap64.h" +#include "paimon/visibility.h" + +namespace paimon { + +/// Search results keyed by real-time offset. Scores follow ascending offset order, not rank. +class PAIMON_EXPORT ScoredRealtimeIndexResult : public RealtimeIndexResult { + public: + static Result> Create(RoaringBitmap64 rows, + std::vector scores) { + if (rows.Cardinality() != static_cast(scores.size())) { + return Status::Invalid("Real-time search score count does not match offsets"); + } + PAIMON_RETURN_NOT_OK(ValidateRowPositions(rows)); + return std::shared_ptr( + new ScoredRealtimeIndexResult(std::move(rows), std::move(scores))); + } + + const std::vector& GetScores() const { + return scores_; + } + + private: + ScoredRealtimeIndexResult(RoaringBitmap64 rows, std::vector scores) + : RealtimeIndexResult(std::move(rows)), scores_(std::move(scores)) {} + + std::vector scores_; +}; + +} // namespace paimon diff --git a/include/paimon/realtime/realtime_store.h b/include/paimon/realtime/realtime_store.h index aef09778f..da9d82275 100644 --- a/include/paimon/realtime/realtime_store.h +++ b/include/paimon/realtime/realtime_store.h @@ -42,6 +42,7 @@ namespace paimon { class FileSystem; class MemoryPool; class Predicate; +class RoaringBitmap64; enum class PAIMON_EXPORT RealtimeStoreMode { APPEND_ONLY, @@ -142,11 +143,22 @@ struct PAIMON_EXPORT RealtimeQueryContext { /// Optional predicate using field indexes from `read_schema`. A non-null predicate allows the /// plugin to prune candidate rows. Exact filtering is applied by the Paimon read framework. std::shared_ptr predicate; - /// Required [disk snapshot committed end, pinned memory end) bounds. - /// Pin memory before selecting the disk snapshot; keep these bounds for the query. + /// Required visible bounds within [disk snapshot committed end, pinned memory end). + /// Source-local reads may narrow this range. Pin memory before selecting the disk snapshot. OffsetRange offset_range; /// Maximum number of rows in a returned query batch. int32_t read_batch_size = 1024; + /// Optional real-time offsets to materialize. Null means no additional offset restriction; + /// an empty bitmap selects no rows. Stores must honor this selection before returning rows. + /// Offset selection is currently unsupported for primary-key merge. + std::shared_ptr selection; + /// Optional real-time offsets to exclude independently of selection. Null or an empty bitmap + /// excludes no additional rows. Stores must apply these deletions before returning rows. + /// Offset deletions are currently unsupported for primary-key merge. + std::shared_ptr offset_deletions; + /// Whether the store should evaluate scalar indexes. Statistics pruning remains available + /// when false; precise reads already perform index evaluation in the framework. + bool enable_index_pruning; }; /// Customizable plugin interface for storing and querying real-time rows before Paimon data-file diff --git a/src/paimon/CMakeLists.txt b/src/paimon/CMakeLists.txt index 30f6250e0..c527d8749 100644 --- a/src/paimon/CMakeLists.txt +++ b/src/paimon/CMakeLists.txt @@ -93,6 +93,7 @@ set(PAIMON_COMMON_SRCS common/global_index/global_index_result.cpp common/global_index/global_indexer_factory.cpp common/global_index/key_serializer.cpp + common/index/index_reader_capabilities.cpp common/io/buffered_input_stream.cpp common/io/byte_array_input_stream.cpp common/io/byte_array_output_stream.cpp @@ -150,6 +151,7 @@ set(PAIMON_COMMON_SRCS common/reader/batch_reader.cpp common/reader/complete_index_score_batch_reader.cpp common/reader/complete_index_score_file_batch_reader.cpp + common/reader/index_score_batch_utils.cpp common/reader/concat_batch_reader.cpp common/reader/late_materializing_file_batch_reader.cpp common/reader/data_file_reader_factory.cpp @@ -424,6 +426,7 @@ set(PAIMON_CORE_SRCS core/manifest/snapshot_live_manifest_entries.cpp core/operation/write_context.cpp core/operation/write_restore.cpp + core/realtime/precise_realtime_index_read.cpp core/realtime/arrow_realtime_store.cpp core/realtime/arrow_deduplicate_realtime_store.cpp core/realtime/arrow_realtime_store_factory.cpp @@ -1014,6 +1017,7 @@ if(PAIMON_BUILD_TESTS) core/operation/commit/realtime_deduplicate_dv_materializer_test.cpp core/operation/commit/sequence_snapshot_properties_test.cpp core/operation/key_value_file_store_write_test.cpp + core/realtime/precise_realtime_index_read_test.cpp core/operation/precise_file_index_read_test.cpp core/operation/internal_read_context_test.cpp core/operation/abstract_split_read_test.cpp diff --git a/src/paimon/common/file_index/file_index_format.cpp b/src/paimon/common/file_index/file_index_format.cpp index 44d31b584..44a828656 100644 --- a/src/paimon/common/file_index/file_index_format.cpp +++ b/src/paimon/common/file_index/file_index_format.cpp @@ -34,7 +34,6 @@ #include "paimon/core/io/file_index_options.h" #include "paimon/file_index/file_indexer.h" #include "paimon/file_index/file_indexer_factory.h" -#include "paimon/io/data_input_stream.h" #include "paimon/memory/bytes.h" #include "paimon/status.h" @@ -49,21 +48,8 @@ class FileIndexFormatReaderImpl : public FileIndexFormat::Reader { const std::map& options) { PAIMON_ASSIGN_OR_RAISE(FileIndexOptions file_index_options, FileIndexOptions::FromMap(options)); - DataInputStream data_input_stream(input_stream); - PAIMON_ASSIGN_OR_RAISE(int64_t magic, data_input_stream.ReadValue()); - if (magic != FileIndexFormat::MAGIC) { - return Status::Invalid("This file is not file index file."); - } - PAIMON_ASSIGN_OR_RAISE(int32_t version, data_input_stream.ReadValue()); - if (version != FileIndexFormat::V_1 && version != FileIndexFormat::V_2) { - return Status::Invalid(fmt::format("Unsupported file index version {}", version)); - } - PAIMON_ASSIGN_OR_RAISE(int64_t file_length, input_stream->Length()); - PAIMON_ASSIGN_OR_RAISE( - FileIndexFormatUtils::FileIndexEntries header, - version == FileIndexFormat::V_1 - ? FileIndexFormatV1::ReadIndexEntries(input_stream, pool, file_length) - : FileIndexFormatV2::ReadIndexEntries(input_stream, pool, file_length)); + PAIMON_ASSIGN_OR_RAISE(FileIndexFormatUtils::FileIndexEntries header, + FileIndexFormatUtils::ReadIndexEntries(input_stream, pool)); return std::unique_ptr(new FileIndexFormatReaderImpl( input_stream, std::move(header), pool, std::move(file_index_options))); } diff --git a/src/paimon/common/file_index/file_index_format_utils.cpp b/src/paimon/common/file_index/file_index_format_utils.cpp index c1565d698..d42344e34 100644 --- a/src/paimon/common/file_index/file_index_format_utils.cpp +++ b/src/paimon/common/file_index/file_index_format_utils.cpp @@ -18,12 +18,32 @@ #include "paimon/common/file_index/file_index_format_utils.h" +#include "fmt/format.h" +#include "paimon/common/file_index/file_index_format_v1.h" +#include "paimon/common/file_index/file_index_format_v2.h" #include "paimon/common/io/offset_input_stream.h" #include "paimon/file_index/file_index_format.h" #include "paimon/io/buffered_input_stream.h" #include "paimon/io/data_input_stream.h" namespace paimon { +Result FileIndexFormatUtils::ReadIndexEntries( + const std::shared_ptr& input, const std::shared_ptr& pool) { + DataInputStream in(input); + PAIMON_ASSIGN_OR_RAISE(int64_t magic, in.ReadValue()); + if (magic != FileIndexFormat::MAGIC) { + return Status::Invalid("This file is not file index file."); + } + PAIMON_ASSIGN_OR_RAISE(int32_t version, in.ReadValue()); + if (version != FileIndexFormat::V_1 && version != FileIndexFormat::V_2) { + return Status::Invalid(fmt::format("Unsupported file index version {}", version)); + } + PAIMON_ASSIGN_OR_RAISE(int64_t file_length, input->Length()); + return version == FileIndexFormat::V_1 + ? FileIndexFormatV1::ReadIndexEntries(input, pool, file_length) + : FileIndexFormatV2::ReadIndexEntries(input, pool, file_length); +} + Result> FileIndexFormatUtils::CreateMetadataInputStream( const std::shared_ptr& input, int64_t start, int64_t length, int64_t file_length, const std::shared_ptr& pool) { diff --git a/src/paimon/common/file_index/file_index_format_utils.h b/src/paimon/common/file_index/file_index_format_utils.h index 5ab3b6c78..39102d67d 100644 --- a/src/paimon/common/file_index/file_index_format_utils.h +++ b/src/paimon/common/file_index/file_index_format_utils.h @@ -25,6 +25,7 @@ #include #include "paimon/result.h" +#include "paimon/visibility.h" namespace paimon { class DataInputStream; @@ -41,6 +42,11 @@ class FileIndexFormatUtils { FileIndexFormatUtils() = delete; ~FileIndexFormatUtils() = delete; + /// Validates the container prefix and reads V1 or V2 entries. The stream must be positioned + /// at the beginning of the container. + PAIMON_EXPORT static Result ReadIndexEntries( + const std::shared_ptr& input, const std::shared_ptr& pool); + /// Creates a bounded, buffered input stream for the container's index metadata. static Result> CreateMetadataInputStream( const std::shared_ptr& input, int64_t start, int64_t length, diff --git a/src/paimon/common/file_index/file_index_reader.cpp b/src/paimon/common/file_index/file_index_reader.cpp index d51910d2b..df2f5d31f 100644 --- a/src/paimon/common/file_index/file_index_reader.cpp +++ b/src/paimon/common/file_index/file_index_reader.cpp @@ -24,15 +24,6 @@ #include "paimon/status.h" namespace paimon { -bool FileIndexReader::SupportsSearch(SearchType) const { - return false; -} - -std::optional FileIndexReader::EstimateSelectivity( - const std::shared_ptr&) const { - return std::nullopt; -} - Result> FileIndexReader::VisitIsNotNull() { return FileIndexResult::Remain(); } diff --git a/src/paimon/common/index/index_reader_capabilities.cpp b/src/paimon/common/index/index_reader_capabilities.cpp new file mode 100644 index 000000000..145ea57c5 --- /dev/null +++ b/src/paimon/common/index/index_reader_capabilities.cpp @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/index/index_reader_capabilities.h" + +namespace paimon { + +bool IndexReaderCapabilities::SupportsSearch(SearchType) const { + return false; +} + +std::optional IndexReaderCapabilities::EstimateSelectivity( + const std::shared_ptr&) const { + return std::nullopt; +} + +} // namespace paimon diff --git a/src/paimon/common/index/index_reader_utils.h b/src/paimon/common/index/index_reader_utils.h new file mode 100644 index 000000000..e46428438 --- /dev/null +++ b/src/paimon/common/index/index_reader_utils.h @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include +#include +#include +#include +#include + +#include "paimon/file_index/bitmap_index_result.h" +#include "paimon/file_index/file_index_reader.h" +#include "paimon/file_index/scored_file_index_result.h" +#include "paimon/predicate/full_text_match.h" +#include "paimon/predicate/function.h" +#include "paimon/predicate/leaf_predicate.h" +#include "paimon/predicate/predicate_utils.h" +#include "paimon/realtime/index/realtime_index_reader.h" + +namespace paimon { + +/// Reader selection and conversion of backend results to row candidates and optional scores. +class IndexReaderUtils { + public: + IndexReaderUtils() = delete; + ~IndexReaderUtils() = delete; + + template + struct FullTextResult { + Bitmap rows; + std::shared_ptr scores; + }; + + template + using FullTextScoredResult = + std::conditional_t, ScoredFileIndexResult, + ScoredRealtimeIndexResult>; + + template + static Result> GetSearchReader( + const std::vector>& readers, + IndexReaderCapabilities::SearchType type, const std::string& multiple_error, + const std::string& missing_error) { + std::shared_ptr selected; + for (const auto& reader : readers) { + if (!reader->SupportsSearch(type)) { + continue; + } + if (selected) { + return Status::Invalid(multiple_error); + } + selected = reader; + } + if (!selected) { + return Status::NotImplemented(missing_error); + } + return selected; + } + + template + static Result> EstimateSelectivity( + const std::shared_ptr& predicate, Indexes* indexes) { + if (predicate->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH) { + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr reader, + indexes->GetSearchReader(predicate->FieldName(), + IndexReaderCapabilities::SearchType::FULL_TEXT_MATCH)); + return reader->EstimateSelectivity(predicate); + } + PAIMON_ASSIGN_OR_RAISE(std::vector> readers, + indexes->GetReaders(predicate->FieldName())); + std::optional estimate; + for (const auto& reader : readers) { + auto value = reader->EstimateSelectivity(predicate); + if (value && std::isfinite(*value) && *value >= 0.0 && *value <= 1.0 && + (!estimate || *value < *estimate)) { + estimate = value; + } + } + return estimate; + } + + static Result> VisitScalar( + const std::shared_ptr& reader, + const std::shared_ptr& predicate) { + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr result, + PredicateUtils::VisitPredicate>(predicate, reader)); + if (!result) { + return Status::Invalid("Scalar index returned a null result"); + } + if (auto bitmap = std::dynamic_pointer_cast(result)) { + PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap32* rows, bitmap->GetBitmap()); + return std::optional(*rows); + } + if (std::dynamic_pointer_cast(result)) { + return std::optional(RoaringBitmap32()); + } + if (std::dynamic_pointer_cast(result)) { + return std::optional(); + } + return Status::Invalid("Invalid scalar index result for predicate ", predicate->ToString()); + } + + static Result> VisitScalar( + const std::shared_ptr& reader, + const std::shared_ptr& predicate) { + return PredicateUtils::VisitPredicate>(predicate, reader); + } + + template + static Result>> VisitFullTextMatch( + const std::shared_ptr& reader, const std::shared_ptr& match, + const Bitmap& candidates, bool needs_scores) { + using ScoredResult = FullTextScoredResult; + auto filtered = std::make_shared(*match); + RoaringBitmap64 eligible(candidates); + if (filtered->include_row_ids) { + *filtered->include_row_ids &= eligible; + } else { + filtered->include_row_ids = std::move(eligible); + } + PAIMON_ASSIGN_OR_RAISE(auto result, reader->VisitFullTextMatch(filtered, needs_scores)); + if (!result) { + return Status::Invalid("Full-text match returned a null result"); + } + if (needs_scores) { + auto scored = std::dynamic_pointer_cast(result); + if (!scored) { + return Status::Invalid("Full-text index did not return requested scores"); + } + return FullTextResult{scored->GetRowPositions(), + std::move(scored)}; + } + if constexpr (std::is_same_v) { + auto bitmap = std::dynamic_pointer_cast(result); + if (!bitmap) { + return Status::Invalid("Full-text index did not return a bitmap"); + } + PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap32* rows, bitmap->GetBitmap()); + return FullTextResult{*rows, nullptr}; + } else { + return FullTextResult{result->GetRowPositions(), nullptr}; + } + } +}; + +} // namespace paimon diff --git a/src/paimon/common/reader/complete_index_score_file_batch_reader.cpp b/src/paimon/common/reader/complete_index_score_file_batch_reader.cpp index 0a778d151..63f0d6529 100644 --- a/src/paimon/common/reader/complete_index_score_file_batch_reader.cpp +++ b/src/paimon/common/reader/complete_index_score_file_batch_reader.cpp @@ -25,9 +25,9 @@ #include "arrow/c/abi.h" #include "arrow/c/bridge.h" #include "fmt/format.h" +#include "paimon/common/reader/index_score_batch_utils.h" #include "paimon/common/reader/reader_utils.h" #include "paimon/common/table/special_fields.h" -#include "paimon/common/utils/arrow/mem_utils.h" #include "paimon/common/utils/arrow/status_utils.h" #include "paimon/common/utils/checked_cast.h" #include "paimon/status.h" @@ -72,77 +72,33 @@ Result CompleteIndexScoreFileBatchReader::Next return batch_with_bitmap; } - auto& [batch, bitmap] = batch_with_bitmap; - auto& [c_array, c_schema] = batch; - PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr arrow_array, - arrow::ImportArray(c_array.get(), c_schema.get())); - if (!arrow_array || arrow_array->type_id() != arrow::Type::STRUCT) { - return Status::Invalid( - "cannot cast array to StructArray in CompleteIndexScoreFileBatchReader"); - } - auto struct_array = checked_pointer_cast(arrow_array); - auto struct_type = struct_array->struct_type(); - arrow::ArrayVector array_vec = struct_array->fields(); + const size_t score_count = batch_with_bitmap.second.Cardinality(); for (const auto& [field_name, scores] : scores_) { - PAIMON_ASSIGN_OR_RAISE(int32_t field_index, GetScoreFieldIndex(struct_type, field_name)); - PAIMON_ASSIGN_OR_RAISE( - array_vec[field_index], - PrepareScoreArray(field_name, scores, struct_array->length(), bitmap)); - } - PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr array_with_score, - arrow::StructArray::Make(array_vec, struct_type->fields())); - PAIMON_RETURN_NOT_OK_FROM_ARROW( - arrow::ExportArray(*array_with_score, c_array.get(), c_schema.get())); - PAIMON_RETURN_NOT_OK(AddArrowArrayLifetime(c_array.get(), c_schema.get(), arrow_pool_)); - score_cursor_ += bitmap.Cardinality(); - return batch_with_bitmap; -} - -Result CompleteIndexScoreFileBatchReader::GetScoreFieldIndex( - const arrow::StructType* struct_type, const std::string& field_name) { - auto iter = index_score_field_indices_.find(field_name); - if (iter != index_score_field_indices_.end()) { - return iter->second; - } - const auto field_indices = struct_type->GetAllFieldIndices(field_name); - if (field_indices.empty()) { - return Status::Invalid(fmt::format("Missing index score field '{}'", field_name)); - } - if (field_indices.size() != 1) { - return Status::Invalid(fmt::format("Duplicate index score field '{}'", field_name)); - } - int32_t field_index = field_indices[0]; - if (struct_type->field(field_index)->type()->id() != arrow::Type::FLOAT) { - return Status::Invalid( - fmt::format("Index score field '{}' must have float32 type", field_name)); + if (score_cursor_ > scores.size() || score_count > scores.size() - score_cursor_) { + ReaderUtils::ReleaseReadBatch(std::move(batch_with_bitmap.first)); + return Status::Invalid( + fmt::format("Index score count {} for field '{}' is smaller than rows read {}", + scores.size(), field_name, score_cursor_ + score_count)); + } } - index_score_field_indices_.emplace(field_name, field_index); - return field_index; -} - -Result> CompleteIndexScoreFileBatchReader::PrepareScoreArray( - const std::string& field_name, const std::vector& scores, int64_t row_count, - const RoaringBitmap32& bitmap) const { - size_t score_count = bitmap.Cardinality(); - if (score_cursor_ > scores.size() || score_count > scores.size() - score_cursor_) { + auto& [c_array, c_schema] = batch_with_bitmap.first; + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr imported, + arrow::ImportArray(c_array.get(), c_schema.get())); + if (!imported || imported->type_id() != arrow::Type::STRUCT) { return Status::Invalid( - fmt::format("Index score count {} for field '{}' is smaller than rows read {}", - scores.size(), field_name, score_cursor_ + score_count)); - } - arrow::FloatBuilder builder(arrow_pool_.get()); - PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.Reserve(row_count)); - bool all_not_null = (row_count == bitmap.Cardinality()); - size_t cursor = score_cursor_; - for (int64_t i = 0; i < row_count; i++) { - if (all_not_null || bitmap.Contains(i)) { - PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.Append(scores[cursor++])); - } else { - PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.AppendNull()); - } + "cannot cast array to StructArray in CompleteIndexScoreFileBatchReader"); } - std::shared_ptr score_array; - PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.Finish(&score_array)); - return score_array; + auto data = checked_pointer_cast(imported); + PAIMON_ASSIGN_OR_RAISE( + BatchReader::ReadBatchWithBitmap completed, + IndexScoreBatchUtils::CompleteScores( + std::move(batch_with_bitmap), data, arrow::schema(data->struct_type()->fields()), + scores_, + [this](const std::string&, const std::vector& scores, int64_t, + size_t selected) -> Result { return scores[score_cursor_ + selected]; }, + arrow_pool_)); + score_cursor_ += score_count; + return completed; } Result> CompleteIndexScoreFileBatchReader::GetFileSchema() const { @@ -153,7 +109,6 @@ Status CompleteIndexScoreFileBatchReader::SetReadSchema( ::ArrowSchema* read_schema, const std::shared_ptr& predicate, const std::optional& selection_bitmap) { score_cursor_ = 0; - index_score_field_indices_.clear(); return reader_->SetReadSchema(read_schema, predicate, selection_bitmap); } diff --git a/src/paimon/common/reader/complete_index_score_file_batch_reader.h b/src/paimon/common/reader/complete_index_score_file_batch_reader.h index 8269ca306..e0a44b5ff 100644 --- a/src/paimon/common/reader/complete_index_score_file_batch_reader.h +++ b/src/paimon/common/reader/complete_index_score_file_batch_reader.h @@ -56,16 +56,7 @@ class CompleteIndexScoreFileBatchReader : public FileBatchReader { std::shared_ptr GetReaderMetrics() const override; private: - Result GetScoreFieldIndex(const arrow::StructType* struct_type, - const std::string& field_name); - - Result> PrepareScoreArray(const std::string& field_name, - const std::vector& scores, - int64_t row_count, - const RoaringBitmap32& bitmap) const; - size_t score_cursor_ = 0; - std::map index_score_field_indices_; std::shared_ptr arrow_pool_; std::unique_ptr reader_; std::map> scores_; diff --git a/src/paimon/common/reader/index_score_batch_utils.cpp b/src/paimon/common/reader/index_score_batch_utils.cpp new file mode 100644 index 000000000..a72c4beeb --- /dev/null +++ b/src/paimon/common/reader/index_score_batch_utils.cpp @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/common/reader/index_score_batch_utils.h" + +#include + +#include "arrow/api.h" +#include "arrow/c/bridge.h" +#include "paimon/common/utils/arrow/mem_utils.h" +#include "paimon/common/utils/arrow/status_utils.h" + +namespace paimon { + +Result IndexScoreBatchUtils::CompleteScoresImpl( + BatchReader::ReadBatchWithBitmap batch, const std::shared_ptr& data, + const std::shared_ptr& output_schema, const ScoreLookups& lookups, + const std::shared_ptr& arrow_pool) { + for (const auto& entry : lookups) { + const auto& field_name = entry.first; + const auto indices = output_schema->GetAllFieldIndices(field_name); + if (indices.empty()) { + return Status::Invalid("Missing index score field '", field_name, "'"); + } + if (indices.size() != 1) { + return Status::Invalid("Duplicate index score field '", field_name, "'"); + } + if (output_schema->field(indices[0])->type()->id() != arrow::Type::FLOAT) { + return Status::Invalid("Index score field '", field_name, "' must have float32 type"); + } + } + arrow::ArrayVector arrays; + for (int32_t position = 0; position < output_schema->num_fields(); ++position) { + const auto& field = output_schema->field(position); + auto found = lookups.find(field->name()); + if (found == lookups.end()) { + auto array = position < data->num_fields() && + data->struct_type()->field(position)->name() == field->name() + ? data->field(position) + : data->GetFieldByName(field->name()); + if (!array) { + return Status::Invalid("Missing output field: ", field->name()); + } + arrays.push_back(std::move(array)); + continue; + } + arrow::FloatBuilder builder(arrow_pool.get()); + PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.Reserve(data->length())); + bool all_not_null = (data->length() == batch.second.Cardinality()); + size_t selected = 0; + for (int64_t row = 0; row < data->length(); ++row) { + if (!all_not_null && !batch.second.Contains(row)) { + PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.AppendNull()); + continue; + } + PAIMON_ASSIGN_OR_RAISE(float score, found->second(row, selected++)); + PAIMON_RETURN_NOT_OK_FROM_ARROW(builder.Append(score)); + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr array, builder.Finish()); + arrays.push_back(std::move(array)); + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr output, + arrow::StructArray::Make(arrays, output_schema->fields())); + PAIMON_RETURN_NOT_OK_FROM_ARROW( + arrow::ExportArray(*output, batch.first.first.get(), batch.first.second.get())); + PAIMON_RETURN_NOT_OK( + AddArrowArrayLifetime(batch.first.first.get(), batch.first.second.get(), arrow_pool)); + return batch; +} + +} // namespace paimon diff --git a/src/paimon/common/reader/index_score_batch_utils.h b/src/paimon/common/reader/index_score_batch_utils.h new file mode 100644 index 000000000..370a9291e --- /dev/null +++ b/src/paimon/common/reader/index_score_batch_utils.h @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include +#include +#include +#include +#include + +#include "arrow/c/abi.h" +#include "paimon/reader/batch_reader.h" + +namespace arrow { +class MemoryPool; +class Schema; +class StructArray; +} // namespace arrow + +namespace paimon { + +/// Completes score columns and restores projection; the caller supplies row-to-score lookup. +class IndexScoreBatchUtils { + public: + IndexScoreBatchUtils() = delete; + ~IndexScoreBatchUtils() = delete; + + template + static Result CompleteScores( + BatchReader::ReadBatchWithBitmap batch, const std::shared_ptr& data, + const std::shared_ptr& output_schema, const Scores& scores, + ScoreLookup lookup, const std::shared_ptr& arrow_pool) { + ScoreLookups lookups; + for (const auto& entry : scores) { + lookups.emplace(entry.first, + [&lookup, field_name = entry.first, values = &entry.second]( + int64_t row, size_t selected) -> Result { + return lookup(field_name, *values, row, selected); + }); + } + return CompleteScoresImpl(std::move(batch), data, output_schema, lookups, arrow_pool); + } + + private: + // Maps score field names to callbacks invoked only for selected rows. The callback receives + // the zero-based row index in the original batch and the zero-based ordinal among selected + // rows in that batch, and returns the row's score or an error. + using ScoreLookups = + std::map(int64_t row, size_t selected)>>; + + static Result CompleteScoresImpl( + BatchReader::ReadBatchWithBitmap batch, const std::shared_ptr& data, + const std::shared_ptr& output_schema, const ScoreLookups& lookups, + const std::shared_ptr& arrow_pool); +}; + +} // namespace paimon diff --git a/src/paimon/core/io/file_index_evaluator.cpp b/src/paimon/core/io/file_index_evaluator.cpp index 425912b11..3912313c9 100644 --- a/src/paimon/core/io/file_index_evaluator.cpp +++ b/src/paimon/core/io/file_index_evaluator.cpp @@ -19,7 +19,6 @@ #include "paimon/core/io/file_index_evaluator.h" #include -#include #include #include #include @@ -32,6 +31,7 @@ #include "fmt/format.h" #include "fmt/ranges.h" #include "paimon/cache/cache.h" +#include "paimon/common/index/index_reader_utils.h" #include "paimon/common/predicate/full_text_match_predicate.h" #include "paimon/common/utils/arrow/status_utils.h" #include "paimon/common/utils/string_utils.h" @@ -171,22 +171,12 @@ Result> FileIndexEvaluator::GetSearchReader( } PAIMON_ASSIGN_OR_RAISE(std::vector> readers, GetReaders(field_name)); - std::shared_ptr selected; - for (const auto& reader : readers) { - if (!reader->SupportsSearch(search_type)) { - continue; - } - if (selected) { - return Status::Invalid(fmt::format( - "Multiple File Index readers support {} for field '{}' in data file '{}'", - SearchTypeToString(search_type), field_name, file_meta_->file_name)); - } - selected = reader; - } - if (!selected) { - return unsupported(); - } - return selected; + return IndexReaderUtils::GetSearchReader( + readers, search_type, + fmt::format("Multiple File Index readers support {} for field '{}' in data file '{}'", + SearchTypeToString(search_type), field_name, file_meta_->file_name), + fmt::format("No File Index reader supports {} for field '{}' in data file '{}'", + SearchTypeToString(search_type), field_name, file_meta_->file_name)); } Result> FileIndexEvaluator::EvaluateLeaf( @@ -219,23 +209,7 @@ Result> FileIndexEvaluator::EvaluateLeaf( Result> FileIndexEvaluator::EstimateSelectivity( const std::shared_ptr& predicate) { - if (predicate->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH) { - PAIMON_ASSIGN_OR_RAISE( - std::shared_ptr reader, - GetSearchReader(predicate->FieldName(), FileIndexReader::SearchType::FULL_TEXT_MATCH)); - return reader->EstimateSelectivity(predicate); - } - PAIMON_ASSIGN_OR_RAISE(std::vector> readers, - GetReaders(predicate->FieldName())); - std::optional estimate; - for (const auto& reader : readers) { - auto value = reader->EstimateSelectivity(predicate); - if (value && std::isfinite(*value) && *value >= 0.0 && *value <= 1.0 && - (!estimate || *value < *estimate)) { - estimate = value; - } - } - return estimate; + return IndexReaderUtils::EstimateSelectivity(predicate, this); } Result> FileIndexEvaluator::SearchVector( diff --git a/src/paimon/core/io/file_index_evaluator.h b/src/paimon/core/io/file_index_evaluator.h index 0f9e78882..4b8b71635 100644 --- a/src/paimon/core/io/file_index_evaluator.h +++ b/src/paimon/core/io/file_index_evaluator.h @@ -70,6 +70,8 @@ class FileIndexEvaluator { Result> GetSearchReader( const std::string& field_name, FileIndexReader::SearchType search_type); + Result>> GetReaders(const std::string& field_name); + Result> EvaluateLeaf( const std::shared_ptr& predicate, bool needs_scores); @@ -88,8 +90,6 @@ class FileIndexEvaluator { std::unique_ptr format_reader, std::shared_ptr cache, std::string uri); - Result>> GetReaders(const std::string& field_name); - static Result> ExtractIndexInputStream( const std::shared_ptr& data_file_path_factory, const std::shared_ptr& file_meta, diff --git a/src/paimon/core/operation/abstract_split_read.cpp b/src/paimon/core/operation/abstract_split_read.cpp index 9b3c7bfb6..9d11df45a 100644 --- a/src/paimon/core/operation/abstract_split_read.cpp +++ b/src/paimon/core/operation/abstract_split_read.cpp @@ -171,6 +171,14 @@ AbstractSplitRead::CreateRawFileReadersWithMetaImpl( FieldMappingBuilder::Create( read_schema, context_->GetPartitionKeys(), context_->EnablePreciseFileIndexEvaluate() ? nullptr : predicate)); + if (context_->EnablePreciseFileIndexEvaluate()) { + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr reader, + CreatePreciseFileIndexReader(partition, file, read_schema, predicate, + field_mapping_builder.get(), dv_factory, row_ranges, + data_file_path_factory, extra_format_options)); + return RawFileReaderWithMeta{file, std::move(reader)}; + } return CreateRawFileReader(partition, file, field_mapping_builder.get(), dv_factory, row_ranges, data_file_path_factory, extra_format_options, attach_blob_context); @@ -323,13 +331,6 @@ Result AbstractSplitRead::CreateRawFil const std::shared_ptr& data_file_path_factory, const std::map& extra_format_options, bool attach_blob_context) const { - if (context_->EnablePreciseFileIndexEvaluate()) { - PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, - CreatePreciseFileIndexReader( - partition, file, field_mapping_builder, std::move(dv_factory), - row_ranges, data_file_path_factory, extra_format_options)); - return RawFileReaderWithMeta{file, std::move(reader)}; - } auto data_file_path = data_file_path_factory->ToPath(file); PAIMON_ASSIGN_OR_RAISE(std::string data_file_identifier, file->FileFormat()); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader_builder, @@ -354,6 +355,7 @@ Result AbstractSplitRead::CreateRawFil Result> AbstractSplitRead::CreatePreciseFileIndexReader( const BinaryRow& partition, const std::shared_ptr& file, + const std::shared_ptr& read_schema, const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory, const std::optional>& row_ranges, const std::shared_ptr& path_factory, diff --git a/src/paimon/core/operation/abstract_split_read.h b/src/paimon/core/operation/abstract_split_read.h index 8a1464c0f..70ea02c0c 100644 --- a/src/paimon/core/operation/abstract_split_read.h +++ b/src/paimon/core/operation/abstract_split_read.h @@ -88,6 +88,8 @@ class AbstractSplitRead : public SplitRead { protected: virtual Result> CreatePreciseFileIndexReader( const BinaryRow& partition, const std::shared_ptr& file, + const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory, const std::optional>& row_ranges, const std::shared_ptr& path_factory, @@ -105,8 +107,8 @@ class AbstractSplitRead : public SplitRead { const std::shared_ptr& memory_pool, const std::shared_ptr& executor); - /// Creates raw readers using each file's predicate for pushdown. The provider is called once - /// per file and may be called concurrently. + /// Creates raw readers using each file's predicate for pushdown or precise evaluation before + /// root search. The provider is called once per file and may be called concurrently. Result>> CreateRawFileReadersWithPredicateProvider( const BinaryRow& partition, const std::vector>& data_files, const std::shared_ptr& read_schema, PredicateProvider predicate_provider, diff --git a/src/paimon/core/operation/internal_read_context.cpp b/src/paimon/core/operation/internal_read_context.cpp index fa86967f5..be0f7a917 100644 --- a/src/paimon/core/operation/internal_read_context.cpp +++ b/src/paimon/core/operation/internal_read_context.cpp @@ -297,9 +297,13 @@ Result> InternalReadContext::CreateFromCore return Status::NotImplemented( "Precise file-index evaluation on primary-key tables requires deletion vectors"); } - if (core_options.DataEvolutionEnabled() || context->GetRealtimeContext()) { + if (core_options.DataEvolutionEnabled()) { + return Status::NotImplemented( + "Precise file-index evaluation does not support data-evolution"); + } + if (context->GetRealtimeContext() && !table_schema->PrimaryKeys().empty()) { return Status::NotImplemented( - "Precise file-index evaluation does not support data-evolution or real-time reads"); + "Precise real-time evaluation does not support primary-key tables"); } } if (context->HasVectorOrFullTextSearch()) { diff --git a/src/paimon/core/operation/key_value_file_store_write_test.cpp b/src/paimon/core/operation/key_value_file_store_write_test.cpp index 7602a2763..b76203a9a 100644 --- a/src/paimon/core/operation/key_value_file_store_write_test.cpp +++ b/src/paimon/core/operation/key_value_file_store_write_test.cpp @@ -273,7 +273,11 @@ class KeyValueFileStoreWriteTest : public ::testing::Test { RealtimeQueryContext query_context{ c_schema.get(), /*predicate=*/nullptr, - views[0].read_view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + views[0].read_view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; PAIMON_ASSIGN_OR_RAISE( std::vector> readers, views[0].store->CreateQueryReaders(views[0].read_view, query_context)); diff --git a/src/paimon/core/operation/precise_file_index_read.cpp b/src/paimon/core/operation/precise_file_index_read.cpp index c538c530a..e6dee96a2 100644 --- a/src/paimon/core/operation/precise_file_index_read.cpp +++ b/src/paimon/core/operation/precise_file_index_read.cpp @@ -47,8 +47,6 @@ #include "paimon/core/schema/schema_manager.h" #include "paimon/core/schema/table_schema.h" #include "paimon/core/utils/field_mapping.h" -#include "paimon/file_index/bitmap_index_result.h" -#include "paimon/file_index/file_index_result.h" #include "paimon/file_index/scored_file_index_result.h" #include "paimon/format/reader_builder.h" #include "paimon/predicate/compound_predicate.h" @@ -79,48 +77,32 @@ Status PreparePredicate( const std::map>& data_by_id, const std::vector& partition_keys, FileIndexEvaluator* indexes, std::map, std::shared_ptr>* mapped_leaves) { - if (!predicate) { - return Status::OK(); - } - if (auto compound = std::dynamic_pointer_cast(predicate)) { - const Function::Type type = compound->GetFunction().GetType(); - if (type != Function::Type::AND && type != Function::Type::OR) { - return Status::Invalid( - fmt::format("unsupported compound file predicate {}", predicate->ToString())); - } - for (const auto& child : compound->Children()) { - PAIMON_RETURN_NOT_OK(PreparePredicate(child, read_by_name, data_by_id, partition_keys, - indexes, mapped_leaves)); - } - return Status::OK(); - } - auto leaf = std::dynamic_pointer_cast(predicate); - if (!leaf) { - return Status::Invalid("File predicate must contain leaf or compound predicates"); - } - bool full_text = leaf->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH; - if (!full_text && ObjectUtils::Contains(partition_keys, leaf->FieldName())) { - mapped_leaves->emplace(leaf, nullptr); - return Status::OK(); - } - std::optional> mapped; - PAIMON_ASSIGN_OR_RAISE(mapped, FieldMappingBuilder::ReconstructPredicateWithDataFields( - leaf, read_by_name, data_by_id)); - std::shared_ptr mapped_leaf; - if (mapped) { - mapped_leaf = std::dynamic_pointer_cast(*mapped); - if (!mapped_leaf) { - return Status::Invalid("Mapped file predicate must be a leaf"); - } - if (full_text) { - PAIMON_ASSIGN_OR_RAISE( - [[maybe_unused]] std::shared_ptr reader, - indexes->GetSearchReader(mapped_leaf->FieldName(), - FileIndexReader::SearchType::FULL_TEXT_MATCH)); - } - } - mapped_leaves->emplace(leaf, std::move(mapped_leaf)); - return Status::OK(); + return PreciseIndexEvaluator::Prepare( + predicate, [&](const std::shared_ptr& leaf) -> Status { + bool full_text = leaf->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH; + if (!full_text && ObjectUtils::Contains(partition_keys, leaf->FieldName())) { + mapped_leaves->emplace(leaf, nullptr); + return Status::OK(); + } + std::optional> mapped; + PAIMON_ASSIGN_OR_RAISE(mapped, FieldMappingBuilder::ReconstructPredicateWithDataFields( + leaf, read_by_name, data_by_id)); + std::shared_ptr mapped_leaf; + if (mapped) { + mapped_leaf = std::dynamic_pointer_cast(*mapped); + if (!mapped_leaf) { + return Status::Invalid("Mapped file predicate must be a leaf"); + } + if (full_text) { + PAIMON_ASSIGN_OR_RAISE( + [[maybe_unused]] std::shared_ptr reader, + indexes->GetSearchReader(mapped_leaf->FieldName(), + FileIndexReader::SearchType::FULL_TEXT_MATCH)); + } + } + mapped_leaves->emplace(leaf, std::move(mapped_leaf)); + return Status::OK(); + }); } Status PrepareSearch(const std::shared_ptr& context, @@ -155,7 +137,8 @@ Status PrepareSearch(const std::shared_ptr& context, Result> PreciseFileIndexRead::Create( const RawFileSplitRead& split_read, const BinaryRow& partition, - const std::shared_ptr& file, const FieldMappingBuilder* field_mapping_builder, + const std::shared_ptr& file, const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, const std::shared_ptr& path_factory, const std::map& extra_format_options) { std::shared_ptr file_schema = split_read.context_->GetTableSchema(); @@ -170,9 +153,8 @@ Result> PreciseFileIndexRead::Create( } PAIMON_ASSIGN_OR_RAISE(std::vector data_fields, DataField::ConvertArrowSchemaToDataFields(data_schema)); - PAIMON_ASSIGN_OR_RAISE( - std::vector read_fields, - DataField::ConvertArrowSchemaToDataFields(split_read.context_->GetReadSchema())); + PAIMON_ASSIGN_OR_RAISE(std::vector read_fields, + DataField::ConvertArrowSchemaToDataFields(read_schema)); auto field_names = FieldMappingBuilder::CreateFieldNameMapping( split_read.context_->GetTableSchema()->Fields(), data_fields); std::map index_options; @@ -196,21 +178,22 @@ Result> PreciseFileIndexRead::Create( } std::map, std::shared_ptr> mapped_leaves; // Precheck the entire tree before an empty candidate set can short-circuit it. - PAIMON_RETURN_NOT_OK(PreparePredicate(split_read.context_->GetPredicate(), read_by_name, - data_by_id, split_read.context_->GetPartitionKeys(), - indexes.get(), &mapped_leaves)); + PAIMON_RETURN_NOT_OK(PreparePredicate(predicate, read_by_name, data_by_id, + split_read.context_->GetPartitionKeys(), indexes.get(), + &mapped_leaves)); std::shared_ptr vector_search; std::shared_ptr full_text_search; PAIMON_RETURN_NOT_OK(PrepareSearch(split_read.context_, field_names, file->file_name, indexes.get(), &vector_search, &full_text_search)); return std::unique_ptr(new PreciseFileIndexRead( - split_read, partition, file, field_mapping_builder, path_factory, extra_format_options, - std::move(indexes), mapped_leaves, vector_search, full_text_search)); + split_read, partition, file, read_schema, predicate, field_mapping_builder, path_factory, + extra_format_options, std::move(indexes), mapped_leaves, vector_search, full_text_search)); } PreciseFileIndexRead::PreciseFileIndexRead( const RawFileSplitRead& split_read, const BinaryRow& partition, - const std::shared_ptr& file, const FieldMappingBuilder* field_mapping_builder, + const std::shared_ptr& file, const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, const std::shared_ptr& path_factory, const std::map& extra_format_options, std::unique_ptr indexes, @@ -220,6 +203,8 @@ PreciseFileIndexRead::PreciseFileIndexRead( : split_read_(split_read), partition_(partition), file_(file), + read_schema_(read_schema), + predicate_(predicate), field_mapping_builder_(field_mapping_builder), path_factory_(path_factory), extra_format_options_(extra_format_options), @@ -252,110 +237,6 @@ Result PreciseFileIndexRead::CreateCandidates( return candidates; } -Result PreciseFileIndexRead::Evaluate( - const std::shared_ptr& predicate, const RoaringBitmap32& candidates) const { - if (!predicate || candidates.IsEmpty()) { - return Evaluation{candidates, {}}; - } - if (auto leaf = std::dynamic_pointer_cast(predicate)) { - return EvaluateLeaf(leaf, candidates); - } - if (auto compound = std::dynamic_pointer_cast(predicate)) { - return EvaluateCompound(compound, candidates); - } - return Status::Invalid(fmt::format("unsupported file predicate {}", predicate->ToString())); -} - -Result PreciseFileIndexRead::EvaluateCompound( - const std::shared_ptr& predicate, const RoaringBitmap32& candidates) const { - const Function::Type type = predicate->GetFunction().GetType(); - if (type == Function::Type::OR) { - Evaluation result; - for (const auto& child : predicate->Children()) { - // Every branch must see the same input, including rows matched by earlier branches. - PAIMON_ASSIGN_OR_RAISE(Evaluation child_result, Evaluate(child, candidates)); - result.rows |= child_result.rows; - result.scores.insert(child_result.scores.begin(), child_result.scores.end()); - } - return result; - } - if (type != Function::Type::AND) { - return Status::Invalid( - fmt::format("unsupported compound file predicate {}", predicate->ToString())); - } - - struct Child { - std::shared_ptr predicate; - std::optional selectivity; - }; - std::vector children; - children.reserve(predicate->Children().size()); - for (const auto& child : predicate->Children()) { - PAIMON_ASSIGN_OR_RAISE(std::optional selectivity, EstimateSelectivity(child)); - children.push_back({child, selectivity}); - } - std::stable_sort(children.begin(), children.end(), [](const Child& lhs, const Child& rhs) { - if (!lhs.selectivity) { - return false; - } - return !rhs.selectivity || *lhs.selectivity < *rhs.selectivity; - }); - - Evaluation result{candidates, {}}; - for (const auto& child : children) { - PAIMON_ASSIGN_OR_RAISE(Evaluation child_result, Evaluate(child.predicate, result.rows)); - result.rows = std::move(child_result.rows); - result.scores.insert(child_result.scores.begin(), child_result.scores.end()); - if (result.rows.IsEmpty()) { - break; - } - } - return result; -} - -Result> PreciseFileIndexRead::EstimateSelectivity( - const std::shared_ptr& predicate) const { - if (!predicate) { - return std::optional(); - } - if (auto leaf = std::dynamic_pointer_cast(predicate)) { - PAIMON_ASSIGN_OR_RAISE(std::optional estimate, EstimateLeafSelectivity(leaf)); - if (estimate && (!std::isfinite(*estimate) || *estimate < 0.0 || *estimate > 1.0)) { - return std::optional(); - } - return estimate; - } - auto compound = std::dynamic_pointer_cast(predicate); - if (!compound) { - return Status::Invalid(fmt::format("unsupported file predicate {}", predicate->ToString())); - } - const Function::Type type = compound->GetFunction().GetType(); - if (type != Function::Type::AND && type != Function::Type::OR) { - return Status::Invalid( - fmt::format("unsupported compound file predicate {}", predicate->ToString())); - } - std::optional estimate = - type == Function::Type::AND ? std::nullopt : std::make_optional(0.0); - // These are scheduling hints. Continue visiting all children to propagate errors. - for (const auto& child : compound->Children()) { - PAIMON_ASSIGN_OR_RAISE(std::optional child_estimate, EstimateSelectivity(child)); - if (type == Function::Type::AND) { - if (child_estimate) { - if (estimate.has_value()) { - estimate = std::min(*estimate, *child_estimate); - } else { - estimate = *child_estimate; - } - } - } else if (estimate && child_estimate) { - estimate = std::min(1.0, *estimate + *child_estimate); - } else { - estimate = std::nullopt; - } - } - return estimate; -} - Result> PreciseFileIndexRead::EstimateLeafSelectivity( const std::shared_ptr& predicate) const { auto iter = mapped_leaves_.find(predicate); @@ -370,57 +251,18 @@ Result> PreciseFileIndexRead::EstimateLeafSelectivity( Result PreciseFileIndexRead::EvaluateLeaf( const std::shared_ptr& predicate, const RoaringBitmap32& candidates) const { - bool is_full_text = predicate->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH; - bool needs_scores = - is_full_text && - split_read_.context_->NeedsScores( - {InternalReadContext::IndexScoreSource::Type::FULL_TEXT_MATCH, predicate->FieldName()}); - std::shared_ptr index_result = FileIndexResult::Remain(); auto iter = mapped_leaves_.find(predicate); if (iter == mapped_leaves_.end()) { return Status::Invalid("File predicate mapping was not prepared"); } - if (iter->second) { - // TODO(lisizhuo.lsz): Pass candidates through FileIndexEvaluator::EvaluateLeaf to index - // readers so they can skip rows excluded by earlier predicates, row ranges, or deletion - // vectors. - PAIMON_ASSIGN_OR_RAISE(index_result, indexes_->EvaluateLeaf(iter->second, needs_scores)); - } - if (!index_result) { - return Status::Invalid("file predicate index returned a null result"); - } - Evaluation result{candidates, {}}; - if (needs_scores) { - auto scored = std::dynamic_pointer_cast(index_result); - if (!scored) { - return Status::Invalid( - fmt::format("full-text index for field '{}' did not return requested scores", - predicate->FieldName())); - } - result.rows &= scored->GetRowPositions(); - result.scores.emplace(predicate->FieldName(), std::move(scored)); - return result; - } - - if (auto bitmap = std::dynamic_pointer_cast(index_result)) { - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap32* rows, bitmap->GetBitmap()); - result.rows &= *rows; - } else if (is_full_text) { - return Status::Invalid(fmt::format("full-text index for field '{}' did not return a bitmap", - predicate->FieldName())); - } else if (std::dynamic_pointer_cast(index_result)) { - return Evaluation{}; - } else if (!std::dynamic_pointer_cast(index_result)) { - return Status::Invalid( - fmt::format("invalid index result for predicate {}", predicate->ToString())); - } - // TODO(lisizhuo.lsz): Whether to ScanScalar depends on whether the index returns exact results. - if (!is_full_text && !result.rows.IsEmpty()) { - // Scalar indexes may contain false positives, even if they return a bitmap. - PAIMON_ASSIGN_OR_RAISE(RoaringBitmap32 exact_rows, ScanScalar(predicate, result.rows)); - result.rows &= exact_rows; - } - return result; + bool needs_scores = split_read_.context_->NeedsScores( + {InternalReadContext::IndexScoreSource::Type::FULL_TEXT_MATCH, predicate->FieldName()}); + return Evaluator::EvaluateLeaf( + predicate, iter->second, candidates, needs_scores, + [this](const std::shared_ptr& leaf, const RoaringBitmap32& rows) { + return ScanScalar(leaf, rows); + }, + indexes_.get()); } Result> PreciseFileIndexRead::CreateSelectedFileReader( @@ -435,7 +277,7 @@ Result> PreciseFileIndexRead::CreateSelectedFil Result PreciseFileIndexRead::ScanScalar( const std::shared_ptr& predicate, const RoaringBitmap32& input) const { - auto field = split_read_.context_->GetReadSchema()->GetFieldByName(predicate->FieldName()); + auto field = read_schema_->GetFieldByName(predicate->FieldName()); if (!field) { return Status::Invalid(fmt::format( "Scalar predicate field '{}' is missing from the read schema", predicate->FieldName())); @@ -479,79 +321,28 @@ Result PreciseFileIndexRead::ScanScalar( } Status PreciseFileIndexRead::ApplyRootSearch(Evaluation* evaluated) const { - std::shared_ptr root_scores; - std::string field_name; - if (vector_search_) { - auto eligible = std::make_shared(evaluated->rows); - auto user_filter = vector_search_->pre_filter; - auto search = vector_search_->ReplacePreFilter([eligible, user_filter](int64_t row) { - return row >= 0 && row < RoaringBitmap32::MAX_VALUE && - eligible->Contains(static_cast(row)) && - (!user_filter || user_filter(row)); - }); - PAIMON_ASSIGN_OR_RAISE(root_scores, indexes_->SearchVector(search)); - field_name = split_read_.context_->GetVectorSearch()->field_name; - } else if (full_text_search_) { - RoaringBitmap64 eligible(evaluated->rows); - if (full_text_search_->include_row_ids) { - eligible &= *full_text_search_->include_row_ids; - } - auto search = std::make_shared(*full_text_search_); - search->include_row_ids = std::move(eligible); - PAIMON_ASSIGN_OR_RAISE(root_scores, indexes_->SearchFullText(search)); - field_name = split_read_.context_->GetFullTextSearch()->field_name; - } else { - return Status::OK(); - } - evaluated->rows &= root_scores->GetRowPositions(); // Score bindings use the original field name, not the historical file schema's name. - evaluated->scores.emplace(field_name, std::move(root_scores)); - return Status::OK(); -} - -Result> PreciseFileIndexRead::AlignScores( - const std::shared_ptr& source, const RoaringBitmap32& rows) { - if (!source) { - return Status::Invalid("Projected search source did not return scores"); - } - std::vector aligned; - aligned.reserve(rows.Cardinality()); - size_t score = 0; - for (auto iter = source->GetRowPositions().Begin(); iter != source->GetRowPositions().End(); - ++iter, ++score) { - if (rows.Contains(*iter)) { - aligned.push_back(source->GetScores()[score]); - } - } - if (aligned.size() != static_cast(rows.Cardinality())) { - return Status::Invalid("Projected scores do not cover all selected file rows"); - } - return aligned; -} - -Result>> PreciseFileIndexRead::CreateOutputScores( - const Evaluation& evaluated) const { - std::map> output_scores; - for (const auto& [position, source] : split_read_.context_->GetIndexScoreBindings()) { - auto found = evaluated.scores.find(source.field_name); - if (found == evaluated.scores.end()) { - return Status::Invalid(fmt::format("Projected search field '{}' did not return scores", - source.field_name)); - } - PAIMON_ASSIGN_OR_RAISE(std::vector aligned, - AlignScores(found->second, evaluated.rows)); - output_scores.emplace(split_read_.context_->GetReadSchema()->field(position)->name(), - std::move(aligned)); + const auto& context = split_read_.context_; + std::string field; + if (context->GetVectorSearch()) { + field = context->GetVectorSearch()->field_name; + } else if (context->GetFullTextSearch()) { + field = context->GetFullTextSearch()->field_name; } - return output_scores; + return Evaluator::ApplyRootSearch(vector_search_, full_text_search_, field, + indexes_.get(), evaluated); } Result> PreciseFileIndexRead::CreateReader( const DeletionVector::Factory& dv_factory, const std::optional>& row_ranges) const { PAIMON_ASSIGN_OR_RAISE(RoaringBitmap32 candidates, CreateCandidates(dv_factory, row_ranges)); - PAIMON_ASSIGN_OR_RAISE(Evaluation evaluated, - Evaluate(split_read_.context_->GetPredicate(), candidates)); + Evaluator evaluator([this](const std::shared_ptr& leaf, + const RoaringBitmap32& rows) { return EvaluateLeaf(leaf, rows); }, + [this](const std::shared_ptr& leaf) { + return EstimateLeafSelectivity(leaf); + }); + PAIMON_ASSIGN_OR_RAISE(Evaluation evaluated, evaluator.Evaluate(predicate_, candidates)); if (evaluated.rows.IsEmpty()) { return std::unique_ptr(); } @@ -560,7 +351,10 @@ Result> PreciseFileIndexRead::CreateReader( return std::unique_ptr(); } std::map> output_scores; - PAIMON_ASSIGN_OR_RAISE(output_scores, CreateOutputScores(evaluated)); + PAIMON_ASSIGN_OR_RAISE( + output_scores, + Evaluator::CreateOutputScores(evaluated, split_read_.context_->GetIndexScoreBindings(), + split_read_.context_->GetReadSchema())); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, CreateSelectedFileReader(evaluated.rows, field_mapping_builder_)); if (!output_scores.empty()) { diff --git a/src/paimon/core/operation/precise_file_index_read.h b/src/paimon/core/operation/precise_file_index_read.h index 3c373620c..686c49b6e 100644 --- a/src/paimon/core/operation/precise_file_index_read.h +++ b/src/paimon/core/operation/precise_file_index_read.h @@ -27,6 +27,7 @@ #include #include "paimon/core/deletionvectors/deletion_vector.h" +#include "paimon/core/operation/precise_index_evaluator.h" #include "paimon/reader/file_batch_reader.h" #include "paimon/result.h" #include "paimon/status.h" @@ -50,9 +51,14 @@ struct VectorSearch; /// Evaluates a file predicate and root search before opening the selected output rows. class PreciseFileIndexRead { public: + /// Uses the file-specific predicate and schema supplied by the caller, including any fields + /// added for visibility filtering. The predicate is evaluated before root search. static Result> Create( const RawFileSplitRead& split_read, const BinaryRow& partition, - const std::shared_ptr& file, const FieldMappingBuilder* field_mapping_builder, + const std::shared_ptr& file, + const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, + const FieldMappingBuilder* field_mapping_builder, const std::shared_ptr& path_factory, const std::map& extra_format_options); @@ -62,14 +68,13 @@ class PreciseFileIndexRead { const std::optional>& row_ranges) const; private: - struct Evaluation { - RoaringBitmap32 rows; - // Sources retain physical row positions until all filtering and root TopK are complete. - std::map> scores; - }; + using Evaluator = PreciseIndexEvaluator; + using Evaluation = Evaluator::Evaluation; PreciseFileIndexRead(const RawFileSplitRead& split_read, const BinaryRow& partition, const std::shared_ptr& file, + const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, const std::shared_ptr& path_factory, const std::map& extra_format_options, @@ -82,14 +87,8 @@ class PreciseFileIndexRead { Result CreateCandidates( const DeletionVector::Factory& dv_factory, const std::optional>& row_ranges) const; - Result Evaluate(const std::shared_ptr& predicate, - const RoaringBitmap32& candidates) const; - Result EvaluateCompound(const std::shared_ptr& predicate, - const RoaringBitmap32& candidates) const; Result EvaluateLeaf(const std::shared_ptr& predicate, const RoaringBitmap32& candidates) const; - Result> EstimateSelectivity( - const std::shared_ptr& predicate) const; Result> EstimateLeafSelectivity( const std::shared_ptr& predicate) const; /// Opens a fresh mapped reader for selected physical rows without re-evaluating indexes, @@ -99,15 +98,13 @@ class PreciseFileIndexRead { Result ScanScalar(const std::shared_ptr& predicate, const RoaringBitmap32& candidates) const; Status ApplyRootSearch(Evaluation* evaluated) const; - Result>> CreateOutputScores( - const Evaluation& evaluated) const; - static Result> AlignScores( - const std::shared_ptr& source, const RoaringBitmap32& rows); private: const RawFileSplitRead& split_read_; const BinaryRow& partition_; std::shared_ptr file_; + std::shared_ptr read_schema_; + std::shared_ptr predicate_; const FieldMappingBuilder* field_mapping_builder_; std::shared_ptr path_factory_; std::map extra_format_options_; diff --git a/src/paimon/core/operation/precise_file_index_read_test.cpp b/src/paimon/core/operation/precise_file_index_read_test.cpp index 640179ef9..c8f1bfffe 100644 --- a/src/paimon/core/operation/precise_file_index_read_test.cpp +++ b/src/paimon/core/operation/precise_file_index_read_test.cpp @@ -32,6 +32,7 @@ #include "gtest/gtest.h" #include "paimon/common/data/binary_row.h" #include "paimon/common/file_index/bitmap/bitmap_file_index.h" +#include "paimon/common/index/index_reader_utils.h" #include "paimon/common/predicate/leaf_predicate_impl.h" #include "paimon/common/types/data_field.h" #include "paimon/core/deletionvectors/bitmap64_deletion_vector.h" @@ -184,7 +185,8 @@ class PreciseFileIndexReadTest : public ::testing::Test { const BinaryRow partition = BinaryRowGenerator::GenerateRow({10, 0}, pool_.get()); PAIMON_ASSIGN_OR_RAISE( std::unique_ptr precise_read, - PreciseFileIndexRead::Create(*split_read, partition, file, field_mapping_builder.get(), + PreciseFileIndexRead::Create(*split_read, partition, file, context->GetReadSchema(), + context->GetPredicate(), field_mapping_builder.get(), path_factory, {})); if (!deletion_vector) { deletion_vector = std::make_shared(deleted_rows); @@ -230,6 +232,40 @@ class PreciseFileIndexReadTest : public ::testing::Test { std::make_shared(); }; +TEST_F(PreciseFileIndexReadTest, FullTextMatchPushesDownCandidates) { + using MatchResult = IndexReaderUtils::FullTextResult; + auto reader = std::make_shared( + "f0", SearchIndexType::FULL_TEXT, + MockSearchFileIndex::ScoredRows{{2, 0.9f}, {0, 0.8f}, {1, 0.7f}}, statistics_); + auto match = std::make_shared("f0", "all", RoaringBitmap64::From({0, 1})); + auto candidates = RoaringBitmap32::From({0, 2}); + for (bool needs_scores : {false, true}) { + ASSERT_OK_AND_ASSIGN(MatchResult result, IndexReaderUtils::VisitFullTextMatch( + reader, match, candidates, needs_scores)); + // Inspect the adapter result before the evaluator's final intersection. + ASSERT_EQ(result.rows, RoaringBitmap32::From({0})); + if (needs_scores) { + ASSERT_NE(result.scores, nullptr); + ASSERT_EQ(result.scores->GetScores(), (std::vector{0.8f})); + } else { + ASSERT_EQ(result.scores, nullptr); + } + ASSERT_EQ(*match->include_row_ids, RoaringBitmap64::From({0, 1})); + } + auto unrestricted = std::make_shared("f0", "all", std::nullopt); + ASSERT_OK_AND_ASSIGN(MatchResult result, IndexReaderUtils::VisitFullTextMatch( + reader, unrestricted, candidates, true)); + ASSERT_EQ(result.rows, candidates); + ASSERT_NE(result.scores, nullptr); + ASSERT_EQ(result.scores->GetScores(), (std::vector{0.8f, 0.9f})); + ASSERT_FALSE(unrestricted->include_row_ids.has_value()); + ASSERT_OK_AND_ASSIGN(MatchResult empty, IndexReaderUtils::VisitFullTextMatch( + reader, match, RoaringBitmap32(), true)); + ASSERT_TRUE(empty.rows.IsEmpty()); + ASSERT_NE(empty.scores, nullptr); + ASSERT_TRUE(empty.scores->GetScores().empty()); +} + TEST_F(PreciseFileIndexReadTest, TestMixedPredicateNestedAndOrWithPartitionConstants) { const std::string first_query = R"({"match":{"query":"first"}})"; const std::string second_query = R"({"match":{"query":"second"}})"; @@ -688,6 +724,81 @@ TEST_F(PreciseFileIndexReadTest, TestMixedPredicateFullTextAndVectorScoresAfterF } } +TEST_F(PreciseFileIndexReadTest, TestPredicateProviderFiltersExtraFieldsBeforeRootSearch) { + const std::string query = R"({"match":{"query":"ranked"}})"; + for (bool vector_search : {false, true}) { + SCOPED_TRACE(vector_search); + const std::string score_field = vector_search ? "_f3_INDEX_SCORE" : "_f0_INDEX_SCORE"; + ReadContextBuilder builder(SearchTablePath()); + builder.EnablePreciseFileIndexEvaluate(true) + .SetReadFieldNames({"f0", score_field}) + .SetPredicate(PredicateBuilder::Equal(0, "f0", FieldType::STRING, + Literal(FieldType::STRING, "Bob", 3))); + if (vector_search) { + builder.SetVectorSearch(std::make_shared( + "f3", 1, std::vector{1.0f}, nullptr, nullptr, + VectorSearch::DistanceType::EUCLIDEAN, std::map{})); + } else { + builder.SetFullTextSearch(std::make_shared("f0", query, 1)); + } + ASSERT_OK_AND_ASSIGN(std::shared_ptr read_context, builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr split_read, + CreateSplitRead(read_context)); + const auto& context = split_read->context_; + ASSERT_EQ(-1, context->GetReadSchema()->GetFieldIndex("f3")); + arrow::FieldVector fields = context->GetReadSchema()->fields(); + fields.push_back(DataField::ConvertDataFieldToArrowField( + DataField(3, arrow::field("f3", arrow::float64())))); + const auto read_schema = arrow::schema(fields, context->GetReadSchema()->metadata()); + const MockSearchFileIndex::ScoredRows matches{{0, 0.9f}, {1, 0.8f}, {2, 0.7f}}; + std::shared_ptr index; + if (vector_search) { + ASSERT_OK_AND_ASSIGN( + index, MockSearchFileIndex::MakeIndex("f3", MockSearchFileIndex::VECTOR_IDENTIFIER, + matches, pool_)); + } else { + ASSERT_OK_AND_ASSIGN(index, MockSearchFileIndex::MakeQueryIndex( + "f0", MockSearchFileIndex::FULL_TEXT_IDENTIFIER, + {{query, {matches, std::nullopt}}}, pool_)); + } + auto first = PrepareDataFile(); + first->embedded_index = index; + auto second = std::make_shared(*first); + auto unfiltered = std::make_shared(*first); + AbstractSplitRead::PredicateProvider provider = + [first, second]( + const std::shared_ptr& file) -> Result> { + if (file == first) { + return PredicateBuilder::NotIn(2, "f3", FieldType::DOUBLE, {Literal(12.1)}); + } + if (file == second) { + return PredicateBuilder::NotIn(2, "f3", FieldType::DOUBLE, + {Literal(12.1), Literal(13.1)}); + } + return std::shared_ptr(); + }; + std::shared_ptr dv = + std::make_shared(RoaringBitmap32::From({0})); + DeletionVector::Factory dv_factory = + [dv](const std::string&) -> Result> { return dv; }; + auto paths = std::make_shared(); + ASSERT_OK(paths->Init(SearchTablePath() + "/f1=10/f2=0/bucket-0", "orc", "data-", nullptr)); + const BinaryRow partition = BinaryRowGenerator::GenerateRow({10, 0}, pool_.get()); + ASSERT_OK_AND_ASSIGN(std::vector> readers, + split_read->CreateRawFileReadersWithPredicateProvider( + partition, {first, second, unfiltered}, read_schema, provider, + dv_factory, std::nullopt, paths, {})); + ASSERT_EQ(3, readers.size()); + const std::vector expected{R"([["Emily",0.8,13.1]])", R"([["Tony",0.7,14.1]])", + R"([["Emily",0.8,13.1]])"}; + for (size_t i = 0; i < readers.size(); ++i) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(readers[i]))); + CheckSearchResult(result, fields, expected[i]); + } + } +} + TEST_F(PreciseFileIndexReadTest, TestFullTextRootScoresAndLimitAfterScalarDvAndRanges) { const std::string query = R"({"match":{"query":"ranked"}})"; auto search = std::make_shared("f0", query, 1, RoaringBitmap64::From({0, 2})); diff --git a/src/paimon/core/operation/precise_index_evaluator.h b/src/paimon/core/operation/precise_index_evaluator.h new file mode 100644 index 000000000..20cddc936 --- /dev/null +++ b/src/paimon/core/operation/precise_index_evaluator.h @@ -0,0 +1,354 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arrow/type.h" +#include "fmt/format.h" +#include "paimon/common/index/index_reader_utils.h" +#include "paimon/common/predicate/full_text_match_predicate.h" +#include "paimon/macros.h" +#include "paimon/predicate/compound_predicate.h" +#include "paimon/predicate/full_text_search.h" +#include "paimon/predicate/leaf_predicate.h" +#include "paimon/predicate/vector_search.h" + +namespace paimon { + +/// Shared precise evaluation over file row positions or real-time offsets. +/// Storage adapters supply readers and physical scans; this class owns tree and leaf semantics. +template +class PreciseIndexEvaluator { + public: + struct Evaluation { + Bitmap rows; + // Sources retain physical row positions or real-time offsets until all filtering and + // root TopK are complete. + std::map> scores; + }; + using LeafEvaluator = + std::function(const std::shared_ptr&, const Bitmap&)>; + using LeafEstimator = + std::function>(const std::shared_ptr&)>; + + PreciseIndexEvaluator(LeafEvaluator evaluate_leaf, LeafEstimator estimate_leaf) + : evaluate_leaf_(std::move(evaluate_leaf)), estimate_leaf_(std::move(estimate_leaf)) {} + + template + static Status Prepare(const std::shared_ptr& predicate, PrepareLeaf prepare_leaf) { + if (!predicate) { + return Status::OK(); + } + if (auto compound = std::dynamic_pointer_cast(predicate)) { + const Function::Type type = compound->GetFunction().GetType(); + if (type != Function::Type::AND && type != Function::Type::OR) { + return Status::Invalid("Unsupported compound predicate: ", predicate->ToString()); + } + for (const auto& child : compound->Children()) { + PAIMON_RETURN_NOT_OK(Prepare(child, prepare_leaf)); + } + return Status::OK(); + } + auto leaf = std::dynamic_pointer_cast(predicate); + if (!leaf) { + return Status::Invalid("Predicate must contain leaf or compound predicates"); + } + if (leaf->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH && + !std::dynamic_pointer_cast(leaf)) { + return Status::Invalid("FullTextMatch requires a full-text match predicate"); + } + return prepare_leaf(leaf); + } + + /// Field mapping and physical scans remain with the storage adapter. Scalar indexes only + /// prune candidates; every surviving scalar candidate is checked against the stored value. + /// @param predicate Original predicate in the query's logical schema, used for exact + /// scalar filtering and binding scores to the original field name. + /// @param index_predicate Predicate mapped to the source's physical schema for index + /// lookup. May be null when no indexed field maps to the logical field; scalar filtering + /// still uses predicate. Real-time sources currently use the original predicate directly. + /// @param candidates Rows still eligible after earlier predicates, ranges and deletions. + /// @param needs_scores Whether full-text matching must also return scores. + /// @param scan_scalar Callback that precisely filters candidate rows using stored values. + /// @param indexes Provider of scalar and search index readers for this source. + /// @return Selected rows and optional scores, or an error. + template + static Result EvaluateLeaf(const std::shared_ptr& predicate, + const std::shared_ptr& index_predicate, + const Bitmap& candidates, bool needs_scores, + ScalarScanner scan_scalar, Indexes* indexes) { + Evaluation result{candidates, {}}; + if (predicate->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH) { + auto full_text = std::dynamic_pointer_cast(index_predicate); + if (!full_text) { + return Status::Invalid("Full-text predicate has no matching indexed field"); + } + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr reader, + indexes->GetSearchReader(index_predicate->FieldName(), + IndexReaderCapabilities::SearchType::FULL_TEXT_MATCH)); + using FullTextResult = IndexReaderUtils::FullTextResult; + PAIMON_ASSIGN_OR_RAISE( + FullTextResult indexed, + IndexReaderUtils::VisitFullTextMatch(reader, full_text->GetFullTextMatch(), + candidates, needs_scores)); + result.rows &= indexed.rows; + if (needs_scores) { + result.scores.emplace(predicate->FieldName(), std::move(indexed.scores)); + } + return result; + } + if (index_predicate) { + PAIMON_ASSIGN_OR_RAISE(std::vector> readers, + indexes->GetReaders(index_predicate->FieldName())); + for (const auto& reader : readers) { + // TODO(lisizhuo.lsz): Pass remaining candidates through + // IndexReaderUtils::VisitScalar to index readers so they can skip rows excluded + // by earlier predicates, row ranges, or deletion vectors. + PAIMON_ASSIGN_OR_RAISE(std::optional indexed, + IndexReaderUtils::VisitScalar(reader, index_predicate)); + if (indexed) { + result.rows &= *indexed; + if (result.rows.IsEmpty()) { + break; + } + } + } + } + // TODO(lisizhuo.lsz): Whether to ScanScalar depends on whether the index returns exact + // results. + if (!result.rows.IsEmpty()) { + // Scalar indexes may contain false positives, even if they return a bitmap. + PAIMON_ASSIGN_OR_RAISE(Bitmap exact_rows, scan_scalar(predicate, result.rows)); + result.rows &= exact_rows; + } + return result; + } + + Result Evaluate(const std::shared_ptr& predicate, + const Bitmap& candidates) const { + if (!predicate || candidates.IsEmpty()) { + return Evaluation{candidates, {}}; + } + if (auto leaf = std::dynamic_pointer_cast(predicate)) { + return evaluate_leaf_(leaf, candidates); + } + if (auto compound = std::dynamic_pointer_cast(predicate)) { + return EvaluateCompound(compound, candidates); + } + return Status::Invalid(fmt::format("unsupported file predicate {}", predicate->ToString())); + } + + Result EvaluateCompound(const std::shared_ptr& predicate, + const Bitmap& candidates) const { + const Function::Type type = predicate->GetFunction().GetType(); + if (type == Function::Type::OR) { + Evaluation result; + for (const auto& child : predicate->Children()) { + // Every branch must see the same input, including rows matched by earlier branches. + PAIMON_ASSIGN_OR_RAISE(Evaluation child_result, Evaluate(child, candidates)); + result.rows |= child_result.rows; + result.scores.insert(child_result.scores.begin(), child_result.scores.end()); + } + return result; + } + if (type != Function::Type::AND) { + return Status::Invalid( + fmt::format("unsupported compound file predicate {}", predicate->ToString())); + } + + struct Child { + std::shared_ptr predicate; + std::optional selectivity; + }; + std::vector children; + children.reserve(predicate->Children().size()); + for (const auto& child : predicate->Children()) { + PAIMON_ASSIGN_OR_RAISE(std::optional selectivity, EstimateSelectivity(child)); + children.push_back({child, selectivity}); + } + std::stable_sort(children.begin(), children.end(), [](const Child& lhs, const Child& rhs) { + if (!lhs.selectivity) { + return false; + } + return !rhs.selectivity || *lhs.selectivity < *rhs.selectivity; + }); + + Evaluation result{candidates, {}}; + for (const auto& child : children) { + PAIMON_ASSIGN_OR_RAISE(Evaluation child_result, Evaluate(child.predicate, result.rows)); + result.rows = std::move(child_result.rows); + result.scores.insert(child_result.scores.begin(), child_result.scores.end()); + if (result.rows.IsEmpty()) { + break; + } + } + return result; + } + + Result> EstimateSelectivity( + const std::shared_ptr& predicate) const { + if (!predicate) { + return std::optional(); + } + if (auto leaf = std::dynamic_pointer_cast(predicate)) { + PAIMON_ASSIGN_OR_RAISE(std::optional estimate, estimate_leaf_(leaf)); + if (estimate && (!std::isfinite(*estimate) || *estimate < 0.0 || *estimate > 1.0)) { + return std::optional(); + } + return estimate; + } + auto compound = std::dynamic_pointer_cast(predicate); + if (!compound) { + return Status::Invalid( + fmt::format("unsupported file predicate {}", predicate->ToString())); + } + const Function::Type type = compound->GetFunction().GetType(); + if (type != Function::Type::AND && type != Function::Type::OR) { + return Status::Invalid( + fmt::format("unsupported compound file predicate {}", predicate->ToString())); + } + std::optional estimate = + type == Function::Type::AND ? std::nullopt : std::make_optional(0.0); + // These are scheduling hints. Continue visiting all children to propagate errors. + for (const auto& child : compound->Children()) { + PAIMON_ASSIGN_OR_RAISE(std::optional child_estimate, + EstimateSelectivity(child)); + if (type == Function::Type::AND) { + if (child_estimate) { + if (estimate.has_value()) { + estimate = std::min(*estimate, *child_estimate); + } else { + estimate = *child_estimate; + } + } + } else if (estimate && child_estimate) { + estimate = std::min(1.0, *estimate + *child_estimate); + } else { + estimate = std::nullopt; + } + } + return estimate; + } + + static std::shared_ptr FilterVectorSearch( + const std::shared_ptr& search, const Bitmap& rows) { + auto eligible = std::make_shared(rows); + auto user_filter = search->pre_filter; + return search->ReplacePreFilter([eligible, user_filter](int64_t row) { + return row >= 0 && row < Bitmap::MAX_VALUE && eligible->Contains(row) && + (!user_filter || user_filter(row)); + }); + } + + static std::shared_ptr FilterFullTextSearch( + const std::shared_ptr& search, const Bitmap& rows) { + RoaringBitmap64 eligible(rows); + if (search->include_row_ids) { + eligible &= *search->include_row_ids; + } + auto filtered = std::make_shared(*search); + filtered->include_row_ids = std::move(eligible); + return filtered; + } + + template + static Status ApplyRootSearch(const std::shared_ptr& vector_search, + const std::shared_ptr& full_text_search, + const std::string& score_field, Indexes* indexes, + Evaluation* evaluated) { + std::shared_ptr scored; + if (vector_search) { + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr reader, + indexes->GetSearchReader(vector_search->field_name, + IndexReaderCapabilities::SearchType::VECTOR_SEARCH)); + PAIMON_ASSIGN_OR_RAISE(scored, reader->VisitVectorSearch( + FilterVectorSearch(vector_search, evaluated->rows))); + } else if (full_text_search) { + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr reader, + indexes->GetSearchReader(full_text_search->field_name, + IndexReaderCapabilities::SearchType::FULL_TEXT_SEARCH)); + PAIMON_ASSIGN_OR_RAISE(scored, reader->VisitFullTextSearch(FilterFullTextSearch( + full_text_search, evaluated->rows))); + } else { + return Status::OK(); + } + if (!scored) { + return Status::Invalid("Root search returned a null result"); + } + evaluated->rows &= scored->GetRowPositions(); + evaluated->scores.emplace(score_field, std::move(scored)); + return Status::OK(); + } + + static Result> AlignScores(const std::shared_ptr& source, + const Bitmap& rows) { + if (!source) { + return Status::Invalid("Projected search source did not return scores"); + } + std::vector aligned; + aligned.reserve(rows.Cardinality()); + size_t score = 0; + for (auto iter = source->GetRowPositions().Begin(); iter != source->GetRowPositions().End(); + ++iter, ++score) { + if (rows.Contains(*iter)) { + aligned.push_back(source->GetScores()[score]); + } + } + if (aligned.size() != static_cast(rows.Cardinality())) { + return Status::Invalid("Projected scores do not cover all selected file rows"); + } + return aligned; + } + + template + static Result>> CreateOutputScores( + const Evaluation& evaluated, const ScoreBindings& bindings, + const std::shared_ptr& output_schema) { + std::map> scores; + for (const auto& [position, source] : bindings) { + auto found = evaluated.scores.find(source.field_name); + if (found == evaluated.scores.end()) { + return Status::Invalid("Projected search field did not return scores: ", + source.field_name); + } + PAIMON_ASSIGN_OR_RAISE(std::vector aligned, + AlignScores(found->second, evaluated.rows)); + scores.emplace(output_schema->field(position)->name(), std::move(aligned)); + } + return scores; + } + + private: + LeafEvaluator evaluate_leaf_; + LeafEstimator estimate_leaf_; +}; + +} // namespace paimon diff --git a/src/paimon/core/operation/raw_file_split_read.cpp b/src/paimon/core/operation/raw_file_split_read.cpp index 3bd92c349..a1fb07f52 100644 --- a/src/paimon/core/operation/raw_file_split_read.cpp +++ b/src/paimon/core/operation/raw_file_split_read.cpp @@ -237,14 +237,15 @@ Result RawFileSplitRead::Match(const std::shared_ptr& split, Result> RawFileSplitRead::CreatePreciseFileIndexReader( const BinaryRow& partition, const std::shared_ptr& file, + const std::shared_ptr& read_schema, const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory, const std::optional>& row_ranges, const std::shared_ptr& path_factory, const std::map& extra_format_options) const { PAIMON_ASSIGN_OR_RAISE( std::unique_ptr precise_read, - PreciseFileIndexRead::Create(*this, partition, file, field_mapping_builder, path_factory, - extra_format_options)); + PreciseFileIndexRead::Create(*this, partition, file, read_schema, predicate, + field_mapping_builder, path_factory, extra_format_options)); return precise_read->CreateReader(dv_factory, row_ranges); } diff --git a/src/paimon/core/operation/raw_file_split_read.h b/src/paimon/core/operation/raw_file_split_read.h index 1e97a45cb..797fb4285 100644 --- a/src/paimon/core/operation/raw_file_split_read.h +++ b/src/paimon/core/operation/raw_file_split_read.h @@ -95,9 +95,10 @@ class RawFileSplitRead : public AbstractSplitRead { const std::shared_ptr& read_schema, const std::shared_ptr& predicate); - /// Uses file-specific predicates for pushdown and the supplied residual predicate for - /// optional filtering after concatenation. Providers and DV factories must own their captures; - /// they may run concurrently and after CreateReader returns. + /// Uses complete file-specific predicates for pushdown or precise evaluation before root + /// search. Ordinary reads use the supplied residual predicate for optional filtering after + /// concatenation. Providers and DV factories must own their captures; they may run concurrently + /// and after CreateReader returns. Result> CreateReaderWithPredicateProvider( const BinaryRow& partition, int32_t bucket, const std::vector>& files, DeletionVector::Factory dv_factory, @@ -110,6 +111,8 @@ class RawFileSplitRead : public AbstractSplitRead { protected: Result> CreatePreciseFileIndexReader( const BinaryRow& partition, const std::shared_ptr& file, + const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory, const std::optional>& row_ranges, const std::shared_ptr& path_factory, diff --git a/src/paimon/core/realtime/arrow_deduplicate_realtime_store.cpp b/src/paimon/core/realtime/arrow_deduplicate_realtime_store.cpp index 845e6c855..1ba1698a4 100644 --- a/src/paimon/core/realtime/arrow_deduplicate_realtime_store.cpp +++ b/src/paimon/core/realtime/arrow_deduplicate_realtime_store.cpp @@ -32,9 +32,11 @@ #include "paimon/common/utils/arrow/mem_utils.h" #include "paimon/common/utils/arrow/status_utils.h" #include "paimon/common/utils/checked_cast.h" +#include "paimon/common/utils/scope_guard.h" #include "paimon/core/realtime/realtime_utils.h" #include "paimon/macros.h" #include "paimon/metrics.h" +#include "paimon/realtime/index/realtime_index_read_source.h" #include "paimon/record_batch.h" namespace paimon { @@ -136,18 +138,23 @@ class ArrowDeduplicateRealtimeStore::SegmentHandle final : public RealtimeSegmen std::shared_ptr offset_deletions_; }; -class ArrowDeduplicateRealtimeStore::ReadView final : public RealtimeReadView { +class ArrowDeduplicateRealtimeStore::ReadView final : public RealtimeIndexedReadView { public: ReadView(std::shared_ptr delegate, std::optional offset_range, - std::shared_ptr offset_deletions) + std::shared_ptr offset_deletions, + const std::shared_ptr& arrow_pool) : delegate_(std::move(delegate)), offset_range_(offset_range), - offset_deletions_(std::move(offset_deletions)) {} + offset_deletions_(std::move(offset_deletions)), + arrow_pool_(arrow_pool) {} std::optional GetOffsetRange() const override { return offset_range_; } + Result>> CreateIndexReadSources( + const OffsetRange& offset_range) const override; + const std::shared_ptr& Delegate() const { return delegate_; } @@ -160,6 +167,7 @@ class ArrowDeduplicateRealtimeStore::ReadView final : public RealtimeReadView { std::shared_ptr delegate_; std::optional offset_range_; std::shared_ptr offset_deletions_; + std::shared_ptr arrow_pool_; }; class ArrowDeduplicateRealtimeStore::RetiredReadView final : public RealtimeReadView { @@ -234,6 +242,47 @@ class ArrowDeduplicateRealtimeStore::OffsetFilteringBatchReader final : public B std::shared_ptr arrow_pool_; }; +class ArrowDeduplicateRealtimeStore::IndexReadSource final : public RealtimeIndexReadSource { + public: + IndexReadSource(std::shared_ptr source, + const std::shared_ptr& deletions) + : source_(std::move(source)), deletions_(deletions) {} + + OffsetRange GetOffsetRange() const override { + return source_->GetOffsetRange(); + } + + Result CreateCandidates() const override { + PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 candidates, source_->CreateCandidates()); + candidates -= *deletions_; + return candidates; + } + + Result>> CreateIndexReaders( + const std::string& field_name) const override { + return source_->CreateIndexReaders(field_name); + } + + private: + std::shared_ptr source_; + std::shared_ptr deletions_; +}; + +Result>> +ArrowDeduplicateRealtimeStore::ReadView::CreateIndexReadSources( + const OffsetRange& offset_range) const { + auto indexed = std::dynamic_pointer_cast(delegate_); + if (!indexed) { + return Status::NotImplemented("Deduplicate delegate has no precise index read capability"); + } + PAIMON_ASSIGN_OR_RAISE(std::vector> sources, + indexed->CreateIndexReadSources(offset_range)); + for (auto& source : sources) { + source = std::make_shared(std::move(source), offset_deletions_); + } + return sources; +} + Result> ArrowDeduplicateRealtimeStore::Create( const std::shared_ptr& write_schema, const std::vector& deduplicate_key_fields, @@ -498,7 +547,7 @@ Result> ArrowDeduplicateRealtimeStore::Acquire : building_offset_range_; } return std::shared_ptr( - new ReadView(std::move(delegate_view), range, pending_offset_deletions_)); + new ReadView(std::move(delegate_view), range, pending_offset_deletions_, arrow_pool_)); } Result>> ArrowDeduplicateRealtimeStore::CreateQueryReaders( @@ -510,24 +559,27 @@ Result>> ArrowDeduplicateRealtimeStore: if (!context.read_schema || !context.read_schema->release) { return Status::Invalid("deduplicate query read schema is null"); } + if (context.offset_range.begin < 0 || context.offset_range.begin > context.offset_range.end) { + return Status::Invalid("real-time query offset range is invalid"); + } PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr output_schema, arrow::ImportSchema(context.read_schema)); PAIMON_RETURN_NOT_OK(RealtimeUtils::ValidateOffsetField(output_schema)); ArrowSchema inner_c_schema; ArrowSchemaMarkReleased(&inner_c_schema); PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*output_schema, &inner_c_schema)); - RealtimeQueryContext inner_context{&inner_c_schema, context.predicate, context.offset_range, - context.read_batch_size}; - PAIMON_ASSIGN_OR_RAISE( - std::vector> inner, - delegate_->CreateQueryReaders(deduplicate_view->Delegate(), inner_context)); - std::vector> readers; - readers.reserve(inner.size()); - for (std::unique_ptr& reader : inner) { - readers.push_back(std::make_unique( - std::move(reader), output_schema, deduplicate_view->OffsetDeletions(), arrow_pool_)); - } - return readers; + ScopeGuard schema_guard([&]() { ArrowSchemaRelease(&inner_c_schema); }); + RealtimeQueryContext inner_context = context; + inner_context.read_schema = &inner_c_schema; + const auto& deletions = deduplicate_view->OffsetDeletions(); + if (!context.offset_deletions || context.offset_deletions->IsEmpty()) { + inner_context.offset_deletions = deletions; + } else if (deletions && !deletions->IsEmpty()) { + auto combined = std::make_shared(*context.offset_deletions); + *combined |= *deletions; + inner_context.offset_deletions = std::move(combined); + } + return delegate_->CreateQueryReaders(deduplicate_view->Delegate(), inner_context); } Status ArrowDeduplicateRealtimeStore::AdvanceCommittedOffset(int64_t) { diff --git a/src/paimon/core/realtime/arrow_deduplicate_realtime_store.h b/src/paimon/core/realtime/arrow_deduplicate_realtime_store.h index 7403dd837..7de69cecf 100644 --- a/src/paimon/core/realtime/arrow_deduplicate_realtime_store.h +++ b/src/paimon/core/realtime/arrow_deduplicate_realtime_store.h @@ -113,6 +113,7 @@ class ArrowDeduplicateRealtimeStore final : public RealtimeStore { class SegmentHandle; class ReadView; + class IndexReadSource; class RetiredReadView; class OffsetFilteringBatchReader; diff --git a/src/paimon/core/realtime/arrow_deduplicate_realtime_store_test.cpp b/src/paimon/core/realtime/arrow_deduplicate_realtime_store_test.cpp index 8cc962044..92c2b9c84 100644 --- a/src/paimon/core/realtime/arrow_deduplicate_realtime_store_test.cpp +++ b/src/paimon/core/realtime/arrow_deduplicate_realtime_store_test.cpp @@ -31,6 +31,7 @@ #include "arrow/api.h" #include "arrow/c/bridge.h" #include "arrow/ipc/json_simple.h" +#include "paimon/common/reader/reader_utils.h" #include "paimon/common/table/special_fields.h" #include "paimon/common/utils/arrow/mem_utils.h" #include "paimon/common/utils/arrow/status_utils.h" @@ -99,14 +100,22 @@ class BitmapSelectingRealtimeStore final : public RealtimeStore { : inner_(std::move(inner)), bitmap_(std::move(bitmap)) {} Result NextBatch() override { - return Status::Invalid("bitmap input must not be materialized"); + PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, NextBatchWithBitmap()); + return ReaderUtils::ApplyBitmapToReadBatch(std::move(batch), + GetArrowPool(GetDefaultPool())); } Result NextBatchWithBitmap() override { - PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, inner_->NextBatchWithBitmap()); - if (!IsEofBatch(batch)) { - batch.second = bitmap_; + while (true) { + PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, inner_->NextBatchWithBitmap()); + if (IsEofBatch(batch)) { + return batch; + } + batch.second &= bitmap_; + if (!batch.second.IsEmpty()) { + return batch; + } + ReaderUtils::ReleaseReadBatch(std::move(batch.first)); } - return batch; } std::shared_ptr GetReaderMetrics() const override { return inner_->GetReaderMetrics(); @@ -197,9 +206,13 @@ class ArrowDeduplicateRealtimeStoreTest : public testing::Test { int32_t batch_size) const { auto c_schema = std::make_unique(); PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*schema, c_schema.get())); - RealtimeQueryContext context{ - c_schema.get(), - /*predicate=*/nullptr, view->GetOffsetRange().value_or(OffsetRange(0, 0)), batch_size}; + RealtimeQueryContext context{c_schema.get(), + /*predicate=*/nullptr, + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + batch_size, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; return store_->CreateQueryReaders(view, context); } @@ -217,9 +230,29 @@ class ArrowDeduplicateRealtimeStoreTest : public testing::Test { Result> ReadOffsets(const std::shared_ptr& view, const std::shared_ptr& predicate, const OffsetRange& offset_range) const { + return ReadOffsets(view, predicate, offset_range, nullptr); + } + + Result> ReadOffsets( + const std::shared_ptr& view, const std::shared_ptr& predicate, + const OffsetRange& offset_range, + const std::shared_ptr& selection) const { + return ReadOffsets(view, predicate, offset_range, selection, nullptr); + } + + Result> ReadOffsets( + const std::shared_ptr& view, const std::shared_ptr& predicate, + const OffsetRange& offset_range, const std::shared_ptr& selection, + const std::shared_ptr& offset_deletions) const { auto c_schema = std::make_unique(); PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*schema_, c_schema.get())); - RealtimeQueryContext context{c_schema.get(), predicate, offset_range, 1024}; + RealtimeQueryContext context{c_schema.get(), + predicate, + offset_range, + 1024, + selection, + offset_deletions, + /*enable_index_pruning=*/true}; PAIMON_ASSIGN_OR_RAISE(std::vector> readers, store_->CreateQueryReaders(view, context)); std::vector result; @@ -255,6 +288,77 @@ class ArrowDeduplicateRealtimeStoreTest : public testing::Test { std::shared_ptr empty_lookup_; }; +TEST_F(ArrowDeduplicateRealtimeStoreTest, TestQueryPreservesStatisticsPredicate) { + ASSERT_OK_AND_ASSIGN( + delegate_, + ArrowRealtimeStore::Create(schema_, RealtimeStoreMode::APPEND_ONLY, StatisticsMode::FULL, + "", nullptr, "zstd", 1, {}, pool_, arrow_pool_)); + ASSERT_OK_AND_ASSIGN(store_, ArrowDeduplicateRealtimeStore::Create(schema_, {"id"}, delegate_, + pool_, arrow_pool_)); + ASSERT_OK(store_->WriteAndLookup(MakeBatch(R"([[0,1,"a"],[1,2,"b"]])"), + {RecordBatch::RowKind::INSERT, RecordBatch::RowKind::INSERT}, + OffsetRange(0, 2), RoaringBitmap64())); + ASSERT_OK_AND_ASSIGN(std::shared_ptr view, store_->AcquireReadView()); + ArrowSchema read_schema; + ArrowSchemaMarkReleased(&read_schema); + ASSERT_TRUE(arrow::ExportSchema(*schema_, &read_schema).ok()); + ScopeGuard schema_guard([&]() { ArrowSchemaRelease(&read_schema); }); + auto predicate = + PredicateBuilder::GreaterThan(1, "id", FieldType::BIGINT, Literal(int64_t{100})); + RealtimeQueryContext context{&read_schema, + predicate, + OffsetRange(0, 2), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; + ASSERT_OK_AND_ASSIGN(std::vector> readers, + store_->CreateQueryReaders(view, context)); + ASSERT_EQ(readers.size(), 1); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, readers[0]->NextBatchWithBitmap()); + ASSERT_TRUE(BatchReader::IsEofBatch(batch)); + readers[0]->Close(); +} + +TEST_F(ArrowDeduplicateRealtimeStoreTest, TestQuerySelectionCombinesPinnedDeletions) { + ASSERT_OK(store_->WriteAndLookup(MakeBatch(R"([[10,1,"old"],[20,2,"other"]])"), + {RecordBatch::RowKind::INSERT, RecordBatch::RowKind::INSERT}, + OffsetRange(10, 21), RoaringBitmap64())); + ASSERT_OK_AND_ASSIGN(std::shared_ptr old_view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(std::optional> sealed, + store_->SealForCommit()); + ASSERT_TRUE(sealed.has_value()); + ASSERT_OK(store_->WriteAndLookup(MakeBatch(R"([[30,1,"new"]])"), {RecordBatch::RowKind::INSERT}, + OffsetRange(30, 31), RoaringBitmap64::From({10}))); + ASSERT_OK_AND_ASSIGN(std::shared_ptr new_view, store_->AcquireReadView()); + auto selection = std::make_shared(RoaringBitmap64::From({10, 20})); + ASSERT_OK_AND_ASSIGN(std::vector old_offsets, + ReadOffsets(old_view, nullptr, OffsetRange(0, 31), selection)); + ASSERT_EQ(old_offsets, (std::vector{10, 20})); + ASSERT_OK_AND_ASSIGN(std::vector selected_offsets, + ReadOffsets(new_view, nullptr, OffsetRange(0, 31), selection)); + ASSERT_EQ(selected_offsets, (std::vector{20})); + ASSERT_EQ(*selection, RoaringBitmap64::From({10, 20})); + ASSERT_OK_AND_ASSIGN(std::vector all_offsets, ReadOffsets(new_view)); + ASSERT_EQ(all_offsets, (std::vector{20, 30})); + ASSERT_OK_AND_ASSIGN( + std::vector empty_offsets, + ReadOffsets(new_view, nullptr, OffsetRange(0, 31), std::make_shared())); + ASSERT_TRUE(empty_offsets.empty()); + + auto deletions = std::make_shared(RoaringBitmap64::From({20})); + auto all_selected = std::make_shared(RoaringBitmap64::From({10, 20, 30})); + ASSERT_OK_AND_ASSIGN( + std::vector remaining_offsets, + ReadOffsets(new_view, nullptr, OffsetRange(0, 31), all_selected, deletions)); + ASSERT_EQ(remaining_offsets, (std::vector{30})); + ASSERT_OK_AND_ASSIGN(remaining_offsets, + ReadOffsets(old_view, nullptr, OffsetRange(0, 31), nullptr, deletions)); + ASSERT_EQ(remaining_offsets, (std::vector{10})); + ASSERT_EQ(*deletions, RoaringBitmap64::From({20})); + ASSERT_EQ(*all_selected, RoaringBitmap64::From({10, 20, 30})); +} + TEST_F(ArrowDeduplicateRealtimeStoreTest, TestReclaimsOnlyCompleteSegmentPrefix) { WriteAndSeal(R"([[2, 1, "old"]])", OffsetRange(0, 10), RoaringBitmap64()); RoaringBitmap64 overwritten_offsets; diff --git a/src/paimon/core/realtime/arrow_realtime_store.cpp b/src/paimon/core/realtime/arrow_realtime_store.cpp index 19f962362..298ac476f 100644 --- a/src/paimon/core/realtime/arrow_realtime_store.cpp +++ b/src/paimon/core/realtime/arrow_realtime_store.cpp @@ -50,6 +50,8 @@ #include "paimon/core/utils/nested_projection_utils.h" #include "paimon/fs/file_system.h" #include "paimon/macros.h" +#include "paimon/realtime/index/realtime_index.h" +#include "paimon/realtime/index/realtime_index_read_source.h" namespace paimon { namespace { @@ -198,8 +200,13 @@ std::vector> SliceQueryReaders( class ArrowRealtimeStore::Segment : public RealtimeSegmentHandle { public: Segment(const OffsetRange& offset_range, int64_t row_count, - std::shared_ptr index) - : offset_range_(offset_range), row_count_(row_count), index_(std::move(index)) {} + std::shared_ptr index, const std::shared_ptr& memory_pool, + const std::shared_ptr& arrow_pool) + : memory_pool_(memory_pool), + arrow_pool_(arrow_pool), + offset_range_(offset_range), + row_count_(row_count), + index_(std::move(index)) {} OffsetRange GetOffsetRange() const override { return offset_range_; @@ -216,6 +223,9 @@ class ArrowRealtimeStore::Segment : public RealtimeSegmentHandle { } private: + // Release derived data and indexes before their allocators. + std::shared_ptr memory_pool_; + std::shared_ptr arrow_pool_; OffsetRange offset_range_; int64_t row_count_; std::shared_ptr index_; @@ -224,8 +234,10 @@ class ArrowRealtimeStore::Segment : public RealtimeSegmentHandle { class ArrowRealtimeStore::MemorySegment final : public ArrowRealtimeStore::Segment { public: MemorySegment(const OffsetRange& offset_range, std::vector&& batches, - std::shared_ptr index) - : Segment(offset_range, CountRows(batches), std::move(index)), + std::shared_ptr index, + const std::shared_ptr& memory_pool, + const std::shared_ptr& arrow_pool) + : Segment(offset_range, CountRows(batches), std::move(index), memory_pool, arrow_pool), batches_(std::move(batches)) { for (const StoredBatch& batch : batches_) { memory_usage_ += batch.memory_usage; @@ -284,8 +296,10 @@ class ArrowRealtimeStore::SpilledSegment final : public ArrowRealtimeStore::Segm public: SpilledSegment(const OffsetRange& offset_range, int64_t row_count, std::vector&& batches, std::shared_ptr file, - std::shared_ptr index) - : Segment(offset_range, row_count, std::move(index)), + std::shared_ptr index, + const std::shared_ptr& memory_pool, + const std::shared_ptr& arrow_pool) + : Segment(offset_range, row_count, std::move(index), memory_pool, arrow_pool), batches_(std::move(batches)), file_(std::move(file)) { for (const SpilledBatch& batch : batches_) { @@ -346,10 +360,10 @@ class ArrowRealtimeStore::SharedSpillFileReader { std::mutex mutex_; }; -class ArrowRealtimeStore::ReadView : public RealtimeReadView { +class ArrowRealtimeStore::ReadView : public RealtimeIndexedReadView { public: - explicit ReadView(std::vector>&& segments) - : segments_(std::move(segments)) { + ReadView(std::vector>&& segments, bool append_mode) + : segments_(std::move(segments)), append_mode_(append_mode) { if (!segments_.empty()) { offset_range_ = OffsetRange(segments_.front()->GetOffsetRange().begin, segments_.back()->GetOffsetRange().end); @@ -360,6 +374,9 @@ class ArrowRealtimeStore::ReadView : public RealtimeReadView { return offset_range_; } + Result>> CreateIndexReadSources( + const OffsetRange& offset_range) const override; + const std::vector>& GetSegments() const { return segments_; } @@ -367,6 +384,7 @@ class ArrowRealtimeStore::ReadView : public RealtimeReadView { private: std::vector> segments_; std::optional offset_range_; + bool append_mode_; }; class ArrowRealtimeStore::AppendCommitBatchReader : public BatchReader { @@ -480,23 +498,34 @@ class ArrowRealtimeStore::SpillBatchReader : public BatchReader { class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { public: - AppendQueryBatchReader(const std::shared_ptr& view, - const std::shared_ptr& read_schema, - const std::shared_ptr& predicate_filter, - std::vector&& statistics_mapping, - const std::shared_ptr& predicate, - const OffsetRange& offset_range, - const std::shared_ptr& arrow_pool, - const std::shared_ptr& memory_pool) + AppendQueryBatchReader( + const std::shared_ptr& view, const std::shared_ptr& read_schema, + const std::shared_ptr& predicate, std::vector&& statistics_mapping, + const OffsetRange& offset_range, const std::shared_ptr& selection, + const std::shared_ptr& offset_deletions, bool enable_index_pruning, + const std::shared_ptr& arrow_pool, + const std::shared_ptr& memory_pool) : view_(view), read_schema_(read_schema), arrow_pool_(arrow_pool), memory_pool_(memory_pool), - predicate_filter_(predicate_filter), + predicate_filter_(std::dynamic_pointer_cast(predicate)), statistics_mapping_(std::move(statistics_mapping)), predicate_(predicate), offset_range_(offset_range), - metrics_(std::make_shared()) {} + selection_(selection), + offset_deletions_(offset_deletions), + enable_index_pruning_(enable_index_pruning), + metrics_(std::make_shared()) { + // Pinned segments are ordered and non-overlapping; skip the prefix outside this query. + const auto& segments = view_->GetSegments(); + const auto first = + std::lower_bound(segments.begin(), segments.end(), offset_range_.begin, + [](const std::shared_ptr& segment, int64_t offset) { + return segment->GetOffsetRange().end <= offset; + }); + next_segment_ = first - segments.begin(); + } Result NextBatch() override { PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, NextBatchWithBitmap()); @@ -506,6 +535,10 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { Result NextBatchWithBitmap() override { while (view_ && next_segment_ < view_->GetSegments().size()) { const std::shared_ptr& segment = view_->GetSegments()[next_segment_]; + if (segment->GetOffsetRange().begin >= offset_range_.end) { + next_segment_ = view_->GetSegments().size(); + break; + } PAIMON_ASSIGN_OR_RAISE(bool may_match, PrepareSegment(segment)); if (!may_match) { NextSegment(); @@ -543,12 +576,23 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { return false; } if (!segment_initialized_) { - if (segment->GetIndex()) { + if (selection_) { + candidates_ = *selection_; + } + if (enable_index_pruning_ && segment->GetIndex() && predicate_) { PAIMON_ASSIGN_OR_RAISE( - candidates_, RealtimeIndexEvaluator::Evaluate( - segment->GetIndex(), predicate_, - OffsetRange(std::max(segment_range.begin, offset_range_.begin), - std::min(segment_range.end, offset_range_.end)))); + std::optional indexed, + RealtimeIndexEvaluator::Evaluate( + segment->GetIndex(), predicate_, + OffsetRange(std::max(segment_range.begin, offset_range_.begin), + std::min(segment_range.end, offset_range_.end)))); + if (indexed) { + if (candidates_) { + *candidates_ &= *indexed; + } else { + candidates_ = std::move(indexed); + } + } } segment_initialized_ = true; } @@ -604,7 +648,8 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { const StoredBatch& stored = memory_segment->GetBatches()[batch_index]; row_count = stored.data->length(); statistics = &stored.statistics; - source = stored.data; + // Readers share buffers, but need separate wrappers for Arrow's lazy field caches. + source = std::make_shared(stored.data->data()); batch_range = stored.offset_range; } else if (spilled_segment) { const SpilledBatch& stored = spilled_segment->GetBatches()[batch_index]; @@ -669,8 +714,6 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { RoaringBitmap32 SelectRows(const std::shared_ptr& source, const OffsetRange& visible_range) const { - // TODO(xinyu.lxy): Push down the real-time DV to avoid another offset-to-row mapping - // upstream. RoaringBitmap32 selection; if (source->length() == 0) { return selection; @@ -682,7 +725,15 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { std::lower_bound(values, values + offsets->length(), visible_range.begin); const int64_t* end = std::lower_bound(begin, values + offsets->length(), visible_range.end); if (!candidates_) { - selection.AddRange(begin - values, end - values); + if (!offset_deletions_ || offset_deletions_->IsEmpty()) { + selection.AddRange(begin - values, end - values); + } else { + for (const int64_t* row = begin; row != end; ++row) { + if (!offset_deletions_->Contains(*row)) { + selection.Add(row - values); + } + } + } return selection; } // Offsets may have gaps: sparse candidates seek by offset; dense candidates scan. @@ -700,13 +751,15 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { for (RoaringBitmap64::Iterator iter = first; iter != sentinel && *iter < visible_range.end; ++iter) { begin = std::lower_bound(begin, end, *iter); - if (begin != end && *begin == *iter) { + if (begin != end && *begin == *iter && + (!offset_deletions_ || !offset_deletions_->Contains(*iter))) { selection.Add(begin - values); } } } else { for (const int64_t* row = begin; row != end; ++row) { - if (candidates_->Contains(*row)) { + if (candidates_->Contains(*row) && + (!offset_deletions_ || !offset_deletions_->Contains(*row))) { selection.Add(row - values); } } @@ -722,6 +775,9 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { std::vector statistics_mapping_; std::shared_ptr predicate_; OffsetRange offset_range_; + std::shared_ptr selection_; + std::shared_ptr offset_deletions_; + bool enable_index_pruning_; std::optional candidates_; bool segment_initialized_ = false; std::shared_ptr metrics_; @@ -730,6 +786,61 @@ class ArrowRealtimeStore::AppendQueryBatchReader : public BatchReader { size_t next_batch_ = 0; }; +class ArrowRealtimeStore::IndexReadSource final : public RealtimeIndexReadSource { + public: + IndexReadSource(const std::shared_ptr& segment, const OffsetRange& offset_range) + : segment_(segment), offset_range_(offset_range) {} + + OffsetRange GetOffsetRange() const override { + return offset_range_; + } + + Result CreateCandidates() const override { + RoaringBitmap64 candidates; + candidates.AddRange(offset_range_.begin, offset_range_.end); + return candidates; + } + + Result>> CreateIndexReaders( + const std::string& field_name) const override { + std::vector> readers; + if (segment_->GetIndex()) { + for (const auto& index : segment_->GetIndex()->FindIndexes(field_name)) { + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr reader, + index->CreateReader(offset_range_)); + if (reader) { + readers.push_back(std::move(reader)); + } + } + } + return readers; + } + + private: + std::shared_ptr segment_; + OffsetRange offset_range_; +}; + +Result>> +ArrowRealtimeStore::ReadView::CreateIndexReadSources(const OffsetRange& offset_range) const { + if (!append_mode_) { + return Status::NotImplemented("Primary-key views do not support precise index sources"); + } + if (offset_range.begin < 0 || offset_range.begin > offset_range.end) { + return Status::Invalid("Invalid real-time index source offset range"); + } + std::vector> sources; + for (const auto& segment : segments_) { + const OffsetRange bounds = segment->GetOffsetRange(); + const OffsetRange range(std::max(bounds.begin, offset_range.begin), + std::min(bounds.end, offset_range.end)); + if (range.begin < range.end) { + sources.push_back(std::make_shared(segment, range)); + } + } + return sources; +} + Result> ArrowRealtimeStore::Create( const std::shared_ptr& write_schema, RealtimeStoreMode mode, StatisticsMode statistics_mode, const std::string& temp_directory, @@ -855,12 +966,13 @@ Result ArrowRealtimeStore::MayMatchStatistics( return true; } const BatchStatistics& batch_statistics = statistics.value(); - std::shared_ptr min_row = std::make_shared( - batch_statistics.min_values, batch_statistics.min_values->fields(), memory_pool, - /*row_id=*/0); - std::shared_ptr max_row = std::make_shared( - batch_statistics.max_values, batch_statistics.max_values->fields(), memory_pool, - /*row_id=*/0); + // Keep Arrow's lazy field caches local while sharing the statistics buffers. + auto min_values = std::make_shared(batch_statistics.min_values->data()); + auto max_values = std::make_shared(batch_statistics.max_values->data()); + std::shared_ptr min_row = + std::make_shared(min_values, min_values->fields(), memory_pool, /*row_id=*/0); + std::shared_ptr max_row = + std::make_shared(max_values, max_values->fields(), memory_pool, /*row_id=*/0); ProjectedRow projected_min(min_row, statistics_mapping); ProjectedRow projected_max(max_row, statistics_mapping); std::shared_ptr null_counts = @@ -939,8 +1051,9 @@ ArrowRealtimeStore::FreezeBuildingSegment() { return std::optional>(); } PAIMON_ASSIGN_OR_RAISE(std::shared_ptr next_index, CreateBuildingIndex()); - memory_segment = std::make_shared( - building_range_.value(), std::move(building_batches_), std::move(building_index_)); + memory_segment = + std::make_shared(building_range_.value(), std::move(building_batches_), + std::move(building_index_), memory_pool_, arrow_pool_); building_index_ = std::move(next_index); sealed_segments_.push_back(memory_segment); sealed_memory_usage_ += memory_segment->GetMemoryUsage(); @@ -997,7 +1110,7 @@ Result> ArrowRealtimeStore::SpillFrozenSe // TODO(xinyu.lxy): Real-time indexes do not support spill yet. auto spilled_segment = std::make_shared( memory_segment->GetOffsetRange(), memory_segment->GetRowCount(), std::move(spilled_batches), - std::move(spill_file), memory_segment->GetIndex()); + std::move(spill_file), memory_segment->GetIndex(), memory_pool_, arrow_pool_); cleanup_guard.Release(); { @@ -1062,9 +1175,11 @@ Result> ArrowRealtimeStore::AcquireReadView() std::vector> segments = sealed_segments_; if (!building_batches_.empty()) { segments.push_back(std::make_shared( - building_range_.value(), std::vector(building_batches_), building_index_)); + building_range_.value(), std::vector(building_batches_), building_index_, + memory_pool_, arrow_pool_)); } - std::shared_ptr view = std::make_shared(std::move(segments)); + std::shared_ptr view = + std::make_shared(std::move(segments), mode_ != RealtimeStoreMode::PRIMARY_KEY); return std::shared_ptr(std::move(view)); } @@ -1083,22 +1198,25 @@ Result>> ArrowRealtimeStore::CreateQuer if (context.offset_range.begin < 0 || context.offset_range.begin > context.offset_range.end) { return Status::Invalid("real-time query offset range is invalid"); } + if (mode_ == RealtimeStoreMode::PRIMARY_KEY && context.selection) { + return Status::NotImplemented("Offset selection is not supported for primary-key merge"); + } + if (mode_ == RealtimeStoreMode::PRIMARY_KEY && context.offset_deletions) { + return Status::NotImplemented("Offset deletions are not supported for primary-key merge"); + } PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr read_schema, arrow::ImportSchema(context.read_schema)); std::vector> readers; if (!arrow_view->GetOffsetRange()) { return SliceQueryReaders(std::move(readers), context.read_batch_size, arrow_pool_); } - std::shared_ptr predicate_filter; - if (context.predicate) { - predicate_filter = std::dynamic_pointer_cast(context.predicate); - } std::vector statistics_mapping; statistics_mapping.reserve(read_schema->num_fields()); for (const std::shared_ptr& field : read_schema->fields()) { statistics_mapping.push_back(write_schema_->GetFieldIndex(field->name())); } if (mode_ == RealtimeStoreMode::PRIMARY_KEY) { + auto predicate_filter = std::dynamic_pointer_cast(context.predicate); for (const std::shared_ptr& segment : arrow_view->GetSegments()) { std::shared_ptr memory_segment = std::dynamic_pointer_cast(segment); @@ -1159,8 +1277,9 @@ Result>> ArrowRealtimeStore::CreateQuer return readers; } std::unique_ptr reader = std::make_unique( - arrow_view, read_schema, predicate_filter, std::move(statistics_mapping), context.predicate, - visible_range, arrow_pool_, memory_pool_); + arrow_view, read_schema, context.predicate, std::move(statistics_mapping), visible_range, + context.selection, context.offset_deletions, context.enable_index_pruning, arrow_pool_, + memory_pool_); readers.push_back(std::move(reader)); return SliceQueryReaders(std::move(readers), context.read_batch_size, arrow_pool_); } @@ -1198,7 +1317,8 @@ Result> ArrowRealtimeStore::RetireCommittedSeg sealed_memory_usage_ -= reclaimed_memory_usage; sealed_row_count_ -= reclaimed_row_count; } - return std::shared_ptr(new ReadView(std::move(reclaimed_segments))); + return std::shared_ptr( + new ReadView(std::move(reclaimed_segments), mode_ != RealtimeStoreMode::PRIMARY_KEY)); } RealtimeStoreDataUsage ArrowRealtimeStore::GetDataUsage() const { diff --git a/src/paimon/core/realtime/arrow_realtime_store.h b/src/paimon/core/realtime/arrow_realtime_store.h index 4bb6d3700..94db7123f 100644 --- a/src/paimon/core/realtime/arrow_realtime_store.h +++ b/src/paimon/core/realtime/arrow_realtime_store.h @@ -121,6 +121,7 @@ class ArrowRealtimeStore final : public TwoPhaseSealableRealtimeStore { class SpilledSegment; class SpillFile; class ReadView; + class IndexReadSource; class AppendCommitBatchReader; class StoredBatchReader; class SharedSpillFileReader; diff --git a/src/paimon/core/realtime/arrow_realtime_store_test.cpp b/src/paimon/core/realtime/arrow_realtime_store_test.cpp index 7ac277fd8..fe00d43b9 100644 --- a/src/paimon/core/realtime/arrow_realtime_store_test.cpp +++ b/src/paimon/core/realtime/arrow_realtime_store_test.cpp @@ -41,6 +41,7 @@ #include "paimon/testing/utils/read_result_collector.h" #include "paimon/testing/utils/test_helper.h" #include "paimon/testing/utils/testharness.h" +#include "paimon/utils/roaring_bitmap64.h" namespace paimon::test { namespace { @@ -226,8 +227,10 @@ TEST_F(ArrowRealtimeStoreTest, TestQueryReaderClipsCommittedOffsetWithBitmap) { }); { std::unique_ptr c_schema = MakeReadSchema(read_schema); - RealtimeQueryContext context{c_schema.get(), - /*predicate=*/nullptr, OffsetRange(12, 15)}; + RealtimeQueryContext context{ + c_schema.get(), + /*predicate=*/nullptr, OffsetRange(12, 15), /*read_batch_size=*/1024, + /*selection=*/nullptr, /*offset_deletions=*/nullptr, /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store_->CreateQueryReaders(view, context)); ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, readers[0]->NextBatch()); @@ -240,8 +243,10 @@ TEST_F(ArrowRealtimeStoreTest, TestQueryReaderClipsCommittedOffsetWithBitmap) { } { std::unique_ptr c_schema = MakeReadSchema(read_schema); - RealtimeQueryContext context{c_schema.get(), - /*predicate=*/nullptr, OffsetRange(12, 15)}; + RealtimeQueryContext context{ + c_schema.get(), + /*predicate=*/nullptr, OffsetRange(12, 15), /*read_batch_size=*/1024, + /*selection=*/nullptr, /*offset_deletions=*/nullptr, /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store_->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -276,7 +281,11 @@ TEST_F(ArrowRealtimeStoreTest, TestQueryReaderClipsCommittedOffsetWithBitmap) { std::unique_ptr c_schema = MakeReadSchema(read_schema); RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store_->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -353,7 +362,10 @@ TEST_F(ArrowRealtimeStoreTest, TestSealSpillsAndKeepsPinnedMemoryViewReadable) { RealtimeQueryContext context{read_schema.get(), /*predicate=*/nullptr, pinned_view->GetOffsetRange().value_or(OffsetRange(0, 0)), - /*read_batch_size=*/1}; + /*read_batch_size=*/1, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> query_readers, store->CreateQueryReaders(pinned_view, context)); ASSERT_EQ(1, query_readers.size()); @@ -367,10 +379,11 @@ TEST_F(ArrowRealtimeStoreTest, TestSealSpillsAndKeepsPinnedMemoryViewReadable) { ASSERT_OK_AND_ASSIGN(std::shared_ptr spilled_view, store->AcquireReadView()); std::unique_ptr spilled_read_schema = MakeReadSchema(schema_); - RealtimeQueryContext spilled_context{spilled_read_schema.get(), - /*predicate=*/nullptr, - spilled_view->GetOffsetRange().value_or(OffsetRange(0, 0)), - /*read_batch_size=*/1}; + RealtimeQueryContext spilled_context{ + spilled_read_schema.get(), + /*predicate=*/nullptr, spilled_view->GetOffsetRange().value_or(OffsetRange(0, 0)), + /*read_batch_size=*/1, /*selection=*/nullptr, + /*offset_deletions=*/nullptr, /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> spilled_readers, store->CreateQueryReaders(spilled_view, spilled_context)); ASSERT_EQ(1, spilled_readers.size()); @@ -480,6 +493,99 @@ TEST_F(ArrowRealtimeStoreTest, TestFactoryRejectsFrameworkOwnedMode) { ASSERT_NOK_WITH_MSG(factory.Create(std::move(request)), "invalid real-time store mode"); } +TEST_F(ArrowRealtimeStoreTest, TestQuerySelectionUsesStatisticsWithProjectedSchema) { + for (StatisticsMode mode : {StatisticsMode::FULL, StatisticsMode::NONE}) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr store, CreateStore(mode)); + ASSERT_OK(store->Write( + RealtimeWriteBatch{MakeBatch(R"([[0,0,"a"],[1,1,"b"]])"), OffsetRange(0, 2)})); + ASSERT_OK(store->Write( + RealtimeWriteBatch{MakeBatch(R"([[2,5,"c"],[3,10,"d"]])"), OffsetRange(2, 4)})); + ASSERT_OK_AND_ASSIGN(std::shared_ptr view, store->AcquireReadView()); + // Read field positions differ from those in the stored statistics. + auto projected = arrow::schema({schema_->field(1), schema_->field(0)}); + auto read_schema = MakeReadSchema(projected); + auto predicate = + PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{7})); + auto selection = std::make_shared(RoaringBitmap64::From({0, 2, 3})); + RealtimeQueryContext context{read_schema.get(), + predicate, + OffsetRange(0, 4), + 1024, + selection, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/false}; + ASSERT_OK_AND_ASSIGN(std::vector> readers, + store->CreateQueryReaders(view, context)); + ASSERT_EQ(readers.size(), 1); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(readers[0]))); + // Statistics prune whole batches; exact row filtering remains with the caller. + ASSERT_EQ(ReadIds(result), mode == StatisticsMode::FULL ? (std::vector{5, 10}) + : (std::vector{0, 5, 10})); + } +} + +TEST_F(ArrowRealtimeStoreTest, TestQuerySelectionAndDeletionsRemainIndependent) { + for (bool spill : {false, true}) { + std::unique_ptr directory = UniqueTestDirectory::Create(); + ASSERT_NE(nullptr, directory); + ArrowRealtimeStoreFactory factory; + RealtimeStoreCreateRequest request{ + MakeReadSchema(schema_), + {{Options::REALTIME_SPILL_ENABLED, spill ? "true" : "false"}}, + pool_, + RealtimeStoreMode::APPEND_ONLY, + StatisticsMode::NONE}; + request.temp_directory = directory->Str(); + request.file_system = directory->GetFileSystem(); + ASSERT_OK_AND_ASSIGN(std::shared_ptr store, + factory.Create(std::move(request))); + ASSERT_OK(store->Write( + RealtimeWriteBatch{MakeBatch(R"([[10,10,"a"],[20,20,"b"],[30,30,"c"],[40,40,"d"], + [50,50,"e"],[60,60,"f"],[70,70,"g"],[80,80,"h"]])"), + OffsetRange(10, 81)})); + ASSERT_OK_AND_ASSIGN(std::optional> segment, + store->SealForCommit()); + ASSERT_TRUE(segment.has_value()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr view, store->AcquireReadView()); + auto deletions = std::make_shared(RoaringBitmap64::From({20, 60, 99})); + struct TestCase { + std::shared_ptr selection; + std::vector expected; + }; + const std::vector cases{ + {nullptr, {10, 30, 40, 50, 70, 80}}, + {std::make_shared(), {}}, + {std::make_shared(RoaringBitmap64::From({30, 999})), {30}}, + {std::make_shared(RoaringBitmap64::From({35, 999})), {}}, + {std::make_shared(RoaringBitmap64::From({20})), {}}, + {std::make_shared(RoaringBitmap64::From({10, 20, 30, 50, 60, 70})), + {10, 30, 50, 70}}}; + for (const auto& test_case : cases) { + auto read_schema = MakeReadSchema(schema_); + RealtimeQueryContext context{read_schema.get(), + /*predicate=*/nullptr, + OffsetRange(0, 90), + 1024, + test_case.selection, + deletions, + /*enable_index_pruning=*/false}; + ASSERT_OK_AND_ASSIGN(std::vector> readers, + store->CreateQueryReaders(view, context)); + ASSERT_EQ(1, readers.size()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(readers[0]))); + if (test_case.expected.empty()) { + ASSERT_EQ(nullptr, result); + } else { + ASSERT_NE(nullptr, result); + ASSERT_EQ(ReadIds(result), test_case.expected); + } + } + ASSERT_EQ(*deletions, RoaringBitmap64::From({20, 60, 99})); + } +} + TEST_F(ArrowRealtimeStoreTest, TestFullStatisticsPrunesNonMatchingBatch) { ArrowRealtimeStoreFactory factory; std::unique_ptr write_schema = MakeReadSchema(schema_); @@ -500,8 +606,13 @@ TEST_F(ArrowRealtimeStoreTest, TestFullStatisticsPrunesNonMatchingBatch) { std::unique_ptr read_schema = MakeReadSchema(schema_); std::shared_ptr predicate = PredicateBuilder::GreaterThan( /*field_index=*/1, /*field_name=*/"id", FieldType::BIGINT, Literal(int64_t{5})); - RealtimeQueryContext context{read_schema.get(), predicate, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + RealtimeQueryContext context{read_schema.get(), + predicate, + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -514,9 +625,12 @@ TEST_F(ArrowRealtimeStoreTest, TestFullStatisticsPrunesNonMatchingBatch) { ASSERT_EQ(std::vector({11}), ReadIds(filtered)); std::unique_ptr unfiltered_read_schema = MakeReadSchema(schema_); - RealtimeQueryContext unfiltered_context{unfiltered_read_schema.get(), - /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + RealtimeQueryContext unfiltered_context{ + unfiltered_read_schema.get(), + /*predicate=*/nullptr, view->GetOffsetRange().value_or(OffsetRange(0, 0)), 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> unfiltered_readers, store->CreateQueryReaders(view, unfiltered_context)); ASSERT_EQ(1, unfiltered_readers.size()); @@ -542,8 +656,13 @@ TEST_F(ArrowRealtimeStoreTest, TestFullStatisticsPrunesNonMatchingTimeBatch) { std::unique_ptr read_schema = MakeReadSchema(schema_); std::shared_ptr predicate = PredicateBuilder::GreaterThan( /*field_index=*/2, /*field_name=*/"t", FieldType::TIME, Literal(FieldType::TIME, 1000)); - RealtimeQueryContext context{read_schema.get(), predicate, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + RealtimeQueryContext context{read_schema.get(), + predicate, + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -562,8 +681,13 @@ TEST_F(ArrowRealtimeStoreTest, TestMissingStatisticsRetainsNonMatchingBatch) { std::unique_ptr read_schema = MakeReadSchema(schema_); std::shared_ptr predicate = PredicateBuilder::GreaterThan( /*field_index=*/1, /*field_name=*/"id", FieldType::BIGINT, Literal(int64_t{5})); - RealtimeQueryContext context{read_schema.get(), predicate, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + RealtimeQueryContext context{read_schema.get(), + predicate, + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store_->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -596,7 +720,13 @@ TEST_F(ArrowRealtimeStoreTest, TestHashIndexPinsHistoryAndUsesBothOffsetBounds) std::unique_ptr read_schema = MakeReadSchema(schema_); const std::shared_ptr key = PredicateBuilder::Equal(1, "id", FieldType::BIGINT, Literal(int64_t{1})); - RealtimeQueryContext old_context{read_schema.get(), key, OffsetRange(0, 100), 1024}; + RealtimeQueryContext old_context{read_schema.get(), + key, + OffsetRange(0, 100), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> old_readers, store->CreateQueryReaders(old_view, old_context)); ASSERT_EQ(1, old_readers.size()); @@ -607,7 +737,13 @@ TEST_F(ArrowRealtimeStoreTest, TestHashIndexPinsHistoryAndUsesBothOffsetBounds) read_schema = MakeReadSchema(schema_); const std::shared_ptr value = PredicateBuilder::Equal(2, "value", FieldType::STRING, Literal(FieldType::STRING, "a", 1)); - RealtimeQueryContext context{read_schema.get(), value, OffsetRange(11, 30), 1}; + RealtimeQueryContext context{read_schema.get(), + value, + OffsetRange(11, 30), + 1, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(current_view, context)); ASSERT_EQ(1, readers.size()); @@ -645,7 +781,13 @@ TEST_F(ArrowRealtimeStoreTest, TestQueryResultsWithOffsetBounds) { .ValueOrDie(); for (int32_t batch_size : {2, 1024}) { std::unique_ptr c_schema = MakeReadSchema(read_schema); - RealtimeQueryContext context{c_schema.get(), key, OffsetRange(11, 31), batch_size}; + RealtimeQueryContext context{c_schema.get(), + key, + OffsetRange(11, 31), + batch_size, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -684,7 +826,13 @@ TEST_F(ArrowRealtimeStoreTest, TestQueryBoundsExcludeUnreclaimedPrefixAndLaterWr std::unique_ptr read_schema = MakeReadSchema(schema_); const std::shared_ptr key = PredicateBuilder::Equal(1, "id", FieldType::BIGINT, Literal(int64_t{1})); - RealtimeQueryContext context{read_schema.get(), key, OffsetRange(14, 21)}; + RealtimeQueryContext context{read_schema.get(), + key, + OffsetRange(14, 21), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -730,7 +878,13 @@ TEST_F(ArrowRealtimeStoreTest, TestHashIndexRemainsInMemoryAcrossSpill) { std::unique_ptr read_schema = MakeReadSchema(schema_); const std::shared_ptr key = PredicateBuilder::Equal(1, "id", FieldType::BIGINT, Literal(int64_t{1})); - RealtimeQueryContext context{read_schema.get(), key, OffsetRange(11, 21), 1024}; + RealtimeQueryContext context{read_schema.get(), + key, + OffsetRange(11, 21), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -745,7 +899,12 @@ TEST_F(ArrowRealtimeStoreTest, TestRejectsHandlesFromAnotherStoreImplementation) std::unique_ptr read_schema = MakeReadSchema(schema_); RealtimeQueryContext context{read_schema.get(), - /*predicate=*/nullptr, OffsetRange(0, 0)}; + /*predicate=*/nullptr, + OffsetRange(0, 0), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_NOK_WITH_MSG(store_->CreateQueryReaders(std::make_shared(), context), "read view was not created by the Arrow real-time store"); diff --git a/src/paimon/core/realtime/index/hash/hash_realtime_index.cpp b/src/paimon/core/realtime/index/hash/hash_realtime_index.cpp index 5d830fecd..552d5e29e 100644 --- a/src/paimon/core/realtime/index/hash/hash_realtime_index.cpp +++ b/src/paimon/core/realtime/index/hash/hash_realtime_index.cpp @@ -38,6 +38,8 @@ #include "paimon/core/realtime/realtime_utils.h" #include "paimon/defs.h" #include "paimon/macros.h" +#include "paimon/predicate/function.h" +#include "paimon/predicate/leaf_predicate.h" #include "tbb/concurrent_vector.h" namespace paimon { @@ -51,6 +53,29 @@ HashRealtimeIndex::Reader::Reader(std::shared_ptr index const OffsetRange& offset_range) : index_(std::move(index)), offset_range_(offset_range) {} +std::optional HashRealtimeIndex::Reader::EstimateSelectivity( + const std::shared_ptr& predicate) const { + const Function::Type type = predicate->GetFunction().GetType(); + if ((type != Function::Type::EQUAL && type != Function::Type::IN) || offset_range_.Empty()) { + return std::nullopt; + } + // Offsets can have gaps. The envelope is a scheduling hint, not an exact row count. + double count = 0.0; + for (const Literal& literal : predicate->Literals()) { + const auto posting = index_->postings_.Find(literal.HashCode()); + if (!posting) { + continue; + } + const Posting& entry = *posting.value(); + const auto end = + entry.offsets.cbegin() + entry.published_size.load(std::memory_order_acquire); + const auto first = std::lower_bound(entry.offsets.cbegin(), end, offset_range_.begin); + const auto last = std::lower_bound(first, end, offset_range_.end); + count += last - first; + } + return std::min(1.0, count / static_cast(offset_range_.end - offset_range_.begin)); +} + Result> HashRealtimeIndex::Reader::VisitEqual( const Literal& literal) { return VisitIn({literal}); diff --git a/src/paimon/core/realtime/index/hash/hash_realtime_index.h b/src/paimon/core/realtime/index/hash/hash_realtime_index.h index 02cebe059..44ae0b2da 100644 --- a/src/paimon/core/realtime/index/hash/hash_realtime_index.h +++ b/src/paimon/core/realtime/index/hash/hash_realtime_index.h @@ -62,6 +62,9 @@ class HashRealtimeIndex final : public RealtimeIndex, public: Reader(std::shared_ptr index, const OffsetRange& offset_range); + std::optional EstimateSelectivity( + const std::shared_ptr& predicate) const override; + Result> VisitEqual(const Literal& literal) override; Result> VisitIn( const std::vector& literals) override; diff --git a/src/paimon/core/realtime/index/realtime_index_reader.cpp b/src/paimon/core/realtime/index/realtime_index_reader.cpp index c51c886a3..9ee217280 100644 --- a/src/paimon/core/realtime/index/realtime_index_reader.cpp +++ b/src/paimon/core/realtime/index/realtime_index_reader.cpp @@ -25,6 +25,21 @@ namespace paimon { +Result> RealtimeIndexReader::VisitVectorSearch( + const std::shared_ptr&) { + return Status::NotImplemented("Real-time index does not support vector search"); +} + +Result> RealtimeIndexReader::VisitFullTextSearch( + const std::shared_ptr&) { + return Status::NotImplemented("Real-time index does not support full-text search"); +} + +Result> RealtimeIndexReader::VisitFullTextMatch( + const std::shared_ptr&, bool) { + return Status::NotImplemented("Real-time index does not support full-text match"); +} + Result> RealtimeIndexReader::VisitIsNotNull() { return std::optional(); } diff --git a/src/paimon/core/realtime/index/realtime_index_set.cpp b/src/paimon/core/realtime/index/realtime_index_set.cpp index eadd5ba3c..7d5ef3acb 100644 --- a/src/paimon/core/realtime/index/realtime_index_set.cpp +++ b/src/paimon/core/realtime/index/realtime_index_set.cpp @@ -171,6 +171,16 @@ std::vector> RealtimeIndexSet::FindIndexes( return result; } +std::vector> RealtimeIndexSet::FindIndexes( + const std::string& field_name) const { + std::vector> result; + const auto range = indexes_.equal_range(field_name); + for (auto iter = range.first; iter != range.second; ++iter) { + result.push_back(iter->second.index); + } + return result; +} + uint64_t RealtimeIndexSet::GetMemoryUsage() const { uint64_t bytes = 0; for (const auto& item : indexes_) { diff --git a/src/paimon/core/realtime/index/realtime_index_set.h b/src/paimon/core/realtime/index/realtime_index_set.h index dde885a6c..61d214245 100644 --- a/src/paimon/core/realtime/index/realtime_index_set.h +++ b/src/paimon/core/realtime/index/realtime_index_set.h @@ -60,6 +60,8 @@ class RealtimeIndexSet { std::vector> FindIndexes(const std::string& field_name, FieldType field_type) const; + std::vector> FindIndexes(const std::string& field_name) const; + uint64_t GetMemoryUsage() const; private: diff --git a/src/paimon/core/realtime/index/realtime_index_test.cpp b/src/paimon/core/realtime/index/realtime_index_test.cpp index 799e27c83..8ae7152a3 100644 --- a/src/paimon/core/realtime/index/realtime_index_test.cpp +++ b/src/paimon/core/realtime/index/realtime_index_test.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "arrow/api.h" @@ -37,6 +38,7 @@ #include "paimon/common/utils/scope_guard.h" #include "paimon/core/realtime/index/realtime_index_evaluator.h" #include "paimon/core/realtime/index/realtime_index_set.h" +#include "paimon/file_index/file_index_reader.h" #include "paimon/memory/memory_pool.h" #include "paimon/predicate/predicate_builder.h" #include "paimon/realtime/index/realtime_index_factory.h" @@ -46,6 +48,9 @@ namespace paimon::test { namespace { +static_assert(std::is_same_v, + "File and real-time readers must share the same search capability type"); + class RealtimeIndexTest : public testing::Test { public: void SetUp() override { diff --git a/src/paimon/core/realtime/precise_realtime_index_read.cpp b/src/paimon/core/realtime/precise_realtime_index_read.cpp new file mode 100644 index 000000000..4b02f4d21 --- /dev/null +++ b/src/paimon/core/realtime/precise_realtime_index_read.cpp @@ -0,0 +1,462 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/core/realtime/precise_realtime_index_read.h" + +#include +#include +#include +#include +#include +#include + +#include "arrow/api.h" +#include "arrow/c/bridge.h" +#include "arrow/c/helpers.h" +#include "fmt/format.h" +#include "paimon/common/index/index_reader_utils.h" +#include "paimon/common/reader/complete_row_kind_batch_reader.h" +#include "paimon/common/reader/concat_batch_reader.h" +#include "paimon/common/reader/index_score_batch_utils.h" +#include "paimon/common/reader/predicate_batch_reader.h" +#include "paimon/common/reader/reader_utils.h" +#include "paimon/common/table/special_fields.h" +#include "paimon/common/types/data_field.h" +#include "paimon/common/utils/arrow/status_utils.h" +#include "paimon/common/utils/checked_cast.h" +#include "paimon/common/utils/scope_guard.h" +#include "paimon/core/operation/internal_read_context.h" +#include "paimon/core/operation/precise_index_evaluator.h" +#include "paimon/core/realtime/realtime_reader.h" +#include "paimon/core/realtime/realtime_schema_layout.h" +#include "paimon/core/realtime/realtime_store_read_pipeline.h" +#include "paimon/core/schema/table_schema.h" +#include "paimon/predicate/full_text_match.h" +#include "paimon/predicate/predicate_utils.h" +#include "paimon/realtime/index/realtime_index_read_source.h" + +namespace paimon { +namespace { + +using Evaluator = PreciseIndexEvaluator; +using Evaluation = Evaluator::Evaluation; +// Outer map: output score column name (e.g. _embedding_INDEX_SCORE) -> scores by real-time offset. +// Inner map: real-time offset -> search score for that row. +using OutputScores = std::map>; + +/// Completes virtual scores and restores projection while offsets are still available. +class RealtimeScoreBatchReader final : public BatchReader { + public: + RealtimeScoreBatchReader(std::unique_ptr reader, + std::shared_ptr output_schema, OutputScores scores, + const std::shared_ptr& arrow_pool) + : reader_(std::move(reader)), + output_schema_(std::move(output_schema)), + scores_(std::move(scores)), + arrow_pool_(arrow_pool) {} + + Result NextBatch() override { + PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, NextBatchWithBitmap()); + return ReaderUtils::ApplyBitmapToReadBatch(std::move(batch), arrow_pool_); + } + + Result NextBatchWithBitmap() override { + PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, reader_->NextBatchWithBitmap()); + if (IsEofBatch(batch)) { + return batch; + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( + std::shared_ptr imported, + arrow::ImportArray(batch.first.first.get(), batch.first.second.get())); + if (!imported || imported->type_id() != arrow::Type::STRUCT) { + return Status::Invalid("Real-time score reader requires struct batches"); + } + auto data = checked_pointer_cast(imported); + auto offsets = std::dynamic_pointer_cast( + data->GetFieldByName(SpecialFields::RealtimeOffset().Name())); + if (!offsets || offsets->null_count() != 0) { + return Status::Invalid("Real-time score reader requires non-null offsets"); + } + return IndexScoreBatchUtils::CompleteScores( + std::move(batch), data, output_schema_, scores_, + [&offsets](const std::string&, const std::map& scores, int64_t row, + size_t) -> Result { + auto found = scores.find(offsets->Value(row)); + if (found == scores.end()) { + return Status::Invalid( + "Search score is missing for a selected real-time offset"); + } + return found->second; + }, + arrow_pool_); + } + + std::shared_ptr GetReaderMetrics() const override { + return reader_->GetReaderMetrics(); + } + + void Close() override { + reader_->Close(); + } + + private: + std::unique_ptr reader_; + std::shared_ptr output_schema_; + OutputScores scores_; + std::shared_ptr arrow_pool_; +}; + +class RealtimeSourceRead { + public: + RealtimeSourceRead(const std::shared_ptr& context, + const std::shared_ptr& store, + const std::shared_ptr& view, + const std::shared_ptr& source, + const RealtimeSchemaLayout& layout) + : context_(context), store_(store), view_(view), source_(source), layout_(layout) {} + + Status Prepare(const std::shared_ptr& predicate) { + return Evaluator::Prepare( + predicate, [this](const std::shared_ptr& leaf) -> Status { + PAIMON_ASSIGN_OR_RAISE( + [[maybe_unused]] std::vector> readers, + GetReaders(leaf->FieldName())); + if (leaf->GetFunction().GetType() == Function::Type::FULL_TEXT_MATCH) { + PAIMON_ASSIGN_OR_RAISE( + [[maybe_unused]] std::shared_ptr reader, + GetSearchReader(leaf->FieldName(), + RealtimeIndexReader::SearchType::FULL_TEXT_MATCH)); + } + return Status::OK(); + }); + } + + Status PrepareSearch() { + if (context_->GetVectorSearch()) { + PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] std::shared_ptr reader, + GetSearchReader(context_->GetVectorSearch()->field_name, + RealtimeIndexReader::SearchType::VECTOR_SEARCH)); + } + if (context_->GetFullTextSearch()) { + PAIMON_ASSIGN_OR_RAISE( + [[maybe_unused]] std::shared_ptr reader, + GetSearchReader(context_->GetFullTextSearch()->field_name, + RealtimeIndexReader::SearchType::FULL_TEXT_SEARCH)); + } + return Status::OK(); + } + + Result> CreateReader() { + PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 candidates, source_->CreateCandidates()); + Evaluator evaluator( + [this](const std::shared_ptr& leaf, const RoaringBitmap64& rows) { + return EvaluateLeaf(leaf, rows); + }, + [this](const std::shared_ptr& leaf) { + return EstimateSelectivity(leaf); + }); + PAIMON_ASSIGN_OR_RAISE(Evaluation evaluated, + evaluator.Evaluate(context_->GetPredicate(), candidates)); + if (evaluated.rows.IsEmpty()) { + return std::unique_ptr(); + } + PAIMON_RETURN_NOT_OK(ApplyRootSearch(&evaluated)); + if (evaluated.rows.IsEmpty()) { + return std::unique_ptr(); + } + + using AlignedScores = std::map>; + PAIMON_ASSIGN_OR_RAISE( + AlignedScores aligned_scores, + Evaluator::CreateOutputScores(evaluated, context_->GetIndexScoreBindings(), + context_->GetReadSchema())); + OutputScores scores; + for (const auto& [field, aligned] : aligned_scores) { + auto& output = scores[field]; + size_t index = 0; + for (auto iter = evaluated.rows.Begin(); iter != evaluated.rows.End(); ++iter) { + output.emplace(*iter, aligned[index++]); + } + } + arrow::FieldVector fields; + for (const auto& field : context_->GetReadSchema()->fields()) { + if (!SpecialFields::ParseIndexScoreFieldName(field->name())) { + fields.push_back(field); + } + } + if (context_->GetReadSchema()->GetFieldIndex(OffsetField()->name()) < 0) { + fields.push_back(OffsetField()); + } + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, + ReadLogical(arrow::schema(fields), /*predicate=*/nullptr, + std::make_shared(evaluated.rows))); + return std::unique_ptr(std::make_unique( + std::move(reader), context_->GetReadSchema(), std::move(scores), + context_->GetArrowMemoryPool())); + } + + Result>> GetReaders( + const std::string& field_name) { + auto found = readers_.find(field_name); + if (found == readers_.end()) { + PAIMON_ASSIGN_OR_RAISE(std::vector> readers, + source_->CreateIndexReaders(field_name)); + for (const auto& reader : readers) { + if (!reader) { + return Status::Invalid("Real-time source returned a null index reader"); + } + } + found = readers_.emplace(field_name, std::move(readers)).first; + } + return found->second; + } + + Result> GetSearchReader( + const std::string& field_name, RealtimeIndexReader::SearchType type) { + PAIMON_ASSIGN_OR_RAISE(std::vector> readers, + GetReaders(field_name)); + return IndexReaderUtils::GetSearchReader( + readers, type, + fmt::format("Multiple real-time indexes support search on: {}", field_name), + fmt::format("No real-time index supports requested search on: {}", field_name)); + } + + private: + std::shared_ptr OffsetField() const { + return layout_.StoreWriteSchema()->GetFieldByName(SpecialFields::RealtimeOffset().Name()); + } + + Result> EstimateSelectivity(const std::shared_ptr& leaf) { + return IndexReaderUtils::EstimateSelectivity(leaf, this); + } + + Result EvaluateLeaf(const std::shared_ptr& leaf, + const RoaringBitmap64& candidates) { + bool needs_scores = context_->NeedsScores( + {InternalReadContext::IndexScoreSource::Type::FULL_TEXT_MATCH, leaf->FieldName()}); + return Evaluator::EvaluateLeaf( + leaf, leaf, candidates, needs_scores, + [this](const std::shared_ptr& predicate, const RoaringBitmap64& rows) { + return ScanScalar(predicate, rows); + }, + this); + } + + Result ScanScalar(const std::shared_ptr& leaf, + const RoaringBitmap64& candidates) const { + auto field = context_->GetReadSchema()->GetFieldByName(leaf->FieldName()); + if (!field) { + return Status::Invalid("Missing real-time scalar field: ", leaf->FieldName()); + } + arrow::FieldVector fields{field}; + if (field->name() != OffsetField()->name()) { + fields.push_back(OffsetField()); + } + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, + ReadLogical(arrow::schema(fields), leaf, + std::make_shared(candidates))); + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr filtered, + PredicateBatchReader::Create(std::move(reader), leaf, context_->GetArrowMemoryPool())); + return CollectOffsets(std::move(filtered)); + } + + Status ApplyRootSearch(Evaluation* evaluated) { + const auto& vector = context_->GetVectorSearch(); + const auto& text = context_->GetFullTextSearch(); + const std::string field = vector ? vector->field_name : text ? text->field_name : ""; + return Evaluator::ApplyRootSearch(vector, text, field, this, + evaluated); + } + + Result> ReadLogical( + const std::shared_ptr& schema, + const std::shared_ptr& predicate, + const std::shared_ptr& selection) const { + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr pipeline, + RealtimeStoreReadPipeline::Create(schema, layout_, context_->GetMemoryPool(), + context_->GetArrowMemoryPool())); + ArrowSchema c_schema; + ArrowSchemaMarkReleased(&c_schema); + PAIMON_RETURN_NOT_OK_FROM_ARROW( + arrow::ExportSchema(*pipeline->StoreReadSchema(), &c_schema)); + ScopeGuard schema_guard([&]() { ArrowSchemaRelease(&c_schema); }); + std::shared_ptr store_predicate; + if (predicate) { + const auto& field_name = predicate->FieldName(); + auto logical_field = schema->GetFieldByName(field_name); + auto physical_field = pipeline->StoreReadSchema()->GetFieldByName(field_name); + auto stored_field = layout_.StoreWriteSchema()->GetFieldByName(field_name); + // Conversion plans may change the value representation. Only push predicates on + // fields whose logical, requested physical and stored types agree. + if (logical_field && physical_field && stored_field && + logical_field->type()->Equals(physical_field->type()) && + logical_field->type()->Equals(stored_field->type())) { + PAIMON_ASSIGN_OR_RAISE( + store_predicate, + PredicateUtils::CreatePickedFieldFilter( + predicate, + {{field_name, pipeline->StoreReadSchema()->GetFieldIndex(field_name)}})); + } + } + RealtimeQueryContext query{&c_schema, + store_predicate, + source_->GetOffsetRange(), + context_->GetCoreOptions().GetReadBatchSize(), + selection, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/false}; + PAIMON_ASSIGN_OR_RAISE(std::vector> readers, + store_->CreateQueryReaders(view_, query)); + ScopeGuard close([&]() { + for (const auto& reader : readers) { + if (reader) { + reader->Close(); + } + } + }); + for (auto& reader : readers) { + if (!reader) { + return Status::Invalid("Real-time index source returned a null data reader"); + } + PAIMON_ASSIGN_OR_RAISE(reader, + pipeline->Wrap(std::move(reader), source_->GetOffsetRange())); + } + std::unique_ptr result = + std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); + close.Release(); + return result; + } + + Result CollectOffsets(std::unique_ptr reader) const { + ScopeGuard close([&]() { reader->Close(); }); + RoaringBitmap64 offsets; + const OffsetRange range = source_->GetOffsetRange(); + while (true) { + PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap batch, + reader->NextBatchWithBitmap()); + if (BatchReader::IsEofBatch(batch)) { + break; + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( + std::shared_ptr imported, + arrow::ImportArray(batch.first.first.get(), batch.first.second.get())); + if (!imported || imported->type_id() != arrow::Type::STRUCT) { + return Status::Invalid("Real-time offset collection requires struct batches"); + } + auto data = checked_pointer_cast(imported); + auto values = std::dynamic_pointer_cast( + data->GetFieldByName(OffsetField()->name())); + if (!values || values->null_count() != 0) { + return Status::Invalid("Real-time offset collection requires non-null offsets"); + } + for (auto iter = batch.second.Begin(); iter != batch.second.End(); ++iter) { + if (*iter < 0 || *iter >= values->length()) { + return Status::Invalid("Invalid real-time batch row position"); + } + const int64_t offset = values->Value(*iter); + if (offset < range.begin || offset >= range.end) { + return Status::Invalid( + "Real-time source returned an offset outside its bounds"); + } + offsets.Add(offset); + } + } + return offsets; + } + + std::shared_ptr context_; + std::shared_ptr store_; + std::shared_ptr view_; + std::shared_ptr source_; + const RealtimeSchemaLayout& layout_; + std::map>> readers_; +}; + +} // namespace + +Result> PreciseRealtimeIndexRead::CreateReader( + const std::shared_ptr& context, RealtimeStoreMode mode, + const std::shared_ptr& store, const std::shared_ptr& view, + const OffsetRange& offset_range) { + if (mode == RealtimeStoreMode::PRIMARY_KEY) { + return Status::NotImplemented( + "Precise real-time search does not support primary-key merge"); + } + if (!store) { + return Status::Invalid("Precise real-time read requires a store"); + } + auto indexed = std::dynamic_pointer_cast(view); + if (!indexed) { + return Status::NotImplemented("Real-time store has no precise index read capability"); + } + auto table_schema = + DataField::ConvertDataFieldsToArrowSchema(context->GetTableSchema()->Fields()); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr layout, + RealtimeSchemaLayout::Create(mode, table_schema)); + PAIMON_ASSIGN_OR_RAISE(std::vector> sources, + indexed->CreateIndexReadSources(offset_range)); + std::vector> plans; + std::vector source_ranges; + for (const auto& source : sources) { + if (!source) { + return Status::Invalid("Real-time view returned a null index source"); + } + const OffsetRange range = source->GetOffsetRange(); + if (range.begin < offset_range.begin || range.end > offset_range.end || + range.begin >= range.end) { + return Status::Invalid("Real-time index source has invalid offset bounds"); + } + source_ranges.push_back(range); + auto plan = std::make_unique(context, store, view, source, *layout); + PAIMON_RETURN_NOT_OK(plan->Prepare(context->GetPredicate())); + PAIMON_RETURN_NOT_OK(plan->PrepareSearch()); + plans.push_back(std::move(plan)); + } + std::sort( + source_ranges.begin(), source_ranges.end(), + [](const OffsetRange& left, const OffsetRange& right) { return left.begin < right.begin; }); + for (size_t i = 1; i < source_ranges.size(); ++i) { + if (source_ranges[i - 1].end > source_ranges[i].begin) { + return Status::Invalid("Real-time index sources have overlapping offset bounds"); + } + } + std::vector> readers; + ScopeGuard close([&]() { + for (const auto& reader : readers) { + reader->Close(); + } + }); + for (const auto& plan : plans) { + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, plan->CreateReader()); + if (reader) { + readers.push_back(std::move(reader)); + } + } + std::unique_ptr result = + std::make_unique(std::move(readers), context->GetArrowMemoryPool()); + close.Release(); + result = std::make_unique(std::move(result), + context->GetArrowMemoryPool()); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr pinned, + RealtimeReader::Create(view, std::move(result))); + return std::unique_ptr(std::move(pinned)); +} + +} // namespace paimon diff --git a/src/paimon/core/realtime/precise_realtime_index_read.h b/src/paimon/core/realtime/precise_realtime_index_read.h new file mode 100644 index 000000000..9ef9f51b6 --- /dev/null +++ b/src/paimon/core/realtime/precise_realtime_index_read.h @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + +#include "paimon/reader/batch_reader.h" +#include "paimon/realtime/realtime_store.h" + +namespace paimon { +class InternalReadContext; + +/// Executes the same precise predicate/search plan as file reads, using pinned real-time offsets. +/// TopK is source-local, matching the existing file-local search contract. +class PreciseRealtimeIndexRead { + public: + PreciseRealtimeIndexRead() = delete; + ~PreciseRealtimeIndexRead() = delete; + + static Result> CreateReader( + const std::shared_ptr& context, RealtimeStoreMode mode, + const std::shared_ptr& store, const std::shared_ptr& view, + const OffsetRange& offset_range); +}; + +} // namespace paimon diff --git a/src/paimon/core/realtime/precise_realtime_index_read_test.cpp b/src/paimon/core/realtime/precise_realtime_index_read_test.cpp new file mode 100644 index 000000000..ede5191bd --- /dev/null +++ b/src/paimon/core/realtime/precise_realtime_index_read_test.cpp @@ -0,0 +1,495 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/core/realtime/precise_realtime_index_read.h" + +#include +#include +#include +#include +#include +#include + +#include "arrow/api.h" +#include "arrow/c/bridge.h" +#include "arrow/c/helpers.h" +#include "arrow/ipc/json_simple.h" +#include "paimon/common/index/index_reader_utils.h" +#include "paimon/common/table/special_fields.h" +#include "paimon/common/types/data_field.h" +#include "paimon/common/types/row_kind.h" +#include "paimon/common/utils/arrow/mem_utils.h" +#include "paimon/common/utils/arrow/status_utils.h" +#include "paimon/common/utils/checked_cast.h" +#include "paimon/common/utils/scope_guard.h" +#include "paimon/core/operation/internal_read_context.h" +#include "paimon/core/realtime/arrow_deduplicate_realtime_store.h" +#include "paimon/core/realtime/arrow_realtime_store.h" +#include "paimon/core/realtime/realtime_context_impl.h" +#include "paimon/core/realtime/realtime_schema_layout.h" +#include "paimon/core/schema/table_schema.h" +#include "paimon/core/table/source/append_only_table_read.h" +#include "paimon/core/table/source/realtime_split.h" +#include "paimon/factories/factory_creator.h" +#include "paimon/fs/local/local_file_system.h" +#include "paimon/memory/memory_pool.h" +#include "paimon/predicate/full_text_match.h" +#include "paimon/predicate/full_text_search.h" +#include "paimon/predicate/predicate_builder.h" +#include "paimon/predicate/vector_search.h" +#include "paimon/read_context.h" +#include "paimon/record_batch.h" +#include "paimon/testing/mock/mock_search_realtime_index.h" +#include "paimon/testing/utils/read_result_collector.h" +#include "paimon/testing/utils/test_helper.h" +#include "paimon/testing/utils/testharness.h" + +namespace paimon::test { +namespace { + +class PreciseRealtimeIndexReadTest : public testing::Test { + public: + void SetUp() override { + user_schema_ = arrow::schema({arrow::field("id", arrow::int64(), false), + arrow::field("category", arrow::int32()), + arrow::field("text", arrow::utf8()), + arrow::field("embedding", arrow::list(arrow::float32()))}); + ASSERT_OK_AND_ASSIGN(std::unique_ptr schema, + TableSchema::Create(0, user_schema_, {}, {}, {})); + ASSERT_OK_AND_ASSIGN(std::unique_ptr layout, + RealtimeSchemaLayout::Create( + RealtimeStoreMode::APPEND_ONLY, + DataField::ConvertDataFieldsToArrowSchema(schema->Fields()))); + write_schema_ = layout->StoreWriteSchema(); + state_ = std::make_shared(); + state_->ranked = {{10, 0.9f}, {20, 0.8f}, {30, 0.7f}, {40, 0.6f}}; + using SearchType = IndexReaderCapabilities::SearchType; + const std::vector> index_types{ + {"precise-mock-vector", SearchType::VECTOR_SEARCH}, + {"precise-mock-text", SearchType::FULL_TEXT_SEARCH}}; + for (const auto& [identifier, search_type] : index_types) { + auto factory = + std::make_unique(identifier, search_type, state_); + FactoryCreator::GetInstance()->Register( + identifier + RealtimeIndexFactory::REALTIME_INDEX_IDENTIFIER_SUFFIX, + factory.release()); + } + ASSERT_OK_AND_ASSIGN(store_, CreateStore("", nullptr)); + ASSERT_OK_AND_ASSIGN(realtime_context_, RealtimeContext::Create()); + } + + void TearDown() override { + for (const std::string identifier : {"precise-mock-vector", "precise-mock-text"}) { + FactoryCreator::GetInstance()->TEST_Unregister( + identifier + RealtimeIndexFactory::REALTIME_INDEX_IDENTIFIER_SUFFIX); + } + } + + Result> CreateStore( + const std::string& temp, const std::shared_ptr& fs) const { + return ArrowRealtimeStore::Create( + write_schema_, RealtimeStoreMode::APPEND_ONLY, StatisticsMode::NONE, temp, fs, "zstd", + 1, + {{"realtime.index.hash.columns", "category"}, + {"realtime.index.precise-mock-vector.columns", "embedding"}, + {"realtime.index.precise-mock-text.columns", "text"}}, + pool_, arrow_pool_); + } + + std::shared_ptr MakeBatch(const std::string& json) const { + return checked_pointer_cast( + arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(write_schema_->fields()), json) + .ValueOrDie()); + } + + Status Write(const std::string& json, const OffsetRange& range) const { + auto data = MakeBatch(json); + ArrowArray array; + ArrowArrayMarkReleased(&array); + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportArray(*data, &array)); + ScopeGuard release([&]() { ArrowArrayRelease(&array); }); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr batch, + RecordBatchBuilder(&array).Finish()); + return store_->Write({std::move(batch), range}); + } + + Result> Context( + const std::vector& fields, const std::shared_ptr& predicate, + const std::shared_ptr& vector, const std::shared_ptr& text, + RealtimeStoreMode mode) const { + std::map options{{Options::READ_BATCH_SIZE, "1"}}; + if (mode == RealtimeStoreMode::DEDUPLICATE) { + options["realtime.deduplicate-key-fields"] = "id"; + } + arrow::FieldVector table_fields = user_schema_->fields(); + if (mode == RealtimeStoreMode::DEDUPLICATE) { + table_fields.insert(table_fields.begin(), + arrow::field("_REALTIME_OFFSET", arrow::int64(), false)); + } + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr schema, + TableSchema::Create(0, arrow::schema(table_fields), {}, {}, options)); + ReadContextBuilder builder(directory_->Str()); + builder.SetReadFieldNames(fields) + .SetPredicate(predicate) + .SetVectorSearch(vector) + .SetFullTextSearch(text) + .EnablePreciseFileIndexEvaluate(true) + .EnablePredicateFilter(false) + .WithRealtimeContext(realtime_context_) + .AddOption(Options::READ_BATCH_SIZE, "1"); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr read_context, builder.Finish()); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr internal, + InternalReadContext::Create(read_context, schema, options)); + return internal; + } + + std::shared_ptr Vector(int32_t limit) const { + return std::make_shared("embedding", limit, std::vector{1.0f}, nullptr, + nullptr, VectorSearch::DistanceType::COSINE, + std::map{}); + } + + std::shared_ptr Category(int32_t category) const { + return PredicateBuilder::Equal(1, "category", FieldType::INT, Literal(category)); + } + + Result> Read( + const std::shared_ptr& context, + const std::shared_ptr& view, const OffsetRange& range, + RealtimeStoreMode mode) const { + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr reader, + PreciseRealtimeIndexRead::CreateReader( + context, mode, query_store_ ? query_store_ : store_, view, range)); + return ReadResultCollector::CollectResult(std::move(reader)); + } + + void CheckResult(const std::shared_ptr& context, + const std::shared_ptr& result, + const std::string& json) const { + // Match ReadResultCollector's nullable fields after dictionary decoding. + arrow::FieldVector fields; + for (const auto& field : context->GetReadSchema()->fields()) { + fields.push_back(arrow::field(field->name(), field->type())); + } + auto data_result = arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(fields), json); + ASSERT_TRUE(data_result.ok()) << data_result.status().ToString(); + auto data = checked_pointer_cast(data_result.ValueOrDie()); + if (data->length() == 0) { + ASSERT_EQ(result, nullptr); + return; + } + ASSERT_TRUE(result); + auto row_kind_result = arrow::MakeArrayFromScalar( + arrow::Int8Scalar(RowKind::Insert()->ToByteValue()), data->length(), arrow_pool_.get()); + ASSERT_TRUE(row_kind_result.ok()) << row_kind_result.status().ToString(); + arrow::ArrayVector arrays{row_kind_result.ValueOrDie()}; + arrays.insert(arrays.end(), data->fields().begin(), data->fields().end()); + fields.insert(fields.begin(), + arrow::field(SpecialFields::ValueKind().Name(), arrow::int8())); + auto expected_array = + std::make_shared(arrow::struct_(fields), data->length(), arrays); + auto expected = std::make_shared(expected_array); + ASSERT_TRUE(result->Equals(expected)) + << "expected: " << expected->ToString() << "\nactual: " << result->ToString(); + } + + protected: + std::shared_ptr pool_ = GetDefaultPool(); + std::shared_ptr arrow_pool_ = GetArrowPool(pool_); + std::unique_ptr directory_ = UniqueTestDirectory::Create(); + std::shared_ptr user_schema_; + std::shared_ptr write_schema_; + std::shared_ptr store_; + std::shared_ptr query_store_; + std::shared_ptr realtime_context_; + std::shared_ptr state_; +}; + +TEST_F(PreciseRealtimeIndexReadTest, FullTextMatchScoresAreOptional) { + using MatchResult = + IndexReaderUtils::FullTextResult; + // Scores must follow offset order even when the index ranks matches differently. + state_->ranked = {{30, 0.9f}, {10, 0.8f}, {20, 0.7f}}; + auto reader = std::make_shared( + IndexReaderCapabilities::SearchType::FULL_TEXT_SEARCH, OffsetRange(10, 31), + RoaringBitmap64::From({10, 20, 30}), state_); + auto match = + std::make_shared("text", "all", RoaringBitmap64::From({10, 20, 30})); + auto candidates = RoaringBitmap64::From({10, 30}); + ASSERT_OK_AND_ASSIGN(MatchResult bitmap, + IndexReaderUtils::VisitFullTextMatch(reader, match, candidates, false)); + ASSERT_EQ(bitmap.rows, candidates); + ASSERT_EQ(bitmap.scores, nullptr); + ASSERT_OK_AND_ASSIGN(MatchResult scored, + IndexReaderUtils::VisitFullTextMatch(reader, match, candidates, true)); + ASSERT_EQ(scored.rows, candidates); + ASSERT_NE(scored.scores, nullptr); + ASSERT_EQ(scored.scores->GetScores(), (std::vector{0.8f, 0.9f})); + ASSERT_EQ(*match->include_row_ids, RoaringBitmap64::From({10, 20, 30})); +} + +TEST_F(PreciseRealtimeIndexReadTest, EmptyFullTextMatchPreservesRequestedScoreType) { + using MatchResult = + IndexReaderUtils::FullTextResult; + auto reader = std::make_shared( + IndexReaderCapabilities::SearchType::FULL_TEXT_SEARCH, OffsetRange(10, 41), + RoaringBitmap64::From({10, 20, 30, 40}), state_); + auto match = std::make_shared("text", "all", std::nullopt); + ASSERT_OK_AND_ASSIGN(MatchResult bitmap, IndexReaderUtils::VisitFullTextMatch( + reader, match, RoaringBitmap64(), false)); + ASSERT_EQ(bitmap.rows.Cardinality(), 0); + ASSERT_EQ(bitmap.scores, nullptr); + ASSERT_OK_AND_ASSIGN(MatchResult scored, IndexReaderUtils::VisitFullTextMatch( + reader, match, RoaringBitmap64(), true)); + ASSERT_EQ(scored.rows.Cardinality(), 0); + ASSERT_NE(scored.scores, nullptr); + ASSERT_TRUE(scored.scores->GetScores().empty()); +} + +TEST_F(PreciseRealtimeIndexReadTest, FullTextMatchRejectsMissingRequestedScores) { + class UnscoredReader final : public RealtimeIndexReader { + public: + Result> VisitFullTextMatch( + const std::shared_ptr&, bool) override { + return RealtimeIndexResult::Create(RoaringBitmap64()); + } + }; + auto reader = std::make_shared(); + auto match = std::make_shared("text", "all", std::nullopt); + ASSERT_NOK_WITH_MSG( + IndexReaderUtils::VisitFullTextMatch(reader, match, RoaringBitmap64(), true), + "Full-text index did not return requested scores"); +} + +TEST_F(PreciseRealtimeIndexReadTest, AppendTableRechecksScalarBeforeVectorTopK) { + ASSERT_OK(Write(R"([[10,1,0,"a",[1]],[20,2,0,"b",[2]], + [30,3,1,"c",[3]],[40,4,1,"d",[4]]])", + OffsetRange(10, 41))); + ASSERT_OK_AND_ASSIGN(std::shared_ptr view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, + Context({"id", "category", "_embedding_INDEX_SCORE"}, Category(1), + Vector(2), nullptr, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime, + RealtimeContextImpl::Cast(realtime_context_)); + ASSERT_OK_AND_ASSIGN( + std::string ticket, + realtime->PinReadView({RealtimePartitionBucket({}, 0), store_, view, + RealtimeStoreMode::APPEND_ONLY, nullptr, write_schema_}, + 60000)); + auto split = std::make_shared( + RealtimeSplit::kCurrentVersion, std::nullopt, std::map{}, 0, + std::vector>{}, 0, 41, ticket); + AppendOnlyTableRead table_read(nullptr, context, pool_, nullptr); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, table_read.CreateReader(split)); + ASSERT_OK_AND_ASSIGN(auto result, ReadResultCollector::CollectResult(std::move(reader))); + CheckResult(context, result, R"([[3,1,0.7],[4,1,0.6]])"); + ASSERT_NOK(realtime->ResolveReadView(ticket)); +} + +TEST_F(PreciseRealtimeIndexReadTest, UnsupportedScalarVisitScansBeforeTopK) { + ASSERT_OK( + Write(R"([[10,1,0,"a",[1]],[20,2,0,"b",[2]],[30,3,1,"c",[3]]])", OffsetRange(10, 31))); + auto predicate = PredicateBuilder::GreaterThan(1, "category", FieldType::INT, Literal(0)); + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category"}, predicate, Vector(1), nullptr, + RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 31), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[3,1]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, + FullTextPredicateAndVectorScoresAreAlignedAfterScalarFiltering) { + ASSERT_OK( + Write(R"([[10,1,0,"a",[1]],[20,2,1,"b",[2]],[30,3,1,"c",[3]]])", OffsetRange(10, 31))); + state_->text_matches["a"] = RoaringBitmap64::From({10, 30}); + ASSERT_OK_AND_ASSIGN(auto text, + PredicateBuilder::FullTextMatch( + 2, std::make_shared("text", "a", std::nullopt))); + ASSERT_OK_AND_ASSIGN(auto predicate, PredicateBuilder::And({text, Category(1)})); + ASSERT_OK_AND_ASSIGN( + auto context, + Context({"id", "category", "text", "_text_INDEX_SCORE", "_embedding_INDEX_SCORE"}, + predicate, Vector(1), nullptr, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 31), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[3,1,"c",0.7,0.7]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, OrBranchesSeeTheSameCandidatesAndMissingIndexesScan) { + ASSERT_OK( + Write(R"([[10,1,0,"a",[1]],[20,2,0,"b",[2]],[30,3,0,"c",[3]]])", OffsetRange(10, 31))); + state_->text_matches["a"] = RoaringBitmap64::From({10, 30}); + state_->text_matches["b"] = RoaringBitmap64::From({20, 30}); + ASSERT_OK_AND_ASSIGN(auto a, + PredicateBuilder::FullTextMatch( + 1, std::make_shared("text", "a", std::nullopt))); + ASSERT_OK_AND_ASSIGN(auto b, + PredicateBuilder::FullTextMatch( + 1, std::make_shared("text", "b", std::nullopt))); + auto scalar = PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{2})); + ASSERT_OK_AND_ASSIGN(auto left, PredicateBuilder::And({a, scalar})); + ASSERT_OK_AND_ASSIGN(auto predicate, PredicateBuilder::Or({left, b})); + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "text"}, predicate, nullptr, nullptr, + RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 31), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[2,"b"],[3,"c"]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, FullTextTopKRespectsScalarAndIncludeOffsets) { + ASSERT_OK( + Write(R"([[10,1,0,"a",[1]],[20,2,1,"b",[2]],[30,3,1,"c",[3]]])", OffsetRange(10, 31))); + auto text = std::make_shared("text", "all", 1, RoaringBitmap64::From({10, 30})); + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category", "_text_INDEX_SCORE"}, Category(1), + nullptr, text, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 31), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[3,1,0.7]])"); + ASSERT_EQ(*text->include_row_ids, RoaringBitmap64::From({10, 30})); +} + +TEST_F(PreciseRealtimeIndexReadTest, LargeSparseOffsetsAndUserFilterRespectBothPinnedBounds) { + constexpr int64_t base = int64_t{1} << 40; + state_->ranked = {{base + 1, 0.9f}, {base + 100, 0.8f}, {base + 1000, 0.7f}}; + ASSERT_OK(Write(R"([[1099511627777,1,1,"a",[1]],[1099511627876,2,1,"b",[2]]])", + OffsetRange(base + 1, base + 101))); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK(Write(R"([[1099511628776,3,1,"c",[3]]])", OffsetRange(base + 1000, base + 1001))); + auto search = Vector(1); + search->pre_filter = [](int64_t offset) { return offset != base + 1; }; + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category"}, Category(1), search, nullptr, + RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto result, Read(context, view, OffsetRange(base + 1, base + 101), + RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[2,1]])"); + ASSERT_TRUE(search->pre_filter); + ASSERT_OK_AND_ASSIGN(auto bounded, Context({"id", "category"}, Category(1), Vector(1), nullptr, + RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto bounded_result, Read(bounded, view, OffsetRange(base + 2, base + 101), + RealtimeStoreMode::APPEND_ONLY)); + CheckResult(bounded, bounded_result, R"([[2,1]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, PinnedSourcesSurviveSealSpillAndReclamation) { + ASSERT_OK_AND_ASSIGN(store_, CreateStore(directory_->Str(), directory_->GetFileSystem())); + ASSERT_OK(Write(R"([[10,1,1,"a",[1]],[20,2,1,"b",[2]]])", OffsetRange(10, 21))); + ASSERT_OK_AND_ASSIGN(auto building_view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto segment, store_->SealForCommit()); + ASSERT_TRUE(segment); + ASSERT_OK_AND_ASSIGN(auto spilled_view, store_->AcquireReadView()); + ASSERT_OK(store_->AdvanceCommittedOffset(21)); + ASSERT_OK_AND_ASSIGN( + auto context, Context({"id"}, nullptr, Vector(1), nullptr, RealtimeStoreMode::APPEND_ONLY)); + for (const auto& view : {building_view, spilled_view}) { + ASSERT_OK_AND_ASSIGN( + auto result, Read(context, view, OffsetRange(0, 21), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[1]])"); + } +} + +TEST_F(PreciseRealtimeIndexReadTest, DeduplicateRemovesOldOffsetsBeforeTopKAndPinsVisibility) { + ASSERT_OK_AND_ASSIGN(auto context, + Context({"id", "category", "_embedding_INDEX_SCORE"}, nullptr, Vector(1), + nullptr, RealtimeStoreMode::DEDUPLICATE)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr layout, + RealtimeSchemaLayout::Create(RealtimeStoreMode::DEDUPLICATE, + DataField::ConvertDataFieldsToArrowSchema( + context->GetTableSchema()->Fields()))); + write_schema_ = layout->StoreWriteSchema(); + ASSERT_OK_AND_ASSIGN(store_, CreateStore("", nullptr)); + ASSERT_OK_AND_ASSIGN(auto dedup, ArrowDeduplicateRealtimeStore::Create( + write_schema_, {"id"}, store_, pool_, arrow_pool_)); + query_store_ = dedup; + auto first = MakeBatch(R"([[10,1,1,"old",[1]],[20,2,1,"other",[2]]])"); + ASSERT_OK(dedup->WriteAndLookup(first, + {RecordBatch::RowKind::INSERT, RecordBatch::RowKind::INSERT}, + OffsetRange(10, 21), RoaringBitmap64())); + ASSERT_OK_AND_ASSIGN(auto old_view, dedup->AcquireReadView()); + ASSERT_OK(dedup->WriteAndLookup(MakeBatch(R"([[30,1,1,"new",[3]]])"), + {RecordBatch::RowKind::INSERT}, OffsetRange(30, 31), + RoaringBitmap64())); + ASSERT_OK_AND_ASSIGN(auto new_view, dedup->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto old_result, Read(context, old_view, OffsetRange(0, 21), + RealtimeStoreMode::DEDUPLICATE)); + CheckResult(context, old_result, R"([[1,1,0.9]])"); + ASSERT_OK_AND_ASSIGN(auto new_result, Read(context, new_view, OffsetRange(0, 31), + RealtimeStoreMode::DEDUPLICATE)); + CheckResult(context, new_result, R"([[2,1,0.8]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, MissingSearchCapabilityFailsBeforeEmptyPredicateShortCircuit) { + ASSERT_OK(Write(R"([[10,1,1,"a",[1]]])", OffsetRange(10, 11))); + state_->supports_search = false; + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category"}, Category(999), Vector(1), + nullptr, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_NOK_WITH_MSG( + PreciseRealtimeIndexRead::CreateReader(context, RealtimeStoreMode::APPEND_ONLY, store_, + view, OffsetRange(0, 11)), + "No real-time index supports requested search"); +} + +TEST_F(PreciseRealtimeIndexReadTest, ScalarOnlyPreciseReadDoesNotRequireSearchCapability) { + ASSERT_OK(Write(R"([[10,1,0,"a",[1]],[20,2,1,"b",[2]]])", OffsetRange(10, 21))); + state_->supports_search = false; + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category"}, Category(1), nullptr, nullptr, + RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 21), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[2,1]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, EmptyScalarCandidatesReturnNoRows) { + ASSERT_OK(Write(R"([[10,1,1,"a",[1]]])", OffsetRange(10, 11))); + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "category"}, Category(999), Vector(1), + nullptr, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 11), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, "[]"); +} + +TEST_F(PreciseRealtimeIndexReadTest, EachPinnedSegmentExecutesItsOwnTopK) { + // A configured search hit must not be returned unless the segment indexed that offset. + state_->ranked.insert(state_->ranked.begin(), {15, 1.0f}); + ASSERT_OK(Write(R"([[10,1,1,"a",[1]],[20,2,1,"b",[2]]])", OffsetRange(10, 21))); + ASSERT_OK_AND_ASSIGN(auto segment, store_->SealForCommit()); + ASSERT_TRUE(segment); + ASSERT_OK(Write(R"([[30,3,1,"c",[3]],[40,4,1,"d",[4]]])", OffsetRange(30, 41))); + ASSERT_OK_AND_ASSIGN(auto context, Context({"id", "_embedding_INDEX_SCORE"}, nullptr, Vector(1), + nullptr, RealtimeStoreMode::APPEND_ONLY)); + ASSERT_OK_AND_ASSIGN(auto view, store_->AcquireReadView()); + ASSERT_OK_AND_ASSIGN(auto result, + Read(context, view, OffsetRange(0, 41), RealtimeStoreMode::APPEND_ONLY)); + CheckResult(context, result, R"([[1,0.9],[3,0.7]])"); +} + +TEST_F(PreciseRealtimeIndexReadTest, RejectsMisalignedScores) { + ASSERT_NOK_WITH_MSG(ScoredRealtimeIndexResult::Create(RoaringBitmap64::From({10, 20}), {1.0f}), + "score count"); +} + +} // namespace +} // namespace paimon::test diff --git a/src/paimon/core/realtime/primary_key_realtime_store_test.cpp b/src/paimon/core/realtime/primary_key_realtime_store_test.cpp index 6cc76a80e..647cd7ad4 100644 --- a/src/paimon/core/realtime/primary_key_realtime_store_test.cpp +++ b/src/paimon/core/realtime/primary_key_realtime_store_test.cpp @@ -221,7 +221,10 @@ TEST(PrimaryKeyRealtimeStoreTest, TestSpilledFileReturnsOneIndependentReaderPerS RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, view->GetOffsetRange().value_or(OffsetRange(0, 0)), - /*read_batch_size=*/1}; + /*read_batch_size=*/1, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> query_readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(2, query_readers.size()); @@ -303,7 +306,11 @@ TEST(PrimaryKeyRealtimeStoreTest, TestSlicedReadersExportZeroOffsets) { ASSERT_TRUE(arrow::ExportSchema(*schema, c_schema.get()).ok()); RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); AssertSlicedBatch(readers[0].get()); @@ -350,7 +357,11 @@ TEST(PrimaryKeyRealtimeStoreTest, TestReclaimKeepsReadView) { ASSERT_TRUE(arrow::ExportSchema(*StoreWriteSchema(), c_schema.get()).ok()); RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, - retained_view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + retained_view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(retained_view, context)); ASSERT_OK_AND_ASSIGN(std::shared_ptr actual, ReadArray(std::move(readers))); @@ -376,7 +387,11 @@ TEST(PrimaryKeyRealtimeStoreTest, TestQueryReaderPerStoredBatch) { ASSERT_TRUE(arrow::ExportSchema(*StoreWriteSchema(), c_schema.get()).ok()); RealtimeQueryContext context{/*read_schema=*/c_schema.get(), /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(2, readers.size()); @@ -400,8 +415,13 @@ TEST(PrimaryKeyRealtimeStoreTest, TestFullStatisticsPrunesNonMatchingStoredBatch ASSERT_TRUE(arrow::ExportSchema(*StoreWriteSchema(), c_schema.get()).ok()); std::shared_ptr predicate = PredicateBuilder::Equal( /*field_index=*/3, /*field_name=*/"id", FieldType::BIGINT, Literal(int64_t{10})); - RealtimeQueryContext context{c_schema.get(), predicate, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + RealtimeQueryContext context{c_schema.get(), + predicate, + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -431,7 +451,11 @@ TEST(PrimaryKeyRealtimeStoreTest, TestQueryBatchOutlivesStoreAndReader) { ASSERT_TRUE(arrow::ExportSchema(*stored_schema, c_schema.get()).ok()); RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_EQ(1, readers.size()); @@ -489,7 +513,11 @@ TEST(PrimaryKeyRealtimeStoreTest, TestQueryReaderProjectsNestedFields) { ASSERT_TRUE(arrow::ExportSchema(*requested_schema, c_schema.get()).ok()); RealtimeQueryContext context{c_schema.get(), /*predicate=*/nullptr, - view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; ASSERT_OK_AND_ASSIGN(std::vector> readers, store->CreateQueryReaders(view, context)); ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, readers[0]->NextBatch()); diff --git a/src/paimon/core/realtime/realtime_context_impl.cpp b/src/paimon/core/realtime/realtime_context_impl.cpp index fcff706e5..73f03c0a4 100644 --- a/src/paimon/core/realtime/realtime_context_impl.cpp +++ b/src/paimon/core/realtime/realtime_context_impl.cpp @@ -47,6 +47,7 @@ #include "paimon/core/core_options.h" #include "paimon/core/realtime/arrow_deduplicate_realtime_store.h" #include "paimon/core/realtime/realtime_deduplicate_state.h" +#include "paimon/core/realtime/realtime_schema_layout.h" #include "paimon/macros.h" #include "paimon/realtime/realtime_store.h" #include "paimon/status.h" @@ -67,6 +68,20 @@ std::string PartitionToString(const std::map& partitio } // namespace +Status RealtimePartitionBucketView::ValidateTableSchema( + const std::shared_ptr& table_schema) const { + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr layout, + RealtimeSchemaLayout::Create(mode, table_schema)); + if (!write_schema || + !write_schema->Equals(*layout->StoreWriteSchema(), /*check_metadata=*/true)) { + return Status::Invalid( + fmt::format("real-time store schema mismatch for partition {}, bucket {}; recreate the " + "RealtimeContext", + PartitionToString(partition_bucket.partition), partition_bucket.bucket)); + } + return Status::OK(); +} + struct RealtimeContextImpl::RefreshNotification { std::shared_ptr store; std::shared_ptr deduplicate_state; @@ -317,9 +332,9 @@ Result RealtimeContextImpl::AcquireReadState() { if (!read_view) { return Status::Invalid("real-time store returned a null read view"); } - result.views.push_back(RealtimePartitionBucketView{partition_bucket, store.store, - std::move(read_view), store.mode, - std::move(offset_deletions)}); + result.views.push_back(RealtimePartitionBucketView{ + partition_bucket, store.store, std::move(read_view), store.mode, + std::move(offset_deletions), store.write_schema}); } return result; } diff --git a/src/paimon/core/realtime/realtime_context_impl.h b/src/paimon/core/realtime/realtime_context_impl.h index 586ee87ab..9bfd726d6 100644 --- a/src/paimon/core/realtime/realtime_context_impl.h +++ b/src/paimon/core/realtime/realtime_context_impl.h @@ -35,6 +35,7 @@ #include "paimon/realtime/realtime_context.h" #include "paimon/realtime/realtime_store.h" #include "paimon/result.h" +#include "paimon/status.h" #include "paimon/visibility.h" struct ArrowSchema; @@ -67,6 +68,12 @@ struct RealtimePartitionBucketView { std::shared_ptr read_view; RealtimeStoreMode mode = RealtimeStoreMode::APPEND_ONLY; std::shared_ptr offset_deletions; + /// Full physical write schema fixed when the store was created. + std::shared_ptr write_schema; + + /// Checks the full table schema before opening readers or evaluating indexes. Query + /// projections are validated separately and do not change the expected store schema. + Status ValidateTableSchema(const std::shared_ptr& table_schema) const; }; struct RealtimeReadState { diff --git a/src/paimon/core/realtime/realtime_context_test.cpp b/src/paimon/core/realtime/realtime_context_test.cpp index 5a67ce8bc..2da687e41 100644 --- a/src/paimon/core/realtime/realtime_context_test.cpp +++ b/src/paimon/core/realtime/realtime_context_test.cpp @@ -41,6 +41,7 @@ #include "paimon/core/realtime/realtime_context_impl.h" #include "paimon/core/realtime/realtime_deduplicate_state.h" #include "paimon/core/realtime/realtime_offset_file_index_lookup.h" +#include "paimon/core/realtime/realtime_schema_layout.h" #include "paimon/fs/local/local_file_system.h" #include "paimon/memory/memory_pool.h" #include "paimon/realtime/arrow_realtime_store_factory.h" @@ -463,6 +464,48 @@ TEST(RealtimeContextTest, TestRejectsMismatchedSchemaOnStoreReuse) { ASSERT_EQ(1, factory->stores.size()); } +TEST(RealtimeContextTest, TestReadViewRejectsMismatchedTableSchema) { + const auto id = arrow::field("id", arrow::int64()); + const auto payload = arrow::field( + "payload", + arrow::struct_({arrow::field("x", arrow::int64()), arrow::field("y", arrow::utf8())})); + const auto table_schema = arrow::schema({id, payload}); + for (RealtimeStoreMode mode : + {RealtimeStoreMode::APPEND_ONLY, RealtimeStoreMode::PRIMARY_KEY}) { + SCOPED_TRACE(static_cast(mode)); + auto factory = std::make_shared(); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, CreateContext(factory)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr layout, + RealtimeSchemaLayout::Create(mode, table_schema)); + const RealtimePartitionBucket partition_bucket({{"dt", "2026-08-02"}}, 0); + ASSERT_OK(context->GetOrCreateRealtimeStore( + RealtimeStoreCreateRequest{ExportSchema(layout->StoreWriteSchema()), /*options=*/{}, + GetDefaultPool(), mode}, + partition_bucket)); + ASSERT_OK_AND_ASSIGN(RealtimeReadState state, context->AcquireReadState()); + ASSERT_EQ(1, state.views.size()); + const auto& view = state.views[0]; + ASSERT_TRUE( + view.write_schema->Equals(*layout->StoreWriteSchema(), /*check_metadata=*/true)); + ASSERT_OK(view.ValidateTableSchema(table_schema)); + const std::vector> incompatible{ + arrow::schema({arrow::field("id", arrow::int32()), payload}), + arrow::schema({id}), + arrow::schema({id, payload, arrow::field("added", arrow::utf8())}), + arrow::schema({payload, id}), + arrow::schema( + {id, arrow::field("payload", arrow::struct_({arrow::field("x", arrow::int64())}))}), + arrow::schema( + {id->WithMetadata(arrow::key_value_metadata({"field_id"}, {"changed"})), payload})}; + for (const auto& schema : incompatible) { + ASSERT_NOK_WITH_MSG( + view.ValidateTableSchema(schema), + "real-time store schema mismatch for partition {dt=2026-08-02}, bucket 0; " + "recreate the RealtimeContext"); + } + } +} + TEST(RealtimeContextTest, TestReconcilesPrimaryKeyInitialSequence) { auto factory = std::make_shared(); ASSERT_OK_AND_ASSIGN(std::shared_ptr context, CreateContext(factory)); @@ -774,6 +817,7 @@ TEST(RealtimeContextTest, TestPinsResolvesAndReleasesReadViewTicket) { ASSERT_EQ(views[0].partition_bucket, resolved.partition_bucket); ASSERT_EQ(views[0].store, resolved.store); ASSERT_EQ(views[0].read_view, resolved.read_view); + ASSERT_EQ(views[0].write_schema, resolved.write_schema); ASSERT_OK(context->ReleaseReadView(ticket)); ASSERT_OK(context->ReleaseReadView(ticket)); diff --git a/src/paimon/core/realtime/realtime_key_offset_index_test.cpp b/src/paimon/core/realtime/realtime_key_offset_index_test.cpp index 3a810c476..40c3338dd 100644 --- a/src/paimon/core/realtime/realtime_key_offset_index_test.cpp +++ b/src/paimon/core/realtime/realtime_key_offset_index_test.cpp @@ -94,7 +94,7 @@ TEST(RealtimeKeyOffsetIndexTest, TestMutableIndexSnapshotIsCopyOnWrite) { ASSERT_OK_AND_ASSIGN(std::shared_ptr index, MutableKeyOffsetIndex::Create(key_field, GetDefaultPool())); ASSERT_OK(index->Add(MakeKeys(key_field, R"([[1], [2]])"), MakeOffsets("[10, 20]"))); - const std::shared_ptr snapshot = index->Snapshot(); + const std::shared_ptr snapshot = index->Snapshot(); ASSERT_OK(index->Add(MakeKeys(key_field, R"([[1]])"), MakeOffsets("[30]"))); ASSERT_OK(index->Erase(MakeKeys(key_field, R"([[2]])"))); @@ -106,7 +106,7 @@ TEST(RealtimeKeyOffsetIndexTest, TestMutableIndexSnapshotIsCopyOnWrite) { snapshot->LookupOffsets(MakeKeys(key_field, R"([[1], [2], [3]])"))); AssertOffsets(pinned, {10, 20}); - const std::shared_ptr deleted_snapshot = index->Snapshot(); + const std::shared_ptr deleted_snapshot = index->Snapshot(); ASSERT_OK(index->Add(MakeKeys(key_field, R"([[2]])"), MakeOffsets("[40]"))); ASSERT_OK(index->Erase(MakeKeys(key_field, R"([[99]])"))); ASSERT_OK_AND_ASSIGN(current, index->LookupOffsets(MakeKeys(key_field, R"([[1], [2]])"))); diff --git a/src/paimon/core/table/source/append_only_table_read.cpp b/src/paimon/core/table/source/append_only_table_read.cpp index 86518f757..cc0cb2a07 100644 --- a/src/paimon/core/table/source/append_only_table_read.cpp +++ b/src/paimon/core/table/source/append_only_table_read.cpp @@ -42,6 +42,7 @@ #include "paimon/core/operation/data_evolution_split_read.h" #include "paimon/core/operation/internal_read_context.h" #include "paimon/core/operation/raw_file_split_read.h" +#include "paimon/core/realtime/precise_realtime_index_read.h" #include "paimon/core/realtime/realtime_context_impl.h" #include "paimon/core/realtime/realtime_file_schema.h" #include "paimon/core/realtime/realtime_reader.h" @@ -120,7 +121,7 @@ AppendOnlyTableRead::AppendOnlyTableRead(const std::shared_ptr& context, const std::shared_ptr& memory_pool, const std::shared_ptr& executor) - : context_(context) { + : context_(context), path_factory_(path_factory) { const auto& core_options = context->GetCoreOptions(); if (!core_options.GetRealtimeDeduplicateKeyFields().empty()) { realtime_file_schema_ = std::make_shared( @@ -221,6 +222,9 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( if (!memory_range || memory_range->end != realtime_split->MemoryEndOffset()) { return Status::Invalid("real-time read-view ticket does not match the split offset range"); } + std::shared_ptr table_schema = + DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()); + PAIMON_RETURN_NOT_OK(memory.ValidateTableSchema(table_schema)); for (const std::shared_ptr& disk_split : realtime_split->DiskSplits()) { std::unique_ptr disk_reader; @@ -233,8 +237,23 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( readers.push_back(std::move(disk_reader)); } - std::shared_ptr table_schema = - DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()); + if (context_->EnablePreciseFileIndexEvaluate()) { + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr memory_reader, + PreciseRealtimeIndexRead::CreateReader( + context_, memory.mode, memory.store, memory.read_view, + OffsetRange(realtime_split->CommittedEndOffset(), + realtime_split->MemoryEndOffset()))); + readers.push_back(std::move(memory_reader)); + if (release_ticket) { + PAIMON_RETURN_NOT_OK( + realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); + } + std::unique_ptr result = + std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); + readers_guard.Release(); + return result; + } + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr schema_layout, RealtimeSchemaLayout::Create(memory.mode, table_schema)); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr pipeline, @@ -253,9 +272,13 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( PredicateUtils::CreatePickedFieldFilter(context_->GetPredicate(), realtime_field_name_to_index)); RealtimeQueryContext query_context{ - c_read_schema.get(), std::move(realtime_predicate), + c_read_schema.get(), + std::move(realtime_predicate), OffsetRange(realtime_split->CommittedEndOffset(), realtime_split->MemoryEndOffset()), - context_->GetCoreOptions().GetReadBatchSize()}; + context_->GetCoreOptions().GetReadBatchSize(), + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; PAIMON_ASSIGN_OR_RAISE(std::vector> memory_readers, memory.store->CreateQueryReaders(memory.read_view, query_context)); const size_t first_memory_reader = readers.size(); diff --git a/src/paimon/core/table/source/append_only_table_read.h b/src/paimon/core/table/source/append_only_table_read.h index e9becc165..20aae4184 100644 --- a/src/paimon/core/table/source/append_only_table_read.h +++ b/src/paimon/core/table/source/append_only_table_read.h @@ -68,6 +68,7 @@ class AppendOnlyTableRead : public TableRead { std::vector> split_reads_; std::shared_ptr context_; + std::shared_ptr path_factory_; std::shared_ptr realtime_file_schema_; }; diff --git a/src/paimon/core/table/source/key_value_table_read.cpp b/src/paimon/core/table/source/key_value_table_read.cpp index 40b415583..2637f4aca 100644 --- a/src/paimon/core/table/source/key_value_table_read.cpp +++ b/src/paimon/core/table/source/key_value_table_read.cpp @@ -121,9 +121,13 @@ Result>> CreateMemoryReaders( PredicateUtils::CreatePickedFieldFilter(context->GetPredicate(), primary_key_name_to_index)); RealtimeQueryContext query_context{ - c_schema.get(), std::move(primary_key_predicate), + c_schema.get(), + std::move(primary_key_predicate), OffsetRange(split->CommittedEndOffset(), split->MemoryEndOffset()), - context->GetCoreOptions().GetReadBatchSize()}; + context->GetCoreOptions().GetReadBatchSize(), + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; PAIMON_ASSIGN_OR_RAISE(std::vector> batch_readers, memory.store->CreateQueryReaders(memory.read_view, query_context)); PAIMON_ASSIGN_OR_RAISE( @@ -321,6 +325,8 @@ Result> KeyValueTableRead::CreateRealtimeReader( if (!memory_range || memory_range->end != realtime_split->MemoryEndOffset()) { return Status::Invalid("real-time read-view ticket does not match the split offset range"); } + PAIMON_RETURN_NOT_OK(memory.ValidateTableSchema( + DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()))); for (const std::unique_ptr& read : split_reads_) { auto* merge_read = dynamic_cast(read.get()); if (merge_read) { diff --git a/src/paimon/core/table/source/table_read_test.cpp b/src/paimon/core/table/source/table_read_test.cpp index 3433cd670..893cf0bbc 100644 --- a/src/paimon/core/table/source/table_read_test.cpp +++ b/src/paimon/core/table/source/table_read_test.cpp @@ -333,8 +333,7 @@ TEST(TableReadTest, TestPreciseScalarEvaluationRejectsUnsupportedTableModes) { const std::string expected_error = primary_key ? "Precise file-index evaluation on primary-key tables requires " "deletion vectors" - : "Precise file-index evaluation does not support data-evolution or " - "real-time reads"; + : "Precise file-index evaluation does not support data-evolution"; ASSERT_NOK_WITH_MSG(TableRead::Create(std::move(context)), expected_error); } } diff --git a/src/paimon/testing/mock/CMakeLists.txt b/src/paimon/testing/mock/CMakeLists.txt index ec600151f..36c949e52 100644 --- a/src/paimon/testing/mock/CMakeLists.txt +++ b/src/paimon/testing/mock/CMakeLists.txt @@ -18,6 +18,7 @@ if(PAIMON_BUILD_TESTS) set(PAIMON_MOCK_FILE_FORMAT mock_file_system_factory.cpp mock_search_file_index.cpp + mock_search_realtime_index.cpp mock_file_format_factory.cpp mock_file_format.cpp mock_format_writer_builder.cpp diff --git a/src/paimon/testing/mock/mock_search_file_index.cpp b/src/paimon/testing/mock/mock_search_file_index.cpp index 27e6d47e4..27831e7c7 100644 --- a/src/paimon/testing/mock/mock_search_file_index.cpp +++ b/src/paimon/testing/mock/mock_search_file_index.cpp @@ -22,6 +22,7 @@ #include "arrow/c/bridge.h" #include "paimon/common/file_index/empty/empty_file_index_reader.h" +#include "paimon/common/file_index/file_index_format_utils.h" #include "paimon/common/io/byte_array_output_stream.h" #include "paimon/common/io/data_output_stream.h" #include "paimon/common/io/memory_segment_output_stream.h" @@ -310,6 +311,37 @@ Result> MockSearchFileIndex::MakeQueryIndex( return Serialize(indexes, pool); } +Result MockSearchFileIndex::ReadIndexes( + const std::shared_ptr& input, const std::shared_ptr& pool) { + FileIndexFormat::ColumnIndexes indexes; + if (!input) { + return indexes; + } + DataInputStream in(input); + PAIMON_RETURN_NOT_OK(in.Seek(0)); + PAIMON_ASSIGN_OR_RAISE(FileIndexFormatUtils::FileIndexEntries entries, + FileIndexFormatUtils::ReadIndexEntries(input, pool)); + PAIMON_ASSIGN_OR_RAISE(int64_t length, in.Length()); + for (const auto& [column, types] : entries) { + for (const auto& [type, bounds] : types) { + const auto& [offset, size] = bounds; + if (offset == FileIndexFormat::EMPTY_INDEX_FLAG) { + indexes[column][type] = nullptr; + continue; + } + if (offset < 0 || offset > length || size < 0 || size > length - offset) { + return Status::Invalid( + "Invalid file index payload bounds while merging test indexes"); + } + auto bytes = std::make_shared(static_cast(size), pool.get()); + PAIMON_RETURN_NOT_OK(in.Seek(offset)); + PAIMON_RETURN_NOT_OK(in.ReadBytes(bytes.get())); + indexes[column][type] = std::move(bytes); + } + } + return indexes; +} + Result> MockSearchFileIndex::Serialize( const FileIndexFormat::ColumnIndexes& indexes, const std::shared_ptr& pool) { auto output = MakeOutput(pool); diff --git a/src/paimon/testing/mock/mock_search_file_index.h b/src/paimon/testing/mock/mock_search_file_index.h index 06cc91f3f..3de70e83b 100644 --- a/src/paimon/testing/mock/mock_search_file_index.h +++ b/src/paimon/testing/mock/mock_search_file_index.h @@ -81,6 +81,11 @@ class MockSearchFileIndex { static Result> MakePayload(const ScoredRows& rows, const std::shared_ptr& pool); + /// Read all raw payloads from a V1 or V2 container for merging test indexes. Null input + /// returns an empty map; empty index entries retain their null payloads. + static Result ReadIndexes( + const std::shared_ptr& input, const std::shared_ptr& pool); + /// Wrap per-column index payloads in a complete FileIndexFormat container. /// @param indexes Payloads keyed by field name and index identifier. A nullptr payload /// represents an empty index. diff --git a/src/paimon/testing/mock/mock_search_realtime_index.cpp b/src/paimon/testing/mock/mock_search_realtime_index.cpp new file mode 100644 index 000000000..7e82457f3 --- /dev/null +++ b/src/paimon/testing/mock/mock_search_realtime_index.cpp @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "paimon/testing/mock/mock_search_realtime_index.h" + +#include + +#include "arrow/api.h" +#include "arrow/c/bridge.h" +#include "paimon/common/utils/arrow/status_utils.h" +#include "paimon/common/utils/checked_cast.h" + +namespace paimon::test { + +MockRealtimeIndex::MockRealtimeIndex(std::shared_ptr schema, + IndexReaderCapabilities::SearchType search_type, + const std::shared_ptr& state) + : schema_(std::move(schema)), search_type_(search_type), state_(state) {} + +Status MockRealtimeIndex::AddBatch(::ArrowArray* batch) { + if (!batch || !batch->release) { + return Status::Invalid("Mock real-time index batch is null"); + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr imported, + arrow::ImportArray(batch, arrow::struct_(schema_->fields()))); + auto data = checked_pointer_cast(imported); + auto offsets = checked_pointer_cast(data->field(0)); + if (data->null_count() != 0 || offsets->null_count() != 0) { + return Status::Invalid("Mock real-time index requires non-null rows and offsets"); + } + std::lock_guard lock(mutex_); + for (int64_t row = 0; row < offsets->length(); ++row) { + offsets_.Add(offsets->Value(row)); + } + return Status::OK(); +} + +Result> MockRealtimeIndex::CreateReader( + const OffsetRange& offset_range) const { + if (offset_range.begin < 0 || offset_range.begin > offset_range.end) { + return Status::Invalid("Invalid mock real-time index reader bounds"); + } + std::lock_guard lock(mutex_); + return std::shared_ptr( + std::make_shared(search_type_, offset_range, offsets_, state_)); +} + +uint64_t MockRealtimeIndex::GetMemoryUsage() const { + std::lock_guard lock(mutex_); + return offsets_.GetSizeInBytes(); +} + +MockRealtimeIndexFactory::MockRealtimeIndexFactory(std::string identifier, + IndexReaderCapabilities::SearchType search_type, + const std::shared_ptr& state) + : identifier_(std::move(identifier) + REALTIME_INDEX_IDENTIFIER_SUFFIX), + search_type_(search_type), + state_(state) {} + +const char* MockRealtimeIndexFactory::Identifier() const { + return identifier_.c_str(); +} + +Result> MockRealtimeIndexFactory::Create( + ::ArrowSchema* write_schema, const std::map&, + const std::shared_ptr&) const { + if (!write_schema || !write_schema->release || !state_) { + return Status::Invalid("Mock real-time index requires a schema and search state"); + } + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr schema, + arrow::ImportSchema(write_schema)); + // Validate the plugin input without depending on internal DataField symbols in libpaimon. + constexpr char kOffsetName[] = "_REALTIME_OFFSET"; + if (schema->num_fields() != 2 || schema->field(0)->name() != kOffsetName || + schema->field(1)->name() == kOffsetName) { + return Status::Invalid("Mock real-time index requires offset and one indexed field"); + } + if (schema->field(0)->type()->id() != arrow::Type::INT64 || schema->field(0)->nullable()) { + return Status::Invalid("Mock real-time index requires non-null int64 _REALTIME_OFFSET"); + } + if (search_type_ != IndexReaderCapabilities::SearchType::VECTOR_SEARCH && + search_type_ != IndexReaderCapabilities::SearchType::FULL_TEXT_SEARCH) { + return Status::Invalid("Mock real-time index requires vector or full-text search"); + } + return std::shared_ptr( + std::make_shared(std::move(schema), search_type_, state_)); +} + +} // namespace paimon::test diff --git a/src/paimon/testing/mock/mock_search_realtime_index.h b/src/paimon/testing/mock/mock_search_realtime_index.h new file mode 100644 index 000000000..d6def8d2b --- /dev/null +++ b/src/paimon/testing/mock/mock_search_realtime_index.h @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include +#include +#include + +#include "paimon/realtime/index/realtime_index.h" +#include "paimon/realtime/index/realtime_index_factory.h" +#include "paimon/testing/mock/mock_search_realtime_index_reader.h" + +namespace arrow { +class Schema; +} // namespace arrow + +namespace paimon::test { + +/// Test index that records written offsets and serves configured search results. +class MockRealtimeIndex final : public RealtimeIndex { + public: + MockRealtimeIndex(std::shared_ptr schema, + IndexReaderCapabilities::SearchType search_type, + const std::shared_ptr& state); + + Status AddBatch(::ArrowArray* batch) override; + + Result> CreateReader( + const OffsetRange& offset_range) const override; + + uint64_t GetMemoryUsage() const override; + + private: + std::shared_ptr schema_; + IndexReaderCapabilities::SearchType search_type_; + std::shared_ptr state_; + mutable std::mutex mutex_; + RoaringBitmap64 offsets_; +}; + +/// Register with FactoryCreator under Identifier(), then configure +/// realtime.index..columns to use mock search with real Arrow segments. +class MockRealtimeIndexFactory final : public RealtimeIndexFactory { + public: + MockRealtimeIndexFactory(std::string identifier, + IndexReaderCapabilities::SearchType search_type, + const std::shared_ptr& state); + + const char* Identifier() const override; + + Result> Create( + ::ArrowSchema* write_schema, const std::map& options, + const std::shared_ptr& pool) const override; + + private: + std::string identifier_; + IndexReaderCapabilities::SearchType search_type_; + std::shared_ptr state_; +}; + +} // namespace paimon::test diff --git a/src/paimon/testing/mock/mock_search_realtime_index_reader.h b/src/paimon/testing/mock/mock_search_realtime_index_reader.h new file mode 100644 index 000000000..c313db424 --- /dev/null +++ b/src/paimon/testing/mock/mock_search_realtime_index_reader.h @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include "paimon/predicate/full_text_match.h" +#include "paimon/predicate/full_text_search.h" +#include "paimon/predicate/vector_search.h" +#include "paimon/realtime/index/realtime_index_reader.h" +#include "paimon/realtime/offset_range.h" + +namespace paimon::test { + +/// Configured search results and capabilities for real-time search tests. +struct MockSearchState { + std::vector> ranked; + std::map text_matches; + bool supports_search = true; +}; + +/// Real-time search test reader. Applies range and candidate filters before TopK and returns +/// scores aligned with ascending offsets. Ranked results are supplied through MockSearchState. +class MockSearchReader final : public RealtimeIndexReader { + public: + MockSearchReader(SearchType search_type, const OffsetRange& range, RoaringBitmap64 offsets, + const std::shared_ptr& state) + : search_type_(search_type), range_(range), offsets_(std::move(offsets)), state_(state) {} + + bool SupportsSearch(SearchType type) const override { + return state_->supports_search && + (type == search_type_ || (search_type_ == SearchType::FULL_TEXT_SEARCH && + type == SearchType::FULL_TEXT_MATCH)); + } + + std::optional EstimateSelectivity( + const std::shared_ptr&) const override { + return 0.1; + } + + Result> VisitVectorSearch( + const std::shared_ptr& search) override { + return Search(search->limit, search->pre_filter); + } + + Result> VisitFullTextSearch( + const std::shared_ptr& search) override { + return Search(search->limit, [search](int64_t offset) { + return !search->include_row_ids || search->include_row_ids->Contains(offset); + }); + } + + Result> VisitFullTextMatch( + const std::shared_ptr& match, bool needs_scores) override { + auto filter = [this, match](int64_t offset) { + auto found = state_->text_matches.find(match->query); + return (!match->include_row_ids || match->include_row_ids->Contains(offset)) && + (found == state_->text_matches.end() || found->second.Contains(offset)); + }; + if (!needs_scores) { + RoaringBitmap64 rows; + for (const auto& entry : state_->ranked) { + int64_t offset = entry.first; + if (offset >= range_.begin && offset < range_.end && offsets_.Contains(offset) && + filter(offset)) { + rows.Add(offset); + } + } + return RealtimeIndexResult::Create(std::move(rows)); + } + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr result, + Search(static_cast(state_->ranked.size()), filter)); + return std::shared_ptr(std::move(result)); + } + + private: + Result> Search( + int32_t limit, const std::function& filter) const { + std::map selected; + for (const auto& [offset, score] : state_->ranked) { + if (selected.size() >= static_cast(limit)) { + break; + } + if (offset >= range_.begin && offset < range_.end && offsets_.Contains(offset) && + (!filter || filter(offset))) { + selected.emplace(offset, score); + } + } + RoaringBitmap64 rows; + std::vector scores; + for (const auto& [offset, score] : selected) { + rows.Add(offset); + scores.push_back(score); + } + return ScoredRealtimeIndexResult::Create(std::move(rows), std::move(scores)); + } + + SearchType search_type_; + OffsetRange range_; + RoaringBitmap64 offsets_; + std::shared_ptr state_; +}; + +} // namespace paimon::test diff --git a/test/inte/realtime_write_inte_test.cpp b/test/inte/realtime_write_inte_test.cpp index 7aceaa6a7..83aff9c4a 100644 --- a/test/inte/realtime_write_inte_test.cpp +++ b/test/inte/realtime_write_inte_test.cpp @@ -57,6 +57,8 @@ #include "paimon/core/core_options.h" #include "paimon/core/deletionvectors/bitmap_deletion_vector.h" #include "paimon/core/deletionvectors/deletion_vectors_index_file.h" +#include "paimon/core/io/data_file_path_factory.h" +#include "paimon/core/io/file_index_evaluator.h" #include "paimon/core/operation/commit/manifest_entry_changes.h" #include "paimon/core/operation/commit/realtime_commit_properties.h" #include "paimon/core/operation/commit/realtime_deduplicate_compaction_changes_provider.h" @@ -72,16 +74,20 @@ #include "paimon/data/shredding/map_shared_shredding_schema_utils.h" #include "paimon/data/variant.h" #include "paimon/defs.h" +#include "paimon/factories/factory.h" #include "paimon/file_store_commit.h" #include "paimon/file_store_write.h" #include "paimon/fs/file_system.h" #include "paimon/fs/local/local_file_system.h" #include "paimon/memory/memory_pool.h" #include "paimon/orphan_files_cleaner.h" +#include "paimon/predicate/full_text_match.h" +#include "paimon/predicate/full_text_search.h" #include "paimon/predicate/leaf_predicate.h" #include "paimon/predicate/predicate.h" #include "paimon/predicate/predicate_builder.h" #include "paimon/predicate/predicate_utils.h" +#include "paimon/predicate/vector_search.h" #include "paimon/read_context.h" #include "paimon/reader/count_reader.h" #include "paimon/realtime/arrow_realtime_store_factory.h" @@ -93,6 +99,8 @@ #include "paimon/table/source/table_read.h" #include "paimon/table/source/table_scan.h" #include "paimon/testing/mock/mock_index_path_factory.h" +#include "paimon/testing/mock/mock_search_file_index.h" +#include "paimon/testing/mock/mock_search_realtime_index.h" #include "paimon/testing/utils/blocking_file_system.h" #include "paimon/testing/utils/dict_array_converter.h" #include "paimon/testing/utils/read_result_collector.h" @@ -1120,7 +1128,11 @@ class RealtimeWriteInteTest : public ::testing::Test { RealtimeQueryContext query_context{ read_schema.get(), /*predicate=*/nullptr, - views[0].read_view->GetOffsetRange().value_or(OffsetRange(0, 0))}; + views[0].read_view->GetOffsetRange().value_or(OffsetRange(0, 0)), + 1024, + /*selection=*/nullptr, + /*offset_deletions=*/nullptr, + /*enable_index_pruning=*/true}; PAIMON_ASSIGN_OR_RAISE( std::vector> readers, views[0].store->CreateQueryReaders(views[0].read_view, query_context)); @@ -6553,6 +6565,689 @@ TEST_F(RealtimeWriteInteTest, TestCountReaderDoesNotSupportRealtimeSplit) { ASSERT_OK(PrepareAndClose(writer.get())); } +class RealtimePreciseIndexInteTest : public RealtimeWriteInteTest { + protected: + struct SearchQuery { + std::vector fields; + std::shared_ptr predicate; + std::shared_ptr vector; + std::shared_ptr text; + }; + + void SetUp() override { + RealtimeWriteInteTest::SetUp(); + ASSERT_FALSE(HasFatalFailure()); + fields_[0] = arrow::field("id", arrow::int64(), /*nullable=*/false); + fields_.push_back(arrow::field("embedding", arrow::list(arrow::float32()))); + schema_ = arrow::schema(fields_); + options_[Options::FILE_FORMAT] = "parquet"; + options_[Options::REALTIME_STORE_STATS_MODE] = "none"; + options_[Options::READ_BATCH_SIZE] = "2"; + options_["realtime.index.hash.columns"] = "id"; + vector_state_ = std::make_shared(); + text_state_ = std::make_shared(); + using SearchType = IndexReaderCapabilities::SearchType; + const std::vector>> + indexes{{"inte-mock-vector", SearchType::VECTOR_SEARCH, vector_state_}, + {"inte-mock-text", SearchType::FULL_TEXT_SEARCH, text_state_}}; + for (const auto& [identifier, type, state] : indexes) { + auto factory = std::make_unique(identifier, type, state); + FactoryCreator::GetInstance()->Register( + identifier + RealtimeIndexFactory::REALTIME_INDEX_IDENTIFIER_SUFFIX, + factory.release()); + } + options_["realtime.index.inte-mock-vector.columns"] = "embedding"; + options_["realtime.index.inte-mock-text.columns"] = "payload"; + } + + void TearDown() override { + for (const std::string identifier : {"inte-mock-vector", "inte-mock-text"}) { + FactoryCreator::GetInstance()->TEST_Unregister( + identifier + RealtimeIndexFactory::REALTIME_INDEX_IDENTIFIER_SUFFIX); + } + RealtimeWriteInteTest::TearDown(); + } + + void CreateSearchTable(bool deduplicate) { + if (deduplicate) { + fields_.insert(fields_.begin(), + arrow::field(SpecialFields::RealtimeOffset().Name(), arrow::int64(), + /*nullable=*/false)); + schema_ = arrow::schema(fields_); + options_[Options::DELETION_VECTORS_ENABLED] = "true"; + options_[Options::REALTIME_DEDUPLICATE_KEY_FIELDS] = "id"; + options_["file-index.bitmap.columns"] = SpecialFields::RealtimeOffset().Name(); + } + CreateTable(/*partition_keys=*/{}); + } + + Status WriteSearchBatch(const std::string& json, + const std::vector& row_kinds, + FileStoreWrite* writer) const { + const RealtimeStoreMode mode = options_.count(Options::REALTIME_DEDUPLICATE_KEY_FIELDS) > 0 + ? RealtimeStoreMode::DEDUPLICATE + : RealtimeStoreMode::APPEND_ONLY; + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr layout, + RealtimeSchemaLayout::Create(mode, schema_)); + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( + std::shared_ptr array, + arrow::ipc::internal::json::ArrayFromJSON( + arrow::struct_(layout->InputSchema()->fields()), json)); + ArrowArray c_array; + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportArray(*array, &c_array)); + PAIMON_ASSIGN_OR_RAISE( + std::unique_ptr batch, + RecordBatchBuilder(&c_array).SetRowKinds(row_kinds).SetBucket(0).Finish()); + return writer->Write(std::move(batch)); + } + + std::shared_ptr Vector(int32_t limit) const { + return std::make_shared("embedding", limit, std::vector{1.0f}, nullptr, + nullptr, VectorSearch::DistanceType::COSINE, + std::map{}); + } + + std::shared_ptr EqualString(const std::string& field, + const std::string& value) const { + return PredicateBuilder::Equal(schema_->GetFieldIndex(field), field, FieldType::STRING, + Literal(FieldType::STRING, value.data(), value.size())); + } + + void SetDiskRanking(const std::vector& progress, + const MockSearchFileIndex::ScoredRows& ranking) { + const std::vector> files = NewFiles(progress); + ASSERT_EQ(1, files.size()); + ASSERT_EQ(static_cast(ranking.size()), files[0]->row_count); + disk_rankings_[files[0]->file_name] = ranking; + } + + Result> WithSearchIndexes(const std::shared_ptr& split) const { + if (auto realtime = std::dynamic_pointer_cast(split)) { + std::vector> disk; + for (const auto& child : realtime->DiskSplits()) { + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr indexed, WithSearchIndexes(child)); + disk.push_back(std::move(indexed)); + } + return std::make_shared( + realtime->Version(), realtime->SnapshotId(), realtime->Partition(), + realtime->Bucket(), std::move(disk), realtime->CommittedEndOffset(), + realtime->MemoryEndOffset(), realtime->OpaqueTicket()); + } + auto data = std::dynamic_pointer_cast(split); + if (!data) { + return Status::Invalid("Expected a data split for mock search indexes"); + } + auto paths = std::make_shared(); + PAIMON_RETURN_NOT_OK( + paths->Init(data->BucketPath(), options_.at(Options::FILE_FORMAT), "data-", nullptr)); + std::vector> files; + for (const auto& original : data->DataFiles()) { + auto ranking = disk_rankings_.find(original->file_name); + if (ranking == disk_rankings_.end()) { + return Status::Invalid("Missing mock ranking for file ", original->file_name); + } + // Preserve the original payloads, including offset bitmap indexes, in the query copy. + auto file = std::make_shared(*original); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr input, + FileIndexEvaluator::ExtractIndexInputStream( + paths, original, dir_->GetFileSystem())); + ScopeGuard close([&input]() { + if (input) { + [[maybe_unused]] Status ignored = input->Close(); + } + }); + PAIMON_ASSIGN_OR_RAISE(FileIndexFormat::ColumnIndexes indexes, + MockSearchFileIndex::ReadIndexes(input, pool_)); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr payload, + MockSearchFileIndex::MakePayload(ranking->second, pool_)); + indexes["embedding"][MockSearchFileIndex::VECTOR_IDENTIFIER] = payload; + indexes["payload"][MockSearchFileIndex::FULL_TEXT_IDENTIFIER] = payload; + auto text_ranking = disk_text_rankings_.find(original->file_name); + if (text_ranking != disk_text_rankings_.end()) { + PAIMON_ASSIGN_OR_RAISE( + indexes["payload"][MockSearchFileIndex::FULL_TEXT_IDENTIFIER], + MockSearchFileIndex::MakePayload(text_ranking->second, pool_)); + } + PAIMON_ASSIGN_OR_RAISE(file->embedded_index, + MockSearchFileIndex::Serialize(indexes, pool_)); + files.push_back(std::move(file)); + } + DataSplitImpl::Builder builder(data->Partition(), data->Bucket(), data->BucketPath(), + std::move(files)); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr indexed, + builder.WithSnapshot(data->SnapshotId()) + .WithTotalBuckets(data->TotalBuckets()) + .WithDataDeletionFiles(data->DeletionFiles()) + .IsStreaming(data->IsStreaming()) + .RawConvertible(data->RawConvertible()) + .Build()); + return indexed; + } + + Result> CreateSearchReader( + const std::shared_ptr& plan, const std::shared_ptr& realtime_context, + const SearchQuery& query) const { + std::map positions; + for (int32_t i = 0; i < static_cast(query.fields.size()); ++i) { + positions.emplace(query.fields[i], i); + } + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr predicate, + PredicateUtils::CreatePickedFieldFilter(query.predicate, positions)); + ReadContextBuilder builder(table_path_); + builder.SetOptions(options_) + .SetReadFieldNames(query.fields) + .SetPredicate(predicate) + .SetVectorSearch(query.vector) + .SetFullTextSearch(query.text) + .EnablePreciseFileIndexEvaluate(true) + .EnablePredicateFilter(false) + .WithRealtimeContext(realtime_context) + .WithMemoryPool(pool_); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr context, builder.Finish()); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr read, + TableRead::Create(std::move(context))); + std::vector> splits; + for (const auto& split : plan->Splits()) { + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr indexed, WithSearchIndexes(split)); + splits.push_back(std::move(indexed)); + } + return read->CreateReader(splits); + } + + void CheckSearchResult(std::unique_ptr reader, const SearchQuery& query, + const std::string& expected_json) const { + ASSERT_OK_AND_ASSIGN(std::shared_ptr actual, + ReadResultCollector::CollectResult(std::move(reader))); + ASSERT_NE(nullptr, actual); + arrow::FieldVector fields{arrow::field(SpecialFields::ValueKind().Name(), arrow::int8())}; + for (const auto& name : query.fields) { + fields.push_back(SpecialFields::ParseIndexScoreFieldName(name) + ? arrow::field(name, arrow::float32()) + : arrow::field(name, schema_->GetFieldByName(name)->type())); + } + std::shared_ptr expected; + ASSERT_TRUE(arrow::ipc::internal::json::ChunkedArrayFromJSON(arrow::struct_(fields), + {expected_json}, &expected) + .ok()); + ASSERT_TRUE(actual->Equals(expected)) + << "expected: " << expected->ToString() << "\nactual: " << actual->ToString(); + } + + void CheckSearch(const std::shared_ptr& plan, + const std::shared_ptr& realtime_context, + const SearchQuery& query, const std::string& expected_json) const { + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + CreateSearchReader(plan, realtime_context, query)); + ASSERT_NO_FATAL_FAILURE(CheckSearchResult(std::move(reader), query, expected_json)); + } + + std::shared_ptr vector_state_; + std::shared_ptr text_state_; + std::map disk_rankings_; + std::map disk_text_rankings_; +}; + +TEST_F(RealtimePreciseIndexInteTest, TestAppendScalarFilteringBeforeVectorTopK) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {1, 0.875f}, {2, 0.5f}}; + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"miss","miss",[1]], [1,2,"hit","keep",[2]], [2,3,"hit","keep",[3]] + ])", + {}, writer.get())); + // Hash equality, an unsupported hash range operation, and an unindexed field all require + // the same exact result before ranking, even with residual filtering disabled. + const std::vector> predicates{ + PredicateBuilder::Equal(0, "id", FieldType::BIGINT, Literal(int64_t{2})), + PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{1})), + EqualString("pt", "keep")}; + for (const auto& predicate : predicates) { + SCOPED_TRACE(predicate->ToString()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, predicate)); + SearchQuery query{ + {"_embedding_INDEX_SCORE", "payload", "id", "pt"}, predicate, Vector(1), nullptr}; + ASSERT_NO_FATAL_FAILURE(CheckSearch(plan, context, query, R"([[0,0.875,"hit",2,"keep"]])")); + } + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestFullTextSearchAndMixedPredicateScores) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {1, 0.875f}, {2, 0.5f}}; + text_state_->ranked = {{0, 0.99f}, {2, 0.625f}, {1, 0.25f}}; + const std::string text_query = R"({"match":{"query":"lake"}})"; + text_state_->text_matches[text_query] = RoaringBitmap64::From({0, 2}); + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"lake","miss",[1]], [1,2,"other","keep",[2]], [2,3,"lake","keep",[3]] + ])", + {}, writer.get())); + + auto scalar = EqualString("pt", "keep"); + auto text = std::make_shared("payload", R"({"match":{"query":"lake other"}})", + 1, RoaringBitmap64::From({0, 1})); + SearchQuery root{{"_payload_INDEX_SCORE", "id", "pt"}, scalar, nullptr, text}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr root_plan, CreatePlan(context, scalar)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(root_plan, context, root, R"([[0,0.25,2,"keep"]])")); + + ASSERT_OK_AND_ASSIGN(std::shared_ptr match, + PredicateBuilder::FullTextMatch( + schema_->GetFieldIndex("payload"), + std::make_shared("payload", text_query, std::nullopt))); + auto range = PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{1})); + ASSERT_OK_AND_ASSIGN(std::shared_ptr predicate, + PredicateBuilder::And({match, range})); + SearchQuery mixed{{"id", "payload", "_payload_INDEX_SCORE", "_embedding_INDEX_SCORE"}, + predicate, + Vector(1), + nullptr}; + // Split planning accepts scalar predicates; FullTextMatch is evaluated by the precise reader. + ASSERT_OK_AND_ASSIGN(std::shared_ptr mixed_plan, CreatePlan(context, range)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(mixed_plan, context, mixed, R"([[0,3,"lake",0.625,0.5]])")); + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestVectorSearchAcrossDiskSealedAndBuildingSources) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {1, 0.875f}, {2, 0.98f}, {3, 0.75f}, + {4, 0.97f}, {5, 0.625f}, {6, 0.96f}, {7, 0.5f}}; + const std::vector disk_batches{R"([[0,1,"miss","p0",[1]],[1,2,"hit","p0",[2]]])", + R"([[2,3,"miss","p0",[3]],[3,4,"hit","p0",[4]]])"}; + for (int64_t i = 0; i < 2; ++i) { + ASSERT_OK(WriteSearchBatch(disk_batches[i], {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector progress, + writer->PrepareCommitWithProgress(i)); + ASSERT_NO_FATAL_FAILURE( + SetDiskRanking(progress, {{0, 0.99f}, {1, i == 0 ? 0.875f : 0.75f}})); + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(progress, i)); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + } + ASSERT_OK( + WriteSearchBatch(R"([[4,5,"miss","p0",[5]],[5,6,"hit","p0",[6]]])", {}, writer.get())); + ASSERT_OK(writer->Seal()); + ASSERT_OK( + WriteSearchBatch(R"([[6,7,"miss","p0",[7]],[7,8,"hit","p0",[8]]])", {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector files, InspectFiles()); + ASSERT_EQ(2, files.size()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr impl, + RealtimeContextImpl::Cast(context)); + ASSERT_OK_AND_ASSIGN(RealtimeReadState state, impl->AcquireReadState()); + ASSERT_EQ(1, state.views.size()); + ASSERT_EQ(2, state.views[0].store->GetDataUsage().sealed_row_count); + ASSERT_EQ(2, state.views[0].store->GetDataUsage().building_row_count); + auto predicate = EqualString("payload", "hit"); + SearchQuery query{{"id", "payload", "_embedding_INDEX_SCORE"}, predicate, Vector(1), nullptr}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, predicate)); + // Each file or segment contributes its own Top1; the public reader concatenates all four. + ASSERT_NO_FATAL_FAILURE(CheckSearch(plan, context, query, R"([ + [0,2,"hit",0.875], [0,4,"hit",0.75], [0,6,"hit",0.625], [0,8,"hit",0.5] + ])")); + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestDeduplicateMemoryDvBeforeVectorTopK) { + CreateSearchTable(/*deduplicate=*/true); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {1, 0.98f}, {2, 0.75f}, {3, 0.875f}, {4, 0.97f}}; + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"old","p0",[1]], [1,2,"deleted","p0",[2]], [2,3,"keep","p0",[3]] + ])", + {}, writer.get())); + ASSERT_OK(writer->Seal()); + ASSERT_OK(WriteSearchBatch(R"([[3,1,"new","p0",[4]],[4,2,"ignored","p0",[5]]])", + {RecordBatch::RowKind::UPDATE_AFTER, RecordBatch::RowKind::DELETE}, + writer.get())); + SearchQuery query{{"id", "payload", "_embedding_INDEX_SCORE"}, nullptr, Vector(1), nullptr}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, nullptr)); + // Both higher-ranked offsets in the sealed segment are invisible. The surviving row must + // fill its Top1, and the replacement is returned separately by the building segment. + ASSERT_NO_FATAL_FAILURE( + CheckSearch(plan, context, query, R"([[0,3,"keep",0.75],[0,1,"new",0.875]])")); + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestCommittedAndRealtimeDiskDvBeforeVectorTopK) { + CreateSearchTable(/*deduplicate=*/true); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {1, 0.98f}, {2, 0.75f}, {3, 0.97f}, {4, 0.875f}}; + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"committed-delete","p0",[1]], [1,2,"old","p0",[2]], [2,3,"keep","p0",[3]] + ])", + {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector initial, + writer->PrepareCommitWithProgress(0)); + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(initial, {{0, 0.99f}, {1, 0.98f}, {2, 0.75f}})); + ASSERT_OK_AND_ASSIGN(int64_t initial_snapshot, Commit(initial, 0)); + ASSERT_OK(writer->RefreshCommittedSnapshot(initial_snapshot)); + ASSERT_OK(WriteSearchBatch(R"([[3,1,"ignored","p0",[4]]])", {RecordBatch::RowKind::DELETE}, + writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector deletion, + writer->PrepareCommitWithProgress(1)); + ASSERT_OK_AND_ASSIGN(int64_t deleted_snapshot, Commit(deletion, 1)); + ASSERT_OK(writer->RefreshCommittedSnapshot(deleted_snapshot)); + ASSERT_OK_AND_ASSIGN(std::vector files, InspectFiles()); + ASSERT_EQ(1, files.size()); + ASSERT_EQ(std::set({0}), files[0].deleted_positions); + + ASSERT_OK(WriteSearchBatch(R"([[4,2,"new","p0",[5]]])", {RecordBatch::RowKind::UPDATE_AFTER}, + writer.get())); + SearchQuery query{{"id", "payload", "_embedding_INDEX_SCORE"}, nullptr, Vector(1), nullptr}; + const std::string expected = R"([[0,3,"keep",0.75],[0,2,"new",0.875]])"; + ASSERT_OK_AND_ASSIGN(std::shared_ptr building, CreatePlan(context, nullptr)); + ASSERT_EQ(1, building->Splits().size()); + auto realtime = std::dynamic_pointer_cast(building->Splits()[0]); + ASSERT_NE(nullptr, realtime); + ASSERT_OK_AND_ASSIGN(std::shared_ptr impl, + RealtimeContextImpl::Cast(context)); + ASSERT_OK_AND_ASSIGN(RealtimePartitionBucketView view, + impl->ResolveReadView(realtime->OpaqueTicket())); + ASSERT_NE(nullptr, view.offset_deletions); + ASSERT_TRUE(view.offset_deletions->Contains(1)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(building, context, query, expected)); + + ASSERT_OK_AND_ASSIGN(std::vector update, + writer->PrepareCommitWithProgress(2)); + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(update, {{0, 0.875f}})); + ASSERT_OK_AND_ASSIGN(std::shared_ptr prepared, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(prepared, context, query, expected)); + ASSERT_OK_AND_ASSIGN(int64_t updated_snapshot, Commit(update, 2)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr committed, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(committed, context, query, expected)); + ASSERT_OK(writer->RefreshCommittedSnapshot(updated_snapshot)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr refreshed, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(refreshed, context, query, expected)); + ASSERT_OK(writer->Close()); +} + +TEST_F(RealtimePreciseIndexInteTest, TestPinnedSearchBoundsAndDvSurviveCommitAndRefresh) { + CreateSearchTable(/*deduplicate=*/true); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {6, 0.96875f}, {5, 0.96f}, {4, 0.9375f}, + {2, 0.875f}, {1, 0.75f}, {3, 0.625f}}; + ASSERT_OK(WriteSearchBatch(R"([[0,1,"disk-old","p0",[1]],[1,2,"disk-keep","p0",[2]]])", {}, + writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector initial, + writer->PrepareCommitWithProgress(0)); + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(initial, {{0, 0.99f}, {1, 0.75f}})); + ASSERT_OK_AND_ASSIGN(int64_t initial_snapshot, Commit(initial, 0)); + // Keep the committed prefix in memory, so the scan must clip it out of index sources. + ASSERT_OK(WriteSearchBatch(R"([[2,1,"memory-old","p0",[3]],[3,3,"memory-keep","p0",[4]]])", {}, + writer.get())); + SearchQuery query{{"id", "payload", "_embedding_INDEX_SCORE"}, nullptr, Vector(10), nullptr}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr old_plan, CreatePlan(context, nullptr)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr reader_plan, CreatePlan(context, nullptr)); + ASSERT_EQ(1, old_plan->Splits().size()); + auto realtime = std::dynamic_pointer_cast(old_plan->Splits()[0]); + ASSERT_NE(nullptr, realtime); + ASSERT_EQ(2, realtime->CommittedEndOffset()); + ASSERT_EQ(4, realtime->MemoryEndOffset()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr impl, + RealtimeContextImpl::Cast(context)); + ASSERT_OK_AND_ASSIGN(RealtimePartitionBucketView pinned, + impl->ResolveReadView(realtime->OpaqueTicket())); + ASSERT_TRUE(pinned.read_view->GetOffsetRange()); + ASSERT_EQ(0, pinned.read_view->GetOffsetRange()->begin); + ASSERT_NE(nullptr, pinned.offset_deletions); + ASSERT_TRUE(pinned.offset_deletions->Contains(0)); + ASSERT_FALSE(pinned.offset_deletions->Contains(2)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr old_reader, + CreateSearchReader(reader_plan, context, query)); + + ASSERT_OK(WriteSearchBatch(R"([ + [4,1,"memory-new","p0",[5]], [5,3,"ignored","p0",[6]], [6,4,"later","p0",[7]] + ])", + {RecordBatch::RowKind::UPDATE_AFTER, RecordBatch::RowKind::DELETE, + RecordBatch::RowKind::INSERT}, + writer.get())); + const std::string latest = R"([ + [0,2,"disk-keep",0.75], [0,1,"memory-new",0.9375], [0,4,"later",0.96875] + ])"; + ASSERT_OK_AND_ASSIGN(std::shared_ptr latest_plan, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(latest_plan, context, query, latest)); + ASSERT_OK_AND_ASSIGN(std::vector update, + writer->PrepareCommitWithProgress(1)); + // Deleted offsets 2, 3, 5 are omitted; surviving offsets 4, 6 become file positions 0, 1. + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(update, {{1, 0.96875f}, {0, 0.9375f}})); + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(update, 1)); + ASSERT_GT(snapshot, initial_snapshot); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + const std::string old = R"([ + [0,2,"disk-keep",0.75], [0,1,"memory-old",0.875], [0,3,"memory-keep",0.625] + ])"; + ASSERT_NO_FATAL_FAILURE(CheckSearch(old_plan, context, query, old)); + ASSERT_NO_FATAL_FAILURE(CheckSearchResult(std::move(old_reader), query, old)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr refreshed, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(refreshed, context, query, latest)); + ASSERT_OK(writer->Close()); +} + +TEST_F(RealtimePreciseIndexInteTest, TestScoreProjectionAcrossBatchSizes) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.99f}, {4, 0.98f}, {7, 0.9375f}, {2, 0.875f}, + {5, 0.75f}, {3, 0.625f}, {1, 0.5f}, {6, 0.25f}}; + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"skip","p0",[1]], [1,2,"disk-two","p0",[2]], + [2,3,"disk-three","p0",[3]], [3,4,"disk-four","p0",[4]] + ])", + {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector progress, + writer->PrepareCommitWithProgress(0)); + ASSERT_NO_FATAL_FAILURE( + SetDiskRanking(progress, {{0, 0.99f}, {2, 0.875f}, {3, 0.625f}, {1, 0.5f}})); + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(progress, 0)); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + ASSERT_OK(WriteSearchBatch(R"([ + [4,0,"skip","p0",[5]], [5,6,"memory-six","p0",[6]], + [6,7,"memory-seven","p0",[7]], [7,8,"memory-eight","p0",[8]] + ])", + {}, writer.get())); + auto predicate = PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{1})); + const std::vector, std::string>> projections{ + {{"_embedding_INDEX_SCORE", "id"}, R"([ + [0,0.5,2], [0,0.875,3], [0,0.625,4], [0,0.75,6], [0,0.25,7], [0,0.9375,8] + ])"}, + {{"id", "_embedding_INDEX_SCORE"}, R"([ + [0,2,0.5], [0,3,0.875], [0,4,0.625], [0,6,0.75], [0,7,0.25], [0,8,0.9375] + ])"}}; + for (int32_t batch_size : {1, 2, 3, 8}) { + SCOPED_TRACE(batch_size); + options_[Options::READ_BATCH_SIZE] = fmt::format("{}", batch_size); + for (const auto& [fields, expected] : projections) { + SCOPED_TRACE(fields.front()); + SearchQuery query{fields, predicate, Vector(3), nullptr}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(plan, context, query, expected)); + } + } + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestEmptyScalarAndSearchResultsAcrossDiskAndMemory) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + vector_state_->ranked = {{0, 0.875f}, {1, 0.75f}, {2, 0.625f}, {3, 0.5f}}; + text_state_->ranked = vector_state_->ranked; + ASSERT_OK(WriteSearchBatch(R"([[0,1,"disk-one","p0",[1]],[1,2,"disk-two","p0",[2]]])", {}, + writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector progress, + writer->PrepareCommitWithProgress(0)); + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(progress, {{0, 0.875f}, {1, 0.75f}})); + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(progress, 0)); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + ASSERT_OK(WriteSearchBatch(R"([[2,3,"memory-three","p0",[3]],[3,4,"memory-four","p0",[4]]])", + {}, writer.get())); + auto scalar = PredicateBuilder::GreaterThan(0, "id", FieldType::BIGINT, Literal(int64_t{100})); + const std::vector queries{ + {{"id", "_embedding_INDEX_SCORE"}, scalar, Vector(1), nullptr}, + {{"id", "_embedding_INDEX_SCORE"}, nullptr, Vector(1), nullptr}, + {{"id", "_payload_INDEX_SCORE"}, + nullptr, + nullptr, + std::make_shared("payload", R"({"match":{"query":"absent"}})", 1, + std::nullopt)}}; + for (size_t i = 0; i < queries.size(); ++i) { + SCOPED_TRACE(i); + if (i == 1) { + // Valid search indexes with no hits must produce EOF, including when scores are + // requested. Leave the physical rows in place for both disk and memory sources. + vector_state_->ranked.clear(); + text_state_->ranked.clear(); + for (auto& entry : disk_rankings_) { + entry.second.clear(); + } + } + // Keep scalar filtering in the reader so planning cannot prune all disk files first. + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, nullptr)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + CreateSearchReader(plan, context, queries[i])); + ScopeGuard close([&]() { reader->Close(); }); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, reader->NextBatch()); + ASSERT_TRUE(BatchReader::IsEofBatch(batch)); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch next, reader->NextBatch()); + ASSERT_TRUE(BatchReader::IsEofBatch(next)); + } + ASSERT_OK(PrepareAndClose(writer.get())); +} + +TEST_F(RealtimePreciseIndexInteTest, TestFullyDeletedSourcesDoNotHideRemainingSearchResults) { + CreateSearchTable(/*deduplicate=*/true); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + // Include scores for every written offset, including delete records 2, 3, 6, 7. + vector_state_->ranked = {{0, 0.99f}, {1, 0.98f}, {2, 0.97f}, {3, 0.96f}, + {6, 0.95f}, {7, 0.94f}, {4, 0.875f}, {5, 0.625f}}; + ASSERT_OK(WriteSearchBatch(R"([[0,1,"old-one","p0",[1]],[1,2,"old-two","p0",[2]]])", {}, + writer.get())); + ASSERT_OK(writer->Seal()); + ASSERT_OK(WriteSearchBatch(R"([ + [2,1,"ignored","p0",[3]], [3,2,"ignored","p0",[4]], + [4,3,"keep-three","p0",[5]], [5,4,"keep-four","p0",[6]] + ])", + {RecordBatch::RowKind::DELETE, RecordBatch::RowKind::DELETE, + RecordBatch::RowKind::INSERT, RecordBatch::RowKind::INSERT}, + writer.get())); + SearchQuery query{{"id", "payload", "_embedding_INDEX_SCORE"}, nullptr, Vector(2), nullptr}; + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE( + CheckSearch(plan, context, query, R"([[0,3,"keep-three",0.875],[0,4,"keep-four",0.625]])")); + + ASSERT_OK(WriteSearchBatch(R"([[6,3,"ignored","p0",[7]],[7,4,"ignored","p0",[8]]])", + {RecordBatch::RowKind::DELETE, RecordBatch::RowKind::DELETE}, + writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector visible, ReadRows(context)); + ASSERT_TRUE(visible.empty()); + auto check_empty = [&](const std::string& stage) { + SCOPED_TRACE(stage); + ASSERT_OK_AND_ASSIGN(std::shared_ptr empty_plan, CreatePlan(context, nullptr)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + CreateSearchReader(empty_plan, context, query)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(reader))); + ASSERT_TRUE(!result || result->length() == 0); + }; + ASSERT_NO_FATAL_FAILURE(check_empty("before prepare")); + + ASSERT_OK_AND_ASSIGN(std::vector progress, + writer->PrepareCommitWithProgress(0)); + // The first segment was sealed before the deletes. It still writes its two physical rows, + // so a non-empty search index must be suppressed by the committed DV after refresh. + ASSERT_NO_FATAL_FAILURE(SetDiskRanking(progress, {{0, 0.99f}, {1, 0.98f}})); + ASSERT_NO_FATAL_FAILURE(check_empty("after prepare")); + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(progress, 0)); + ASSERT_NO_FATAL_FAILURE(check_empty("after commit")); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + ASSERT_NO_FATAL_FAILURE(check_empty("after refresh")); + ASSERT_OK_AND_ASSIGN(std::vector files, InspectFiles()); + ASSERT_EQ(1, files.size()); + ASSERT_EQ(2, files[0].file->row_count); + ASSERT_EQ(std::set({0, 1}), files[0].deleted_positions); + + ASSERT_OK_AND_ASSIGN(Snapshot compacted, + CompactAndCommit(/*partition=*/{}, /*bucket=*/0, /*commit_identifier=*/1, + /*full_compaction=*/true)); + ASSERT_GT(compacted.Id(), snapshot); + ASSERT_EQ(Snapshot::CommitKind::Compact(), compacted.GetCommitKind()); + ASSERT_OK_AND_ASSIGN(std::vector compacted_files, InspectFiles()); + ASSERT_TRUE(compacted_files.empty()); + ASSERT_NO_FATAL_FAILURE(check_empty("after compaction commit")); + ASSERT_OK(writer->RefreshCommittedSnapshot(compacted.Id())); + ASSERT_NO_FATAL_FAILURE(check_empty("after compaction refresh")); + ASSERT_OK(writer->Close()); +} + +TEST_F(RealtimePreciseIndexInteTest, TestFullTextOrUnindexedScalarAcrossDiskAndMemory) { + CreateSearchTable(/*deduplicate=*/false); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context, RealtimeContext::Create()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, CreateRealtimeWriter(context)); + const std::string text_query = R"({"match":{"query":"lake"}})"; + vector_state_->ranked = {{3, 0.99f}, {7, 0.98f}, {0, 0.875f}, {4, 0.75f}, + {2, 0.625f}, {1, 0.5f}, {6, 0.375f}, {5, 0.25f}}; + text_state_->ranked = vector_state_->ranked; + text_state_->text_matches[text_query] = RoaringBitmap64::From({0, 2, 4, 6}); + ASSERT_OK(WriteSearchBatch(R"([ + [0,1,"lake","miss",[1]], [1,2,"other","keep",[2]], + [2,3,"lake","keep",[3]], [3,4,"other","miss",[4]] + ])", + {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::vector progress, + writer->PrepareCommitWithProgress(0)); + ASSERT_NO_FATAL_FAILURE( + SetDiskRanking(progress, {{3, 0.99f}, {0, 0.875f}, {2, 0.625f}, {1, 0.5f}})); + const std::vector> files = NewFiles(progress); + ASSERT_EQ(1, files.size()); + // Full-text matches only file rows 0 and 2; vector candidates include all four rows. + // OR must add scalar-only row 1 and exclude row 3 despite its highest vector score. + disk_text_rankings_[files[0]->file_name] = {{0, 0.875f}, {2, 0.625f}}; + ASSERT_OK_AND_ASSIGN(int64_t snapshot, Commit(progress, 0)); + ASSERT_OK(writer->RefreshCommittedSnapshot(snapshot)); + ASSERT_OK(WriteSearchBatch(R"([ + [4,5,"lake","miss",[5]], [5,6,"other","keep",[6]], + [6,7,"lake","keep",[7]], [7,8,"other","miss",[8]] + ])", + {}, writer.get())); + ASSERT_OK_AND_ASSIGN(std::shared_ptr match, + PredicateBuilder::FullTextMatch( + schema_->GetFieldIndex("payload"), + std::make_shared("payload", text_query, std::nullopt))); + auto scalar = EqualString("pt", "keep"); + const std::vector>> branches{{match, scalar}, + {scalar, match}}; + for (const auto& children : branches) { + SCOPED_TRACE(children.front()->ToString()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr predicate, PredicateBuilder::Or(children)); + SearchQuery query{ + {"pt", "id", "payload", "_embedding_INDEX_SCORE"}, predicate, Vector(3), nullptr}; + // Neither OR branch alone is safe for split pruning. Plan all sources, then evaluate + // the union in the reader before TopK. Rows matching both branches must appear once. + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(context, nullptr)); + ASSERT_NO_FATAL_FAILURE(CheckSearch(plan, context, query, R"([ + [0,"miss",1,"lake",0.875], [0,"keep",2,"other",0.5], [0,"keep",3,"lake",0.625], + [0,"miss",5,"lake",0.75], [0,"keep",6,"other",0.25], [0,"keep",7,"lake",0.375] + ])")); + } + ASSERT_OK(PrepareAndClose(writer.get())); +} + // TODO(xinyu.lxy): Refactor the real-time hash index tests. class RealtimeHashIndexInteTest : public RealtimeWriteInteTest { protected: @@ -8295,6 +8990,7 @@ TEST_F(RealtimeWriteInteTest, TestReopenRealtimeContextAfterRollback) { ASSERT_OK(writer->Close()); } +// TODO(xinyu.lxy): Refactor this concurrent test. TEST_F(RealtimeWriteInteTest, TestConcurrentWritePrepareCommitReadAndRefresh) { auto run_test = [this](bool primary_key) { dir_ = UniqueTestDirectory::Create("local");