Skip to content

Commit 2ef0bb9

Browse files
nguyenvkounelisagis
andauthored
Passing Empty List to multirange_index and df should return Empty Results (#1412)
* `.df` and `.multi_index` Empty Sequence Returns Empty Results * Use TypeVar for EmptyRange --------- Co-authored-by: Agisilaos Kounelis <kounelisagis@gmail.com>
1 parent 6ff4e62 commit 2ef0bb9

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

tiledb/multirange_indexing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Optional,
1818
Sequence,
1919
Tuple,
20+
TypeVar,
2021
Union,
2122
cast,
2223
)
@@ -44,8 +45,17 @@
4445
current_timer: ContextVar[str] = ContextVar("timer_scope")
4546

4647

47-
# sentinel value to denote selecting an empty range
48-
EmptyRange = object()
48+
# sentinel type to denote selecting an empty range
49+
EmptyRange = TypeVar("EmptyRange")
50+
51+
52+
def is_empty_range(idx: Union[EmptyRange, List, Tuple]) -> bool:
53+
if idx is not EmptyRange:
54+
if hasattr(idx, "__len__") and len(idx) == 0 and idx != "":
55+
return True
56+
return False
57+
return True
58+
4959

5060
# TODO: expand with more accepted scalar types
5161
Scalar = Real
@@ -248,7 +258,7 @@ def return_incomplete(self) -> bool:
248258

249259
def __getitem__(self, idx):
250260
with timing("getitem_time"):
251-
if idx is EmptyRange:
261+
if is_empty_range(idx):
252262
self.pyquery = None
253263
self.subarray = None
254264
else:

tiledb/tests/test_multi_index-hp.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ def _direct_query_ranges(array: SparseArray, ranges, order):
3939
q.set_subarray(subarray)
4040

4141
q.submit()
42-
return {k: v[0].view(array.attr(0).dtype) for k, v in q.results().items()}
42+
43+
if ranges == [[]]:
44+
# empty range should give empty result
45+
return {k: [] for k in q.results()}
46+
else:
47+
return {k: v[0].view(array.attr(0).dtype) for k, v in q.results().items()}
4348

4449

4550
# Compound strategies to build valid inputs for multi_index
@@ -90,7 +95,6 @@ def create_array(uri):
9095
def test_multi_index_two_way_query(self, order, ranges, sparse_array_1d):
9196
"""This test checks the result of "direct" range queries using PyQuery
9297
against the result of `multi_index` on the same ranges."""
93-
9498
uri = sparse_array_1d
9599

96100
assert isinstance(uri, str)

tiledb/tests/test_multi_index.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,30 @@ def test_fixed_width_char(self):
903903
with tiledb.open(uri, mode="r") as A:
904904
assert all(A.query(use_arrow=True).df[:][""] == data)
905905

906+
@pytest.mark.skipif(not has_pandas(), reason="pandas>=1.0,<3.0 not installed")
907+
def test_empty_idx(self):
908+
uri = self.path("test_empty_idx")
909+
910+
schema = tiledb.ArraySchema(
911+
domain=tiledb.Domain(tiledb.Dim(name="dim", domain=(0, 9), dtype=np.uint8)),
912+
sparse=True,
913+
attrs=[tiledb.Attr(name="a", dtype=np.float64)],
914+
)
915+
tiledb.Array.create(uri, schema)
916+
917+
data = np.array(np.random.randint(10, size=10), dtype=np.float64)
918+
919+
with tiledb.open(uri, mode="w") as A:
920+
A[np.arange(10)] = data
921+
922+
with tiledb.open(uri, mode="r") as A:
923+
assert_array_equal(A.df[tiledb.EmptyRange]["a"], [])
924+
assert_array_equal(A.multi_index[tiledb.EmptyRange]["a"], [])
925+
assert_array_equal(A.df[[]]["a"], [])
926+
assert_array_equal(A.multi_index[[]]["a"], [])
927+
assert_array_equal(A.df[()]["a"], [])
928+
assert_array_equal(A.multi_index[()]["a"], [])
929+
906930

907931
# parametrize dtype and sparse
908932
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)