ExpressionImplementation.swift 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. // Copyright 2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. extension Expression {
  15. func toBridge() -> ExprBridge {
  16. return (self as! BridgeWrapper).bridge
  17. }
  18. /// Creates an expression applying bitwise AND between this expression and an integer literal.
  19. /// Assumes `self` evaluates to an Integer or Bytes.
  20. ///
  21. /// - Note: This API is in beta.
  22. ///
  23. /// ```swift
  24. /// // Bitwise AND of "flags" field and 0xFF
  25. /// Field("flags").bitAnd(0xFF)
  26. /// ```
  27. ///
  28. /// - Parameter otherBits: The integer literal operand.
  29. /// - Returns: A new "FunctionExpression" representing the bitwise AND operation.
  30. func bitAnd(_ otherBits: Int) -> FunctionExpression {
  31. return FunctionExpression(
  32. functionName: "bit_and",
  33. args: [self, Helper.sendableToExpr(otherBits)]
  34. )
  35. }
  36. /// Creates an expression applying bitwise AND between this expression and a UInt8 literal (often
  37. /// for byte masks).
  38. /// Assumes `self` evaluates to an Integer or Bytes.
  39. /// - Note: This API is in beta.
  40. /// ```swift
  41. /// // Bitwise AND of "byteFlags" field and a byte mask
  42. /// Field("byteFlags").bitAnd(0b00001111 as UInt8)
  43. /// ```
  44. /// - Parameter otherBits: The UInt8 literal operand.
  45. /// - Returns: A new "FunctionExpression" representing the bitwise AND operation.
  46. func bitAnd(_ otherBits: UInt8) -> FunctionExpression {
  47. return FunctionExpression(
  48. functionName: "bit_and",
  49. args: [self, Helper.sendableToExpr(otherBits)]
  50. )
  51. }
  52. /// Creates an expression applying bitwise AND between this expression and another expression.
  53. /// Assumes `self` and `bitsExpression` evaluate to Integer or Bytes.
  54. /// - Note: This API is in beta.
  55. ///
  56. /// ```swift
  57. /// // Bitwise AND of "mask1" and "mask2" fields
  58. /// Field("mask1").bitAnd(Field("mask2"))
  59. /// ```
  60. /// - Parameter bitsExpression: The other `Expr` operand.
  61. /// - Returns: A new "FunctionExpression" representing the bitwise AND operation.
  62. func bitAnd(_ bitsExpression: Expression) -> FunctionExpression {
  63. return FunctionExpression(functionName: "bit_and", args: [self, bitsExpression])
  64. }
  65. /// Creates an expression applying bitwise OR between this expression and an integer literal.
  66. /// Assumes `self` evaluates to an Integer or Bytes.
  67. ///
  68. /// - Note: This API is in beta.
  69. ///
  70. /// ```swift
  71. /// // Bitwise OR of "flags" field and 0x01
  72. /// Field("flags").bitOr(0x01)
  73. /// ```
  74. ///
  75. /// - Parameter otherBits: The integer literal operand.
  76. /// - Returns: A new "FunctionExpression" representing the bitwise OR operation.
  77. func bitOr(_ otherBits: Int) -> FunctionExpression {
  78. return FunctionExpression(
  79. functionName: "bit_or",
  80. args: [self, Helper.sendableToExpr(otherBits)]
  81. )
  82. }
  83. /// Creates an expression applying bitwise OR between this expression and a UInt8 literal.
  84. /// Assumes `self` evaluates to an Integer or Bytes.
  85. /// - Note: This API is in beta.
  86. /// ```swift
  87. /// // Set specific bits in "controlByte"
  88. /// Field("controlByte").bitOr(0b10000001 as UInt8)
  89. /// ```
  90. /// - Parameter otherBits: The UInt8 literal operand.
  91. /// - Returns: A new "FunctionExpression" representing the bitwise OR operation.
  92. func bitOr(_ otherBits: UInt8) -> FunctionExpression {
  93. return FunctionExpression(
  94. functionName: "bit_or",
  95. args: [self, Helper.sendableToExpr(otherBits)]
  96. )
  97. }
  98. /// Creates an expression applying bitwise OR between this expression and another expression.
  99. /// Assumes `self` and `bitsExpression` evaluate to Integer or Bytes.
  100. /// - Note: This API is in beta.
  101. ///
  102. /// ```swift
  103. /// // Bitwise OR of "permissionSet1" and "permissionSet2" fields
  104. /// Field("permissionSet1").bitOr(Field("permissionSet2"))
  105. /// ```
  106. /// - Parameter bitsExpression: The other `Expr` operand.
  107. /// - Returns: A new "FunctionExpression" representing the bitwise OR operation.
  108. func bitOr(_ bitsExpression: Expression) -> FunctionExpression {
  109. return FunctionExpression(functionName: "bit_or", args: [self, bitsExpression])
  110. }
  111. /// Creates an expression applying bitwise XOR between this expression and an integer literal.
  112. /// Assumes `self` evaluates to an Integer or Bytes.
  113. ///
  114. /// - Note: This API is in beta.
  115. ///
  116. /// ```swift
  117. /// // Bitwise XOR of "toggle" field and 0xFFFF
  118. /// Field("toggle").bitXor(0xFFFF)
  119. /// ```
  120. ///
  121. /// - Parameter otherBits: The integer literal operand.
  122. /// - Returns: A new "FunctionExpression" representing the bitwise XOR operation.
  123. func bitXor(_ otherBits: Int) -> FunctionExpression {
  124. return FunctionExpression(
  125. functionName: "bit_xor",
  126. args: [self, Helper.sendableToExpr(otherBits)]
  127. )
  128. }
  129. /// Creates an expression applying bitwise XOR between this expression and a UInt8 literal.
  130. /// Assumes `self` evaluates to an Integer or Bytes.
  131. /// - Note: This API is in beta.
  132. /// ```swift
  133. /// // Toggle bits in "statusByte" using a XOR mask
  134. /// Field("statusByte").bitXor(0b01010101 as UInt8)
  135. /// ```
  136. /// - Parameter otherBits: The UInt8 literal operand.
  137. /// - Returns: A new "FunctionExpression" representing the bitwise XOR operation.
  138. func bitXor(_ otherBits: UInt8) -> FunctionExpression {
  139. return FunctionExpression(
  140. functionName: "bit_xor",
  141. args: [self, Helper.sendableToExpr(otherBits)]
  142. )
  143. }
  144. /// Creates an expression applying bitwise XOR between this expression and another expression.
  145. /// Assumes `self` and `bitsExpression` evaluate to Integer or Bytes.
  146. /// - Note: This API is in beta.
  147. ///
  148. /// ```swift
  149. /// // Bitwise XOR of "key1" and "key2" fields (assuming Bytes)
  150. /// Field("key1").bitXor(Field("key2"))
  151. /// ```
  152. /// - Parameter bitsExpression: The other `Expr` operand.
  153. /// - Returns: A new "FunctionExpression" representing the bitwise XOR operation.
  154. func bitXor(_ bitsExpression: Expression) -> FunctionExpression {
  155. return FunctionExpression(functionName: "bit_xor", args: [self, bitsExpression])
  156. }
  157. /// Creates an expression applying bitwise NOT to this expression.
  158. /// Assumes `self` evaluates to an Integer or Bytes.
  159. ///
  160. /// - Note: This API is in beta.
  161. ///
  162. /// ```swift
  163. /// // Bitwise NOT of "mask" field
  164. /// Field("mask").bitNot()
  165. /// ```
  166. ///
  167. /// - Returns: A new "FunctionExpression" representing the bitwise NOT operation.
  168. func bitNot() -> FunctionExpression {
  169. return FunctionExpression(functionName: "bit_not", args: [self])
  170. }
  171. /// Creates an expression applying bitwise left shift to this expression by a literal number of
  172. /// bits.
  173. /// Assumes `self` evaluates to Integer or Bytes.
  174. ///
  175. /// - Note: This API is in beta.
  176. ///
  177. /// ```swift
  178. /// // Left shift "value" field by 2 bits
  179. /// Field("value").bitLeftShift(2)
  180. /// ```
  181. ///
  182. /// - Parameter y: The number of bits (Int literal) to shift by.
  183. /// - Returns: A new "FunctionExpression" representing the bitwise left shift operation.
  184. func bitLeftShift(_ y: Int) -> FunctionExpression {
  185. return FunctionExpression(
  186. functionName: "bit_left_shift",
  187. args: [self, Helper.sendableToExpr(y)]
  188. )
  189. }
  190. /// Creates an expression applying bitwise left shift to this expression by a number of bits
  191. /// specified by an expression.
  192. /// Assumes `self` evaluates to Integer or Bytes, and `numberExpr` evaluates to an Integer.
  193. /// - Note: This API is in beta.
  194. ///
  195. /// ```swift
  196. /// // Left shift "data" by number of bits in "shiftCount" field
  197. /// Field("data").bitLeftShift(Field("shiftCount"))
  198. /// ```
  199. /// - Parameter numberExpr: An `Expr` (evaluating to an Int) for the number of bits to shift by.
  200. /// - Returns: A new "FunctionExpression" representing the bitwise left shift operation.
  201. func bitLeftShift(_ numberExpression: Expression) -> FunctionExpression {
  202. return FunctionExpression(functionName: "bit_left_shift", args: [self, numberExpression])
  203. }
  204. /// Creates an expression applying bitwise right shift to this expression by a literal number of
  205. /// bits.
  206. /// Assumes `self` evaluates to Integer or Bytes.
  207. ///
  208. /// - Note: This API is in beta.
  209. ///
  210. /// ```swift
  211. /// // Right shift "value" field by 4 bits
  212. /// Field("value").bitRightShift(4)
  213. /// ```
  214. ///
  215. /// - Parameter y: The number of bits (Int literal) to shift by.
  216. /// - Returns: A new "FunctionExpression" representing the bitwise right shift operation.
  217. func bitRightShift(_ y: Int) -> FunctionExpression {
  218. return FunctionExpression(
  219. functionName: "bit_right_shift",
  220. args: [self, Helper.sendableToExpr(y)]
  221. )
  222. }
  223. /// Creates an expression applying bitwise right shift to this expression by a number of bits
  224. /// specified by an expression.
  225. /// Assumes `self` evaluates to Integer or Bytes, and `numberExpr` evaluates to an Integer.
  226. /// - Note: This API is in beta.
  227. ///
  228. /// ```swift
  229. /// // Right shift "data" by number of bits in "shiftCount" field
  230. /// Field("data").bitRightShift(Field("shiftCount"))
  231. /// ```
  232. /// - Parameter numberExpr: An `Expr` (evaluating to an Int) for the number of bits to shift by.
  233. /// - Returns: A new "FunctionExpression" representing the bitwise right shift operation.
  234. func bitRightShift(_ numberExpression: Expression) -> FunctionExpression {
  235. return FunctionExpression(functionName: "bit_right_shift", args: [self, numberExpression])
  236. }
  237. /// Calculates the Manhattan (L1) distance between this vector expression and another vector
  238. /// expression.
  239. /// Assumes both `self` and `other` evaluate to Vectors.
  240. ///
  241. /// - Note: This API is in beta.
  242. ///
  243. /// ```swift
  244. /// // Manhattan distance between "vector1" field and "vector2" field
  245. /// Field("vector1").manhattanDistance(Field("vector2"))
  246. /// ```
  247. ///
  248. /// - Parameter expression: The other vector as an `Expr` to compare against.
  249. /// - Returns: A new `FunctionExpression` representing the Manhattan distance.
  250. func manhattanDistance(_ expression: Expression) -> FunctionExpression {
  251. return FunctionExpression(functionName: "manhattan_distance", args: [self, expression])
  252. }
  253. /// Calculates the Manhattan (L1) distance between this vector expression and another vector
  254. /// literal (`VectorValue`).
  255. /// Assumes `self` evaluates to a Vector.
  256. /// - Note: This API is in beta.
  257. /// ```swift
  258. /// let referencePoint = VectorValue(vector: [5.0, 10.0])
  259. /// Field("dataPoint").manhattanDistance(referencePoint)
  260. /// ```
  261. /// - Parameter vector: The other vector as a `VectorValue` to compare against.
  262. /// - Returns: A new `FunctionExpression` representing the Manhattan distance.
  263. func manhattanDistance(_ vector: VectorValue) -> FunctionExpression {
  264. return FunctionExpression(
  265. functionName: "manhattan_distance",
  266. args: [self, Helper.sendableToExpr(vector)]
  267. )
  268. }
  269. /// Calculates the Manhattan (L1) distance between this vector expression and another vector
  270. /// literal (`[Double]`).
  271. /// Assumes `self` evaluates to a Vector.
  272. /// - Note: This API is in beta.
  273. ///
  274. /// ```swift
  275. /// // Manhattan distance between "point" field and a target point
  276. /// Field("point").manhattanDistance([10.0, 20.0])
  277. /// ```
  278. /// - Parameter vector: The other vector as `[Double]` to compare against.
  279. /// - Returns: A new `FunctionExpression` representing the Manhattan distance.
  280. func manhattanDistance(_ vector: [Double]) -> FunctionExpression {
  281. return FunctionExpression(
  282. functionName: "manhattan_distance",
  283. args: [self, Helper.sendableToExpr(vector)]
  284. )
  285. }
  286. /// Creates an expression that replaces the first occurrence of a literal substring within this
  287. /// string expression with another literal substring.
  288. /// Assumes `self` evaluates to a string.
  289. ///
  290. /// ```swift
  291. /// // Replace the first "hello" with "hi" in the "message" field
  292. /// Field("message").replaceFirst("hello", "hi")
  293. /// ```
  294. ///
  295. /// - Parameter find: The literal string substring to search for.
  296. /// - Parameter replace: The literal string substring to replace the first occurrence with.
  297. /// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
  298. func replaceFirst(_ find: String, with replace: String) -> FunctionExpression {
  299. return FunctionExpression(
  300. functionName: "replace_first",
  301. args: [self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
  302. )
  303. }
  304. /// Creates an expression that replaces the first occurrence of a substring (from an expression)
  305. /// within this string expression with another substring (from an expression).
  306. /// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
  307. ///
  308. /// ```swift
  309. /// // Replace first occurrence of field "findPattern" with field "replacePattern" in "text"
  310. /// Field("text").replaceFirst(Field("findPattern"), Field("replacePattern"))
  311. /// ```
  312. ///
  313. /// - Parameter find: An `Expr` (evaluating to a string) for the substring to search for.
  314. /// - Parameter replace: An `Expr` (evaluating to a string) for the substring to replace the first
  315. /// occurrence with.
  316. /// - Returns: A new `FunctionExpr` representing the string with the first occurrence replaced.
  317. func replaceFirst(_ find: Expression, with replace: Expression) -> FunctionExpression {
  318. return FunctionExpression(functionName: "replace_first", args: [self, find, replace])
  319. }
  320. /// Creates an expression that replaces all occurrences of a literal substring within this string
  321. /// expression with another literal substring.
  322. /// Assumes `self` evaluates to a string.
  323. ///
  324. /// ```swift
  325. /// // Replace all occurrences of " " with "_" in "description"
  326. /// Field("description").stringReplace(" ", "_")
  327. /// ```
  328. ///
  329. /// - Parameter find: The literal string substring to search for.
  330. /// - Parameter replace: The literal string substring to replace all occurrences with.
  331. /// - Returns: A new `FunctionExpr` representing the string with all occurrences replaced.
  332. func stringReplace(_ find: String, with replace: String) -> FunctionExpression {
  333. return FunctionExpression(
  334. functionName: "string_replace",
  335. args: [self, Helper.sendableToExpr(find), Helper.sendableToExpr(replace)]
  336. )
  337. }
  338. /// Creates an expression that replaces all occurrences of a substring (from an expression) within
  339. /// this string expression with another substring (from an expression).
  340. /// Assumes `self` evaluates to a string, and `find`/`replace` evaluate to strings.
  341. ///
  342. /// ```swift
  343. /// // Replace all occurrences of field "target" with field "replacement" in "content"
  344. /// Field("content").stringReplace(Field("target"), Field("replacement"))
  345. /// ```
  346. ///
  347. /// - Parameter find: An `Expression` (evaluating to a string) for the substring to search for.
  348. /// - Parameter replace: An `Expression` (evaluating to a string) for the substring to replace all
  349. /// occurrences with.
  350. /// - Returns: A new `FunctionExpression` representing the string with all occurrences replaced.
  351. func stringReplace(_ find: Expression, with replace: Expression) -> FunctionExpression {
  352. return FunctionExpression(functionName: "string_replace", args: [self, find, replace])
  353. }
  354. }
  355. public extension Expression {
  356. func `as`(_ name: String) -> AliasedExpression {
  357. return AliasedExpression(self, name)
  358. }
  359. // MARK: Arithmetic Operators
  360. func abs() -> FunctionExpression {
  361. return FunctionExpression(functionName: "abs", args: [self])
  362. }
  363. func ceil() -> FunctionExpression {
  364. return FunctionExpression(functionName: "ceil", args: [self])
  365. }
  366. func floor() -> FunctionExpression {
  367. return FunctionExpression(functionName: "floor", args: [self])
  368. }
  369. func ln() -> FunctionExpression {
  370. return FunctionExpression(functionName: "ln", args: [self])
  371. }
  372. func pow(_ exponent: Sendable) -> FunctionExpression {
  373. return FunctionExpression(functionName: "pow", args: [self, Helper.sendableToExpr(exponent)])
  374. }
  375. func pow(_ exponent: Expression) -> FunctionExpression {
  376. return FunctionExpression(functionName: "pow", args: [self, exponent])
  377. }
  378. func round() -> FunctionExpression {
  379. return FunctionExpression(functionName: "round", args: [self])
  380. }
  381. func sqrt() -> FunctionExpression {
  382. return FunctionExpression(functionName: "sqrt", args: [self])
  383. }
  384. func exp() -> FunctionExpression {
  385. return FunctionExpression(functionName: "exp", args: [self])
  386. }
  387. func add(_ value: Expression) -> FunctionExpression {
  388. return FunctionExpression(functionName: "add", args: [self, value])
  389. }
  390. func add(_ value: Sendable) -> FunctionExpression {
  391. return FunctionExpression(functionName: "add", args: [self, Helper.sendableToExpr(value)])
  392. }
  393. func subtract(_ other: Expression) -> FunctionExpression {
  394. return FunctionExpression(functionName: "subtract", args: [self, other])
  395. }
  396. func subtract(_ other: Sendable) -> FunctionExpression {
  397. return FunctionExpression(functionName: "subtract", args: [self, Helper.sendableToExpr(other)])
  398. }
  399. func multiply(_ value: Expression) -> FunctionExpression {
  400. return FunctionExpression(functionName: "multiply", args: [self, value])
  401. }
  402. func multiply(_ value: Sendable) -> FunctionExpression {
  403. return FunctionExpression(functionName: "multiply", args: [self, Helper.sendableToExpr(value)])
  404. }
  405. func divide(_ other: Expression) -> FunctionExpression {
  406. return FunctionExpression(functionName: "divide", args: [self, other])
  407. }
  408. func divide(_ other: Sendable) -> FunctionExpression {
  409. return FunctionExpression(functionName: "divide", args: [self, Helper.sendableToExpr(other)])
  410. }
  411. func mod(_ other: Expression) -> FunctionExpression {
  412. return FunctionExpression(functionName: "mod", args: [self, other])
  413. }
  414. func mod(_ other: Sendable) -> FunctionExpression {
  415. return FunctionExpression(functionName: "mod", args: [self, Helper.sendableToExpr(other)])
  416. }
  417. // MARK: Array Operations
  418. func arrayReverse() -> FunctionExpression {
  419. return FunctionExpression(functionName: "array_reverse", args: [self])
  420. }
  421. func arrayConcat(_ arrays: [Expression]) -> FunctionExpression {
  422. return FunctionExpression(functionName: "array_concat", args: [self] + arrays)
  423. }
  424. func arrayConcat(_ arrays: [[Sendable]]) -> FunctionExpression {
  425. let exprs = [self] + arrays.map { Helper.sendableToExpr($0) }
  426. return FunctionExpression(functionName: "array_concat", args: exprs)
  427. }
  428. func arrayContains(_ element: Expression) -> BooleanExpression {
  429. return BooleanExpression(functionName: "array_contains", args: [self, element])
  430. }
  431. func arrayContains(_ element: Sendable) -> BooleanExpression {
  432. return BooleanExpression(
  433. functionName: "array_contains",
  434. args: [self, Helper.sendableToExpr(element)]
  435. )
  436. }
  437. func arrayContainsAll(_ values: [Expression]) -> BooleanExpression {
  438. return BooleanExpression(functionName: "array_contains_all", args: [self, Helper.array(values)])
  439. }
  440. func arrayContainsAll(_ values: [Sendable]) -> BooleanExpression {
  441. return BooleanExpression(functionName: "array_contains_all", args: [self, Helper.array(values)])
  442. }
  443. func arrayContainsAll(_ arrayExpression: Expression) -> BooleanExpression {
  444. return BooleanExpression(functionName: "array_contains_all", args: [self, arrayExpression])
  445. }
  446. func arrayContainsAny(_ values: [Expression]) -> BooleanExpression {
  447. return BooleanExpression(functionName: "array_contains_any", args: [self, Helper.array(values)])
  448. }
  449. func arrayContainsAny(_ values: [Sendable]) -> BooleanExpression {
  450. return BooleanExpression(functionName: "array_contains_any", args: [self, Helper.array(values)])
  451. }
  452. func arrayContainsAny(_ arrayExpression: Expression) -> BooleanExpression {
  453. return BooleanExpression(functionName: "array_contains_any", args: [self, arrayExpression])
  454. }
  455. func arrayLength() -> FunctionExpression {
  456. return FunctionExpression(functionName: "array_length", args: [self])
  457. }
  458. func arrayGet(_ offset: Int) -> FunctionExpression {
  459. return FunctionExpression(
  460. functionName: "array_get",
  461. args: [self, Helper.sendableToExpr(offset)]
  462. )
  463. }
  464. func arrayGet(_ offsetExpression: Expression) -> FunctionExpression {
  465. return FunctionExpression(functionName: "array_get", args: [self, offsetExpression])
  466. }
  467. func greaterThan(_ other: Expression) -> BooleanExpression {
  468. return BooleanExpression(functionName: "greater_than", args: [self, other])
  469. }
  470. func greaterThan(_ other: Sendable) -> BooleanExpression {
  471. let exprOther = Helper.sendableToExpr(other)
  472. return BooleanExpression(functionName: "greater_than", args: [self, exprOther])
  473. }
  474. func greaterThanOrEqual(_ other: Expression) -> BooleanExpression {
  475. return BooleanExpression(functionName: "greater_than_or_equal", args: [self, other])
  476. }
  477. func greaterThanOrEqual(_ other: Sendable) -> BooleanExpression {
  478. let exprOther = Helper.sendableToExpr(other)
  479. return BooleanExpression(functionName: "greater_than_or_equal", args: [self, exprOther])
  480. }
  481. func lessThan(_ other: Expression) -> BooleanExpression {
  482. return BooleanExpression(functionName: "less_than", args: [self, other])
  483. }
  484. func lessThan(_ other: Sendable) -> BooleanExpression {
  485. let exprOther = Helper.sendableToExpr(other)
  486. return BooleanExpression(functionName: "less_than", args: [self, exprOther])
  487. }
  488. func lessThanOrEqual(_ other: Expression) -> BooleanExpression {
  489. return BooleanExpression(functionName: "less_than_or_equal", args: [self, other])
  490. }
  491. func lessThanOrEqual(_ other: Sendable) -> BooleanExpression {
  492. let exprOther = Helper.sendableToExpr(other)
  493. return BooleanExpression(functionName: "less_than_or_equal", args: [self, exprOther])
  494. }
  495. func equal(_ other: Expression) -> BooleanExpression {
  496. return BooleanExpression(functionName: "equal", args: [self, other])
  497. }
  498. func equal(_ other: Sendable) -> BooleanExpression {
  499. let exprOther = Helper.sendableToExpr(other)
  500. return BooleanExpression(functionName: "equal", args: [self, exprOther])
  501. }
  502. func notEqual(_ other: Expression) -> BooleanExpression {
  503. return BooleanExpression(functionName: "not_equal", args: [self, other])
  504. }
  505. func notEqual(_ other: Sendable) -> BooleanExpression {
  506. return BooleanExpression(functionName: "not_equal", args: [self, Helper.sendableToExpr(other)])
  507. }
  508. func equalAny(_ others: [Expression]) -> BooleanExpression {
  509. return BooleanExpression(functionName: "equal_any", args: [self, Helper.array(others)])
  510. }
  511. func equalAny(_ others: [Sendable]) -> BooleanExpression {
  512. return BooleanExpression(functionName: "equal_any", args: [self, Helper.array(others)])
  513. }
  514. func equalAny(_ arrayExpression: Expression) -> BooleanExpression {
  515. return BooleanExpression(functionName: "equal_any", args: [self, arrayExpression])
  516. }
  517. func notEqualAny(_ others: [Expression]) -> BooleanExpression {
  518. return BooleanExpression(functionName: "not_equal_any", args: [self, Helper.array(others)])
  519. }
  520. func notEqualAny(_ others: [Sendable]) -> BooleanExpression {
  521. return BooleanExpression(functionName: "not_equal_any", args: [self, Helper.array(others)])
  522. }
  523. func notEqualAny(_ arrayExpression: Expression) -> BooleanExpression {
  524. return BooleanExpression(functionName: "not_equal_any", args: [self, arrayExpression])
  525. }
  526. // MARK: Checks
  527. // --- Added Type Check Operations ---
  528. func exists() -> BooleanExpression {
  529. return BooleanExpression(functionName: "exists", args: [self])
  530. }
  531. func isError() -> BooleanExpression {
  532. return BooleanExpression(functionName: "is_error", args: [self])
  533. }
  534. func isAbsent() -> BooleanExpression {
  535. return BooleanExpression(functionName: "is_absent", args: [self])
  536. }
  537. // --- Added String Operations ---
  538. func join(delimiter: String) -> FunctionExpression {
  539. return FunctionExpression(functionName: "join", args: [self, Constant(delimiter)])
  540. }
  541. func length() -> FunctionExpression {
  542. return FunctionExpression(functionName: "length", args: [self])
  543. }
  544. func charLength() -> FunctionExpression {
  545. return FunctionExpression(functionName: "char_length", args: [self])
  546. }
  547. func like(_ pattern: String) -> BooleanExpression {
  548. return BooleanExpression(functionName: "like", args: [self, Helper.sendableToExpr(pattern)])
  549. }
  550. func like(_ pattern: Expression) -> BooleanExpression {
  551. return BooleanExpression(functionName: "like", args: [self, pattern])
  552. }
  553. func regexContains(_ pattern: String) -> BooleanExpression {
  554. return BooleanExpression(
  555. functionName: "regex_contains",
  556. args: [self, Helper.sendableToExpr(pattern)]
  557. )
  558. }
  559. func regexContains(_ pattern: Expression) -> BooleanExpression {
  560. return BooleanExpression(functionName: "regex_contains", args: [self, pattern])
  561. }
  562. func regexMatch(_ pattern: String) -> BooleanExpression {
  563. return BooleanExpression(
  564. functionName: "regex_match",
  565. args: [self, Helper.sendableToExpr(pattern)]
  566. )
  567. }
  568. func regexMatch(_ pattern: Expression) -> BooleanExpression {
  569. return BooleanExpression(functionName: "regex_match", args: [self, pattern])
  570. }
  571. func stringContains(_ substring: String) -> BooleanExpression {
  572. return BooleanExpression(
  573. functionName: "string_contains",
  574. args: [self, Helper.sendableToExpr(substring)]
  575. )
  576. }
  577. func stringContains(_ expression: Expression) -> BooleanExpression {
  578. return BooleanExpression(functionName: "string_contains", args: [self, expression])
  579. }
  580. func startsWith(_ prefix: String) -> BooleanExpression {
  581. return BooleanExpression(
  582. functionName: "starts_with",
  583. args: [self, Helper.sendableToExpr(prefix)]
  584. )
  585. }
  586. func startsWith(_ prefix: Expression) -> BooleanExpression {
  587. return BooleanExpression(functionName: "starts_with", args: [self, prefix])
  588. }
  589. func endsWith(_ suffix: String) -> BooleanExpression {
  590. return BooleanExpression(functionName: "ends_with", args: [self, Helper.sendableToExpr(suffix)])
  591. }
  592. func endsWith(_ suffix: Expression) -> BooleanExpression {
  593. return BooleanExpression(functionName: "ends_with", args: [self, suffix])
  594. }
  595. func toLower() -> FunctionExpression {
  596. return FunctionExpression(functionName: "to_lower", args: [self])
  597. }
  598. func toUpper() -> FunctionExpression {
  599. return FunctionExpression(functionName: "to_upper", args: [self])
  600. }
  601. func trim(_ value: String) -> FunctionExpression {
  602. return FunctionExpression(
  603. functionName: "trim",
  604. args: [self, Helper.sendableToExpr(value)]
  605. )
  606. }
  607. func trim(_ value: Expression) -> FunctionExpression {
  608. return FunctionExpression(functionName: "trim", args: [self, value])
  609. }
  610. func stringConcat(_ strings: [Expression]) -> FunctionExpression {
  611. return FunctionExpression(functionName: "string_concat", args: [self] + strings)
  612. }
  613. func stringConcat(_ strings: [Sendable]) -> FunctionExpression {
  614. let exprs = [self] + strings.map { Helper.sendableToExpr($0) }
  615. return FunctionExpression(functionName: "string_concat", args: exprs)
  616. }
  617. func reverse() -> FunctionExpression {
  618. return FunctionExpression(functionName: "reverse", args: [self])
  619. }
  620. func stringReverse() -> FunctionExpression {
  621. return FunctionExpression(functionName: "string_reverse", args: [self])
  622. }
  623. func byteLength() -> FunctionExpression {
  624. return FunctionExpression(functionName: "byte_length", args: [self])
  625. }
  626. func substring(position: Int, length: Int? = nil) -> FunctionExpression {
  627. let positionExpr = Helper.sendableToExpr(position)
  628. if let length = length {
  629. return FunctionExpression(
  630. functionName: "substring",
  631. args: [self, positionExpr, Helper.sendableToExpr(length)]
  632. )
  633. } else {
  634. return FunctionExpression(functionName: "substring", args: [self, positionExpr])
  635. }
  636. }
  637. func substring(position: Expression, length: Expression? = nil) -> FunctionExpression {
  638. if let length = length {
  639. return FunctionExpression(functionName: "substring", args: [self, position, length])
  640. } else {
  641. return FunctionExpression(functionName: "substring", args: [self, position])
  642. }
  643. }
  644. // --- Added Map Operations ---
  645. func mapGet(_ subfield: String) -> FunctionExpression {
  646. return FunctionExpression(functionName: "map_get", args: [self, Constant(subfield)])
  647. }
  648. func mapRemove(_ key: String) -> FunctionExpression {
  649. return FunctionExpression(functionName: "map_remove", args: [self, Helper.sendableToExpr(key)])
  650. }
  651. func mapRemove(_ keyExpression: Expression) -> FunctionExpression {
  652. return FunctionExpression(functionName: "map_remove", args: [self, keyExpression])
  653. }
  654. func mapMerge(_ maps: [[String: Sendable]]) -> FunctionExpression {
  655. let mapExprs = maps.map { Helper.sendableToExpr($0) }
  656. return FunctionExpression(functionName: "map_merge", args: [self] + mapExprs)
  657. }
  658. func mapMerge(_ maps: [Expression]) -> FunctionExpression {
  659. return FunctionExpression(functionName: "map_merge", args: [self] + maps)
  660. }
  661. func mapSet(key: Expression, value: Sendable) -> FunctionExpression {
  662. return FunctionExpression(
  663. functionName: "map_set",
  664. args: [self, key, Helper.sendableToExpr(value)]
  665. )
  666. }
  667. func mapSet(key: String, value: Sendable) -> FunctionExpression {
  668. return FunctionExpression(
  669. functionName: "map_set",
  670. args: [self, Helper.sendableToExpr(key), Helper.sendableToExpr(value)]
  671. )
  672. }
  673. // --- Added Aggregate Operations (on Expr) ---
  674. func countDistinct() -> AggregateFunction {
  675. return AggregateFunction(functionName: "count_distinct", args: [self])
  676. }
  677. func count() -> AggregateFunction {
  678. return AggregateFunction(functionName: "count", args: [self])
  679. }
  680. func sum() -> AggregateFunction {
  681. return AggregateFunction(functionName: "sum", args: [self])
  682. }
  683. func average() -> AggregateFunction {
  684. return AggregateFunction(functionName: "average", args: [self])
  685. }
  686. func minimum() -> AggregateFunction {
  687. return AggregateFunction(functionName: "minimum", args: [self])
  688. }
  689. func maximum() -> AggregateFunction {
  690. return AggregateFunction(functionName: "maximum", args: [self])
  691. }
  692. // MARK: Logical min/max
  693. func logicalMaximum(_ expressions: [Expression]) -> FunctionExpression {
  694. return FunctionExpression(functionName: "maximum", args: [self] + expressions)
  695. }
  696. func logicalMaximum(_ values: [Sendable]) -> FunctionExpression {
  697. let exprs = [self] + values.map { Helper.sendableToExpr($0) }
  698. return FunctionExpression(functionName: "maximum", args: exprs)
  699. }
  700. func logicalMinimum(_ expressions: [Expression]) -> FunctionExpression {
  701. return FunctionExpression(functionName: "minimum", args: [self] + expressions)
  702. }
  703. func logicalMinimum(_ values: [Sendable]) -> FunctionExpression {
  704. let exprs = [self] + values.map { Helper.sendableToExpr($0) }
  705. return FunctionExpression(functionName: "minimum", args: exprs)
  706. }
  707. // MARK: Vector Operations
  708. func vectorLength() -> FunctionExpression {
  709. return FunctionExpression(functionName: "vector_length", args: [self])
  710. }
  711. func cosineDistance(_ expression: Expression) -> FunctionExpression {
  712. return FunctionExpression(functionName: "cosine_distance", args: [self, expression])
  713. }
  714. func cosineDistance(_ vector: VectorValue) -> FunctionExpression {
  715. return FunctionExpression(
  716. functionName: "cosine_distance",
  717. args: [self, Helper.sendableToExpr(vector)]
  718. )
  719. }
  720. func cosineDistance(_ vector: [Double]) -> FunctionExpression {
  721. return FunctionExpression(
  722. functionName: "cosine_distance",
  723. args: [self, Helper.sendableToExpr(vector)]
  724. )
  725. }
  726. func dotProduct(_ expression: Expression) -> FunctionExpression {
  727. return FunctionExpression(functionName: "dot_product", args: [self, expression])
  728. }
  729. func dotProduct(_ vector: VectorValue) -> FunctionExpression {
  730. return FunctionExpression(
  731. functionName: "dot_product",
  732. args: [self, Helper.sendableToExpr(vector)]
  733. )
  734. }
  735. func dotProduct(_ vector: [Double]) -> FunctionExpression {
  736. return FunctionExpression(
  737. functionName: "dot_product",
  738. args: [self, Helper.sendableToExpr(vector)]
  739. )
  740. }
  741. func euclideanDistance(_ expression: Expression) -> FunctionExpression {
  742. return FunctionExpression(functionName: "euclidean_distance", args: [self, expression])
  743. }
  744. func euclideanDistance(_ vector: VectorValue) -> FunctionExpression {
  745. return FunctionExpression(
  746. functionName: "euclidean_distance",
  747. args: [self, Helper.sendableToExpr(vector)]
  748. )
  749. }
  750. func euclideanDistance(_ vector: [Double]) -> FunctionExpression {
  751. return FunctionExpression(
  752. functionName: "euclidean_distance",
  753. args: [self, Helper.sendableToExpr(vector)]
  754. )
  755. }
  756. // MARK: Timestamp operations
  757. func unixMicrosToTimestamp() -> FunctionExpression {
  758. return FunctionExpression(functionName: "unix_micros_to_timestamp", args: [self])
  759. }
  760. func timestampToUnixMicros() -> FunctionExpression {
  761. return FunctionExpression(functionName: "timestamp_to_unix_micros", args: [self])
  762. }
  763. func unixMillisToTimestamp() -> FunctionExpression {
  764. return FunctionExpression(functionName: "unix_millis_to_timestamp", args: [self])
  765. }
  766. func timestampToUnixMillis() -> FunctionExpression {
  767. return FunctionExpression(functionName: "timestamp_to_unix_millis", args: [self])
  768. }
  769. func unixSecondsToTimestamp() -> FunctionExpression {
  770. return FunctionExpression(functionName: "unix_seconds_to_timestamp", args: [self])
  771. }
  772. func timestampToUnixSeconds() -> FunctionExpression {
  773. return FunctionExpression(functionName: "timestamp_to_unix_seconds", args: [self])
  774. }
  775. func timestampTruncate(granularity: TimeUnit) -> FunctionExpression {
  776. return FunctionExpression(
  777. functionName: "timestamp_trunc",
  778. args: [self, Helper.sendableToExpr(granularity.rawValue)]
  779. )
  780. }
  781. func timestampTruncate(granularity: Sendable) -> FunctionExpression {
  782. return FunctionExpression(
  783. functionName: "timestamp_trunc",
  784. args: [self, Helper.sendableToExpr(granularity)]
  785. )
  786. }
  787. func timestampAdd(_ amount: Int, _ unit: TimeUnit) -> FunctionExpression {
  788. return FunctionExpression(
  789. functionName: "timestamp_add",
  790. args: [self, Helper.sendableToExpr(unit), Helper.sendableToExpr(amount)]
  791. )
  792. }
  793. func timestampAdd(amount: Expression, unit: Sendable) -> FunctionExpression {
  794. return FunctionExpression(
  795. functionName: "timestamp_add",
  796. args: [self, Helper.sendableToExpr(unit), amount]
  797. )
  798. }
  799. func timestampSubtract(_ amount: Int, _ unit: TimeUnit) -> FunctionExpression {
  800. return FunctionExpression(
  801. functionName: "timestamp_subtract",
  802. args: [self, Helper.sendableToExpr(unit), Helper.sendableToExpr(amount)]
  803. )
  804. }
  805. func timestampSubtract(amount: Expression, unit: Sendable) -> FunctionExpression {
  806. return FunctionExpression(
  807. functionName: "timestamp_subtract",
  808. args: [self, Helper.sendableToExpr(unit), amount]
  809. )
  810. }
  811. func documentId() -> FunctionExpression {
  812. return FunctionExpression(functionName: "document_id", args: [self])
  813. }
  814. func collectionId() -> FunctionExpression {
  815. return FunctionExpression(functionName: "collection_id", args: [self])
  816. }
  817. func ifError(_ catchExpression: Expression) -> FunctionExpression {
  818. return FunctionExpression(functionName: "if_error", args: [self, catchExpression])
  819. }
  820. func ifError(_ catchValue: Sendable) -> FunctionExpression {
  821. return FunctionExpression(
  822. functionName: "if_error",
  823. args: [self, Helper.sendableToExpr(catchValue)]
  824. )
  825. }
  826. func ifAbsent(_ defaultValue: Sendable) -> FunctionExpression {
  827. return FunctionExpression(
  828. functionName: "if_absent",
  829. args: [self, Helper.sendableToExpr(defaultValue)]
  830. )
  831. }
  832. // MARK: Sorting
  833. func ascending() -> Ordering {
  834. return Ordering(expression: self, direction: .ascending)
  835. }
  836. func descending() -> Ordering {
  837. return Ordering(expression: self, direction: .descending)
  838. }
  839. func concat(_ values: [Sendable]) -> FunctionExpression {
  840. let exprs = [self] + values.map { Helper.sendableToExpr($0) }
  841. return FunctionExpression(functionName: "concat", args: exprs)
  842. }
  843. }