ExpressionImplementation.swift 32 KB

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