Skip to content

Commit 9638128

Browse files
Fix view_attr not being respected by __getitem__ and subarray (#2139)
Co-authored-by: nguyenv <vivian@tiledb.com>
1 parent 92fb0e2 commit 9638128

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

tiledb/dense_array.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ def __getitem__(self, selection):
7777
7878
"""
7979
if self.view_attr:
80-
result = self.subarray(selection, attrs=(self.view_attr,))
81-
return result[self.view_attr]
80+
return self.subarray(selection)
8281

8382
result = self.subarray(selection)
8483
for i in range(self.schema.nattr):
@@ -291,6 +290,8 @@ def subarray(self, selection, attrs=None, cond=None, coords=False, order=None):
291290
attr = self.schema.attr(0)
292291
if attr.isanon:
293292
return out[attr._internal_name]
293+
if self.view_attr is not None:
294+
return out[self.view_attr]
294295
return out
295296

296297
def _read_dense_subarray(

tiledb/sparse_array.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from collections import OrderedDict
23

34
import numpy as np
@@ -292,6 +293,9 @@ def __getitem__(self, selection):
292293
>>> # A[5.0:579.9]
293294
294295
"""
296+
if self.view_attr is not None:
297+
return self.subarray(selection)
298+
295299
result = self.subarray(selection)
296300
for i in range(self.schema.nattr):
297301
attr = self.schema.attr(i)
@@ -518,7 +522,11 @@ def subarray(self, selection, coords=True, attrs=None, cond=None, order=None):
518522

519523
attr_names = list()
520524

521-
if attrs is None:
525+
if self.view_attr is not None:
526+
if attrs is not None:
527+
warnings.warn("view_attr is set, ignoring attrs parameter", UserWarning)
528+
attr_names.extend(self.view_attr)
529+
elif attrs is None:
522530
attr_names.extend(
523531
self.schema.attr(i)._internal_name for i in range(self.schema.nattr)
524532
)

tiledb/tests/test_libtiledb.py

+45-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .common import (
2424
DiskTestCase,
2525
assert_captured,
26+
assert_dict_arrays_equal,
2627
assert_subarrays_equal,
2728
assert_unordered_equal,
2829
fx_sparse_cell_order, # noqa: F401
@@ -923,8 +924,8 @@ def assert_ts(timestamp, result):
923924
assert_ts((timestamps[2], None), A * 3)
924925
assert_ts((timestamps[2], None), A * 3)
925926

926-
def test_open_attr(self):
927-
uri = self.path("test_open_attr")
927+
def test_open_attr_dense(self):
928+
uri = self.path("test_open_attr_dense")
928929
schema = tiledb.ArraySchema(
929930
domain=tiledb.Domain(
930931
tiledb.Dim(name="dim0", dtype=np.uint32, domain=(1, 4))
@@ -949,6 +950,48 @@ def test_open_attr(self):
949950
assert_array_equal(A[:], np.array((1, 2, 3, 4)))
950951
assert list(A.multi_index[:].keys()) == ["x"]
951952

953+
with tiledb.open(uri, attr="x") as A:
954+
q = A.query(cond="x <= 3")
955+
expected = np.array([1, 2, 3, schema.attr("x").fill[0]])
956+
assert_array_equal(q[:], expected)
957+
958+
def test_open_attr_sparse(self):
959+
uri = self.path("test_open_attr_sparse")
960+
schema = tiledb.ArraySchema(
961+
domain=tiledb.Domain(
962+
tiledb.Dim(name="dim0", dtype=np.uint32, domain=(1, 4))
963+
),
964+
attrs=(
965+
tiledb.Attr(name="x", dtype=np.int32),
966+
tiledb.Attr(name="y", dtype=np.int32),
967+
),
968+
sparse=True,
969+
)
970+
tiledb.Array.create(uri, schema)
971+
972+
with tiledb.open(uri, mode="w") as A:
973+
A[[1, 2, 3, 4]] = {"x": np.array((1, 2, 3, 4)), "y": np.array((5, 6, 7, 8))}
974+
975+
with self.assertRaises(KeyError):
976+
tiledb.open(uri, attr="z")
977+
978+
with self.assertRaises(KeyError):
979+
tiledb.open(uri, attr="dim0")
980+
981+
with tiledb.open(uri, attr="x") as A:
982+
expected = OrderedDict(
983+
[("dim0", np.array([1, 2, 3, 4])), ("x", np.array([1, 2, 3, 4]))]
984+
)
985+
assert_dict_arrays_equal(A[:], expected)
986+
assert list(A.multi_index[:].keys()) == ["dim0", "x"]
987+
988+
with tiledb.open(uri, attr="x") as A:
989+
q = A.query(cond="x <= 3")
990+
expected = OrderedDict(
991+
[("dim0", np.array([1, 2, 3])), ("x", np.array([1, 2, 3]))]
992+
)
993+
assert_dict_arrays_equal(q[:], expected)
994+
952995
def test_ncell_attributes(self):
953996
dom = tiledb.Domain(tiledb.Dim(domain=(0, 9), tile=10, dtype=int))
954997
attr = tiledb.Attr(dtype=[("", np.int32), ("", np.int32), ("", np.int32)])

0 commit comments

Comments
 (0)