|
@@ -0,0 +1,88 @@
|
|
|
|
|
+#include <gtest/gtest.h>
|
|
|
|
|
+
|
|
|
|
|
+#include "vector_search.hpp"
|
|
|
|
|
+
|
|
|
|
|
+using svapi::firstScanDepth;
|
|
|
|
|
+using svapi::kVectorFilterScanCap;
|
|
|
|
|
+using svapi::nextScanDepth;
|
|
|
|
|
+
|
|
|
|
|
+// The filtered walk re-runs the whole similarity search with a growing limit,
|
|
|
|
|
+// because the database client has no filtered similarity search. The round
|
|
|
|
|
+// trips ARE the cost: measured on prod, a filtered top_k 500 issued three
|
|
|
|
|
+// searches (2000, 8000, 10000) at 3.21 s against 0.26 s for the same query
|
|
|
|
|
+// unfiltered. These pin the depth decisions so the emulation can be tuned
|
|
|
|
|
+// without a database and a 12000-row collection.
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, TheFirstPassStartsAtFourTimesTopKWithAFloorOf64) {
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(500, 100000), 2000u);
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(100, 100000), 400u);
|
|
|
|
|
+ // The floor matters: 4 x 5 would be a pointlessly small round trip.
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(5, 100000), 64u);
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(1, 100000), 64u);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, AFilterMatchingFEWERRowsThanTopKGoesStraightToTheCap) {
|
|
|
|
|
+ // The early exit needs topK matches, so with fewer than topK matching rows
|
|
|
|
|
+ // it can never fire and every step before the cap is certain waste.
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(500, 300), kVectorFilterScanCap);
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(500, 0), kVectorFilterScanCap);
|
|
|
|
|
+ // Strictly fewer. With EXACTLY topK matches the exit is still reachable,
|
|
|
|
|
+ // so that case keeps the ordinary start rather than paying for the cap.
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(500, 500), 2000u);
|
|
|
|
|
+ EXPECT_EQ(firstScanDepth(500, 553), 2000u);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, TheFirstPassNeverExceedsTheCap) {
|
|
|
|
|
+ // topK x 4 would overshoot for a large topK; the cap is the ceiling.
|
|
|
|
|
+ EXPECT_LE(firstScanDepth(5000, 100000), kVectorFilterScanCap);
|
|
|
|
|
+ EXPECT_LE(firstScanDepth(1000000, 100000), kVectorFilterScanCap);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, WideningUsesTheOBSERVEDHitRateRatherThanABlindFactor) {
|
|
|
|
|
+ // The prod shape: 553 matching rows of 12447, so a scan of 2000 finds
|
|
|
|
|
+ // about 89. To reach 500 matches needs roughly 2000 x 500 / 89 = 11200,
|
|
|
|
|
+ // which is past the cap - so ONE jump instead of 2000 -> 8000 -> 10000.
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(2000, 500, 89), kVectorFilterScanCap);
|
|
|
|
|
+ // A denser filter needs less: 400 matches in 2000 means a rate of 20%, so
|
|
|
|
|
+ // 500 matches needs about 2500, plus the 25% headroom = 3125. But the
|
|
|
|
|
+ // x4 floor is 8000, which is larger, so the floor wins.
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(2000, 500, 400), 8000u);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, TheFourTimesStepIsAFLOORSoThisCanOnlySKIPStepsNeverShrink) {
|
|
|
|
|
+ // The guarantee that makes a mis-estimate safe: whatever the estimate
|
|
|
|
|
+ // says, the next pass is never smaller than the old blind widening would
|
|
|
|
|
+ // have made it, so progress is certain and nothing gets slower.
|
|
|
|
|
+ for (uint32_t scan : {64u, 256u, 400u, 1000u, 2000u}) {
|
|
|
|
|
+ for (std::size_t m : {std::size_t(1), std::size_t(10), std::size_t(1000)}) {
|
|
|
|
|
+ const uint32_t next = nextScanDepth(scan, 500, m);
|
|
|
|
|
+ EXPECT_GE(next, std::min(scan * 4, kVectorFilterScanCap))
|
|
|
|
|
+ << "scan=" << scan << " matches=" << m;
|
|
|
|
|
+ EXPECT_GT(next, scan) << "must make progress: scan=" << scan;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, ZeroMatchesFallsBackToTheFloorRatherThanDividingByZero) {
|
|
|
|
|
+ // No matches in this pass says nothing about the rate, so there is nothing
|
|
|
|
|
+ // to estimate from - and it must not be an arithmetic fault.
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(64, 500, 0), 256u);
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(2000, 500, 0), 8000u);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, NothingEverExceedsTheCap) {
|
|
|
|
|
+ // The cap is the whole reason the walk is bounded; an estimate must not be
|
|
|
|
|
+ // able to step over it.
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(9999, 500, 1), kVectorFilterScanCap);
|
|
|
|
|
+ EXPECT_EQ(nextScanDepth(5000, 500, 1), kVectorFilterScanCap);
|
|
|
|
|
+ EXPECT_LE(nextScanDepth(2500, 500, 1), kVectorFilterScanCap);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(VectorScan, ALargeScanTimesTopKDoesNotOVERFLOW) {
|
|
|
|
|
+ // scan * topK * 5 is computed in 64 bits precisely because 32 would wrap:
|
|
|
|
|
+ // 10000 * 500 * 5 is 25 million, fine, but a caller-supplied topK is not
|
|
|
|
|
+ // bounded here and a wrap would produce a SMALLER next scan, which would
|
|
|
|
|
+ // break the progress guarantee silently.
|
|
|
|
|
+ EXPECT_LE(nextScanDepth(kVectorFilterScanCap, 4000000000u, 1), kVectorFilterScanCap);
|
|
|
|
|
+ EXPECT_GT(nextScanDepth(1000, 4000000000u, 1), 1000u);
|
|
|
|
|
+}
|