-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathnumeric_ops.py
277 lines (199 loc) · 8.66 KB
/
numeric_ops.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
# Copyright 2025 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.
import dataclasses
import typing
from bigframes import dtypes
from bigframes.operations import base_ops
import bigframes.operations.type as op_typing
sin_op = base_ops.create_unary_op(
name="sin", type_signature=op_typing.UNARY_REAL_NUMERIC
)
cos_op = base_ops.create_unary_op(
name="cos", type_signature=op_typing.UNARY_REAL_NUMERIC
)
tan_op = base_ops.create_unary_op(
name="tan", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arcsin_op = base_ops.create_unary_op(
name="arcsin", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arccos_op = base_ops.create_unary_op(
name="arccos", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arctan_op = base_ops.create_unary_op(
name="arctan", type_signature=op_typing.UNARY_REAL_NUMERIC
)
sinh_op = base_ops.create_unary_op(
name="sinh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
cosh_op = base_ops.create_unary_op(
name="cosh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
tanh_op = base_ops.create_unary_op(
name="tanh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arcsinh_op = base_ops.create_unary_op(
name="arcsinh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arccosh_op = base_ops.create_unary_op(
name="arccosh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
arctanh_op = base_ops.create_unary_op(
name="arctanh", type_signature=op_typing.UNARY_REAL_NUMERIC
)
floor_op = base_ops.create_unary_op(
name="floor", type_signature=op_typing.UNARY_REAL_NUMERIC
)
ceil_op = base_ops.create_unary_op(
name="ceil", type_signature=op_typing.UNARY_REAL_NUMERIC
)
abs_op = base_ops.create_unary_op(
name="abs", type_signature=op_typing.UNARY_NUMERIC_AND_TIMEDELTA
)
pos_op = base_ops.create_unary_op(
name="pos", type_signature=op_typing.UNARY_NUMERIC_AND_TIMEDELTA
)
neg_op = base_ops.create_unary_op(
name="neg", type_signature=op_typing.UNARY_NUMERIC_AND_TIMEDELTA
)
exp_op = base_ops.create_unary_op(
name="exp", type_signature=op_typing.UNARY_REAL_NUMERIC
)
expm1_op = base_ops.create_unary_op(
name="expm1", type_signature=op_typing.UNARY_REAL_NUMERIC
)
ln_op = base_ops.create_unary_op(
name="log", type_signature=op_typing.UNARY_REAL_NUMERIC
)
log10_op = base_ops.create_unary_op(
name="log10", type_signature=op_typing.UNARY_REAL_NUMERIC
)
log1p_op = base_ops.create_unary_op(
name="log1p", type_signature=op_typing.UNARY_REAL_NUMERIC
)
sqrt_op = base_ops.create_unary_op(
name="sqrt", type_signature=op_typing.UNARY_REAL_NUMERIC
)
@dataclasses.dataclass(frozen=True)
class AddOp(base_ops.BinaryOp):
name: typing.ClassVar[str] = "add"
def output_type(self, *input_types):
left_type = input_types[0]
right_type = input_types[1]
if all(map(dtypes.is_string_like, input_types)) and len(set(input_types)) == 1:
# String addition
return input_types[0]
# Temporal addition.
if dtypes.is_datetime_like(left_type) and right_type is dtypes.TIMEDELTA_DTYPE:
return left_type
if left_type is dtypes.TIMEDELTA_DTYPE and dtypes.is_datetime_like(right_type):
return right_type
if left_type == dtypes.DATE_DTYPE and right_type == dtypes.TIMEDELTA_DTYPE:
return dtypes.DATETIME_DTYPE
if left_type == dtypes.TIMEDELTA_DTYPE and right_type == dtypes.DATE_DTYPE:
return dtypes.DATETIME_DTYPE
if left_type is dtypes.TIMEDELTA_DTYPE and right_type is dtypes.TIMEDELTA_DTYPE:
return dtypes.TIMEDELTA_DTYPE
if (left_type is None or dtypes.is_numeric(left_type)) and (
right_type is None or dtypes.is_numeric(right_type)
):
# Numeric addition
return dtypes.coerce_to_common(left_type, right_type)
raise TypeError(f"Cannot add dtypes {left_type} and {right_type}")
add_op = AddOp()
@dataclasses.dataclass(frozen=True)
class SubOp(base_ops.BinaryOp):
name: typing.ClassVar[str] = "sub"
# Note: this is actualyl a vararg op, but we don't model that yet
def output_type(self, *input_types):
left_type = input_types[0]
right_type = input_types[1]
if dtypes.is_datetime_like(left_type) and dtypes.is_datetime_like(right_type):
return dtypes.TIMEDELTA_DTYPE
if left_type == dtypes.DATE_DTYPE and right_type == dtypes.DATE_DTYPE:
return dtypes.TIMEDELTA_DTYPE
if dtypes.is_datetime_like(left_type) and right_type is dtypes.TIMEDELTA_DTYPE:
return left_type
if left_type == dtypes.DATE_DTYPE and right_type == dtypes.TIMEDELTA_DTYPE:
return dtypes.DATETIME_DTYPE
if left_type is dtypes.TIMEDELTA_DTYPE and right_type is dtypes.TIMEDELTA_DTYPE:
return dtypes.TIMEDELTA_DTYPE
if (left_type is None or dtypes.is_numeric(left_type)) and (
right_type is None or dtypes.is_numeric(right_type)
):
# Numeric subtraction
return dtypes.coerce_to_common(left_type, right_type)
raise TypeError(f"Cannot subtract dtypes {left_type} and {right_type}")
sub_op = SubOp()
@dataclasses.dataclass(frozen=True)
class MulOp(base_ops.BinaryOp):
name: typing.ClassVar[str] = "mul"
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
left_type = input_types[0]
right_type = input_types[1]
if left_type is dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right_type):
return dtypes.TIMEDELTA_DTYPE
if dtypes.is_numeric(left_type) and right_type is dtypes.TIMEDELTA_DTYPE:
return dtypes.TIMEDELTA_DTYPE
if (left_type is None or dtypes.is_numeric(left_type)) and (
right_type is None or dtypes.is_numeric(right_type)
):
return dtypes.coerce_to_common(left_type, right_type)
raise TypeError(f"Cannot multiply dtypes {left_type} and {right_type}")
mul_op = MulOp()
@dataclasses.dataclass(frozen=True)
class DivOp(base_ops.BinaryOp):
name: typing.ClassVar[str] = "div"
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
left_type = input_types[0]
right_type = input_types[1]
if left_type is dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right_type):
return dtypes.TIMEDELTA_DTYPE
if left_type is dtypes.TIMEDELTA_DTYPE and right_type is dtypes.TIMEDELTA_DTYPE:
return dtypes.FLOAT_DTYPE
if (left_type is None or dtypes.is_numeric(left_type)) and (
right_type is None or dtypes.is_numeric(right_type)
):
lcd_type = dtypes.coerce_to_common(left_type, right_type)
# Real numeric ops produce floats on int input
return dtypes.FLOAT_DTYPE if lcd_type == dtypes.INT_DTYPE else lcd_type
raise TypeError(f"Cannot divide dtypes {left_type} and {right_type}")
div_op = DivOp()
@dataclasses.dataclass(frozen=True)
class FloorDivOp(base_ops.BinaryOp):
name: typing.ClassVar[str] = "floordiv"
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
left_type = input_types[0]
right_type = input_types[1]
if left_type is dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right_type):
return dtypes.TIMEDELTA_DTYPE
if left_type is dtypes.TIMEDELTA_DTYPE and right_type is dtypes.TIMEDELTA_DTYPE:
return dtypes.INT_DTYPE
if (left_type is None or dtypes.is_numeric(left_type)) and (
right_type is None or dtypes.is_numeric(right_type)
):
return dtypes.coerce_to_common(left_type, right_type)
raise TypeError(f"Cannot floor divide dtypes {left_type} and {right_type}")
floordiv_op = FloorDivOp()
pow_op = base_ops.create_binary_op(name="pow", type_signature=op_typing.BINARY_NUMERIC)
mod_op = base_ops.create_binary_op(name="mod", type_signature=op_typing.BINARY_NUMERIC)
arctan2_op = base_ops.create_binary_op(
name="arctan2", type_signature=op_typing.BINARY_REAL_NUMERIC
)
round_op = base_ops.create_binary_op(
name="round", type_signature=op_typing.BINARY_REAL_NUMERIC
)
unsafe_pow_op = base_ops.create_binary_op(
name="unsafe_pow_op", type_signature=op_typing.BINARY_REAL_NUMERIC
)