Skip to content

Commit b02a467

Browse files
rustyconoverclaude
andcommitted
arguments: reject SQL NULL for non-Optional Args at bind time
Passing `NULL` as a non-vararg argument to a VGI table function was silently propagating past validation and crashing in user process()/update() code with an opaque Python TypeError, e.g.:: SELECT * FROM example.main.sequence(NULL); -- TypeError: '<=' not supported between instances of 'NoneType' and 'int' The C++ extension surfaces that as a generic VGI worker exception without a hint at the cause. The same trap fires for any numeric arg under any constraint (positional or named, with or without ge/le) because the worker does not advertise nullability of its arguments and the framework's Arg validation explicitly skips None values (it would itself TypeError if it tried to compare). Add a small `_accepts_none(annotated_inner_type)` helper that returns True iff the user's declared type is `T | None` / `Optional[T]` / `Union[T, ..., None]`, and call it from `_parse_arguments` to raise a structured `ArgumentValidationError` ("Argument ... cannot be NULL") before reaching `_validate`. Users that want to accept SQL NULL can opt in by declaring the field as `Annotated[int | None, Arg(...)]` — the existing skip-validation-for-None path keeps working for those. Varargs are intentionally left as-is: callers opt into varargs explicitly and may want to inspect the raw pa.Scalar entries (including null scalars), which is the existing behavior of constant_columns. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b77ce27 commit b02a467

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎vgi/arguments.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"""
1313

1414
import re
15+
import types
16+
import typing
1517
import warnings
1618
from collections.abc import Callable, Sequence
1719
from dataclasses import dataclass
@@ -194,6 +196,25 @@ def __bool__(self) -> bool:
194196

195197
_MISSING: Final = _MissingType()
196198

199+
200+
def _accepts_none(annotated_inner_type: Any) -> bool:
201+
"""Whether a declared Arg type allows ``None``.
202+
203+
``annotated_inner_type`` is the first type-arg of an
204+
``Annotated[T, Arg(...)]`` hint — i.e. the user's declared type for
205+
the field. Returns True iff the type is a union that includes
206+
``NoneType`` (e.g. ``int | None``, ``Optional[int]``,
207+
``Union[int, None]``). Used by argument resolvers to reject SQL NULL
208+
when the user did not opt in to nullable arguments.
209+
"""
210+
if annotated_inner_type is type(None):
211+
return True
212+
origin = typing.get_origin(annotated_inner_type)
213+
if origin is typing.Union or origin is types.UnionType:
214+
return type(None) in typing.get_args(annotated_inner_type)
215+
return False
216+
217+
197218
__all__ = [
198219
"AnyArrow",
199220
"AnyArrowValue",
@@ -1077,6 +1098,29 @@ def _describe_valid_range(self) -> str | None:
10771098

10781099
return None
10791100

1101+
def _reject_none(self) -> "ArgumentValidationError":
1102+
"""Build the error raised when SQL NULL is passed to a non-Optional Arg.
1103+
1104+
Callers ``_parse_arguments`` (table_function.py) and ``_resolve``
1105+
(this module) hit ``_validate`` with a None value when the user
1106+
wrote e.g. ``my_func(NULL)``. ``_validate``'s numeric/choice/pattern
1107+
comparisons would then crash with a Python ``TypeError`` deep in
1108+
the worker — which surfaces in the C++ extension as an opaque
1109+
traceback rather than a clean argument error. Callers use this
1110+
helper to emit a structured error before reaching ``_validate``.
1111+
"""
1112+
arg_name = self._name or str(self.position)
1113+
return ArgumentValidationError(
1114+
f"Argument '{arg_name}' cannot be NULL.",
1115+
arg_name=self._name,
1116+
position=self.position,
1117+
value=None,
1118+
constraint="must not be NULL (declare type as `T | None` to accept SQL NULL)",
1119+
doc=self.doc if self.doc else None,
1120+
valid_range=self._describe_valid_range(),
1121+
default=self.default,
1122+
)
1123+
10801124
def _validate(self, value: ArgT) -> None:
10811125
"""Validate value against all constraints.
10821126

‎vgi/table_function.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727
from vgi_rpc.rpc import AuthContext, CallContext, OutputCollector
2828

2929
import vgi.function
30-
from vgi.arguments import Arg, Arguments, Secret, SecretLookupEntry, TableInput, _extract_setting_secret_params
30+
from vgi.arguments import (
31+
Arg,
32+
Arguments,
33+
Secret,
34+
SecretLookupEntry,
35+
TableInput,
36+
_accepts_none,
37+
_extract_setting_secret_params,
38+
)
3139
from vgi.function_storage import BoundStorage
3240
from vgi.invocation import (
3341
BaseInitResponse,
@@ -476,8 +484,17 @@ def _parse_arguments(args_class: type[TArgs], arguments: Arguments) -> TArgs:
476484
kwargs[attr_name] = tuple(arguments.positional[meta.position :])
477485
else:
478486
value = arguments.get(meta.position, default=meta.default)
487+
# Reject SQL NULL for non-Optional Args. Without this,
488+
# None silently propagated through validation and
489+
# crashed deep in the user's process()/update() with
490+
# an opaque Python ``TypeError`` (e.g. ``'<=' not
491+
# supported between instances of NoneType and int``)
492+
# that surfaced in the C++ extension as a worker
493+
# exception with no hint at the cause.
494+
if value is None and not _accepts_none(base_type):
495+
raise meta._reject_none()
479496
# Run Arg constraint validation (ge/le/gt/lt/choices/pattern).
480-
# Skip for None — explicit SQL NULL or default=None.
497+
# Skip for None — accepted via Optional[T].
481498
if value is not None:
482499
meta._validate(value)
483500
kwargs[attr_name] = value

0 commit comments

Comments
 (0)