File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed
SwiftAPI/Pipeline/Expressions Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -296,6 +296,14 @@ public extension Expression {
296
296
return FunctionExpression ( " ln " , [ self ] )
297
297
}
298
298
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
+
299
307
func exp( ) -> FunctionExpression {
300
308
return FunctionExpression ( " exp " , [ self ] )
301
309
}
Original file line number Diff line number Diff line change @@ -36,6 +36,32 @@ public protocol Expression: Sendable {
36
36
37
37
// --- Added Mathematical Operations ---
38
38
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
+
39
65
/// Creates an expression that returns the natural logarithm of self.
40
66
///
41
67
/// ```swift
Original file line number Diff line number Diff line change @@ -2130,6 +2130,32 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
2130
2130
TestHelper . compare ( pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true )
2131
2131
}
2132
2132
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
+
2133
2159
func testExpWorks( ) async throws {
2134
2160
let collRef = collectionRef ( withDocuments: [
2135
2161
" doc1 " : [ " value " : 1 ] ,
You can’t perform that action at this time.
0 commit comments