-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfield.py
More file actions
334 lines (268 loc) · 9.56 KB
/
field.py
File metadata and controls
334 lines (268 loc) · 9.56 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
# Copyright 2019 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
#
# https://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.
"""Helper to deal with field types in Spanner interactions."""
import abc
import base64
import binascii
import datetime
import re
from typing import Any, Optional, Type, Union
import warnings
from google.cloud import spanner
from google.cloud import spanner_v1
from spanner_orm import error
class FieldType(abc.ABC):
"""Base class for column types for Spanner interactions."""
@abc.abstractmethod
def ddl(self) -> str:
"""Returns the DDL for this type."""
raise NotImplementedError
@abc.abstractmethod
def grpc_type(self) -> spanner_v1.Type:
"""Returns the type as used in Cloud Spanner's gRPC API."""
raise NotImplementedError
@abc.abstractmethod
def validate_type(self, value: Any) -> None:
"""Raises error.ValidationError if value doesn't match the type."""
raise NotImplementedError
def comparable_with(self, other: 'FieldType') -> bool:
"""Returns whether two types are comparable."""
# https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#comparable_data_types
return type(self) == type(other)
class Field:
"""Represents a column in a table as a field in a model.
Attributes:
name: Name of the column, or None if this hasn't been bound to a column yet.
"""
name: Optional[str]
def __init__(
self,
field_type: Union[FieldType, Type[FieldType]],
*,
nullable: bool = False,
primary_key: bool = False,
):
"""Initializer.
Args:
field_type: Type of the field. Passing a class instead of an instance of
that class is deprecated.
nullable: Whether the field can be NULL.
primary_key: Whether the field is part of the table's primary key.
"""
self.name = None
if isinstance(field_type, FieldType):
self._type = field_type
else:
warnings.warn(
DeprecationWarning(
'Pass an instance of FieldType instead of a class.'))
self._type = field_type()
self._nullable = nullable
self._primary_key = primary_key
def ddl(self) -> str:
"""Returns DDL for the column."""
if self._nullable:
return self._type.ddl()
return f'{self._type.ddl()} NOT NULL'
def field_type(self) -> FieldType:
"""Returns the type of the field."""
return self._type
def grpc_type(self) -> spanner_v1.Type:
"""Returns the type as used in Cloud Spanner's gRPC API."""
return self._type.grpc_type()
def nullable(self) -> bool:
"""Returns whether the field can be NULL."""
return self._nullable
def primary_key(self) -> bool:
"""Returns whether the field is part of the table's primary key."""
return self._primary_key
def validate(self, value: Any) -> None:
"""Raises error.ValidationError if value isn't compatible with the field."""
if value is None:
if not self._nullable:
raise error.ValidationError('None set for non-nullable field')
else:
self._type.validate_type(value)
class Boolean(FieldType):
"""Represents a boolean type."""
def ddl(self) -> str:
"""See base class."""
del self # Unused.
return 'BOOL'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.BOOL
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, bool):
raise error.ValidationError(f'{value!r} is not of type bool')
class Integer(FieldType):
"""Represents an integer type."""
def ddl(self) -> str:
"""See base class."""
del self # Unused.
return 'INT64'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.INT64
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, int):
raise error.ValidationError(f'{value!r} is not of type int')
class Float(FieldType):
"""Represents a float type."""
def ddl(self) -> str:
"""See base class."""
del self # Unused.
return 'FLOAT64'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.FLOAT64
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, (int, float)):
raise error.ValidationError(f'{value!r} is not of type float')
class String(FieldType):
"""Represents a string type."""
def __init__(self, length: Optional[int] = None):
"""Initializer.
Args:
length: Length of the String. MAX is used if not specified.
"""
if length is not None and length <= 0:
raise error.ValidationError('String length must be positive')
self._length = length
def ddl(self) -> str:
"""See base class."""
if self._length is not None:
return f'STRING({self._length})'
return 'STRING(MAX)'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.STRING
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, str):
raise error.ValidationError(f'{value!r} is not of type str')
class Timestamp(FieldType):
"""Represents a timestamp type."""
def ddl(self) -> str:
"""See base class."""
del self # Unused.
return 'TIMESTAMP'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.TIMESTAMP
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, datetime.datetime):
raise error.ValidationError(f'{value!r} is not of type datetime')
class BytesBase64(FieldType):
"""Represents a bytes type that must be base64 encoded."""
def __init__(self, length: Optional[int] = None):
"""Initializer.
Args:
length: Length of the Bytes. MAX is used if not specified.
"""
if length is not None and length <= 0:
raise error.ValidationError('Bytes length must be positive')
self._length = length
def ddl(self) -> str:
"""See base class."""
if self._length is not None:
return f'BYTES({self._length})'
return 'BYTES(MAX)'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
del self # Unused.
return spanner.param_types.BYTES
def validate_type(self, value: Any) -> None:
"""See base class."""
del self # Unused.
if not isinstance(value, bytes):
raise error.ValidationError(f'{value!r} is not of type bytes')
# Rudimentary test to check for base64 encoding.
try:
base64.b64decode(value, altchars=None, validate=True)
except binascii.Error:
raise error.ValidationError(f'{value!r} must be base64-encoded bytes.')
class Array(FieldType):
"""Represents an array type."""
def __init__(self, element_type: FieldType):
"""Initializer.
Args:
element_type: Type of the values in the array. Can't be an Array type
itself.
"""
if isinstance(element_type, Array):
# https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#array_type
raise error.SpannerError(
'Cloud Spanner does not support arrays of arrays.')
self._element_type = element_type
def ddl(self) -> str:
"""See base class."""
return f'ARRAY<{self._element_type.ddl()}>'
def grpc_type(self) -> spanner_v1.Type:
"""See base class."""
return spanner.param_types.Array(self._element_type.grpc_type())
def validate_type(self, value: Any) -> None:
"""See base class."""
if not isinstance(value, list):
raise error.ValidationError(f'{value!r} is not of type list')
for element in value:
self._element_type.validate_type(element)
def comparable_with(self, other: FieldType) -> bool:
"""See base class."""
# Running `select [1, 2] = [1, 2];` in Cloud Spanner gives this error: Query
# failed: Equality is not defined for arguments of type ARRAY<INT64> at line
# 3, column 8
return False
class StringArray(Array):
"""Deprecated way to represent an array of strings type."""
def __init__(self):
super().__init__(String())
warnings.warn(
DeprecationWarning('Use Array(String()) instead of StringArray().'))
def field_type_from_ddl(ddl: str) -> FieldType:
"""Returns the field type for the given DDL expression."""
if ddl == 'BOOL':
return Boolean()
elif ddl == 'INT64':
return Integer()
elif ddl == 'FLOAT64':
return Float()
elif ddl == 'STRING(MAX)':
return String()
elif (match := re.fullmatch(r'STRING\(([0-9]+)\)', ddl)) is not None:
return String(int(match.group(1)))
elif ddl == 'TIMESTAMP':
return Timestamp()
elif ddl == 'BYTES(MAX)':
return BytesBase64()
elif (match := re.fullmatch(r'BYTES\(([0-9]+)\)', ddl)) is not None:
return BytesBase64(int(match.group(1)))
elif (match := re.fullmatch(r'ARRAY<(.*)>', ddl)) is not None:
return Array(field_type_from_ddl(match.group(1)))
else:
raise error.SpannerError(f'Invalid or unimplemented DDL type: {ddl!r}')