Skip to content

Commit 3e13a83

Browse files
authored
Fix "unexpected object type in string conversion" error when using booleans (#1767)
1 parent 3070f4b commit 3e13a83

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tiledb/npbuffer.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ if (PyUnicode_Check(u.ptr())) {
279279
}
280280

281281
sz = a.nbytes();
282+
} else if (PyBool_Check(o)) {
283+
if (idx < 1)
284+
first_dtype = py::dtype("bool");
285+
286+
auto a = py::cast<py::bool_>(o);
287+
sz = sizeof(bool);
288+
bool bool_value = a;
289+
input_p = reinterpret_cast<const char *>(&bool_value);
282290
} else {
283291
// TODO write the type in the error here
284292
// auto o_h = py::reinterpret_borrow<py::object>(o);
@@ -312,6 +320,11 @@ if (PyUnicode_Check(u.ptr())) {
312320
auto arr = py::cast<py::array>(pyobj_p);
313321
sz = arr.nbytes();
314322
input_p = (const char *)arr.data();
323+
} else if (PyBool_Check(pyobj_p)) {
324+
py::bool_ bool_obj = py::cast<py::bool_>(pyobj_p);
325+
sz = sizeof(bool);
326+
bool bool_value = bool_obj;
327+
input_p = reinterpret_cast<const char *>(&bool_value);
315328
} else {
316329
// TODO add object type
317330
TPY_ERROR_LOC("Unexpected object type in buffer conversion");
@@ -391,6 +404,14 @@ if (PyUnicode_Check(u.ptr())) {
391404
} else if (npy_api.PyArray_Check_(obj_p)) {
392405
// handle (potentially) var-len embedded arrays
393406
sz = py::cast<py::array>(obj_p).nbytes();
407+
} else if (PyBool_Check(obj_p)) {
408+
if (idx < 1)
409+
first_dtype = py::dtype("bool");
410+
411+
py::bool_ bool_obj = py::cast<py::bool_>(obj_p);
412+
sz = sizeof(bool);
413+
bool bool_value = bool_obj;
414+
input_p = reinterpret_cast<const char *>(&bool_value);
394415
} else {
395416
auto errmsg =
396417
std::string("Unexpected object type in string conversion");
@@ -424,6 +445,11 @@ if (PyUnicode_Check(u.ptr())) {
424445
auto o_a = py::cast<py::array>(obj_h);
425446
sz = o_a.nbytes();
426447
input_p = (const char *)o_a.data();
448+
} else if (PyBool_Check(obj_p)) {
449+
py::bool_ bool_obj = py::cast<py::bool_>(obj_p);
450+
sz = sizeof(bool);
451+
bool bool_value = bool_obj;
452+
input_p = reinterpret_cast<const char *>(&bool_value);
427453
} else {
428454
TPY_ERROR_LOC("Unexpected object type in buffer conversion");
429455
}

tiledb/tests/test_query_condition.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,30 @@ def test_qc_enumeration(self):
845845
== list(enum2.values()).index("bb")
846846
)
847847

848+
def test_boolean_insert(self):
849+
path = self.path("test_boolean_insert")
850+
attr = tiledb.Attr("a", dtype=np.bool_, var=True)
851+
dom = tiledb.Domain(tiledb.Dim(domain=(1, 10), tile=1, dtype=np.uint32))
852+
schema = tiledb.ArraySchema(domain=dom, sparse=True, attrs=[attr])
853+
tiledb.Array.create(path, schema)
854+
a = np.array(
855+
list(
856+
[
857+
np.array([True], dtype=np.bool_),
858+
np.array([True], dtype=np.bool_),
859+
np.array([True], dtype=np.bool_),
860+
np.array([True], dtype=np.bool_),
861+
]
862+
),
863+
dtype=object,
864+
)
865+
with tiledb.open(path, "w") as A:
866+
A[range(1, len(a) + 1)] = {"a": a}
867+
868+
with tiledb.open(path, "r") as A:
869+
for k in A[:]["a"]:
870+
assert k[0] == True # noqa: E712
871+
848872

849873
class QueryDeleteTest(DiskTestCase):
850874
def test_basic_sparse(self):

0 commit comments

Comments
 (0)