This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathnodes.py
More file actions
245 lines (185 loc) · 6 KB
/
nodes.py
File metadata and controls
245 lines (185 loc) · 6 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
# 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
import functools
import typing
from typing import Optional, Tuple
import pandas
import bigframes.core.guid
from bigframes.core.ordering import OrderingColumnReference
import bigframes.core.window_spec as window
import bigframes.dtypes
import bigframes.operations as ops
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 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
@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
left_column_ids: typing.Tuple[str, ...]
right_column_ids: typing.Tuple[str, ...]
how: typing.Literal[
"inner",
"left",
"outer",
"right",
"cross",
]
allow_row_identity_join: bool = True
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
return (self.left_child, self.right_child)
@dataclass(frozen=True)
class ConcatNode(BigFrameNode):
children: Tuple[BigFrameNode, ...]
@property
def child_nodes(self) -> typing.Sequence[BigFrameNode]:
return self.children
# Input Nodex
@dataclass(frozen=True)
class ReadLocalNode(BigFrameNode):
feather_bytes: bytes
column_ids: typing.Tuple[str, ...]
# 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,)
# Unary nodes
@dataclass(frozen=True)
class DropColumnsNode(UnaryNode):
columns: Tuple[str, ...]
@dataclass(frozen=True)
class PromoteOffsetsNode(UnaryNode):
col_id: str
@dataclass(frozen=True)
class FilterNode(UnaryNode):
predicate_id: str
keep_null: bool = False
@dataclass(frozen=True)
class OrderByNode(UnaryNode):
by: Tuple[OrderingColumnReference, ...]
@dataclass(frozen=True)
class ReversedNode(UnaryNode):
pass
@dataclass(frozen=True)
class SelectNode(UnaryNode):
column_ids: typing.Tuple[str, ...]
@dataclass(frozen=True)
class ProjectUnaryOpNode(UnaryNode):
input_id: str
op: ops.UnaryOp
output_id: Optional[str] = None
@dataclass(frozen=True)
class ProjectBinaryOpNode(UnaryNode):
left_input_id: str
right_input_id: str
op: ops.BinaryOp
output_id: str
@dataclass(frozen=True)
class ProjectTernaryOpNode(UnaryNode):
input_id1: str
input_id2: str
input_id3: str
op: ops.TernaryOp
output_id: str
@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
# TODO: Unify into aggregate
@dataclass(frozen=True)
class CorrNode(UnaryNode):
corr_aggregations: typing.Tuple[typing.Tuple[str, str, str], ...]
@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
@dataclass(frozen=True)
class ReprojectOpNode(UnaryNode):
pass
@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"
@dataclass(frozen=True)
class AssignNode(UnaryNode):
source_id: str
destination_id: str
@dataclass(frozen=True)
class AssignConstantNode(UnaryNode):
destination_id: str
value: typing.Hashable
dtype: typing.Optional[bigframes.dtypes.Dtype]
@dataclass(frozen=True)
class RandomSampleNode(UnaryNode):
fraction: float
@property
def deterministic(self) -> bool:
return False