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