Skip to content

refactor: Simplify the UUID decoding logic - #255

Merged
Kriskras99 merged 1 commit into
apache:mainfrom
Kriskras99:fix/improve_uuid_decode
Aug 11, 2025
Merged

Kriskras99 merged 1 commit into
apache:mainfrom
Kriskras99:fix/improve_uuid_decode

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

In the original implementation we first read the length and then read the remainder either as Fixed if the length is 16 and String if it is not. It uses a temporary Vec to be able to backtrack if Fixed is wrong.

The backtrack can only happen if it fails to read a Fixed or if it gets a different type than Fixed from decode_internal. The last case can never happen, as decode_internal always returns the type corresponding to the schema. The first case can only happen if there are not enough bytes, because any byte pattern is valid for Fixed. If Fixed cannot read enough bytes, than String won't be able to read enough bytes either.

I did not add a fallback from Uuid::from_slice to Uuuid::parse_str as an UUID string is at least 32 characters, so it would fail anyway.

Comment thread avro/src/decode.rs
let Value::Bytes(bytes) =
decode_internal(&Schema::Bytes, names, enclosing_namespace, reader)?
else {
// Calling decode_internal with Schema::Bytes can only return a Value::Bytes or an error

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.

do we need the else at all ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, otherwise the compiler will throw an error as it can't prove that this is the only option

@martin-g martin-g 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.

nit) IMO the PR title (and commit message) should be refactor: ... since the old code was working (no tests changes => same functionality)

In the original implementation we first read the length and then
read the remainder either as `Fixed` if the length is 16 and `String`
if it is not. It uses a temporary `Vec` to be able to backtrack if
`Fixed` is wrong.

The backtrack can only happen if it fails to read a `Fixed` or if
it gets a different type than `Fixed` from `decode_internal`. The
last case can never happen, as `decode_internal` always returns the
type corresponding to the schema. The first case can only happen if
there are not enough bytes, because any byte pattern is valid for
`Fixed`. If `Fixed` cannot read enough bytes, than `String` won't
be able to read enough bytes either.

I did not add a fallback from `Uuid::from_slice` to `Uuuid::parse_str`
as an UUID string is at least 32 characters, so it would fail anyway.
@Kriskras99
Kriskras99 force-pushed the fix/improve_uuid_decode branch from 6230373 to 64a2d1b Compare August 11, 2025 15:52
@Kriskras99 Kriskras99 changed the title fix: Simplify the UUID decoding logic refactor: Simplify the UUID decoding logic Aug 11, 2025
@Kriskras99

Copy link
Copy Markdown
Contributor Author

Good point! I've renamed and merged it

@Kriskras99
Kriskras99 merged commit 23a9af7 into apache:main Aug 11, 2025
@Kriskras99
Kriskras99 deleted the fix/improve_uuid_decode branch August 11, 2025 15:53
kevinjqliu pushed a commit to apache/iceberg-rust that referenced this pull request Sep 24, 2025
## Which issue does this PR close?

`Fixed[16]` encoded UUIDs are now supported in Avro-rs:
apache/avro-rs#255

This will automatically support String and Fixed[16] logical types if
the `logicalType` annotation is set correctly 👍

## What changes are included in this PR?

<!--
Provide a summary of the modifications in this PR. List the main changes
such as new features, bug fixes, refactoring, or any other updates.
-->

## Are these changes tested?

<!--
Specify what test covers (unit test, integration test, etc.).

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
DerGut pushed a commit to DerGut/iceberg-rust that referenced this pull request Oct 6, 2025
## Which issue does this PR close?

`Fixed[16]` encoded UUIDs are now supported in Avro-rs:
apache/avro-rs#255

This will automatically support String and Fixed[16] logical types if
the `logicalType` annotation is set correctly 👍

## What changes are included in this PR?

<!--
Provide a summary of the modifications in this PR. List the main changes
such as new features, bug fixes, refactoring, or any other updates.
-->

## Are these changes tested?

<!--
Specify what test covers (unit test, integration test, etc.).

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
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