Skip to content

MLE-19601 : Update annTopK in Node API #937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
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
14 changes: 13 additions & 1 deletion lib/plan-builder-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ function castArg(arg, funcName, paramName, argPos, paramTypes) {
}
});
return true;
case 'PlanAnnTopKOptions':
const planAnnTopKOptionsSet = new Set(['maxDistance', 'max-distance', 'searchFactor','search-factor']);
if(Object.getPrototypeOf(arg) === Map.prototype){
arg.forEach((value, key) => {
if(!planAnnTopKOptionsSet.has(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has invalid key- ${key}`
);
}
});
Comment on lines +405 to +413

This comment was marked as off-topic.

}
return true;
default:
return false;
}
Expand Down Expand Up @@ -428,7 +440,7 @@ function castArg(arg, funcName, paramName, argPos, paramTypes) {
break;
case 'string':
if (isProtoChained(paramTypes, [types.XsAnyAtomicType, types.TextNode, types.JsonContentNode, types.XmlContentNode,
types.XsString])) {
types.XsString, types.ServerType])) {
return arg;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/plan-builder-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -8089,7 +8089,7 @@ shortestPath(...args) {
*/
annTopK(...args) {
const namer = bldrbase.getNamer(args, 'inputK');
const paramdefs = [['inputK', [types.XsInteger, PlanParam], true, false], ['vectorColumn', [PlanExprCol, PlanColumn, types.XsString], true, false], ['queryVector', [PlanParam, types.VecVector], true, false], ['distanceColumn', [PlanExprCol, PlanColumn, types.XsString], false, false], ['options', [PlanAnnTopKOptions], false, false]];
const paramdefs = [['inputK', [types.XsInteger, PlanParam], true, false], ['vectorColumn', [PlanExprCol, PlanColumn, types.XsString], true, false], ['queryVector', [PlanParam, types.VecVector], true, false], ['distanceColumn', [PlanExprCol, PlanColumn, types.XsString], false, false], ['options', [PlanAnnTopKOptions], false, true]];
const checkedArgs = (namer !== null) ?
bldrbase.makeNamedArgs(namer, 'PlanModifyPlan.annTopK', 3, new Set(['inputK', 'vectorColumn', 'queryVector', 'distanceColumn', 'options']), paramdefs, args) :
bldrbase.makePositionalArgs('PlanModifyPlan.annTopK', 3, false, paramdefs, args);
Expand Down
82 changes: 72 additions & 10 deletions test-basic/annTopK.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let serverConfiguration = {};
const execPlan = pbb.execPlan;

describe('tests for annTopK', function () {
this.timeout(5000)
before(function (done) {
try {
testlib.findServerConfiguration(serverConfiguration);
Expand All @@ -27,22 +28,83 @@ describe('tests for annTopK', function () {
}
});

it('happy path', function (done) {
it('annTopK without PlanAnnTopKOptions', function (done) {
execPlan(p
.fromView('vectors', 'persons', '')
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'), 0.5)
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'))
.orderBy(p.col('name'))
)
.then(function (response) {
const rows = response.rows;
assert(rows.length === 2, 'Expecting both rows in the view to be returned.');
assert(rows[0].name.value === 'Alice');
assert(rows[0].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
assert(rows[1].name.value === 'Bob');
assert(rows[1].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
done();
verifyResults(response.rows, done);
})
.catch(error => done(error));
});

it('annTopK with PlanAnnTopKOptions as a single string', function (done) {
execPlan(p
.fromView('vectors', 'persons', '')
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'), 'onlyIndex')
.orderBy(p.col('name'))
)
.then(function (response) {
verifyResults(response.rows, done);
})
.catch(done);
.catch(error => done(error));
});

it('annTopK with PlanAnnTopKOptions as an array of string', function (done) {
execPlan(p
.fromView('vectors', 'persons', '')
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
['onlyIndex', "maxDistance=0.15", "searchFactor=1.0"])
.orderBy(p.col('name'))
).then(function (response) {
verifyResults(response.rows, done);
}).catch(error => done(error));
});

it('annTopK with PlanAnnTopKOptions as a map', function (done) {
const planAnnTopKOptionsMap = new Map();
planAnnTopKOptionsMap.set("maxDistance", 0.158454656600952);
planAnnTopKOptionsMap.set("searchFactor", 10.0);
execPlan(p
.fromView('vectors', 'persons', '')
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
planAnnTopKOptionsMap)
.orderBy(p.col('name'))
)
.then(function (response) {
verifyResults(response.rows, done);
})
.catch(error => done(error));
});

it('annTopK with invalid PlanAnnTopKOptions', function (done) {
const planAnnTopKOptionsMap = new Map();
planAnnTopKOptionsMap.set('invalid', 10.0);
try{
execPlan(p
.fromView('vectors', 'persons', '')
.annTopK(10, p.col('embedding'), p.vec.vector([1.1, 2.2, 3.3]), p.col('distance'),
planAnnTopKOptionsMap)
.orderBy(p.col('name'))
);
} catch(error){
assert(error.message.toString().includes('options argument at 4 of PlanModifyPlan.annTopK() has invalid key- invalid'))
done();
}
});

function verifyResults(rows, done){
try {
assert(rows.length === 2, 'Expecting both rows in the view to be returned.');
assert(rows[0].name.value === 'Alice');
assert(rows[0].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
assert(rows[1].name.value === 'Bob');
assert(rows[1].distance.type === 'xs:float', 'Verifying that the distance column was populated.');
done();
} catch (error){
done(error)
}
}
});