Skip to content

Commit 69ced35

Browse files
committed
add pow()
1 parent c720527 commit 69ced35

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ public extension Expression {
296296
return FunctionExpression("ln", [self])
297297
}
298298

299+
func pow(_ exponent: Sendable) -> FunctionExpression {
300+
return FunctionExpression("pow", [self, Helper.sendableToExpr(exponent)])
301+
}
302+
303+
func pow(_ exponent: Expression) -> FunctionExpression {
304+
return FunctionExpression("pow", [self, exponent])
305+
}
306+
299307
func exp() -> FunctionExpression {
300308
return FunctionExpression("exp", [self])
301309
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ public protocol Expression: Sendable {
3636

3737
// --- Added Mathematical Operations ---
3838

39+
/// Creates an expression that returns the value of self raised to the power of Y.
40+
///
41+
/// Returns zero on underflow.
42+
///
43+
/// ```swift
44+
/// // Get the value of the "amount" field raised to the power of 2.
45+
/// Field("amount").pow(2)
46+
/// ```
47+
///
48+
/// - Parameter exponent: The exponent to raise self to.
49+
/// - Returns: A new `FunctionExpression` representing the power of the number.
50+
func pow(_ exponent: Sendable) -> FunctionExpression
51+
52+
/// Creates an expression that returns the value of self raised to the power of Y.
53+
///
54+
/// Returns zero on underflow.
55+
///
56+
/// ```swift
57+
/// // Get the value of the "amount" field raised to the power of the "exponent" field.
58+
/// Field("amount").pow(Field("exponent"))
59+
/// ```
60+
///
61+
/// - Parameter exponent: The exponent to raise self to.
62+
/// - Returns: A new `FunctionExpression` representing the power of the number.
63+
func pow(_ exponent: Expression) -> FunctionExpression
64+
3965
/// Creates an expression that returns the natural logarithm of self.
4066
///
4167
/// ```swift

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,32 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
21302130
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
21312131
}
21322132

2133+
func testPowWorks() async throws {
2134+
let collRef = collectionRef(withDocuments: [
2135+
"doc1": ["base": 2, "exponent": 3],
2136+
"doc2": ["base": 3, "exponent": 2],
2137+
"doc3": ["base": 4, "exponent": 0.5],
2138+
])
2139+
let db = collRef.firestore
2140+
2141+
let pipeline = db.pipeline()
2142+
.collection(collRef.path)
2143+
.select([
2144+
Field("base").pow(Field("exponent")).as("powValue"),
2145+
])
2146+
.sort([Field("powValue").ascending()])
2147+
2148+
let snapshot = try await pipeline.execute()
2149+
2150+
let expectedResults: [[String: Sendable]] = [
2151+
["powValue": 2],
2152+
["powValue": 8],
2153+
["powValue": 9],
2154+
]
2155+
2156+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2157+
}
2158+
21332159
func testExpWorks() async throws {
21342160
let collRef = collectionRef(withDocuments: [
21352161
"doc1": ["value": 1],

0 commit comments

Comments
 (0)