Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-116 UUID conversion error with upsert in H2 #1823

Merged
merged 1 commit into from
Aug 4, 2023

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Aug 3, 2023

Using a UUID column as a key id constraint in H2's MERGE INTO .. USING statement fails with:

org.h2.jdbc.JdbcSQLDataException:
Data conversion error converting "U&'\\fffdK\\fffdS\\001bJr\\fffdG%\\fffdE\\00100\\fffd' (TESTER: ""ID"" UUID NOT NULL)";

This error occurs because the statement uses a derived column list to merge ON (T.ID=S.ID), so the UUID as a ByteArray stored in T.ID needs to be converted internally to compare with the String in S.ID (from generated SQL). This error goes away if a regular integer id column is used or if the key constraint is swapped with another non-UUID type.

The error also goes away if an identical MERGE statement is placed in exec() directly, indicating that the issue lies with how Exposed sends UUID values to the H2 database.

Switching the sent value from a ByteArray to a String allows comparison without conversion. A String is still one of 3 acceptable ways to store a UUID in H2 and sending a String still results in a UUID being retrieved back from the database (via valueFromDB() and readObject()).

Additional:

  • Because H2_Oracle is delegating to Oracle, it uses Oracle's RAW(16) type instead of H2's UUID type and leads to a second error:
org.h2.jdbc.JdbcSQLSyntaxErrorException:
Values of types "BINARY VARYING(16)" and "CHARACTER VARYING" are not comparable;

To resolve this, H2_Oracle could also be made to use the Oracle-specific UPSERT statement, but this syntax fails when sent to H2. Instead, the compatibility mode now uses the H2DatatypeProvider overrides.

Using a UUID column as a key constraint in H2's MERGE INTO -- USING statement
was failing with: org.h2.jdbc.JdbcSQLDataException: Data conversion error converting
"U&'\\fffdK\\fffdS\\001bJr\\fffdG%\\fffdE\\00100\\fffd' (TESTER: ""ID"" UUID NOT NULL)";

This error occurs because the statement uses a derived column list to merge ON
(T.ID=S.ID), so the UUID as a ByteArray in T.ID needs to be converted internally
to compare with the String in S.ID. This error goes away if a regular integer
id column is used or if the key constraint is swapped with another.

The error also goes away if the identical MERGE statement is placed in an exec()
directly, indicating that the issue lies with how Exposed sends UUID values to
the H2 database.

Switching the sent value from a ByteArray to a String allows comparison without
conversion. A String is still one of 3 acceptable ways to store a UUID in H2 and
sending a String still results in a UUID being retrieved back from the database.
@bog-walk bog-walk requested review from e5l and joc-a August 3, 2023 22:46
@bog-walk bog-walk merged commit 4b88ab7 into main Aug 4, 2023
3 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-h2-uuid branch August 4, 2023 14:23
saral pushed a commit to saral/Exposed that referenced this pull request Oct 3, 2023
)

Using a UUID column as a key constraint in H2's MERGE INTO -- USING statement
was failing with: org.h2.jdbc.JdbcSQLDataException: Data conversion error converting
"U&'\\fffdK\\fffdS\\001bJr\\fffdG%\\fffdE\\00100\\fffd' (TESTER: ""ID"" UUID NOT NULL)";

This error occurs because the statement uses a derived column list to merge ON
(T.ID=S.ID), so the UUID as a ByteArray in T.ID needs to be converted internally
to compare with the String in S.ID. This error goes away if a regular integer
id column is used or if the key constraint is swapped with another.

The error also goes away if the identical MERGE statement is placed in an exec()
directly, indicating that the issue lies with how Exposed sends UUID values to
the H2 database.

Switching the sent value from a ByteArray to a String allows comparison without
conversion. A String is still one of 3 acceptable ways to store a UUID in H2 and
sending a String still results in a UUID being retrieved back from the database.
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