Repository navigation
fix(blob): preserve character matrix orientation (#1702) - #1743
Conversation
dimitri-yatsenko
left a comment
There was a problem hiding this comment.
Thank you for tracking this down and for the clear tests.
I verified the change locally:
- For
['abc'; 'def'],packnow writes the header, shape[2 3], classchar, thena d b e c fas 16-bit characters. That is column-major order, the order MATLAB keeps every array in, character arrays included, and the order the reader has always used. The writer also used it until 2019 (8f86851). Those bytes unpack to the original matrix. - 2-D and 3-D
S1arrays round-trip, including Fortran-ordered arrays, transposed views and strided views. - 1-D, one-row, one-column, single-element and 0-d character arrays produce the same bytes as before the change, and 1-D and one-row arrays still read back as a single string.
- Blobs written by the previous code decode exactly as before, since the reader is unchanged.
- Insert and fetch through a
<blob>attribute return the original matrix on MySQL and PostgreSQL, and the stored bytes are in column-major order. - All five new tests fail when the change in
blob.pyis reverted.
Two optional suggestions for the tests, here or in a follow-up. Approving as is.
In tests/unit/test_blob_character_arrays.py, line 27, the expected bytes are computed with the same expression as the code under test. Spelling out MATLAB's layout makes the test independent of the implementation:
# MATLAB stores ['abc'; 'def'] column by column as 16-bit characters: a d b e c f
expected_data = "adbecf".encode("utf-16-le")The issue notes that 1-D and one-row character arrays are not affected. A small test would keep it that way:
@pytest.mark.parametrize(
"characters",
[[b"a", b"b", b"c"], [[b"a", b"b", b"c"]]],
ids=["one-dimensional", "one-row"],
)
def test_char_vector_still_reads_as_string(characters):
vector = np.asarray(characters, dtype="S1")
encoded = pack(vector, compress=False)
assert encoded.endswith("abc".encode("utf-16-le"))
assert unpack(encoded, squeeze=True) == "abc"Values that earlier versions stored from Python keep their old bytes and read back as they do today. I'll mention that in the release notes, with how to rearrange such a value.
|
Thanks for the thorough review and approval! I appreciate the additional verification across MySQL and PostgreSQL. The suggestions for improving the test coverage are helpful, and I can address them in a follow-up. |
Summary
Fixes #1702.
Character matrices with multiple rows were serialized in row-major order but decoded in column-major order, causing their elements to be rearranged during blob round-trips.
Root Cause
The CHAR serialization path in
Blob.pack_array()usedtobytes()without specifying the memory order, whileBlob.read_array()reconstructs arrays usingreshape(shape, order="F").This mismatch caused multi-row character matrices to return with incorrect element positions.
Changes
tobytes(order="F")to match MATLAB's column-major representation.Validation
Scope
Changes are limited to:
src/datajoint/blob.pytests/unit/test_blob_character_arrays.pyPreviously stored blob data retains its existing decoding behavior.