Skip to content

Commit c720527

Browse files
committed
add ln()
1 parent ead6684 commit c720527

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ public extension Expression {
292292
return FunctionExpression("floor", [self])
293293
}
294294

295+
func ln() -> FunctionExpression {
296+
return FunctionExpression("ln", [self])
297+
}
298+
295299
func exp() -> FunctionExpression {
296300
return FunctionExpression("exp", [self])
297301
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ public protocol Expression: Sendable {
3636

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

39-
/// Creates an expression that returns the largest numeric value that isn't greater than X.
39+
/// Creates an expression that returns the natural logarithm of self.
40+
///
41+
/// ```swift
42+
/// // Get the natural logarithm of the "amount" field.
43+
/// Field("amount").ln()
44+
/// ```
45+
///
46+
/// - Returns: A new `FunctionExpression` representing the natural logarithm of the number.
47+
func ln() -> FunctionExpression
48+
49+
/// Creates an expression that returns the largest numeric value that isn't greater than self.
4050
///
4151
/// ```swift
4252
/// // Get the floor of the "amount" field.
@@ -46,7 +56,7 @@ public protocol Expression: Sendable {
4656
/// - Returns: A new `FunctionExpression` representing the floor of the number.
4757
func floor() -> FunctionExpression
4858

49-
/// Creates an expression that returns e to the power of X.
59+
/// Creates an expression that returns e to the power of self.
5060
///
5161
/// Returns zero on underflow and nil on overflow.
5262
///

Firestore/Swift/Tests/Integration/PipelineTests.swift

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

2107+
func testLnWorks() async throws {
2108+
let collRef = collectionRef(withDocuments: [
2109+
"doc1": ["value": 1],
2110+
"doc2": ["value": exp(Double(2))],
2111+
"doc3": ["value": exp(Double(1))],
2112+
])
2113+
let db = collRef.firestore
2114+
2115+
let pipeline = db.pipeline()
2116+
.collection(collRef.path)
2117+
.select([
2118+
Field("value").ln().as("lnValue"),
2119+
])
2120+
.sort([Field("lnValue").ascending()])
2121+
2122+
let snapshot = try await pipeline.execute()
2123+
2124+
let expectedResults: [[String: Sendable]] = [
2125+
["lnValue": 0],
2126+
["lnValue": 1],
2127+
["lnValue": 2],
2128+
]
2129+
2130+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2131+
}
2132+
21072133
func testExpWorks() async throws {
21082134
let collRef = collectionRef(withDocuments: [
21092135
"doc1": ["value": 1],

0 commit comments

Comments
 (0)