Skip to content

Commit 5742661

Browse files
authored
Update C++ code to use TemporalPolicy instead of a timestamp (#356)
1 parent da176af commit 5742661

26 files changed

+467
-302
lines changed

apis/python/src/tiledb/vector_search/module.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "detail/linalg/partitioned_matrix.h"
1818
#include "detail/linalg/tdb_matrix.h"
1919
#include "detail/linalg/tdb_partitioned_matrix.h"
20+
#include "detail/time/temporal_policy.h"
2021

2122
namespace py = pybind11;
2223
using Ctx = tiledb::Context;
@@ -275,9 +276,8 @@ static void declare_nuv_query_heap_finite_ram(
275276
centroids, query_vectors, nprobe, nthreads);
276277

277278
auto temporal_policy{
278-
(timestamp == 0) ?
279-
tiledb::TemporalPolicy() :
280-
tiledb::TemporalPolicy(tiledb::TimeTravel, timestamp)};
279+
(timestamp == 0) ? TemporalPolicy() :
280+
TemporalPolicy(TimeTravel, timestamp)};
281281

282282
auto mat = tdbColMajorPartitionedMatrix<T, Id_Type, Id_Type>(
283283
ctx,
@@ -528,9 +528,8 @@ static void declare_dist_qv(py::module& m, const std::string& suffix) {
528528
size_t upper_bound{0};
529529
auto nthreads = std::thread::hardware_concurrency();
530530
auto temporal_policy{
531-
(timestamp == 0) ?
532-
tiledb::TemporalPolicy() :
533-
tiledb::TemporalPolicy(tiledb::TimeTravel, timestamp)};
531+
(timestamp == 0) ? TemporalPolicy() :
532+
TemporalPolicy(TimeTravel, timestamp)};
534533

535534
py::buffer_info buf_info = active_queries_arr.request();
536535
auto shape = active_queries_arr.shape();
@@ -644,7 +643,11 @@ PYBIND11_MODULE(_tiledbvspy, m) {
644643
size_t start_pos,
645644
size_t end_pos,
646645
uint64_t timestamp) -> std::vector<uint32_t> {
647-
auto r = read_vector<uint32_t>(ctx, uri, start_pos, end_pos, timestamp);
646+
TemporalPolicy temporal_policy =
647+
(timestamp == 0) ? TemporalPolicy() :
648+
TemporalPolicy(TimeTravel, timestamp);
649+
auto r = read_vector<uint32_t>(
650+
ctx, uri, start_pos, end_pos, temporal_policy);
648651
return r;
649652
});
650653
m.def(
@@ -654,7 +657,11 @@ PYBIND11_MODULE(_tiledbvspy, m) {
654657
size_t start_pos,
655658
size_t end_pos,
656659
uint64_t timestamp) -> std::vector<uint64_t> {
657-
auto r = read_vector<uint64_t>(ctx, uri, start_pos, end_pos, timestamp);
660+
TemporalPolicy temporal_policy =
661+
(timestamp == 0) ? TemporalPolicy() :
662+
TemporalPolicy(TimeTravel, timestamp);
663+
auto r = read_vector<uint64_t>(
664+
ctx, uri, start_pos, end_pos, temporal_policy);
658665
return r;
659666
});
660667

apis/python/src/tiledb/vector_search/type_erased_module.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
#include <pybind11/pybind11.h>
3636
#include <pybind11/stl.h>
3737

38+
#include "api/api_defs.h"
3839
#include "api/feature_vector.h"
3940
#include "api/feature_vector_array.h"
40-
4141
#include "api/flat_l2_index.h"
4242
#include "api/ivf_flat_index.h"
4343
#include "api/vamana_index.h"
44-
45-
#include "api/api_defs.h"
44+
#include "detail/time/temporal_policy.h"
4645

4746
namespace py = pybind11;
4847

@@ -180,8 +179,13 @@ void init_type_erased_module(py::module_& m) {
180179
const std::string& ids_uri,
181180
size_t num_vectors,
182181
size_t timestamp) {
183-
new (&instance)
184-
FeatureVectorArray(ctx, uri, ids_uri, num_vectors, timestamp);
182+
new (&instance) FeatureVectorArray(
183+
ctx,
184+
uri,
185+
ids_uri,
186+
num_vectors,
187+
timestamp == 0 ? TemporalPolicy() :
188+
TemporalPolicy(TimeTravel, timestamp));
185189
},
186190
py::keep_alive<1, 2>(), // FeatureVectorArray should keep ctx alive.
187191
py::arg("ctx"),
@@ -276,7 +280,11 @@ void init_type_erased_module(py::module_& m) {
276280
const tiledb::Context& ctx,
277281
const std::string& group_uri,
278282
size_t timestamp) {
279-
new (&instance) IndexVamana(ctx, group_uri, timestamp);
283+
new (&instance) IndexVamana(
284+
ctx,
285+
group_uri,
286+
timestamp == 0 ? TemporalPolicy() :
287+
TemporalPolicy(TimeTravel, timestamp));
280288
},
281289
py::keep_alive<1, 2>(), // IndexVamana should keep ctx alive.
282290
py::arg("ctx"),

apis/python/test/test_ingestion.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,18 @@ def test_ingestion_with_updates(tmp_path):
512512
source_uri=os.path.join(dataset_dir, "data.u8bin"),
513513
partitions=partitions,
514514
)
515+
516+
# TODO(paris): Fix Vamana to have same metadata as Python and re-enable.
517+
# with tiledb.Group(index_uri, "r", ctx={}) as group:
518+
# ingestion_timestamps = [int(x) for x in list(json.loads(group.meta.get("ingestion_timestamps", "[]")))]
519+
# base_sizes = [int(x) for x in list(json.loads(group.meta.get("base_sizes", "[]")))]
520+
# assert len(ingestion_timestamps) == 1
521+
# assert len(base_sizes) == 1
522+
# assert base_sizes[0] == 1000
523+
# timestamp_2030 = 1903946089000
524+
# timestamp_5_minutes_ago = int((time.time() - 5 * 60) * 1000)
525+
# assert ingestion_timestamps[0] > timestamp_5_minutes_ago and ingestion_timestamps[0] < timestamp_2030
526+
515527
_, result = index.query(queries, k=k, nprobe=nprobe)
516528
assert accuracy(result, gt_i) == 1.0
517529

0 commit comments

Comments
 (0)