1
1
import functools
2
2
import inspect
3
- import typing
4
3
from typing import Any
5
4
from typing import Callable
6
5
from typing import List
@@ -60,40 +59,6 @@ def is_valid_callable(obj: Any) -> bool:
60
59
)
61
60
62
61
63
- def verify_mask (obj : Any ) -> bool :
64
- """Verify that the object is a tuple of two vector types."""
65
- if typing .get_origin (obj ) is not tuple or len (typing .get_args (obj )) != 2 :
66
- raise TypeError (
67
- f'Expected a tuple of two vector types, but got { type (obj )} ' ,
68
- )
69
-
70
- args = typing .get_args (obj )
71
-
72
- if not utils .is_vector (args [0 ]):
73
- raise TypeError (
74
- f'Expected a vector type for the first element, but got { args [0 ]} ' ,
75
- )
76
-
77
- if not utils .is_vector (args [1 ]):
78
- raise TypeError (
79
- f'Expected a vector type for the second element, but got { args [1 ]} ' ,
80
- )
81
-
82
- return True
83
-
84
-
85
- def verify_masks (obj : Callable [..., Any ]) -> bool :
86
- """Verify that the function parameters and return value are all masks."""
87
- ann = utils .get_annotations (obj )
88
- for name , value in ann .items ():
89
- if not verify_mask (value ):
90
- raise TypeError (
91
- f'Expected a vector type for the parameter { name } '
92
- f'in function { obj .__name__ } , but got { value } ' ,
93
- )
94
- return True
95
-
96
-
97
62
def expand_types (args : Any ) -> Optional [Union [List [str ], Type [Any ]]]:
98
63
"""Expand the types for the function arguments / return values."""
99
64
if args is None :
@@ -135,8 +100,6 @@ def _func(
135
100
name : Optional [str ] = None ,
136
101
args : Optional [ParameterType ] = None ,
137
102
returns : Optional [ReturnType ] = None ,
138
- with_null_masks : bool = False ,
139
- function_type : str = 'udf' ,
140
103
) -> Callable [..., Any ]:
141
104
"""Generic wrapper for UDF and TVF decorators."""
142
105
@@ -145,8 +108,6 @@ def _func(
145
108
name = name ,
146
109
args = expand_types (args ),
147
110
returns = expand_types (returns ),
148
- with_null_masks = with_null_masks ,
149
- function_type = function_type ,
150
111
).items () if v is not None
151
112
}
152
113
@@ -155,8 +116,6 @@ def _func(
155
116
# in at that time.
156
117
if func is None :
157
118
def decorate (func : Callable [..., Any ]) -> Callable [..., Any ]:
158
- if with_null_masks :
159
- verify_masks (func )
160
119
161
120
def wrapper (* args : Any , ** kwargs : Any ) -> Callable [..., Any ]:
162
121
return func (* args , ** kwargs ) # type: ignore
@@ -167,9 +126,6 @@ def wrapper(*args: Any, **kwargs: Any) -> Callable[..., Any]:
167
126
168
127
return decorate
169
128
170
- if with_null_masks :
171
- verify_masks (func )
172
-
173
129
def wrapper (* args : Any , ** kwargs : Any ) -> Callable [..., Any ]:
174
130
return func (* args , ** kwargs ) # type: ignore
175
131
@@ -194,151 +150,7 @@ def udf(
194
150
The UDF to apply parameters to
195
151
name : str, optional
196
152
The name to use for the UDF in the database
197
- args : str | Callable | List[str | Callable], optional
198
- Specifies the data types of the function arguments. Typically,
199
- the function data types are derived from the function parameter
200
- annotations. These annotations can be overridden. If the function
201
- takes a single type for all parameters, `args` can be set to a
202
- SQL string describing all parameters. If the function takes more
203
- than one parameter and all of the parameters are being manually
204
- defined, a list of SQL strings may be used (one for each parameter).
205
- A dictionary of SQL strings may be used to specify a parameter type
206
- for a subset of parameters; the keys are the names of the
207
- function parameters. Callables may also be used for datatypes. This
208
- is primarily for using the functions in the ``dtypes`` module that
209
- are associated with SQL types with all default options (e.g., ``dt.FLOAT``).
210
- returns : str, optional
211
- Specifies the return data type of the function. If not specified,
212
- the type annotation from the function is used.
213
-
214
- Returns
215
- -------
216
- Callable
217
-
218
- """
219
- return _func (
220
- func = func ,
221
- name = name ,
222
- args = args ,
223
- returns = returns ,
224
- with_null_masks = False ,
225
- function_type = 'udf' ,
226
- )
227
-
228
-
229
- def udf_with_null_masks (
230
- func : Optional [Callable [..., Any ]] = None ,
231
- * ,
232
- name : Optional [str ] = None ,
233
- args : Optional [ParameterType ] = None ,
234
- returns : Optional [ReturnType ] = None ,
235
- ) -> Callable [..., Any ]:
236
- """
237
- Define a user-defined function (UDF) with null masks.
238
-
239
- Parameters
240
- ----------
241
- func : callable, optional
242
- The UDF to apply parameters to
243
- name : str, optional
244
- The name to use for the UDF in the database
245
- args : str | Callable | List[str | Callable], optional
246
- Specifies the data types of the function arguments. Typically,
247
- the function data types are derived from the function parameter
248
- annotations. These annotations can be overridden. If the function
249
- takes a single type for all parameters, `args` can be set to a
250
- SQL string describing all parameters. If the function takes more
251
- than one parameter and all of the parameters are being manually
252
- defined, a list of SQL strings may be used (one for each parameter).
253
- A dictionary of SQL strings may be used to specify a parameter type
254
- for a subset of parameters; the keys are the names of the
255
- function parameters. Callables may also be used for datatypes. This
256
- is primarily for using the functions in the ``dtypes`` module that
257
- are associated with SQL types with all default options (e.g., ``dt.FLOAT``).
258
- returns : str, optional
259
- Specifies the return data type of the function. If not specified,
260
- the type annotation from the function is used.
261
-
262
- Returns
263
- -------
264
- Callable
265
-
266
- """
267
- return _func (
268
- func = func ,
269
- name = name ,
270
- args = args ,
271
- returns = returns ,
272
- with_null_masks = True ,
273
- function_type = 'udf' ,
274
- )
275
-
276
-
277
- def tvf (
278
- func : Optional [Callable [..., Any ]] = None ,
279
- * ,
280
- name : Optional [str ] = None ,
281
- args : Optional [ParameterType ] = None ,
282
- returns : Optional [ReturnType ] = None ,
283
- ) -> Callable [..., Any ]:
284
- """
285
- Define a table-valued function (TVF).
286
-
287
- Parameters
288
- ----------
289
- func : callable, optional
290
- The TVF to apply parameters to
291
- name : str, optional
292
- The name to use for the TVF in the database
293
- args : str | Callable | List[str | Callable], optional
294
- Specifies the data types of the function arguments. Typically,
295
- the function data types are derived from the function parameter
296
- annotations. These annotations can be overridden. If the function
297
- takes a single type for all parameters, `args` can be set to a
298
- SQL string describing all parameters. If the function takes more
299
- than one parameter and all of the parameters are being manually
300
- defined, a list of SQL strings may be used (one for each parameter).
301
- A dictionary of SQL strings may be used to specify a parameter type
302
- for a subset of parameters; the keys are the names of the
303
- function parameters. Callables may also be used for datatypes. This
304
- is primarily for using the functions in the ``dtypes`` module that
305
- are associated with SQL types with all default options (e.g., ``dt.FLOAT``).
306
- returns : str, optional
307
- Specifies the return data type of the function. If not specified,
308
- the type annotation from the function is used.
309
-
310
- Returns
311
- -------
312
- Callable
313
-
314
- """
315
- return _func (
316
- func = func ,
317
- name = name ,
318
- args = args ,
319
- returns = returns ,
320
- with_null_masks = False ,
321
- function_type = 'tvf' ,
322
- )
323
-
324
-
325
- def tvf_with_null_masks (
326
- func : Optional [Callable [..., Any ]] = None ,
327
- * ,
328
- name : Optional [str ] = None ,
329
- args : Optional [ParameterType ] = None ,
330
- returns : Optional [ReturnType ] = None ,
331
- ) -> Callable [..., Any ]:
332
- """
333
- Define a table-valued function (TVF) using null masks.
334
-
335
- Parameters
336
- ----------
337
- func : callable, optional
338
- The TVF to apply parameters to
339
- name : str, optional
340
- The name to use for the TVF in the database
341
- args : str | Callable | List[str | Callable], optional
153
+ args : str | Type | Callable | List[str | Callable], optional
342
154
Specifies the data types of the function arguments. Typically,
343
155
the function data types are derived from the function parameter
344
156
annotations. These annotations can be overridden. If the function
@@ -351,9 +163,10 @@ def tvf_with_null_masks(
351
163
function parameters. Callables may also be used for datatypes. This
352
164
is primarily for using the functions in the ``dtypes`` module that
353
165
are associated with SQL types with all default options (e.g., ``dt.FLOAT``).
354
- returns : str, optional
355
- Specifies the return data type of the function. If not specified,
356
- the type annotation from the function is used.
166
+ returns : str | Type | Callable | List[str | Callable] | Table, optional
167
+ Specifies the return data type of the function. This parameter
168
+ works the same way as `args`. If the function is a table-valued
169
+ function, the return type should be a `Table` object.
357
170
358
171
Returns
359
172
-------
@@ -365,6 +178,4 @@ def tvf_with_null_masks(
365
178
name = name ,
366
179
args = args ,
367
180
returns = returns ,
368
- with_null_masks = True ,
369
- function_type = 'tvf' ,
370
181
)
0 commit comments