-
Notifications
You must be signed in to change notification settings - Fork 44
/
mappers.py
1277 lines (900 loc) · 31 KB
/
mappers.py
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
import base64
import math
import random
import re
import string
from ...utils import half_even_round, half_up_round, MonotonicallyIncreasingIDGenerator, XORShiftRandom
from ..internal_utils.column import resolve_column
from ..types import (
ArrayType, BinaryType, BooleanType, create_row, DoubleType, IntegerType, LongType, MapType, NullType, StringType,
StructField, StructType
)
from ..utils import AnalysisException
from .expressions import Expression, NullSafeColumnOperation, UnaryExpression
from .operators import Cast
JVM_MAX_INTEGER_SIZE = 2 ** 63
class StarOperator(Expression):
@property
def may_output_multiple_cols(self):
return True
def output_fields(self, schema):
return schema.fields
def eval(self, row, schema):
return [row[col] for col in row.__fields__]
def __str__(self):
return "*"
def args(self):
return ()
class CaseWhen(Expression):
def __init__(self, conditions, values):
super().__init__(conditions, values)
self.conditions = conditions
self.values = values
def eval(self, row, schema):
for condition, function in zip(self.conditions, self.values):
condition_value = condition.eval(row, schema)
if condition_value:
return function.eval(row, schema)
return None
def __str__(self):
when_statements = " ".join(
f"WHEN {condition} THEN {value}"
for condition, value in zip(self.conditions, self.values)
)
return f"CASE {when_statements} END"
def args(self):
return (
self.conditions,
self.values
)
def add_when(self, condition, value):
return CaseWhen(
self.conditions + [condition],
self.values + [value]
)
def set_otherwise(self, default):
return Otherwise(
self.conditions,
self.values,
default
)
def data_type(self, schema):
return self.values[0].data_type(schema)
class Otherwise(Expression):
def __init__(self, conditions, values, default):
super().__init__(conditions, values, default)
self.conditions = conditions
self.values = values
self.default = default
def eval(self, row, schema):
for condition, function in zip(self.conditions, self.values):
condition_value = condition.eval(row, schema)
if condition_value:
return function.eval(row, schema)
if self.default is not None:
return self.default.eval(row, schema)
return None
def __str__(self):
whens = " ".join(
f"WHEN {condition} THEN {value}"
for condition, value in zip(self.conditions, self.values)
)
return f"CASE {whens} ELSE {self.default} END"
def args(self):
return (
self.conditions,
self.values,
self.default
)
def data_type(self, schema):
return self.values[0].data_type(schema)
class RegExpExtract(Expression):
pretty_name = "regexp_extract"
def __init__(self, e, exp, groupIdx):
super().__init__(e, exp, groupIdx)
self.exp = exp.get_literal_value()
self.groupIdx = groupIdx.get_literal_value()
self.e = e
regexp = re.compile(self.exp)
def fn(x):
match = regexp.search(x)
if not match:
return ""
ret = match.group(self.groupIdx)
return ret
self.fn = fn
def eval(self, row, schema):
return self.fn(self.e.eval(row, schema))
def args(self):
return (
self.e,
self.exp,
self.groupIdx
)
def data_type(self, schema):
return StringType()
class RegExpReplace(Expression):
pretty_name = "regexp_replace"
def __init__(self, e, exp, replacement):
super().__init__(e, exp, replacement)
self.exp = exp.get_literal_value()
self.replacement = replacement.get_literal_value()
self.e = e
regexp = re.compile(self.exp)
self.fn = lambda x: regexp.sub(self.replacement, x)
def eval(self, row, schema):
return self.fn(self.e.eval(row, schema))
def args(self):
return (
self.e,
self.exp,
self.replacement
)
def data_type(self, schema):
return StringType()
class Round(NullSafeColumnOperation):
pretty_name = "round"
def __init__(self, column, scale):
super().__init__(column)
self.scale = scale.get_literal_value()
def unsafe_operation(self, value):
return half_up_round(value, self.scale)
def args(self):
return (
self.column,
self.scale
)
def data_type(self, schema):
return self.column.data_type(schema)
class Bround(NullSafeColumnOperation):
pretty_name = "bround"
def __init__(self, column, scale):
super().__init__(column)
self.scale = scale.get_literal_value()
def unsafe_operation(self, value):
return half_even_round(value, self.scale)
def args(self):
return (
self.column,
self.scale
)
def data_type(self, schema):
return self.column.data_type(schema)
class FormatNumber(Expression):
pretty_name = "format_number"
def __init__(self, column, digits):
super().__init__(column)
self.column = column
self.digits = digits.get_literal_value()
def eval(self, row, schema):
value = self.column.eval(row, schema)
if self.digits < 0:
return None
if not isinstance(value, (int, float)):
return None
rounded_value = half_even_round(value, self.digits)
return f"{rounded_value:,}"
def args(self):
return (
self.column,
self.digits
)
def data_type(self, schema):
return StringType()
class SubstringIndex(Expression):
pretty_name = "substring_index"
def __init__(self, column, delim, count):
super().__init__(column)
self.column = column
self.delim = delim.get_literal_value()
self.count = count.get_literal_value()
def eval(self, row, schema):
parts = str(self.column.eval(row, schema)).split(self.delim)
return self.delim.join(parts[:self.count] if self.count > 0 else parts[self.count:])
def args(self):
return (
self.column,
self.delim,
self.count
)
def data_type(self, schema):
return StringType()
class Coalesce(Expression):
pretty_name = "coalesce"
def __init__(self, *columns):
super().__init__(columns)
self.columns = columns
def eval(self, row, schema):
for col in self.columns:
col_value = col.eval(row, schema)
if col_value is not None:
return col_value
return None
def args(self):
return self.columns
def data_type(self, schema):
for col in self.columns:
col_type = col.data_type(schema)
if col_type != NullType():
return col_type
return NullType()
class IsNaN(UnaryExpression):
pretty_name = "isnan"
def eval(self, row, schema):
return math.isnan(self.eval(row, schema))
def data_type(self, schema):
return BooleanType()
class NaNvl(Expression):
pretty_name = "nanvl"
def __init__(self, col1, col2):
super().__init__(col1, col2)
self.col1 = col1
self.col2 = col2
def eval(self, row, schema):
nan = float("nan")
col1_value = self.col1.eval(row, schema)
if col1_value is not nan:
return float(col1_value)
return float(self.col2.eval(row, schema))
def args(self):
return (
self.col1,
self.col2
)
def data_type(self, schema):
return DoubleType()
class Hypot(Expression):
pretty_name = "hypot"
def __init__(self, a, b):
super().__init__(a, b)
self.a = a
self.b = b
def eval(self, row, schema):
return math.hypot(self.a, self.b)
def args(self):
return (
self.a,
self.b
)
def data_type(self, schema):
return DoubleType()
class Sqrt(UnaryExpression):
pretty_name = "SQRT"
def eval(self, row, schema):
return math.sqrt(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Cbrt(UnaryExpression):
pretty_name = "CBRT"
def eval(self, row, schema):
return self.column.eval(row, schema) ** 1. / 3.
def data_type(self, schema):
return DoubleType()
class Abs(UnaryExpression):
pretty_name = "ABS"
def eval(self, row, schema):
return abs(self.column.eval(row, schema))
def data_type(self, schema):
return self.column.data_type(schema)
class Acos(UnaryExpression):
pretty_name = "ACOS"
def eval(self, row, schema):
return math.acos(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Asin(UnaryExpression):
pretty_name = "ASIN"
def eval(self, row, schema):
return math.asin(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Atan(UnaryExpression):
pretty_name = "ATAN"
def eval(self, row, schema):
return math.atan(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Atan2(Expression):
pretty_name = "ATAN"
def __init__(self, y, x):
super().__init__(y, x)
self.y = y
self.x = x
def eval(self, row, schema):
return math.atan2(self.y.eval(row, schema), self.x.eval(row, schema))
def args(self):
return (
self.y,
self.x
)
def data_type(self, schema):
return DoubleType()
class Tan(UnaryExpression):
pretty_name = "TAN"
def eval(self, row, schema):
return math.tan(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Tanh(UnaryExpression):
pretty_name = "TANH"
def eval(self, row, schema):
return math.tanh(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Cos(UnaryExpression):
pretty_name = "COS"
def eval(self, row, schema):
return math.cos(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Cosh(UnaryExpression):
pretty_name = "COSH"
def eval(self, row, schema):
return math.cosh(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Sin(UnaryExpression):
pretty_name = "SIN"
def eval(self, row, schema):
return math.sin(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Sinh(UnaryExpression):
pretty_name = "SINH"
def eval(self, row, schema):
return math.sinh(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Exp(UnaryExpression):
pretty_name = "EXP"
def eval(self, row, schema):
return math.exp(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class ExpM1(UnaryExpression):
pretty_name = "EXPM1"
def eval(self, row, schema):
return math.expm1(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Factorial(UnaryExpression):
pretty_name = "factorial"
def eval(self, row, schema):
return math.factorial(self.column.eval(row, schema))
def data_type(self, schema):
return LongType()
class Floor(UnaryExpression):
pretty_name = "FLOOR"
def eval(self, row, schema):
return math.floor(self.column.eval(row, schema))
def data_type(self, schema):
return LongType()
class Ceil(UnaryExpression):
pretty_name = "CEIL"
def eval(self, row, schema):
return math.ceil(self.column.eval(row, schema))
def data_type(self, schema):
return LongType()
class Log(Expression):
pretty_name = "LOG"
def __init__(self, base, value):
super().__init__(base, value)
self.base = base.get_literal_value()
self.value = value
def eval(self, row, schema):
value_eval = self.value.eval(row, schema)
if value_eval == 0:
return None
return math.log(value_eval, self.base)
def args(self):
if self.base == math.e:
return (self.value,)
return (
self.base,
self.value
)
def data_type(self, schema):
return DoubleType()
class Log10(UnaryExpression):
pretty_name = "LOG10"
def eval(self, row, schema):
return math.log10(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Log2(UnaryExpression):
pretty_name = "LOG2"
def eval(self, row, schema):
return math.log(self.column.eval(row, schema), 2)
def data_type(self, schema):
return DoubleType()
class Log1p(UnaryExpression):
pretty_name = "LOG1P"
def eval(self, row, schema):
return math.log1p(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Rint(UnaryExpression):
pretty_name = "ROUND"
def eval(self, row, schema):
return float(round(self.column.eval(row, schema)))
def data_type(self, schema):
return DoubleType()
class Signum(UnaryExpression):
pretty_name = "SIGNUM"
def eval(self, row, schema):
column_value = self.column.eval(row, schema)
if column_value == 0:
return 0
if column_value > 0:
return 1.0
return -1.0
def data_type(self, schema):
return DoubleType()
class ToDegrees(UnaryExpression):
pretty_name = "DEGREES"
def eval(self, row, schema):
return math.degrees(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class ToRadians(UnaryExpression):
pretty_name = "RADIANS"
def eval(self, row, schema):
return math.radians(self.column.eval(row, schema))
def data_type(self, schema):
return DoubleType()
class Rand(Expression):
pretty_name = "rand"
def __init__(self, seed=None):
super().__init__()
self.seed = seed.get_literal_value() if seed is not None else random.random()
self.random_generator = None
def eval(self, row, schema):
return self.random_generator.nextDouble()
def initialize(self, partition_index):
self.random_generator = XORShiftRandom(self.seed + partition_index)
def args(self):
return self.seed
def data_type(self, schema):
return DoubleType()
class Randn(Expression):
pretty_name = "randn"
def __init__(self, seed=None):
super().__init__()
self.seed = seed.get_literal_value()
self.random_generator = None
def eval(self, row, schema):
return self.random_generator.nextGaussian()
def initialize(self, partition_index):
self.random_generator = XORShiftRandom(self.seed + partition_index)
def args(self):
return self.seed
def data_type(self, schema):
return DoubleType()
class SparkPartitionID(Expression):
pretty_name = "SPARK_PARTITION_ID"
def __init__(self):
super().__init__()
self.partition_index = None
def eval(self, row, schema):
return self.partition_index
def initialize(self, partition_index):
self.partition_index = partition_index
def args(self):
return ()
def data_type(self, schema):
return IntegerType()
class CreateStruct(Expression):
pretty_name = "struct"
def __init__(self, *columns):
super().__init__(columns)
self.columns = columns
def eval(self, row, schema):
struct_cols, struct_values = [], []
for col in self.columns:
output_cols, output_values = resolve_column(col, row, schema, allow_generator=False)
struct_cols += output_cols
struct_values += output_values[0]
return create_row(struct_cols, struct_values)
def args(self):
return self.columns
def data_type(self, schema):
return StructType([
StructField(name=str(col), dataType=col.data_type(schema), nullable=True)
for col in self.columns
])
class Bin(UnaryExpression):
pretty_name = "bin"
def eval(self, row, schema):
return format(self.column.eval(row, schema), 'b')
def data_type(self, schema):
return StringType()
class ShiftLeft(Expression):
pretty_name = "shiftleft"
def __init__(self, arg, num_bits):
super().__init__(arg)
self.arg = arg
self.num_bits = num_bits.get_literal_value()
def eval(self, row, schema):
return int(self.arg.eval(row, schema)) << self.num_bits
def args(self):
return (
self.arg,
self.num_bits
)
def data_type(self, schema):
return IntegerType()
class ShiftRight(Expression):
pretty_name = "shiftright"
def __init__(self, arg, num_bits):
super().__init__(arg)
self.arg = arg
self.num_bits = num_bits.get_literal_value()
def eval(self, row, schema):
return int(self.arg.eval(row, schema)) >> self.num_bits
def args(self):
return (
self.arg,
self.num_bits
)
def data_type(self, schema):
return IntegerType()
class ShiftRightUnsigned(Expression):
pretty_name = "shiftrightunsigned"
def __init__(self, arg, num_bits):
super().__init__(arg)
self.arg = arg
self.num_bits = num_bits.get_literal_value()
def eval(self, row, schema):
rightShifted = int(self.arg.eval(row, schema)) >> self.num_bits
return rightShifted % JVM_MAX_INTEGER_SIZE
def args(self):
return (
self.arg,
self.num_bits
)
def data_type(self, schema):
return IntegerType()
class Greatest(Expression):
pretty_name = "greatest"
def __init__(self, *columns):
super().__init__(columns)
self.columns = columns
def eval(self, row, schema):
values = (col.eval(row, schema) for col in self.columns)
return max((value for value in values if value is not None), default=None)
def args(self):
return self.columns
def data_type(self, schema):
if not self.columns:
return NullType()
return self.columns[0].data_type(schema)
class Least(Expression):
pretty_name = "least"
def __init__(self, *columns):
super().__init__(columns)
self.columns = columns
def eval(self, row, schema):
values = (col.eval(row, schema) for col in self.columns)
return min((value for value in values if value is not None), default=None)
def args(self):
return self.columns
def data_type(self, schema):
if not self.columns:
return NullType()
return self.columns[0].data_type(schema)
class Length(UnaryExpression):
pretty_name = "length"
def eval(self, row, schema):
return len(str(self.column.eval(row, schema)))
def data_type(self, schema):
return IntegerType()
class Lower(UnaryExpression):
pretty_name = "lower"
def eval(self, row, schema):
return str(self.column.eval(row, schema)).lower()
def data_type(self, schema):
return StringType()
class Upper(UnaryExpression):
pretty_name = "upper"
def eval(self, row, schema):
return str(self.column.eval(row, schema)).upper()
def data_type(self, schema):
return StringType()
class Concat(Expression):
pretty_name = "concat"
def __init__(self, *columns):
super().__init__(columns)
self.columns = columns
def eval(self, row, schema):
return "".join(str(col.eval(row, schema)) for col in self.columns)
def args(self):
return self.columns
def data_type(self, schema):
return StringType()
class ConcatWs(Expression):
pretty_name = "concat_ws"
def __init__(self, sep, *columns):
super().__init__(columns)
self.sep = sep.get_literal_value()
self.columns = columns
def eval(self, row, schema):
return self.sep.join(str(col.eval(row, schema)) for col in self.columns)
def args(self):
if self.columns:
return [self.sep] + list(self.columns)
return [self.sep]
def data_type(self, schema):
return StringType()
class Reverse(UnaryExpression):
pretty_name = "reverse"
def eval(self, row, schema):
return str(self.column.eval(row, schema))[::-1]
def data_type(self, schema):
return StringType()
class MapKeys(UnaryExpression):
pretty_name = "map_keys"
def eval(self, row, schema):
return list(self.column.eval(row, schema).keys())
def data_type(self, schema):
return ArrayType(elementType=self.column.data_type(schema).keyType)
class MapValues(UnaryExpression):
pretty_name = "map_values"
def eval(self, row, schema):
return list(self.column.eval(row, schema).values())
def data_type(self, schema):
return ArrayType(elementType=self.column.data_type(schema).valueType)
class MapEntries(UnaryExpression):
pretty_name = "map_entries"
def eval(self, row, schema):
return list(self.column.eval(row, schema).items())
def data_type(self, schema):
return ArrayType(elementType=self.column.data_type(schema).valueType)
class MapFromEntries(UnaryExpression):
pretty_name = "map_from_entries"
def eval(self, row, schema):
return dict(self.column.eval(row, schema))
def data_type(self, schema):
map_type = self.column.data_type(schema)
key_type = map_type.keyType
value_type = map_type.valueType
return ArrayType(elementType=StructType([
StructField("key", key_type, True),
StructField("value", value_type, True),
]))
class MapConcat(Expression):
pretty_name = "map_concat"
def __init__(self, *columns):
super().__init__(*columns)
self.columns = columns
def eval(self, row, schema):
result = {}
for column in self.columns:
col_value = column.eval(row, schema)
if isinstance(col_value, dict):
result.update(col_value)
return result
def args(self):
return self.columns
def data_type(self, schema):
if not self.columns:
return MapType(keyType=StringType(), valueType=StringType)
return self.columns[0].data_type(schema)
class StringSplit(Expression):
pretty_name = "split"
def __init__(self, column, regex, limit):
super().__init__(column)
self.column = column
self.regex = regex.get_literal_value()
self.compiled_regex = re.compile(self.regex)
self.limit = limit.get_literal_value()