Skip to content

Fix big-endian handling for ttext type and refactor code to remove redundancy#718

Merged
mschoema merged 4 commits intoMobilityDB:masterfrom
mschoema:text_wkb
Sep 22, 2025
Merged

Fix big-endian handling for ttext type and refactor code to remove redundancy#718
mschoema merged 4 commits intoMobilityDB:masterfrom
mschoema:text_wkb

Conversation

@mschoema
Copy link
Copy Markdown
Member

Each individual commit in this PR contains a specific code change and has a detailed comment. I will try to merge this PR using 'rebase and merge', which avoids an undesirable squash when the commits in a PR have each their own scope and comment.

I'm not sure if this is better than squashing all commits of a PR into a single commit with a single message, but let's try it out and see.

There was a lot of duplicated code in the _from_wkb_state functions.
Remove this redundancy with new inline functions read_wkb_value and
swap_bytes.

Also, remove unused point_from_wkb_state function.
Following the previous PR to remove redundancy from _from_wkb_state
functions, do the same with _to_wkb_buf, using typed_value_to_wkb_buf.

Also, make static the endian_to_wkb_buf and bytes_to_wkb_buf functions,
as they are only used in this file, and remove the unneeded casts, to be
consistent with the corresponding functions in PostGIS.
text_to_wkb_buf was using bytes_to_wkb_buf, which swaps the byte order
according to the endianness. However, this is only needed for multi-byte
int or float types (and date/time types, which are large ints). Text
values are simply arrays of chars, so there should be no byte-swapping
going on. The initial int64 indicating the size of the string should
be swapped if needed, but this is correctly handled by int64_to_wkb_buf.

Also, we can avoid a call to text2cstring, which does an unnecessary
memory allocation. Use the VARSIZE_ANY_EXHDR and VARDATA macros to copy
from the text pointer directly.

Lastly, the old function was allocating space for the null terminator as
well, which is wrong, as we don't write the null terminator in the wkb
output. Fix this, and also correctly add the null terminator in
text_from_wkb_state. Palloc doesn't zero-out the memory, so this is
necessary or cstring2text could fail.
The error fixed in the previous commit was not detected as the tests for
the tbool, tint, tfloat, and ttext types did not test both the little-
and big-endian combinations of asBinary or asHexWKB. This commit fixes
this.
@mschoema mschoema merged commit feca706 into MobilityDB:master Sep 22, 2025
16 checks passed
@mschoema mschoema deleted the text_wkb branch September 22, 2025 20:24
@mschoema
Copy link
Copy Markdown
Member Author

Well the commits look nice after the rebase and merge, but github doesn't automatically add a link to the PR in their message. I don't think that's ideal in our case. I will stick with squash and merge in the future but will try to write a clean commit message with each PR.

@estebanzimanyi
Copy link
Copy Markdown
Member

Thanks for this analysis ! From now on, I will expand on the commit messages for the PRs.

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.

2 participants