Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions pkg/vectorindex/ivfflat/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,27 @@ func (idx *IvfflatSearchIndex[T]) Search(
if err != nil {
return
}
if len(res.Batches) == 0 {
return nil, nil, moerr.NewEmptyVector(proc.Ctx)
}

if len(rt.BackgroundQueries) > 0 {
rt.BackgroundQueries[0] = res.LogicalPlan
}

distances = make([]float64, 0, rt.Limit)
resid := make([]any, 0, rt.Limit)
bat := res.Batches[0]

for i := 0; i < bat.RowCount(); i++ {
pk := vector.GetAny(bat.Vecs[0], i, true)
resid = append(resid, pk)
if len(res.Batches) == 0 {
return resid, distances, nil
}

dist := vector.GetFixedAtNoTypeCheck[float64](bat.Vecs[1], i)
dist = metric.DistanceTransformIvfflat(dist, idxcfg.OpType, metric.MetricType(idxcfg.Ivfflat.Metric))
distances = append(distances, dist)
for _, bat := range res.Batches {
for i := 0; i < bat.RowCount(); i++ {
pk := vector.GetAny(bat.Vecs[0], i, true)
resid = append(resid, pk)

dist := vector.GetFixedAtNoTypeCheck[float64](bat.Vecs[1], i)
dist = metric.DistanceTransformIvfflat(dist, idxcfg.OpType, metric.MetricType(idxcfg.Ivfflat.Metric))
distances = append(distances, dist)
}
}

res.Close()
Expand Down
10 changes: 5 additions & 5 deletions pkg/vectorindex/ivfflat/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import (
)

// give blob
func mock_runSql_streaming(
func mock_runSql(
proc *process.Process,
sql string,
) (executor.Result, error) {
return executor.Result{}, nil
}

func mock_runSql_streaming_parser_error(
func mock_runSql_parser_error(
proc *process.Process,
sql string,
) (executor.Result, error) {
Expand All @@ -44,7 +44,7 @@ func mock_runSql_streaming_parser_error(

func TestIvfSearchRace(t *testing.T) {

runSql = mock_runSql_streaming
runSql = mock_runSql

var idxcfg vectorindex.IndexConfig
var tblcfg vectorindex.IndexTableConfig
Expand All @@ -60,13 +60,13 @@ func TestIvfSearchRace(t *testing.T) {
idx := &IvfflatSearchIndex[float32]{}

_, _, err := idx.Search(proc, idxcfg, tblcfg, v, rt, 4)
require.NotNil(t, err)
require.Nil(t, err)

}

func TestIvfSearchParserError(t *testing.T) {

runSql = mock_runSql_streaming_parser_error
runSql = mock_runSql_parser_error

var idxcfg vectorindex.IndexConfig
var tblcfg vectorindex.IndexTableConfig
Expand Down
13 changes: 13 additions & 0 deletions test/distributed/cases/vector/vector_ivf.result
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ CREATE TABLE `ca_specify_answer_dataset` (
PRIMARY KEY (`id`),
KEY `ca_specify_answer_dataset_vec_index` USING ivfflat (`question_vector`) lists = 256 op_type 'vector_l2_ops'
);
SELECT
id,
question,
output_result,
L2_distance(question_vector, '[-0.02130127, 0.00995636, -0.036193848, -0.04208374, -0.015388489, -0.029830933, -0.0024261475, 0.026489258, -0.031311035, -0.0647583, 0.029464722, 0.0006284714, 0.047943115, -0.04336548, 0.006713867, 0.021942139, -0.017669678, -0.061279297, 0.0070266724, -0.021331787, -0.0032405853, -0.010627747, 0.003643036, -0.014732361, 0.0154418945, 0.046813965, -0.0026054382, -0.018234253, 0.008804321, 0.03567505, -0.012962341, -0.04437256, -0.021362305, 0.02230835, 0.017745972, 0.046325684, -0.008476257, 0.01550293, 0.048339844, -0.07867432, -0.03945923, 0.0049057007, 0.031433105, 0.004245758, -0.04006958, 0.023651123, -0.03338623, -0.034332275, -0.014457703, 0.022323608, -0.018585205, -0.024749756, 0.0077400208, -0.0059280396, 0.024093628, 0.03515625, 0.030960083, -0.044433594, 0.015960693, -0.016342163, 0.009391785, 0.09008789, -0.0028762817, 0.020126343, -0.034240723, -0.046722412, 0.036376953, -0.01574707, 0.009666443, -0.0154418945, 0.023880005, -0.0021953583, -0.001405716, -0.015205383, 0.013786316, -0.050079346, -0.02519226, -0.009552002, -0.017791748, 0.007980347, -0.029647827, -0.06488037, 0.039154053, -0.024734497, -0.005264282, -0.019058228, -0.009140015, 0.35205078, -0.03237915, 0.015899658, 0.019515991, 0.022659302, -0.053710938, -0.026885986, 0.03933716, -0.01864624, 0.0028648376, -0.015602112, -0.0010786057, 0.014198303, 0.038085938, -0.042175293, -0.0060539246, 0.014953613, 0.012786865, 0.009239197, -0.02999878, -0.025756836, -0.027740479, 0.011749268, -0.0024738312, 0.008583069, -0.029159546, -0.007663727, 0.012168884, -0.0158844, -0.047424316, 0.030273438, 0.010398865, -0.0047302246, -0.02758789, -0.00844574, 0.04586792, 0.0035800934, -0.03363037, -0.0060806274, 0.032165527, 0.03253174, -0.016799927, -0.031982422, 0.03225708, -0.004760742, -0.04360962, 0.0030231476, -0.010604858, -0.020309448, 0.0032405853, -0.03677368, 0.02130127, -0.011566162, 0.04711914, -0.027832031, 0.019683838, -0.011367798, 0.019729614, -0.016159058, 0.0015535355, 0.04244995, 0.006034851, 0.018508911, 0.05419922, -0.010040283, -0.021987915, -0.020050049, 0.072143555, 0.002752304, 0.006752014, 0.038970947, 0.03274536, -0.02532959, -0.00092983246, -0.0005965233, -0.031677246, -0.025848389, -0.060577393, 0.025238037, -0.019256592, -0.008171082, 0.030517578, 0.11029053, -0.030029297, -0.020050049, 0.013343811, 0.033447266, -0.06939697, 0.031433105, 0.017211914, -0.03375244, -0.0018796921, 0.03704834, -0.000746727, 0.033111572, 0.012939453, -0.02507019, 0.018600464, 0.0184021, 0.041534424, -0.009666443, 0.012702942, -0.03866577, 0.0121154785, -0.013931274, 0.015235901, 0.0143585205, -0.028060913, -0.016677856, -0.048583984, 0.031036377, -0.012619019, 0.009269714, -0.021438599, 0.021331787, 0.0017442703, 0.061431885, -0.002714157, -0.00069999695, -0.003583908, 0.03274536, 0.008575439, -0.0101623535, 0.03149414, 0.0015354156, 0.011421204, -0.020706177, 0.012763977, 0.007205963, 0.03829956, 0.003200531, -0.02720642, -0.04522705, -0.038848877, -0.013160706, 0.02078247, -0.058135986, -0.0524292, -0.014732361, -0.013885498, -0.005836487, -0.061431885, 0.0017461777, -0.050720215, 0.016067505, 0.02458191, -0.04852295, 0.028640747, 0.009735107, -0.034179688, -0.03286743, 0.022155762, -0.0031642914, -0.08239746, 0.010093689, 0.004524231, 0.03591919, -0.019851685, -0.0037956238, -0.0048713684, 0.021530151, -0.009796143, -0.025375366, 0.0057411194, 0.021102905, 0.0056648254, -0.05355835, -0.016967773, 0.0054512024, 0.018920898, 0.049743652, -0.01637268, -0.009468079, -0.036102295, -0.0071258545, -0.032287598, -0.05328369, 0.019500732, 0.016571045, 0.025100708, -0.036621094, 0.008346558, 0.017990112, 0.021942139, -0.0032196045, 0.029769897, -0.06555176, 0.045684814, -0.008270264, -0.03656006, 0.001244545, 0.01335144, -0.020233154, -0.0010175705, -0.041534424, 0.005012512, 0.010749817, 0.033935547, -0.030899048, -0.014076233, -0.0040016174, -0.004016876, 0.0052948, -0.018341064, -0.06738281, -0.008705139, -0.002002716, -0.0056114197, 0.030593872, -0.018371582, 0.031051636, -0.024795532, -0.017791748, -0.0041236877, -0.015403748, 0.04067993, -0.005252838, 0.02557373, -0.06536865, -0.057250977, -0.0124053955, -0.015220642, 0.021209717, 0.017684937, -0.0118637085, -0.029510498, -0.04611206, -0.020431519, 0.0020008087, 0.05718994, -0.03152466, -0.03665161, 0.061309814, 0.045532227, -0.005844116, 0.003446579, -0.020690918, 0.003704071, 0.028793335, -0.015235901, 0.024917603, -0.03656006, -0.033996582, 0.0031814575, 0.005302429, -0.003255844, -0.02645874, -0.018447876, 0.05532837, 0.034484863, 0.017730713, 0.007827759, 0.029464722, 0.044677734, 0.020935059, 0.016555786, -0.011314392, 0.0071792603, 0.039031982, -0.004333496, 0.034210205, -0.012802124, -0.008453369, -0.026885986, 0.01360321, 0.0014533997, -0.016525269, 0.025558472, 0.0014915466, -0.009277344, -0.009841919, -0.02722168, -0.030395508, 0.017318726, -0.0513916, -0.085876465, 0.0011043549, 0.045684814, -0.050628662, -0.008926392, -0.04849243, 0.004322052, 0.044433594, 0.009796143, 0.053131104, -0.030395508, -0.01737976, 0.022491455, -0.00415802, 0.039276123, 0.0023212433, 0.031982422, -0.04815674, 0.042419434, 0.005634308, 0.021774292, 0.08343506, 0.038360596, 0.0036144257, -0.018005371, -0.017852783, -0.0023841858, 0.013755798, -0.048675537, 0.007408142, -0.030075073, -0.030334473, -0.022216797, 0.04232788, 0.023880005, -0.013160706, 0.032836914, -0.027542114, 0.014518738, -0.03616333, -0.008979797, 0.011795044, 0.026657104, 0.007633209, -0.009994507, -0.029708862, 0.025680542, -0.0013580322, -0.025360107, 0.0423584, -0.071899414, -0.04159546, 0.000685215, 0.00566864, -0.0075531006, -0.014167786, -0.0368042, 0.010765076, 0.011299133, -0.0121536255, 0.02960205, -0.0008907318, 0.011734009, 0.04849243, -0.021499634, -0.010597229, 0.038482666, -0.03857422, -0.0073890686, 0.013084412, 0.0024356842, -0.035461426, -0.033233643, 0.01940918, -0.025527954, -0.02180481, 0.001127243, -0.008872986, 0.00077962875, -0.023071289, -0.0042915344, 0.029403687, -0.04449463, -0.016326904, -0.029754639, -0.030319214, 0.033477783, 0.030975342, 0.040100098, 0.022659302, 0.034576416, -0.036621094, 0.036987305, -0.012145996, -0.040252686, -0.020950317, -0.0008087158, 0.023254395, 0.008201599, 0.0023097992, -0.01586914, -0.048553467, -0.038726807, 0.019012451, 0.013572693, -0.007194519, 0.02633667, -0.014030457, -0.037750244, -0.00004029274, -0.03137207, -0.004211426, 0.013168335, 0.028961182, -0.0680542, 0.0022602081, 0.021484375, -0.030288696, 0.0061950684, -0.035003662, 0.042938232, -0.022415161, 0.015205383, 0.019866943, -0.030410767, -0.025360107, -0.009132385, -0.010826111, -0.056030273, -0.016525269, -0.04550171, -0.002571106, 0.010765076, 0.0032310486, 0.03967285, -0.025161743, -0.01335144, -0.027496338, -0.033966064, 0.033996582, 0.011421204, 0.007095337, -0.0112838745, 0.009727478, -0.060180664, -0.010177612, -0.025604248, 0.020736694, 0.029846191, -0.0067634583, -0.034057617, -0.0018396378, -0.029342651, 0.0579834, 0.017028809, 0.035186768, 0.02482605, 0.068115234, 0.012435913, -0.014274597, 0.006401062, 0.023620605, 0.010772705, 0.032196045, 0.026824951, -0.012535095, -0.025970459, -0.010345459, -0.013328552, 0.00080537796, 0.018188477, -0.00007379055, -0.0007443428, -0.030593872, 0.033203125, 0.008705139, 0.05441284, 0.01235199, -0.0064926147, -0.027648926, 0.043518066, 0.044708252, -0.0023498535, 0.012413025, 0.005569458, -0.016815186, -0.011146545, -0.0018472672, 0.0021820068, -0.01914978, 0.028518677, 0.011352539, 0.013893127, -0.0501709, -0.0045433044, -0.043914795, -0.010292053, 0.005962372, -0.016098022, -0.004650116, 0.013473511, -0.046447754, -0.00034213066, 0.010437012, -0.004753113, 0.035186768, 0.010665894, -0.043395996, -0.007583618, -0.049621582, 0.0064811707, -0.042266846, -0.02758789, -0.06591797, -0.022644043, 0.039855957, -0.016204834, 0.03918457, 0.02999878, -0.026977539, -0.002374649, 0.037475586, 0.020980835, 0.034606934, -0.02368164, 0.002544403, -0.06726074, 0.029510498, -0.031280518, 0.027450562, -0.04333496, -0.056030273, -0.046966553, 0.010116577, 0.002878189, -0.007774353, 0.01222229, -0.06982422, -0.020462036, 0.030929565, -0.01751709, -0.00932312, -0.070007324, -0.019897461, 0.012252808, 0.03427124, -0.011856079, -0.007118225, -0.034057617, -0.002117157, 0.032104492, 0.04058838, -0.032348633, -0.025314331, 0.006580353, 0.024765015, 0.018997192, -0.026641846, 0.020523071, 0.026016235, -0.005531311, -0.006362915, -0.07092285, -0.04373169, -0.050476074, -0.013511658, -0.018920898, -0.03945923, 0.020462036, 0.015914917, -0.047088623, -0.013694763, 0.018081665, 0.039520264, -0.004989624, 0.007926941, 0.003780365, -0.0008049011, -0.019821167, 0.0036888123, -0.004611969, -0.045776367, 0.027175903, -0.040924072, -0.017105103, -0.030807495, 0.05130005, -0.010978699, -0.0390625, -0.002067566, -0.021591187, -0.006187439, -0.022232056, -0.003993988, -0.022583008, -0.004787445, -0.0035629272, 0.032684326, 0.0032920837, 0.000018417835, -0.030807495, 0.05557251, -0.031036377, 0.0017738342, 0.07098389, 0.042785645, 0.032196045, 0.021499634, 0.037597656, -0.0041236877, 0.0070152283, -0.0013198853, -0.0056762695, 0.0067329407, 0.0025424957, -0.04333496, -0.026046753, -0.05090332, 0.019104004, 0.03857422, 0.04876709, 0.022521973, 0.07684326, 0.035614014, 0.007949829, 0.021331787, 0.033966064, -0.010810852, 0.018859863, -0.0256958, -0.032836914, 0.051116943, 0.04043579, 0.013412476, 0.023620605, 0.0020828247, 0.015106201, 0.012283325, -0.041229248, -0.0023441315, -0.013633728, -0.014389038, 0.08380127, -0.035217285, -0.018707275, 0.012779236, -0.013587952, -0.023254395, -0.004070282, 0.024017334, -0.016052246, 0.015579224, 0.010437012, -0.0060043335, 0.007144928, 0.037506104, -0.014976501, 0.029647827, 0.030929565, -0.020385742, -0.041748047, -0.005580902, 0.018447876, -0.006095886, 0.019485474, -0.001912117, 0.0057640076, 0.052642822, -0.01158905, 0.0064849854, -0.01625061, -0.04309082, -0.009208679, 0.021774292, 0.0129852295, -0.041503906, -0.00856781, -0.042053223, -0.007881165, 0.009628296, 0.031707764, 0.017913818, 0.004852295, 0.07196045, -0.012420654, 0.006580353, -0.087402344, 0.009407043, 0.033691406, 0.006462097, -0.00061130524, -0.019256592, 0.021362305, 0.020370483, -0.002696991, -0.010108948, -0.010467529, 0.025131226, -0.00088739395, -0.023468018, 0.0041503906, -0.043029785, 0.028671265, 0.01802063, -0.0048294067, -0.008674622, 0.03564453, 0.03189087, -0.0053253174, 0.04928589, 0.019134521, -0.036590576, -0.022964478, -0.03378296, 0.0184021, 0.013648987, 0.020996094, 0.025436401, 0.019042969, 0.026245117, -0.08337402, 0.04574585, 0.0018444061, -0.0046424866, 0.0385437, 0.007083893, 0.025878906, 0.015823364, -0.005378723, -0.023391724, -0.060943604, -0.007347107, -0.013893127, 0.06689453, -0.043792725, -0.020187378, -0.030166626, 0.012512207, 0.012252808, 0.028839111, -0.020858765, 0.014823914, 0.02633667, -0.024139404, 0.008094788, 0.038024902, -0.036987305, -0.0008869171, -0.03189087, -0.014816284, -0.010940552, 0.062347412, 0.029190063, -0.0001834631, 0.033966064, -0.013427734, 0.0008239746, 0.0028858185, -0.00484848, -0.015037537, 0.0018930435, -0.0048065186, 0.0513916, -0.01876831, -0.024475098, 0.00024163723, -0.036315918, -0.0031967163, -0.008987427, -0.033203125, -0.018035889, 0.016601562, -0.017471313, 0.058624268, -0.038726807, -0.022903442, -0.013221741, -0.024307251, 0.019958496, 0.08746338, 0.07147217, -0.025283813, 0.0034866333, -0.031921387, -0.020355225, -0.012908936, 0.009223938, 0.07098389, 0.024871826, 0.027770996, -0.0011377335, 0.012069702, -0.038024902, 0.0107803345, 0.099731445, 0.04925537, -0.005672455, -0.0234375, -0.011878967, -0.003829956, -0.024215698, -0.019363403, 0.032440186, -0.004337311, -0.0044670105, 0.02633667, -0.016845703, -0.020019531, 0.011405945, 0.052978516, 0.002298355, -0.030395508, -0.0037193298, 0.016021729, -0.06414795, 0.0058631897, 0.03805542, -0.0423584, -0.03387451, 0.017547607, -0.0149383545, 0.05105591, 0.0075798035, 0.056365967, -0.016601562, -0.016098022, 0.0131073, 0.019302368, 0.049682617, -0.04260254, 0.011550903, -0.035736084, -0.04421997, 0.0015802383, 0.031173706, 0.0143585205, -0.035583496, 0.03363037, 0.021972656, -0.014015198, 0.0011940002, -0.01146698, -0.009613037, 0.018173218, -0.04498291, -0.0006213188, -0.016967773, 0.00035572052, 0.04727173, 0.018096924, -0.002822876, 0.0061798096, 0.009613037, 0.011810303, -0.003376007, -0.0491333, 0.004676819, -0.03543091, 0.010574341, 0.04449463, -0.03302002, -0.020935059, 0.026138306, 0.015403748, 0.030731201, -0.02406311, 0.04675293, -0.00031733513, 0.0010662079, 0.0060157776, -0.0030078888, -0.04925537, -0.044891357, 0.039520264, -0.023162842, -0.034576416, -0.012031555, 0.035339355, 0.053222656, 0.05178833, -0.020980835, -0.0051651, 0.021118164, -0.02709961, 0.006252289, 0.024963379, -0.0010414124, -0.03479004, -0.017608643, 0.013183594, -0.0015010834, -0.009483337, 0.061798096, -0.06335449, 0.026931763, -0.016296387, 0.043060303, -0.047851562, 0.010414124, 0.003271103, -0.003408432, 0.048736572, -0.05041504, -0.010398865, -0.02458191, -0.013320923, -0.0033168793, -0.022369385, -0.05557251, 0.04309082, 0.036987305, -0.030975342, -0.024230957, -0.020935059, -0.0027713776, 0.0021038055, -0.014511108, 0.026168823, 0.0030136108, 0.001627922, 0.0006275177, 0.01272583, -0.035949707, -0.030731201, -0.027374268, -0.0048294067, -0.019683838, 0.032073975, -0.028778076, 0.008834839, 0.04147339, 0.026489258, -0.0028266907, -0.013786316, 0.002325058, 0.06463623, -0.015075684, 0.029266357, -0.019454956, 0.072021484, -0.00894928, -0.001332283, -0.053863525, 0.014480591, -0.019943237, 0.021743774, 0.017410278, 0.026763916, -0.033569336, -0.005004883, 0.033081055, -0.00844574, -0.0234375, -0.030319214, -0.012359619, -0.014640808, -0.012825012, 0.0046844482, 0.010536194, 0.01083374, 0.014762878, -0.02330017]') AS distance
FROM
`ca_specify_answer_dataset`
WHERE
status = 1
ORDER BY
distance ASC
LIMIT 10;
id question output_result distance
load data infile '$resources/vector/ca_specify_answer_dataset.csv' into table ca_specify_answer_dataset FIELDS TERMINATED BY ',' ignore 1 lines;
SELECT
id,
Expand Down
Loading
Loading