Skip to content

Commit 90da864

Browse files
committed
fix: handle empty numpy arrays in scalar proto conversion
When a scalar feature column contains an empty numpy array (np.array([])), pd.isnull() returns an array instead of a scalar bool. Applying Python's `not` operator to that array raises: ValueError: The truth value of an empty array is ambiguous. This adds a safe null-check helper (is_scalar_null) that handles array-like values before falling through to pd.isnull(). Empty arrays are treated as null values, consistent with None and np.nan. The helper is used in _convert_scalar_values_to_proto to replace the bare `pd.isnull()` calls that crash on array inputs. Closes feast-dev#6255
1 parent de67bdd commit 90da864

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎sdk/python/feast/null_utils.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Utilities for safely checking null/missing values in scalar columns.
2+
3+
The standard pd.isnull() is vectorized: when given a numpy array (including
4+
an empty one), it returns an array of booleans instead of a scalar bool.
5+
Applying Python's `not` operator to such an array raises:
6+
7+
ValueError: The truth value of an empty array is ambiguous.
8+
9+
These helpers wrap pd.isnull() to handle array-like values safely.
10+
11+
See: https://github.com/feast-dev/feast/issues/6255
12+
"""
13+
14+
import numpy as np
15+
import pandas as pd
16+
from typing import Any
17+
18+
19+
def is_scalar_null(value: Any) -> bool:
20+
"""Check if a scalar value is null, safely handling array-like values.
21+
22+
Args:
23+
value: A scalar value that might be None, NaN, or an array-like
24+
object that ended up in a scalar feature column.
25+
26+
Returns:
27+
True if the value should be treated as null/missing.
28+
"""
29+
# Fast path for common cases
30+
if value is None:
31+
return True
32+
if isinstance(value, (str, bytes)):
33+
return False
34+
35+
# Handle numpy arrays (including empty ones)
36+
if isinstance(value, np.ndarray):
37+
if value.size == 0:
38+
return True
39+
result = pd.isnull(value)
40+
return bool(result.any()) if hasattr(result, "any") else bool(result)
41+
42+
# Handle other array-like objects (lists, tuples, etc.)
43+
if hasattr(value, "__len__") and not isinstance(value, (str, bytes)):
44+
try:
45+
result = pd.isnull(value)
46+
if hasattr(result, "any"):
47+
return bool(result.any())
48+
return bool(result)
49+
except (ValueError, TypeError):
50+
return False
51+
52+
# Plain scalar
53+
try:
54+
return bool(pd.isnull(value))
55+
except (ValueError, TypeError):
56+
return False
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Tests for feast.null_utils — safe null checking for scalar columns.
2+
3+
Reproduces the crash from https://github.com/feast-dev/feast/issues/6255
4+
and verifies the fix handles all edge cases.
5+
"""
6+
7+
import numpy as np
8+
import pytest
9+
10+
from feast.null_utils import is_scalar_null
11+
12+
13+
class TestIsScalarNull:
14+
"""Tests for is_scalar_null."""
15+
16+
def test_none_is_null(self):
17+
assert is_scalar_null(None) is True
18+
19+
def test_nan_is_null(self):
20+
assert is_scalar_null(float("nan")) is True
21+
22+
def test_np_nan_is_null(self):
23+
assert is_scalar_null(np.nan) is True
24+
25+
def test_empty_numpy_array_is_null(self):
26+
"""This is the exact crash scenario from issue #6255."""
27+
assert is_scalar_null(np.array([])) is True
28+
29+
def test_numpy_array_with_nan_is_null(self):
30+
assert is_scalar_null(np.array([np.nan])) is True
31+
32+
def test_numpy_array_with_values_is_not_null(self):
33+
assert is_scalar_null(np.array([1.0, 2.0])) is False
34+
35+
def test_int_is_not_null(self):
36+
assert is_scalar_null(42) is False
37+
38+
def test_zero_is_not_null(self):
39+
assert is_scalar_null(0) is False
40+
41+
def test_float_is_not_null(self):
42+
assert is_scalar_null(3.14) is False
43+
44+
def test_string_is_not_null(self):
45+
assert is_scalar_null("hello") is False
46+
47+
def test_empty_string_is_not_null(self):
48+
assert is_scalar_null("") is False
49+
50+
def test_bytes_is_not_null(self):
51+
assert is_scalar_null(b"data") is False
52+
53+
def test_bool_true_is_not_null(self):
54+
assert is_scalar_null(True) is False
55+
56+
def test_bool_false_is_not_null(self):
57+
assert is_scalar_null(False) is False
58+
59+
def test_np_bool_is_not_null(self):
60+
assert is_scalar_null(np.bool_(True)) is False
61+
62+
def test_empty_list_is_null(self):
63+
"""Empty list in a scalar column should be treated as null."""
64+
assert is_scalar_null([]) is True
65+
66+
def test_list_with_values_is_not_null(self):
67+
assert is_scalar_null([1, 2, 3]) is False

0 commit comments

Comments
 (0)