forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plan.py
909 lines (772 loc) · 28.8 KB
/
plan.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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
from typing import (
List,
Optional,
Sequence,
Tuple,
Union,
cast,
TYPE_CHECKING,
Mapping,
)
import pyspark.sql.connect.proto as proto
from pyspark.sql.connect.column import (
Column,
Expression,
SortOrder,
)
if TYPE_CHECKING:
from pyspark.sql.connect.typing import ColumnOrString, ExpressionOrString
from pyspark.sql.connect.client import RemoteSparkSession
class InputValidationError(Exception):
pass
class LogicalPlan(object):
INDENT = 2
def __init__(self, child: Optional["LogicalPlan"]) -> None:
self._child = child
def unresolved_attr(self, colName: str) -> proto.Expression:
"""Creates an unresolved attribute from a column name."""
exp = proto.Expression()
exp.unresolved_attribute.unparsed_identifier = colName
return exp
def to_attr_or_expression(
self, col: "ColumnOrString", session: "RemoteSparkSession"
) -> proto.Expression:
"""Returns either an instance of an unresolved attribute or the serialized
expression value of the column."""
if type(col) is str:
return self.unresolved_attr(col)
else:
return cast(Column, col).to_plan(session)
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
...
def command(self, session: "RemoteSparkSession") -> proto.Command:
...
def _verify(self, session: "RemoteSparkSession") -> bool:
"""This method is used to verify that the current logical plan
can be serialized to Proto and back and afterwards is identical."""
plan = proto.Plan()
plan.root.CopyFrom(self.plan(session))
serialized_plan = plan.SerializeToString()
test_plan = proto.Plan()
test_plan.ParseFromString(serialized_plan)
return test_plan == plan
def to_proto(self, session: "RemoteSparkSession", debug: bool = False) -> proto.Plan:
"""
Generates connect proto plan based on this LogicalPlan.
Parameters
----------
session : :class:`RemoteSparkSession`, optional.
a session that connects remote spark cluster.
debug: bool
if enabled, the proto plan will be printed.
"""
plan = proto.Plan()
plan.root.CopyFrom(self.plan(session))
if debug:
print(plan)
return plan
def print(self, indent: int = 0) -> str:
...
def _repr_html_(self) -> str:
...
def _child_repr_(self) -> str:
return self._child._repr_html_() if self._child is not None else ""
class DataSource(LogicalPlan):
"""A datasource with a format and optional a schema from which Spark reads data"""
def __init__(
self,
format: str = "",
schema: Optional[str] = None,
options: Optional[Mapping[str, str]] = None,
) -> None:
super().__init__(None)
self.format = format
self.schema = schema
self.options = options
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
plan = proto.Relation()
if self.format is not None:
plan.read.data_source.format = self.format
if self.schema is not None:
plan.read.data_source.schema = self.schema
if self.options is not None:
for k in self.options.keys():
v = self.options.get(k)
if v is not None:
plan.read.data_source.options[k] = v
return plan
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>DataSource</b><br />
format: {self.format}
schema: {self.schema}
options: {self.options}
</li>
</ul>
"""
class Read(LogicalPlan):
def __init__(self, table_name: str) -> None:
super().__init__(None)
self.table_name = table_name
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
plan = proto.Relation()
plan.read.named_table.unparsed_identifier = self.table_name
return plan
def print(self, indent: int = 0) -> str:
return f"{' ' * indent}<Read table_name={self.table_name}>\n"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Read</b><br />
table name: {self.table_name}
</li>
</ul>
"""
class Project(LogicalPlan):
"""Logical plan object for a projection.
All input arguments are directly serialized into the corresponding protocol buffer
objects. This class only provides very limited error handling and input validation.
To be compatible with PySpark, we validate that the input arguments are all
expressions to be able to serialize them to the server.
"""
def __init__(self, child: Optional["LogicalPlan"], *columns: "ExpressionOrString") -> None:
super().__init__(child)
self._raw_columns = list(columns)
self.alias: Optional[str] = None
self._verify_expressions()
def _verify_expressions(self) -> None:
"""Ensures that all input arguments are instances of Expression or String."""
for c in self._raw_columns:
if not isinstance(c, (Expression, str)):
raise InputValidationError(
f"Only Expressions or String can be used for projections: '{c}'."
)
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
proj_exprs = []
for c in self._raw_columns:
if isinstance(c, Expression):
proj_exprs.append(c.to_plan(session))
elif c == "*":
exp = proto.Expression()
exp.unresolved_star.SetInParent()
proj_exprs.append(exp)
else:
proj_exprs.append(self.unresolved_attr(c))
plan = proto.Relation()
plan.project.input.CopyFrom(self._child.plan(session))
plan.project.expressions.extend(proj_exprs)
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<Project cols={self._raw_columns}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Project</b><br />
Columns: {",".join([str(c) for c in self._raw_columns])}
{self._child._repr_html_() if self._child is not None else ""}
</li>
</uL>
"""
class Filter(LogicalPlan):
def __init__(self, child: Optional["LogicalPlan"], filter: Expression) -> None:
super().__init__(child)
self.filter = filter
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.filter.input.CopyFrom(self._child.plan(session))
plan.filter.condition.CopyFrom(self.filter.to_plan(session))
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<Filter filter={self.filter}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Filter</b><br />
Condition: {self.filter}
{self._child_repr_()}
</li>
</uL>
"""
class Limit(LogicalPlan):
def __init__(self, child: Optional["LogicalPlan"], limit: int) -> None:
super().__init__(child)
self.limit = limit
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.limit.input.CopyFrom(self._child.plan(session))
plan.limit.limit = self.limit
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<Limit limit={self.limit}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Limit</b><br />
Limit: {self.limit} <br />
{self._child_repr_()}
</li>
</uL>
"""
class Offset(LogicalPlan):
def __init__(self, child: Optional["LogicalPlan"], offset: int = 0) -> None:
super().__init__(child)
self.offset = offset
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.offset.input.CopyFrom(self._child.plan(session))
plan.offset.offset = self.offset
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<Offset={self.offset}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Limit</b><br />
Offset: {self.offset} <br />
{self._child_repr_()}
</li>
</uL>
"""
class Deduplicate(LogicalPlan):
def __init__(
self,
child: Optional["LogicalPlan"],
all_columns_as_keys: bool = False,
column_names: Optional[List[str]] = None,
) -> None:
super().__init__(child)
self.all_columns_as_keys = all_columns_as_keys
self.column_names = column_names
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.deduplicate.all_columns_as_keys = self.all_columns_as_keys
if self.column_names is not None:
plan.deduplicate.column_names.extend(self.column_names)
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return (
f"{' ' * indent}<all_columns_as_keys={self.all_columns_as_keys} "
f"column_names={self.column_names}>\n{c_buf}"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b></b>Deduplicate<br />
all_columns_as_keys: {self.all_columns_as_keys} <br />
column_names: {self.column_names} <br />
{self._child_repr_()}
</li>
</uL>
"""
class Sort(LogicalPlan):
def __init__(
self,
child: Optional["LogicalPlan"],
columns: List[Union[SortOrder, Column, str]],
is_global: bool,
) -> None:
super().__init__(child)
self.columns = columns
self.is_global = is_global
def col_to_sort_field(
self, col: Union[SortOrder, Column, str], session: "RemoteSparkSession"
) -> proto.Sort.SortField:
if isinstance(col, SortOrder):
sf = proto.Sort.SortField()
sf.expression.CopyFrom(col.ref.to_plan(session))
sf.direction = (
proto.Sort.SortDirection.SORT_DIRECTION_ASCENDING
if col.ascending
else proto.Sort.SortDirection.SORT_DIRECTION_DESCENDING
)
sf.nulls = (
proto.Sort.SortNulls.SORT_NULLS_FIRST
if not col.nullsLast
else proto.Sort.SortNulls.SORT_NULLS_LAST
)
return sf
else:
sf = proto.Sort.SortField()
# Check string
if isinstance(col, Column):
sf.expression.CopyFrom(col.to_plan(session))
else:
sf.expression.CopyFrom(self.unresolved_attr(col))
sf.direction = proto.Sort.SortDirection.SORT_DIRECTION_ASCENDING
sf.nulls = proto.Sort.SortNulls.SORT_NULLS_LAST
return sf
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.sort.input.CopyFrom(self._child.plan(session))
plan.sort.sort_fields.extend([self.col_to_sort_field(x, session) for x in self.columns])
plan.sort.is_global = self.is_global
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<Sort columns={self.columns}, global={self.is_global}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Sort</b><br />
{", ".join([str(c) for c in self.columns])}
global: {self.is_global} <br />
{self._child_repr_()}
</li>
</uL>
"""
class Sample(LogicalPlan):
def __init__(
self,
child: Optional["LogicalPlan"],
lower_bound: float,
upper_bound: float,
with_replacement: bool,
seed: Optional[int],
) -> None:
super().__init__(child)
self.lower_bound = lower_bound
self.upper_bound = upper_bound
self.with_replacement = with_replacement
self.seed = seed
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.sample.input.CopyFrom(self._child.plan(session))
plan.sample.lower_bound = self.lower_bound
plan.sample.upper_bound = self.upper_bound
plan.sample.with_replacement = self.with_replacement
if self.seed is not None:
plan.sample.seed = self.seed
return plan
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return (
f"{' ' * indent}"
f"<Sample lowerBound={self.lower_bound}, upperBound={self.upper_bound}, "
f"withReplacement={self.with_replacement}, seed={self.seed}>"
f"\n{c_buf}"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Sample</b><br />
LowerBound: {self.lower_bound} <br />
UpperBound: {self.upper_bound} <br />
WithReplacement: {self.with_replacement} <br />
Seed: {self.seed} <br />
{self._child_repr_()}
</li>
</uL>
"""
class Aggregate(LogicalPlan):
MeasureType = Tuple["ExpressionOrString", str]
MeasuresType = Sequence[MeasureType]
OptMeasuresType = Optional[MeasuresType]
def __init__(
self,
child: Optional["LogicalPlan"],
grouping_cols: List[Column],
measures: OptMeasuresType,
) -> None:
super().__init__(child)
self.grouping_cols = grouping_cols
self.measures = measures if measures is not None else []
def _convert_measure(self, m: MeasureType, session: "RemoteSparkSession") -> proto.Expression:
exp, fun = m
proto_expr = proto.Expression()
measure = proto_expr.unresolved_function
measure.parts.append(fun)
if type(exp) is str:
measure.arguments.append(self.unresolved_attr(exp))
else:
measure.arguments.append(cast(Expression, exp).to_plan(session))
return proto_expr
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
groupings = [x.to_plan(session) for x in self.grouping_cols]
agg = proto.Relation()
agg.aggregate.input.CopyFrom(self._child.plan(session))
agg.aggregate.result_expressions.extend(
list(map(lambda x: self._convert_measure(x, session), self.measures))
)
agg.aggregate.grouping_expressions.extend(groupings)
return agg
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return (
f"{' ' * indent}<Sort columns={self.grouping_cols}"
f"measures={self.measures}>\n{c_buf}"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Aggregation</b><br />
{self._child_repr_()}
</li>
</uL>
"""
class Join(LogicalPlan):
def __init__(
self,
left: Optional["LogicalPlan"],
right: "LogicalPlan",
on: Optional[Union[str, List[str], Column]],
how: Optional[str],
) -> None:
super().__init__(left)
self.left = cast(LogicalPlan, left)
self.right = right
self.on = on
if how is None:
join_type = proto.Join.JoinType.JOIN_TYPE_INNER
elif how == "inner":
join_type = proto.Join.JoinType.JOIN_TYPE_INNER
elif how in ["outer", "full", "fullouter"]:
join_type = proto.Join.JoinType.JOIN_TYPE_FULL_OUTER
elif how in ["leftouter", "left"]:
join_type = proto.Join.JoinType.JOIN_TYPE_LEFT_OUTER
elif how in ["rightouter", "right"]:
join_type = proto.Join.JoinType.JOIN_TYPE_RIGHT_OUTER
elif how in ["leftsemi", "semi"]:
join_type = proto.Join.JoinType.JOIN_TYPE_LEFT_SEMI
elif how in ["leftanti", "anti"]:
join_type = proto.Join.JoinType.JOIN_TYPE_LEFT_ANTI
else:
raise NotImplementedError(
"""
Unsupported join type: %s. Supported join types include:
"inner", "outer", "full", "fullouter", "full_outer",
"leftouter", "left", "left_outer", "rightouter",
"right", "right_outer", "leftsemi", "left_semi",
"semi", "leftanti", "left_anti", "anti",
"""
% how
)
self.how = join_type
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
rel = proto.Relation()
rel.join.left.CopyFrom(self.left.plan(session))
rel.join.right.CopyFrom(self.right.plan(session))
if self.on is not None:
if not isinstance(self.on, list):
if isinstance(self.on, str):
rel.join.using_columns.append(self.on)
else:
rel.join.join_condition.CopyFrom(self.to_attr_or_expression(self.on, session))
else:
rel.join.using_columns.extend(self.on)
rel.join.join_type = self.how
return rel
def print(self, indent: int = 0) -> str:
i = " " * indent
o = " " * (indent + LogicalPlan.INDENT)
n = indent + LogicalPlan.INDENT * 2
return (
f"{i}<Join on={self.on} how={self.how}>\n{o}"
f"left=\n{self.left.print(n)}\n{o}right=\n{self.right.print(n)}"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Join</b><br />
Left: {self.left._repr_html_()}
Right: {self.right._repr_html_()}
</li>
</uL>
"""
class SetOperation(LogicalPlan):
def __init__(
self,
child: Optional["LogicalPlan"],
other: Optional["LogicalPlan"],
set_op: str,
is_all: bool = True,
by_name: bool = False,
) -> None:
super().__init__(child)
self.other = other
self.by_name = by_name
self.is_all = is_all
self.set_op = set_op
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
rel = proto.Relation()
if self._child is not None:
rel.set_op.left_input.CopyFrom(self._child.plan(session))
if self.other is not None:
rel.set_op.right_input.CopyFrom(self.other.plan(session))
if self.set_op == "union":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_UNION
elif self.set_op == "intersect":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_INTERSECT
elif self.set_op == "except":
rel.set_op.set_op_type = proto.SetOperation.SET_OP_TYPE_EXCEPT
else:
raise NotImplementedError(
"""
Unsupported set operation type: %s.
"""
% rel.set_op.set_op_type
)
rel.set_op.is_all = self.is_all
rel.set_op.by_name = self.by_name
return rel
def print(self, indent: int = 0) -> str:
assert self._child is not None
assert self.other is not None
i = " " * indent
o = " " * (indent + LogicalPlan.INDENT)
n = indent + LogicalPlan.INDENT * 2
return (
f"{i}SetOperation\n{o}child1=\n{self._child.print(n)}"
f"\n{o}child2=\n{self.other.print(n)}"
)
def _repr_html_(self) -> str:
assert self._child is not None
assert self.other is not None
return f"""
<ul>
<li>
<b>SetOperation</b><br />
Left: {self._child._repr_html_()}
Right: {self.other._repr_html_()}
</li>
</uL>
"""
class Repartition(LogicalPlan):
"""Repartition Relation into a different number of partitions."""
def __init__(self, child: Optional["LogicalPlan"], num_partitions: int, shuffle: bool) -> None:
super().__init__(child)
self._num_partitions = num_partitions
self._shuffle = shuffle
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
rel = proto.Relation()
if self._child is not None:
rel.repartition.input.CopyFrom(self._child.plan(session))
rel.repartition.shuffle = self._shuffle
rel.repartition.num_partitions = self._num_partitions
return rel
def print(self, indent: int = 0) -> str:
plan_name = "repartition" if self._shuffle else "coalesce"
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<{plan_name} num_partitions={self._num_partitions}>\n{c_buf}"
def _repr_html_(self) -> str:
plan_name = "repartition" if self._shuffle else "coalesce"
return f"""
<ul>
<li>
<b>{plan_name}</b><br />
Child: {self._child_repr_()}
num_partitions: {self._num_partitions}
</li>
</ul>
"""
class SubqueryAlias(LogicalPlan):
"""Alias for a relation."""
def __init__(self, child: Optional["LogicalPlan"], alias: str) -> None:
super().__init__(child)
self._alias = alias
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
rel = proto.Relation()
if self._child is not None:
rel.subquery_alias.input.CopyFrom(self._child.plan(session))
rel.subquery_alias.alias = self._alias
return rel
def print(self, indent: int = 0) -> str:
c_buf = self._child.print(indent + LogicalPlan.INDENT) if self._child else ""
return f"{' ' * indent}<SubqueryAlias alias={self._alias}>\n{c_buf}"
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>SubqueryAlias</b><br />
Child: {self._child_repr_()}
Alias: {self._alias}
</li>
</ul>
"""
class SQL(LogicalPlan):
def __init__(self, query: str) -> None:
super().__init__(None)
self._query = query
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
rel = proto.Relation()
rel.sql.query = self._query
return rel
def print(self, indent: int = 0) -> str:
i = " " * indent
sub_query = self._query.replace("\n", "")[:50]
return f"""{i}<SQL query='{sub_query}...'>"""
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>SQL</b><br />
Statement: <pre>{self._query}</pre>
</li>
</ul>
"""
class Range(LogicalPlan):
def __init__(
self,
start: int,
end: int,
step: int,
num_partitions: Optional[int] = None,
) -> None:
super().__init__(None)
self._start = start
self._end = end
self._step = step
self._num_partitions = num_partitions
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
rel = proto.Relation()
rel.range.start = self._start
rel.range.end = self._end
rel.range.step = self._step
if self._num_partitions is not None:
rel.range.num_partitions = self._num_partitions
return rel
def print(self, indent: int = 0) -> str:
return (
f"{' ' * indent}"
f"<Range start={self._start}, end={self._end}, "
f"step={self._step}, num_partitions={self._num_partitions}>"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Range</b><br />
Start: {self._start} <br />
End: {self._end} <br />
Step: {self._step} <br />
NumPartitions: {self._num_partitions} <br />
{self._child_repr_()}
</li>
</uL>
"""
class StatSummary(LogicalPlan):
def __init__(self, child: Optional["LogicalPlan"], statistics: List[str]) -> None:
super().__init__(child)
self.statistics = statistics
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.summary.input.CopyFrom(self._child.plan(session))
plan.summary.statistics.extend(self.statistics)
return plan
def print(self, indent: int = 0) -> str:
i = " " * indent
return f"""{i}<Summary statistics='{self.statistics}'>"""
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Summary</b><br />
Statistics: {self.statistics} <br />
{self._child_repr_()}
</li>
</ul>
"""
class StatCrosstab(LogicalPlan):
def __init__(self, child: Optional["LogicalPlan"], col1: str, col2: str) -> None:
super().__init__(child)
self.col1 = col1
self.col2 = col2
def plan(self, session: "RemoteSparkSession") -> proto.Relation:
assert self._child is not None
plan = proto.Relation()
plan.crosstab.input.CopyFrom(self._child.plan(session))
plan.crosstab.col1 = self.col1
plan.crosstab.col2 = self.col2
return plan
def print(self, indent: int = 0) -> str:
i = " " * indent
return f"""{i}<Crosstab col1='{self.col1}' col2='{self.col2}'>"""
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>Crosstab</b><br />
Col1: {self.col1} <br />
Col2: {self.col2} <br />
{self._child_repr_()}
</li>
</ul>
"""
class CreateView(LogicalPlan):
def __init__(
self, child: Optional["LogicalPlan"], name: str, is_global: bool, replace: bool
) -> None:
super().__init__(child)
self._name = name
self._is_gloal = is_global
self._replace = replace
def command(self, session: "RemoteSparkSession") -> proto.Command:
assert self._child is not None
plan = proto.Command()
plan.create_dataframe_view.replace = self._replace
plan.create_dataframe_view.is_global = self._is_gloal
plan.create_dataframe_view.name = self._name
plan.create_dataframe_view.input.CopyFrom(self._child.plan(session))
return plan
def print(self, indent: int = 0) -> str:
i = " " * indent
return (
f"{i}"
f"<CreateView name='{self._name}' "
f"is_global='{self._is_gloal} "
f"replace='{self._replace}'>"
)
def _repr_html_(self) -> str:
return f"""
<ul>
<li>
<b>CreateView</b><br />
name: {self._name} <br />
is_global: {self._is_gloal} <br />
replace: {self._replace} <br />
{self._child_repr_()}
</li>
</ul>
"""