Skip to content

fix(blob): preserve character matrix orientation (#1702) - #1743

Merged
dimitri-yatsenko merged 3 commits into
datajoint:masterfrom
godara-alt:fix/1702-char-array-order
Nov 24, 2026
Merged

dimitri-yatsenko merged 3 commits into
datajoint:masterfrom
godara-alt:fix/1702-char-array-order

Conversation

@godara-alt

@godara-alt godara-alt commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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() used tobytes() without specifying the memory order, while Blob.read_array() reconstructs arrays using reshape(shape, order="F").

This mismatch caused multi-row character matrices to return with incorrect element positions.

Changes

  • Serialize CHAR array data using tobytes(order="F") to match MATLAB's column-major representation.
  • Preserve the existing blob decoding behavior.
  • Add regression tests for 2×3, 3×2, and 2×2 character matrices.
  • Verify the serialized character byte order.
  • Add compressed character-matrix round-trip coverage.

Validation

  • Fork GitHub Actions: Test and Lint passed on commit f8cf4c6.
  • Upstream PR GitHub Actions: Lint passed; Test workflow is still running.
  • Added regression tests for character matrix orientation, MATLAB-compatible byte ordering, and compressed round-trips.
  • Existing blob decoder remains unchanged.

Scope

Changes are limited to:

  • src/datajoint/blob.py
  • tests/unit/test_blob_character_arrays.py

Previously stored blob data retains its existing decoding behavior.

@dimitri-yatsenko dimitri-yatsenko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for tracking this down and for the clear tests.

I verified the change locally:

  • For ['abc'; 'def'], pack now writes the header, shape [2 3], class char, then a d b e c f as 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 S1 arrays 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.py is 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.

@godara-alt

Copy link
Copy Markdown
Contributor Author

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.

@dimitri-yatsenko
dimitri-yatsenko merged commit b2994e5 into datajoint:master Nov 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Character matrices with more than one row come back transposed from <blob>

2 participants