Skip to content

Commit 80a79ae

Browse files
committed
add array maximun and minimum
1 parent d0f60c4 commit 80a79ae

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ public extension Expression {
523523
return FunctionExpression(functionName: "array_get", args: [self, offsetExpression])
524524
}
525525

526+
func arrayMaximum() -> FunctionExpression {
527+
return FunctionExpression(functionName: "maximum", args: [self])
528+
}
529+
530+
func arrayMinimum() -> FunctionExpression {
531+
return FunctionExpression(functionName: "minimum", args: [self])
532+
}
533+
526534
func greaterThan(_ other: Expression) -> BooleanExpression {
527535
return BooleanExpression(functionName: "greater_than", args: [self, other])
528536
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Expression.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,30 @@ public protocol Expression: Sendable {
461461
/// - Returns: A new `FunctionExpression` representing the "arrayGet" operation.
462462
func arrayGet(_ offsetExpression: Expression) -> FunctionExpression
463463

464+
/// Creates an expression that returns the maximum element of an array.
465+
///
466+
/// Assumes `self` evaluates to an array.
467+
///
468+
/// ```swift
469+
/// // Get the maximum value in the "scores" array.
470+
/// Field("scores").arrayMaximum()
471+
/// ```
472+
///
473+
/// - Returns: A new `FunctionExpression` representing the maximum element of the array.
474+
func arrayMaximum() -> FunctionExpression
475+
476+
/// Creates an expression that returns the minimum element of an array.
477+
///
478+
/// Assumes `self` evaluates to an array.
479+
///
480+
/// ```swift
481+
/// // Get the minimum value in the "scores" array.
482+
/// Field("scores").arrayMinimum()
483+
/// ```
484+
///
485+
/// - Returns: A new `FunctionExpression` representing the minimum element of the array.
486+
func arrayMinimum() -> FunctionExpression
487+
464488
/// Creates a `BooleanExpression` that returns `true` if this expression is greater
465489
/// than the given expression.
466490
///

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,4 +3917,33 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
39173917

39183918
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: true)
39193919
}
3920+
3921+
func testArrayMaxMinWorks() async throws {
3922+
let collRef = collectionRef(withDocuments: [
3923+
"doc1": ["scores": [10, 20, 5]],
3924+
"doc2": ["scores": [-1, -5, 0]],
3925+
"doc3": ["scores": [100.5, 99.5, 100.6]],
3926+
"doc4": ["scores": []],
3927+
])
3928+
let db = collRef.firestore
3929+
3930+
let pipeline = db.pipeline()
3931+
.collection(collRef.path)
3932+
.sort([Field(FieldPath.documentID()).ascending()])
3933+
.select([
3934+
Field("scores").arrayMaximum().as("maxScore"),
3935+
Field("scores").arrayMinimum().as("minScore"),
3936+
])
3937+
3938+
let snapshot = try await pipeline.execute()
3939+
3940+
let expectedResults: [[String: Sendable?]] = [
3941+
["maxScore": 20, "minScore": 5],
3942+
["maxScore": 0, "minScore": -5],
3943+
["maxScore": 100.6, "minScore": 99.5],
3944+
["maxScore": nil, "minScore": nil],
3945+
]
3946+
3947+
TestHelper.compare(snapshot: snapshot, expected: expectedResults, enforceOrder: true)
3948+
}
39203949
}

0 commit comments

Comments
 (0)