Skip to content

Commit b0ce164

Browse files
authored
[Python] Keep NumpyTablet null bitmaps aligned when sorting timestamps (#19403)
* Keep NumpyTablet null bitmaps aligned when sorting timestamps * Vectorize NumpyTablet bitmap sorting and release value columns incrementally
1 parent 3d643e7 commit b0ce164

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎iotdb-client/client-py/iotdb/utils/NumpyTablet.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def __init__(
7474
timestamps = timestamps[index]
7575
for i in range(len(values)):
7676
values[i] = values[i][index]
77+
if bitmaps is not None:
78+
bitmaps = [self._sort_bitmap(bitmap, index) for bitmap in bitmaps]
7779

7880
if timestamps.dtype != TSDataType.INT64.np_dtype():
7981
timestamps = timestamps.astype(TSDataType.INT64.np_dtype())
@@ -96,6 +98,21 @@ def __init__(
9698
else:
9799
self.__column_types = column_types
98100

101+
@staticmethod
102+
def _sort_bitmap(bitmap, index):
103+
if bitmap is None:
104+
return None
105+
sorted_bitmap = BitMap(bitmap.get_size())
106+
if not bitmap.is_all_unmarked():
107+
packed = np.asarray(bitmap.bits, dtype=np.uint8)
108+
# BitMap stores the first row in the low bit of each byte.
109+
rows = np.unpackbits(packed).reshape(-1, 8)[:, ::-1]
110+
sorted_rows = np.zeros(rows.size, dtype=np.uint8)
111+
sorted_rows[: len(index)] = rows.flat[index]
112+
sorted_bytes = np.packbits(sorted_rows.reshape(-1, 8)[:, ::-1])
113+
sorted_bitmap.bits = sorted_bytes.tolist()
114+
return sorted_bitmap
115+
99116
@staticmethod
100117
def check_sorted(timestamps):
101118
for i in range(1, len(timestamps)):

‎iotdb-client/client-py/tests/unit/test_numpy_tablet.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#
1818

1919
import numpy as np
20+
import pytest
2021

2122
from iotdb.utils.BitMap import BitMap
2223
from iotdb.utils.IoTDBConstants import TSDataType
@@ -153,6 +154,32 @@ def test_sort_numpy_tablet():
153154
assert tablet_.get_binary_values() == np_tablet_.get_binary_values()
154155

155156

157+
@pytest.mark.parametrize("row_count", [8, 9])
158+
def test_sort_numpy_tablet_with_null_bitmaps(row_count):
159+
timestamps = [row_count, *range(1, row_count)]
160+
columns = ["a", "b", "c"]
161+
types = [TSDataType.INT32] * len(columns)
162+
first_nulls = BitMap(len(timestamps))
163+
first_nulls.mark(0) # The null moves to bit 7 or 8.
164+
second_nulls = BitMap(len(timestamps))
165+
second_nulls.mark(1)
166+
rows = [
167+
(None if t == row_count else t, None if t == 1 else t, t) for t in timestamps
168+
]
169+
expected = Tablet("root.sg.d", columns, types, rows, timestamps)
170+
tablet = NumpyTablet(
171+
"root.sg.d",
172+
columns,
173+
types,
174+
[np.array([0 if v is None else v for v in column]) for column in zip(*rows)],
175+
np.array(timestamps),
176+
[first_nulls, second_nulls, None],
177+
)
178+
179+
assert tablet.get_binary_timestamps() == expected.get_binary_timestamps()
180+
assert tablet.get_binary_values() == expected.get_binary_values()
181+
182+
156183
def test_numpy_tablet_auto_correct_datatype():
157184

158185
measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]

0 commit comments

Comments
 (0)