Skip to content

Commit

Permalink
Handle empty string passed to Query condition (#1774)
Browse files Browse the repository at this point in the history
This treats an empty string `""` the same as None when applying a query
condition. This is useful as users are passing strings for query
condition and avoids the user needing to validate they've successfully
built a condition before passing it in.
  • Loading branch information
Shelnutt2 committed May 27, 2023
1 parent ae8ff8a commit 36a1843
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tiledb/libtiledb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ cdef class DenseArrayImpl(Array):
q = PyQuery(self._ctx_(), self, tuple(attr_names), tuple(), <int32_t>layout, False)
self.pyquery = q

if cond is not None:
if cond is not None and cond != "":
from .query_condition import QueryCondition

if isinstance(cond, str):
Expand Down Expand Up @@ -2921,7 +2921,7 @@ cdef class SparseArrayImpl(Array):
q = PyQuery(self._ctx_(), self, tuple(attr_names), tuple(), <int32_t>layout, False)
self.pyquery = q

if cond is not None:
if cond is not None and cond != "":
from .query_condition import QueryCondition

if isinstance(cond, str):
Expand Down
30 changes: 30 additions & 0 deletions tiledb/tests/test_query_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,33 @@ def test_delete_with_string_dimension(self):
with tiledb.open(path, "r") as A:
assert_array_equal(A[:]["d"], [b"c"])
assert_array_equal(A[:]["a"], [30])

def test_qc_dense_empty(self):
path = self.path("test_qc_dense_empty")

dom = tiledb.Domain(tiledb.Dim(name="d", domain=(1, 1), tile=1, dtype=np.uint8))
attrs = [tiledb.Attr(name="a", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=False)
tiledb.Array.create(path, schema)

with tiledb.open(path, mode="w") as A:
A[:] = np.arange(1)

with tiledb.open(path) as A:
assert_array_equal(A.query(cond="")[:]["a"], [0])

def test_qc_sparse_empty(self):
path = self.path("test_qc_sparse_empty")

dom = tiledb.Domain(
tiledb.Dim(name="d", domain=(1, 10), tile=1, dtype=np.uint8)
)
attrs = [tiledb.Attr(name="a", dtype=np.uint8)]
schema = tiledb.ArraySchema(domain=dom, attrs=attrs, sparse=True)
tiledb.Array.create(path, schema)

with tiledb.open(path, mode="w") as A:
A[1] = {"a": np.arange(1)}

with tiledb.open(path) as A:
assert_array_equal(A.query(cond="")[:]["a"], [0])

0 comments on commit 36a1843

Please sign in to comment.