forked from google/bigframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
374 lines (281 loc) · 9.09 KB
/
Copy pathnodes.py
File metadata and controls
374 lines (281 loc) · 9.09 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
# Copyright 2023 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.
from __future__ import annotations
from dataclasses import dataclass, field, fields
import functools
import itertools
import typing
from typing import Tuple
import pandas
import bigframes.core.expression as ex
import bigframes.core.guid
from bigframes.core.join_def import JoinDefinition
from bigframes.core.ordering import OrderingColumnReference
import bigframes.core.window_spec as window
import bigframes.dtypes
import bigframes.operations.aggregations as agg_ops
if typing.TYPE_CHECKING:
import ibis.expr.types as ibis_types
import bigframes.core.ordering as orderings
import bigframes.session
@dataclass(frozen=True)
class BigFrameNode:
"""
Immutable node for representing 2D typed array as a tree of operators.
All subclasses must be hashable so as to be usable as caching key.
"""
@property
def deterministic(self) -> bool:
"""Whether this node will evaluates deterministically."""
return True
@property
def row_preserving(self) -> bool:
"""Whether this node preserves input rows."""
return True
@property
def non_local(self) -> bool:
"""
Whether this node combines information across multiple rows instead of processing rows independently.
Used as an approximation for whether the expression may require shuffling to execute (and therefore be expensive).
"""
return False
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
"""Direct children of this node"""
return tuple([])
@functools.cached_property
def session(self):
sessions = []
for child in self.child_nodes:
if child.session is not None:
sessions.append(child.session)
unique_sessions = len(set(sessions))
if unique_sessions > 1:
raise ValueError("Cannot use combine sources from multiple sessions.")
elif unique_sessions == 1:
return sessions[0]
return None
# BigFrameNode trees can be very deep so its important avoid recalculating the hash from scratch
# Each subclass of BigFrameNode should use this property to implement __hash__
# The default dataclass-generated __hash__ method is not cached
@functools.cached_property
def _node_hash(self):
return hash(tuple(hash(getattr(self, field.name)) for field in fields(self)))
@property
def peekable(self) -> bool:
"""Indicates whether the node can be sampled efficiently"""
return all(child.peekable for child in self.child_nodes)
@property
def roots(self) -> typing.Set[BigFrameNode]:
roots = itertools.chain.from_iterable(
map(lambda child: child.roots, self.child_nodes)
)
return set(roots)
@dataclass(frozen=True)
class UnaryNode(BigFrameNode):
child: BigFrameNode
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
return (self.child,)
@dataclass(frozen=True)
class JoinNode(BigFrameNode):
left_child: BigFrameNode
right_child: BigFrameNode
join: JoinDefinition
allow_row_identity_join: bool = True
@property
def row_preserving(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
return (self.left_child, self.right_child)
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
children_peekable = all(child.peekable for child in self.child_nodes)
single_root = len(self.roots) == 1
return children_peekable and single_root
@dataclass(frozen=True)
class ConcatNode(BigFrameNode):
children: Tuple[BigFrameNode, ...]
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
return self.children
def __hash__(self):
return self._node_hash
# Input Nodex
@dataclass(frozen=True)
class ReadLocalNode(BigFrameNode):
feather_bytes: bytes
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
return True
@property
def roots(self) -> typing.Set[BigFrameNode]:
return {self}
# TODO: Refactor to take raw gbq object reference
@dataclass(frozen=True)
class ReadGbqNode(BigFrameNode):
table: ibis_types.Table = field()
table_session: bigframes.session.Session = field()
columns: Tuple[ibis_types.Value, ...] = field()
hidden_ordering_columns: Tuple[ibis_types.Value, ...] = field()
ordering: orderings.ExpressionOrdering = field()
@property
def session(self):
return self.table_session
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
return True
@property
def roots(self) -> typing.Set[BigFrameNode]:
return {self}
# Unary nodes
@dataclass(frozen=True)
class PromoteOffsetsNode(UnaryNode):
col_id: str
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
return False
@property
def non_local(self) -> bool:
return False
@dataclass(frozen=True)
class FilterNode(UnaryNode):
predicate: ex.Expression
@property
def row_preserving(self) -> bool:
return False
def __hash__(self):
return self._node_hash
@dataclass(frozen=True)
class OrderByNode(UnaryNode):
by: Tuple[OrderingColumnReference, ...]
def __hash__(self):
return self._node_hash
@dataclass(frozen=True)
class ReversedNode(UnaryNode):
# useless field to make sure has distinct hash
reversed: bool = True
def __hash__(self):
return self._node_hash
@dataclass(frozen=True)
class ProjectionNode(UnaryNode):
assignments: typing.Tuple[typing.Tuple[ex.Expression, str], ...]
def __hash__(self):
return self._node_hash
# TODO: Merge RowCount and Corr into Aggregate Node
@dataclass(frozen=True)
class RowCountNode(UnaryNode):
@property
def row_preserving(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
@dataclass(frozen=True)
class AggregateNode(UnaryNode):
aggregations: typing.Tuple[typing.Tuple[str, agg_ops.AggregateOp, str], ...]
by_column_ids: typing.Tuple[str, ...] = tuple([])
dropna: bool = True
@property
def row_preserving(self) -> bool:
return False
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
# TODO: Unify into aggregate
@dataclass(frozen=True)
class CorrNode(UnaryNode):
corr_aggregations: typing.Tuple[typing.Tuple[str, str, str], ...]
def __hash__(self):
return self._node_hash
@property
def row_preserving(self) -> bool:
return False
@property
def peekable(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
@dataclass(frozen=True)
class WindowOpNode(UnaryNode):
column_name: str
op: agg_ops.WindowOp
window_spec: window.WindowSpec
output_name: typing.Optional[str] = None
never_skip_nulls: bool = False
skip_reproject_unsafe: bool = False
def __hash__(self):
return self._node_hash
@property
def peekable(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
@dataclass(frozen=True)
class ReprojectOpNode(UnaryNode):
def __hash__(self):
return self._node_hash
@dataclass(frozen=True)
class UnpivotNode(UnaryNode):
row_labels: typing.Tuple[typing.Hashable, ...]
unpivot_columns: typing.Tuple[
typing.Tuple[str, typing.Tuple[typing.Optional[str], ...]], ...
]
passthrough_columns: typing.Tuple[str, ...] = ()
index_col_ids: typing.Tuple[str, ...] = ("index",)
dtype: typing.Union[
bigframes.dtypes.Dtype, typing.Tuple[bigframes.dtypes.Dtype, ...]
] = (pandas.Float64Dtype(),)
how: typing.Literal["left", "right"] = "left"
def __hash__(self):
return self._node_hash
@property
def row_preserving(self) -> bool:
return False
@property
def non_local(self) -> bool:
return True
@property
def peekable(self) -> bool:
return False
@dataclass(frozen=True)
class RandomSampleNode(UnaryNode):
fraction: float
@property
def deterministic(self) -> bool:
return False
@property
def row_preserving(self) -> bool:
return False
def __hash__(self):
return self._node_hash