This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtype.py
More file actions
257 lines (208 loc) · 9.2 KB
/
type.py
File metadata and controls
257 lines (208 loc) · 9.2 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
# 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.
import abc
import dataclasses
from typing import Callable
import bigframes.dtypes
from bigframes.dtypes import ExpressionType
@dataclasses.dataclass
class TypeSignature(abc.ABC):
"""
Type Signature represent a mapping from input types to output type.
Type signatures should throw a TypeError if the input types cannot be handled by the operation.
"""
@property
@abc.abstractmethod
def as_method(self):
"""Convert the signature into an object method. Convenience function for constructing ops that use the signature."""
...
class UnaryTypeSignature(TypeSignature):
@abc.abstractmethod
def output_type(self, input_type: ExpressionType) -> ExpressionType:
...
@property
def as_method(self):
def meth(_, *input_types: ExpressionType) -> ExpressionType:
assert len(input_types) == 1
return self.output_type(input_types[0])
return meth
class BinaryTypeSignature(TypeSignature):
@abc.abstractmethod
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
...
@property
def as_method(self):
def meth(_, *input_types: ExpressionType) -> ExpressionType:
assert len(input_types) == 2
return self.output_type(input_types[0], input_types[1])
return meth
@dataclasses.dataclass
class TypePreserving(UnaryTypeSignature):
type_predicate: Callable[[ExpressionType], bool]
description: str
def output_type(self, input_type: ExpressionType) -> ExpressionType:
if not self.type_predicate(input_type):
raise TypeError(
f"Type {input_type} is not supported. Type must be {self.description}"
)
return input_type
@dataclasses.dataclass
class FixedOutputType(UnaryTypeSignature):
type_predicate: Callable[[ExpressionType], bool]
fixed_type: ExpressionType
description: str
def output_type(self, input_type: ExpressionType) -> ExpressionType:
if (input_type is not None) and not self.type_predicate(input_type):
raise TypeError(
f"Type {input_type} is not supported. Type must be {self.description}"
)
return self.fixed_type
@dataclasses.dataclass
class UnaryRealNumeric(UnaryTypeSignature):
"""Type signature for real-valued functions like exp, log, sin, tan."""
def output_type(self, type: ExpressionType) -> ExpressionType:
if type is None:
return bigframes.dtypes.FLOAT_DTYPE
if not bigframes.dtypes.is_numeric(type):
raise TypeError(f"Type {type} is not numeric")
if type in (bigframes.dtypes.INT_DTYPE, bigframes.dtypes.BOOL_DTYPE):
# Real numeric ops produce floats on int input
return bigframes.dtypes.FLOAT_DTYPE
return type
@dataclasses.dataclass
class BinaryNumeric(BinaryTypeSignature):
"""Type signature for numeric functions like multiply, modulo that can map ints to ints."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if (left_type is not None) and not bigframes.dtypes.is_numeric(left_type):
raise TypeError(f"Type {left_type} is not numeric")
if (right_type is not None) and not bigframes.dtypes.is_numeric(right_type):
raise TypeError(f"Type {right_type} is not numeric")
return bigframes.dtypes.coerce_to_common(left_type, right_type)
@dataclasses.dataclass
@dataclasses.dataclass
class BinaryGeo(BinaryTypeSignature):
"""Type signature for geo functions like difference that can map geo to geo."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if (left_type is not None) and not bigframes.dtypes.is_geo_like(left_type):
raise TypeError(f"Type {left_type} is not geo")
if (right_type is not None) and not bigframes.dtypes.is_geo_like(right_type):
raise TypeError(f"Type {right_type} is not numeric")
return bigframes.dtypes.GEO_DTYPE
class BinaryNumericGeo(BinaryTypeSignature):
"""Type signature for geo functions like from_xy that can map ints to ints."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if (left_type is not None) and not bigframes.dtypes.is_numeric(left_type):
raise TypeError(f"Type {left_type} is not numeric")
if (right_type is not None) and not bigframes.dtypes.is_numeric(right_type):
raise TypeError(f"Type {right_type} is not numeric")
return bigframes.dtypes.GEO_DTYPE
@dataclasses.dataclass
class BinaryRealNumeric(BinaryTypeSignature):
"""Type signature for real-valued functions like divide, arctan2, pow."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if (left_type is not None) and not bigframes.dtypes.is_numeric(left_type):
raise TypeError(f"Type {left_type} is not numeric")
if (right_type is not None) and not bigframes.dtypes.is_numeric(right_type):
raise TypeError(f"Type {right_type} is not numeric")
lcd_type = bigframes.dtypes.coerce_to_common(left_type, right_type)
if lcd_type == bigframes.dtypes.INT_DTYPE:
# Real numeric ops produce floats on int input
return bigframes.dtypes.FLOAT_DTYPE
return lcd_type
@dataclasses.dataclass
class CoerceCommon(BinaryTypeSignature):
"""Attempt to coerce inputs to a compatible type."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
return bigframes.dtypes.coerce_to_common(left_type, right_type)
@dataclasses.dataclass
class Comparison(BinaryTypeSignature):
"""Type signature for comparison operators."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if not bigframes.dtypes.can_compare(left_type, right_type):
raise TypeError(f"Types {left_type} and {right_type} are not comparable")
return bigframes.dtypes.BOOL_DTYPE
@dataclasses.dataclass
class Logical(BinaryTypeSignature):
"""Type signature for logical operators like AND, OR and NOT."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if left_type is None or right_type is None:
return bigframes.dtypes.BOOL_DTYPE
if not bigframes.dtypes.is_binary_like(left_type):
raise TypeError(f"Type {left_type} is not binary")
if not bigframes.dtypes.is_binary_like(right_type):
raise TypeError(f"Type {right_type} is not binary")
if left_type != right_type:
raise TypeError(
"Bitwise operands {left_type} and {right_type} do not match"
)
return left_type
@dataclasses.dataclass
class VectorMetric(BinaryTypeSignature):
"""Type signature for logical operators like AND, OR and NOT."""
def output_type(
self, left_type: ExpressionType, right_type: ExpressionType
) -> ExpressionType:
if not bigframes.dtypes.is_array_like(left_type):
raise TypeError(f"Type {left_type} is not array-like")
if not bigframes.dtypes.is_array_like(right_type):
raise TypeError(f"Type {right_type} is not array-like")
if left_type != right_type:
raise TypeError(
"Vector op operands {left_type} and {right_type} do not match"
)
return bigframes.dtypes.FLOAT_DTYPE
# Common type signatures
UNARY_NUMERIC = TypePreserving(bigframes.dtypes.is_numeric, description="numeric")
UNARY_NUMERIC_AND_TIMEDELTA = TypePreserving(
lambda x: bigframes.dtypes.is_numeric(x) or x is bigframes.dtypes.TIMEDELTA_DTYPE,
description="numeric_and_timedelta",
)
UNARY_REAL_NUMERIC = UnaryRealNumeric()
BINARY_NUMERIC = BinaryNumeric()
BINARY_REAL_NUMERIC = BinaryRealNumeric()
BLOB_TRANSFORM = TypePreserving(bigframes.dtypes.is_struct_like, description="blob")
COMPARISON = Comparison()
COERCE = CoerceCommon()
LOGICAL = Logical()
STRING_TRANSFORM = TypePreserving(
bigframes.dtypes.is_string_like, description="numeric"
)
STRING_PREDICATE = FixedOutputType(
bigframes.dtypes.is_string_like,
bigframes.dtypes.BOOL_DTYPE,
description="string-like",
)
DATELIKE_ACCESSOR = FixedOutputType(
bigframes.dtypes.is_date_like, bigframes.dtypes.INT_DTYPE, description="date-like"
)
TIMELIKE_ACCESSOR = FixedOutputType(
bigframes.dtypes.is_time_like, bigframes.dtypes.INT_DTYPE, description="time-like"
)
VECTOR_METRIC = VectorMetric()