Skip to content

Commit 5272a73

Browse files
committed
Fix memory error
1 parent 236b7b8 commit 5272a73

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,4 +1508,57 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
15081508

15091509
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: false)
15101510
}
1511+
1512+
func testFindNearest() async throws {
1513+
let collRef = collectionRef(withDocuments: bookDocs)
1514+
let db = collRef.firestore
1515+
1516+
let measures: [DistanceMeasure] = [.euclidean, .dotProduct, .cosine]
1517+
let expectedResults: [[String: Sendable]] = [
1518+
["title": "The Hitchhiker's Guide to the Galaxy"],
1519+
["title": "One Hundred Years of Solitude"],
1520+
["title": "The Handmaid's Tale"],
1521+
]
1522+
1523+
for measure in measures {
1524+
let pipeline = db.pipeline()
1525+
.collection(collRef.path)
1526+
.findNearest(
1527+
field: Field("embedding"),
1528+
vectorValue: [10, 1, 3, 1, 2, 1, 1, 1, 1, 1],
1529+
distanceMeasure: measure, limit: 3
1530+
)
1531+
.select("title")
1532+
let snapshot = try await pipeline.execute()
1533+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
1534+
}
1535+
}
1536+
1537+
func testFindNearestWithDistance() async throws {
1538+
let collRef = collectionRef(withDocuments: bookDocs)
1539+
let db = collRef.firestore
1540+
1541+
let expectedResults: [[String: Sendable]] = [
1542+
[
1543+
"title": "The Hitchhiker's Guide to the Galaxy",
1544+
"computedDistance": 1.0,
1545+
],
1546+
[
1547+
"title": "One Hundred Years of Solitude",
1548+
"computedDistance": 12.041594578792296,
1549+
],
1550+
]
1551+
1552+
let pipeline = db.pipeline()
1553+
.collection(collRef.path)
1554+
.findNearest(
1555+
field: Field("embedding"),
1556+
vectorValue: [10, 1, 2, 1, 1, 1, 1, 1, 1, 1],
1557+
distanceMeasure: .euclidean, limit: 2,
1558+
distanceField: "computedDistance"
1559+
)
1560+
.select("title", "computedDistance")
1561+
let snapshot = try await pipeline.execute()
1562+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: false)
1563+
}
15111564
}

Firestore/core/src/api/stages.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace firebase {
2828
namespace firestore {
2929
namespace api {
3030

31+
using model::DeepClone;
32+
3133
google_firestore_v1_Pipeline_Stage CollectionSource::to_proto() const {
3234
google_firestore_v1_Pipeline_Stage result;
3335

@@ -191,7 +193,7 @@ google_firestore_v1_Pipeline_Stage FindNearestStage::to_proto() const {
191193
result.args_count = 3;
192194
result.args = nanopb::MakeArray<google_firestore_v1_Value>(3);
193195
result.args[0] = property_->to_proto();
194-
result.args[1] = *vector_;
196+
result.args[1] = *DeepClone(*vector_).release();
195197
result.args[2] = distance_measure_.proto();
196198

197199
nanopb::SetRepeatedField(
@@ -200,7 +202,7 @@ google_firestore_v1_Pipeline_Stage FindNearestStage::to_proto() const {
200202
nanopb::SharedMessage<google_firestore_v1_Value>>&
201203
entry) {
202204
return _google_firestore_v1_Pipeline_Stage_OptionsEntry{
203-
nanopb::MakeBytesArray(entry.first), *entry.second};
205+
nanopb::MakeBytesArray(entry.first), *DeepClone(*entry.second).release()};
204206
});
205207

206208
return result;

0 commit comments

Comments
 (0)