-
Notifications
You must be signed in to change notification settings - Fork 44
/
column.py
726 lines (598 loc) · 23.7 KB
/
column.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
from pysparkling.sql.expressions.expressions import Expression
from pysparkling.sql.expressions.fields import find_position_in_schema
from pysparkling.sql.expressions.literals import Literal
from pysparkling.sql.expressions.mappers import StarOperator, CaseWhen
from pysparkling.sql.expressions.operators import Negate, Add, Minus, Time, Divide, Mod, Pow, \
Equal, LessThan, LessThanOrEqual, GreaterThanOrEqual, GreaterThan, EqNullSafe, And, Or, \
Invert, BitwiseOr, BitwiseAnd, BitwiseXor, GetField, Contains, IsNull, IsNotNull, StartsWith, \
EndsWith, Substring, IsIn, Alias, Cast
from pysparkling.sql.expressions.orders import DescNullsLast, DescNullsFirst, Desc, \
AscNullsLast, AscNullsFirst, Asc, SortOrder
from pysparkling.sql.types import string_to_type, DataType, StructField
from pysparkling.sql.utils import IllegalArgumentException
class Column(object):
"""
A column in a DataFrame.
:class:`Column` instances can be created by::
# 1. Select a column out of a DataFrame
df.colName
df["colName"]
# 2. Create from an expression
df.colName + 1
1 / df.colName
"""
def __init__(self, expr):
self.expr = expr
# arithmetic operators
def __neg__(self):
return Column(Negate(self))
def __add__(self, other):
return Column(Add(self, parse_operator(other)))
def __sub__(self, other):
return Column(Minus(self, parse_operator(other)))
def __mul__(self, other):
return Column(Time(self, parse_operator(other)))
def __div__(self, other):
return Column(Divide(self, parse_operator(other)))
def __truediv__(self, other):
return Column(Divide(self, parse_operator(other)))
def __mod__(self, other):
return Column(Mod(self, parse_operator(other)))
def __radd__(self, other):
return Column(Add(self, parse_operator(other)))
def __rsub__(self, other):
return Column(Minus(parse_operator(other), self))
def __rmul__(self, other):
return Column(Time(parse_operator(other), self))
def __rdiv__(self, other):
return Column(Divide(parse_operator(other), self))
def __rtruediv__(self, other):
return Column(Divide(parse_operator(other), self))
def __rmod__(self, other):
return Column(Mod(parse_operator(other), self))
def __pow__(self, power):
return Column(Pow(self, parse_operator(power)))
def __rpow__(self, power):
return Column(Pow(parse_operator(power), self))
# comparison operators
def __eq__(self, other):
return Column(Equal(self, parse_operator(other)))
def __ne__(self, other):
return Column(Negate(Equal(self, parse_operator(other))))
def __lt__(self, other):
return Column(LessThan(self, parse_operator(other)))
def __le__(self, other):
return Column(LessThanOrEqual(self, parse_operator(other)))
def __ge__(self, other):
return Column(GreaterThanOrEqual(self, parse_operator(other)))
def __gt__(self, other):
return Column(GreaterThan(self, parse_operator(other)))
def between(self, lowerBound, upperBound):
"""
A boolean expression that is evaluated to true if the value of this
expression is between the given columns.
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.name, df.age.between(2, 4)).show()
+-----+---------------------------+
| name|((age >= 2) AND (age <= 4))|
+-----+---------------------------+
|Alice| true|
| Bob| false|
+-----+---------------------------+
"""
return (self >= lowerBound) & (self <= upperBound)
def eqNullSafe(self, other):
return Column(EqNullSafe(self, parse_operator(other)))
# `and`, `or`, `not` cannot be overloaded in Python,
# so bitwise operators are used as boolean operators
def __and__(self, other):
return Column(And(self, parse_operator(other)))
def __or__(self, other):
return Column(Or(self, parse_operator(other)))
def __invert__(self):
return Column(Invert(self))
def __rand__(self, other):
return Column(And(parse_operator(other), self))
def __ror__(self, other):
return Column(Or(parse_operator(other), self))
def __contains__(self, item):
raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
"in a string column or 'array_contains' function for an array column.")
def bitwiseOR(self, other):
return Column(BitwiseOr(self, parse_operator(other)))
def bitwiseAND(self, other):
return Column(BitwiseAnd(self, parse_operator(other)))
def bitwiseXOR(self, other):
return Column(BitwiseXor(self, parse_operator(other)))
def getItem(self, key):
"""
An expression that gets an item at position ``ordinal`` out of a list,
or gets an item by key out of a dict.
>>> from pysparkling import Context
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame([([1, 2], {"key": "value"})], ["l", "d"])
>>> df.select(df.l.getItem(0), df.d.getItem("key")).show()
+----+------+
|l[0]|d[key]|
+----+------+
| 1| value|
+----+------+
>>> df.select(df.l[0], df.d["key"]).show()
+----+------+
|l[0]|d[key]|
+----+------+
| 1| value|
+----+------+
"""
return self[key]
def getField(self, name):
"""
An expression that gets a field by name in a StructField.
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame([Row(r=Row(a=1, b="b"))])
>>> df.select(df.r.getField("b")).show()
+---+
|r.b|
+---+
| b|
+---+
>>> df.select(df.r.a).show()
+---+
|r.a|
+---+
| 1|
+---+
"""
if not isinstance(name, Column):
name = Literal(name)
return Column(GetField(self, name))
def __getattr__(self, item):
if item.startswith("__"):
raise AttributeError(item)
return self.getField(item)
def __getitem__(self, k):
if isinstance(k, slice):
if k.step is not None:
raise ValueError("slice with step is not supported.")
return self.substr(k.start, k.stop)
return self.getField(k)
def __iter__(self):
raise TypeError("Column is not iterable")
def contains(self, other):
return Column(Contains(self, parse_operator(other)))
# pylint: disable=W0511
# todo: Like
def rlike(self, other):
raise NotImplementedError("rlike is not yet implemented in pysparkling")
def like(self, other):
raise NotImplementedError("like is not yet implemented in pysparkling")
def startswith(self, substr):
return Column(StartsWith(self, parse_operator(substr)))
def endswith(self, substr):
return Column(EndsWith(self, parse_operator(substr)))
def substr(self, startPos, length):
"""
Return a :class:`Column` which is a substring of the column.
:param startPos: start position (int or Column)
:param length: length of the substring (int or Column)
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.name.substr(1, 3).alias("col")).collect()
[Row(col='Ali'), Row(col='Bob')]
"""
if not isinstance(startPos, type(length)):
raise TypeError(
"startPos and length must be the same type. "
"Got {0} and {1}, respectively.".format(type(startPos), type(length))
)
return Column(Substring(self, startPos, length))
def isin(self, *exprs):
"""
A boolean expression that is evaluated to true if the value of this
expression is contained by the evaluated values of the arguments.
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df[df.name.isin("Bob", "Mike")].collect()
[Row(age=5, name='Bob')]
>>> df[df.age.isin([1, 2, 3])].collect()
[Row(age=2, name='Alice')]
"""
if len(exprs) == 1 and isinstance(exprs[0], (list, set)):
exprs = exprs[0]
return Column(IsIn(self, exprs))
def asc(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> # asc is the default order
>>> df = spark.range(5).withColumn(
... "order", when(col('id')%2 == 0, col('id'))
... ).orderBy("order").show()
+---+-----+
| id|order|
+---+-----+
| 1| null|
| 3| null|
| 0| 0|
| 2| 2|
| 4| 4|
+---+-----+
>>> df = spark.range(5).withColumn(
... "order", when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").asc()).show()
+---+-----+
| id|order|
+---+-----+
| 1| null|
| 3| null|
| 0| 0|
| 2| 2|
| 4| 4|
+---+-----+
"""
return Column(Asc(self))
def asc_nulls_first(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> df = spark.range(5).withColumn("order",
... when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").asc_nulls_first()).show()
+---+-----+
| id|order|
+---+-----+
| 1| null|
| 3| null|
| 0| 0|
| 2| 2|
| 4| 4|
+---+-----+
"""
return Column(AscNullsFirst(self))
def asc_nulls_last(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> df = spark.range(5).withColumn("order",
... when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").asc_nulls_last()).show()
+---+-----+
| id|order|
+---+-----+
| 0| 0|
| 2| 2|
| 4| 4|
| 1| null|
| 3| null|
+---+-----+
"""
return Column(AscNullsLast(self))
def desc(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> df = spark.range(5).withColumn("order",
... when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").desc()).show()
+---+-----+
| id|order|
+---+-----+
| 4| 4|
| 2| 2|
| 0| 0|
| 1| null|
| 3| null|
+---+-----+
"""
return Column(Desc(self))
def desc_nulls_first(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> df = spark.range(5).withColumn("order",
... when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").desc_nulls_first()).show()
+---+-----+
| id|order|
+---+-----+
| 1| null|
| 3| null|
| 4| 4|
| 2| 2|
| 0| 0|
+---+-----+
"""
return Column(DescNullsFirst(self))
def desc_nulls_last(self):
"""
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.functions import when, col
>>> spark = SparkSession(Context())
>>> df = spark.range(5).withColumn("order",
... when(col('id')%2 == 0, col('id'))
... ).orderBy(col("order").desc_nulls_last()).show()
+---+-----+
| id|order|
+---+-----+
| 4| 4|
| 2| 2|
| 0| 0|
| 1| null|
| 3| null|
+---+-----+
"""
return Column(DescNullsLast(self))
def isNull(self):
return Column(IsNull(self))
def isNotNull(self):
return Column(IsNotNull(self))
def alias(self, *alias, **kwargs):
"""
Returns this column aliased with a new name or names (in the case of expressions that
return more than one column, such as explode).
:param alias: strings of desired column names (collects all positional arguments passed)
:param metadata: a dict of information to be stored in ``metadata`` attribute of the
corresponding :class: `StructField` (optional, keyword only argument)
.. versionchanged:: 2.2
Added optional ``metadata`` argument.
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.age.alias("age2")).collect()
[Row(age2=2), Row(age2=5)]
>>> from pysparkling.sql.functions import map_from_arrays, array
>>> spark.range(3).select(map_from_arrays(array("id"), array("id"))).show()
+-------------------------------------+
|map_from_arrays(array(id), array(id))|
+-------------------------------------+
| [0 -> 0]|
| [1 -> 1]|
| [2 -> 2]|
+-------------------------------------+
"""
metadata = kwargs.pop('metadata', None)
assert not kwargs, 'Unexpected kwargs where passed: %s' % kwargs
if metadata:
# pylint: disable=W0511
# todo: support it
raise ValueError('Pysparkling does not support alias with metadata')
if len(alias) == 1:
return Column(Alias(self, alias[0]))
# pylint: disable=W0511
# todo: support it
raise ValueError('Pysparkling does not support multiple aliases')
def name(self, *alias, **kwargs):
return self.alias(*alias, **kwargs)
def cast(self, dataType):
""" Convert the column into type ``dataType``.
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> from pysparkling.sql.types import StringType
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.name, df.age.between(2, 4).alias('taapero')).collect()
[Row(name='Alice', taapero=True), Row(name='Bob', taapero=False)]
>>> df.select(df.age.cast("string").alias('ages')).collect()
[Row(ages='2'), Row(ages='5')]
>>> df.select(df.age.cast(StringType()).alias('ages')).collect()
[Row(ages='2'), Row(ages='5')]
>>> df.select(df.age.cast('float')).show()
+---+
|age|
+---+
|2.0|
|5.0|
+---+
>>> df.select(df.age.cast('decimal(5, 0)')).show()
+---+
|age|
+---+
| 2|
| 5|
+---+
"""
if isinstance(dataType, str):
dataType = string_to_type(dataType)
elif not isinstance(dataType, DataType):
raise NotImplementedError("Unknown cast type: {}".format(dataType))
return Column(Cast(self, dataType))
def astype(self, dataType):
return self.cast(dataType)
def when(self, condition, value):
"""
Evaluates a list of conditions and returns one of multiple possible result expressions.
If :func:`Column.otherwise` is not invoked, None is returned for unmatched conditions.
See :func:`pyspark.sql.functions.when` for example usage.
:param condition: a boolean :class:`Column` expression.
:param value: a literal value, or a :class:`Column` expression.
>>> from pysparkling.sql import functions as F
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.name, F.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)).show()
+-----+------------------------------------------------------------+
| name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0 END|
+-----+------------------------------------------------------------+
|Alice| -1|
| Bob| 1|
+-----+------------------------------------------------------------+
"""
if not isinstance(condition, Column):
raise TypeError("condition should be a Column")
if not isinstance(self.expr, CaseWhen):
raise IllegalArgumentException(
'when() can only be applied on a Column previously generated by when()'
)
return Column(self.expr.add_when(parse(condition), parse(value)))
def otherwise(self, value):
"""
Evaluates a list of conditions and returns one of multiple possible result expressions.
If :func:`Column.otherwise` is not invoked, None is returned for unmatched conditions.
See :func:`pyspark.sql.functions.when` for example usage.
:param value: a literal value, or a :class:`Column` expression.
>>> from pysparkling.sql import functions as F
>>> from pysparkling import Context, Row
>>> from pysparkling.sql.session import SparkSession
>>> spark = SparkSession(Context())
>>> df = spark.createDataFrame(
... [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
... )
>>> df.select(df.name, F.when(df.age > 3, 1).otherwise(0)).show()
+-----+-------------------------------------+
| name|CASE WHEN (age > 3) THEN 1 ELSE 0 END|
+-----+-------------------------------------+
|Alice| 0|
| Bob| 1|
+-----+-------------------------------------+
"""
if not isinstance(self.expr, CaseWhen):
raise IllegalArgumentException(
'otherwise() can only be applied on a Column previously generated by when()'
)
return Column(self.expr.set_otherwise(parse(value)))
def eval(self, row, schema):
if isinstance(self.expr, Expression):
return self.expr.eval(row, schema)
return row[self.find_position_in_schema(schema)]
def find_fields_in_schema(self, schema):
if isinstance(self.expr, Expression):
return self.expr.output_fields(schema)
return [schema[self.find_position_in_schema(schema)]]
def find_position_in_schema(self, schema):
return find_position_in_schema(schema, self.expr)
@property
def may_output_multiple_cols(self):
if isinstance(self.expr, Expression):
return self.expr.may_output_multiple_cols
return False
@property
def may_output_multiple_rows(self):
if isinstance(self.expr, Expression):
return self.expr.may_output_multiple_rows
return False
@property
def is_an_aggregation(self):
if isinstance(self.expr, Expression):
return self.expr.is_an_aggregation
if isinstance(self.expr, str):
return False
raise NotImplementedError(
"Not implemented column expression type: {0}".format(type(self.expr))
)
def output_fields(self, schema):
if isinstance(self.expr, Expression):
return self.expr.output_fields(schema)
return [StructField(
name=self.col_name,
dataType=self.data_type,
nullable=self.is_nullable
)]
def merge(self, row, schema):
if isinstance(self.expr, Expression):
self.expr.recursive_merge(row, schema)
return self
def mergeStats(self, row, schema):
if isinstance(self.expr, Expression):
self.expr.recursive_merge_stats(row, schema)
return self
def initialize(self, partition_index):
if isinstance(self.expr, Expression):
self.expr.recursive_initialize(partition_index)
return self
def with_pre_evaluation_schema(self, pre_evaluation_schema):
if isinstance(self.expr, Expression):
self.expr.recursive_pre_evaluation_schema(pre_evaluation_schema)
return self
@property
def sort_order(self):
if isinstance(self.expr, SortOrder):
return self.expr.sort_order
return "ASC NULLS FIRST"
# pylint: disable=W0511
# todo: support of window functions
def over(self, window):
"""
Define a windowing column.
:param window: a :class:`WindowSpec`
:return: a Column
# >>> from pyspark.sql import Window
# >>> window = Window.partitionBy("name").orderBy("age").rowsBetween(-1, 1)
# >>> from pysparkling.sql.functions import rank, min
# >>> # df.select(rank().over(window), min('age').over(window))
"""
raise NotImplementedError("window functions are not yet supported by pysparkling")
def __nonzero__(self):
raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
"'~' for 'not' when building DataFrame boolean expressions.")
__bool__ = __nonzero__
@property
def data_type(self):
# pylint: disable=W0511
# todo: be more specific
return DataType()
@property
def is_nullable(self):
return True
def __str__(self):
return str(self.expr)
@property
def col_name(self):
return str(self)
def __repr__(self):
return "Column<{0!r}>".format(self.expr)
def parse(arg):
"""
:rtype: Column
"""
if isinstance(arg, Column):
return arg
if arg == "*":
return Column(StarOperator())
if isinstance(arg, (str, Expression)):
return Column(arg)
return Literal(value=arg)
def parse_operator(arg):
"""
Column operations such as df.name == "Alice" consider "Alice" as a lit, not a column
:rtype: Column
"""
if isinstance(arg, Column):
return arg
if arg == "*":
return Column(StarOperator())
return Literal(value=arg)