You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "clesaec (via GitHub)" <gi...@apache.org> on 2023/12/26 13:34:15 UTC

[PR] AVRO-3918: add uuid with bytes and fixed [avro]

clesaec opened a new pull request, #2652:
URL: https://github.com/apache/avro/pull/2652

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   Proposal for [AVRO-3918](https://issues.apache.org/jira/browse/AVRO-3918) to allow store uuid logical type in fixed or byte type.
   
   
   ## Verifying this change
   
   Unit test added
   
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (yes)
   - If yes, how is the feature documented? (no)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1442745011


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   Given that UUIDs are 128 bit labels (source with extra references: https://en.wikipedia.org/wiki/Universally_unique_identifier#Binary_wire_format), I think using a fixed byte order of BigEndian is the best choice.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1469329181


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   I'm not fully comprehending the section 4.1.2. It is meant to clarify the byte order, but it doesn't help me a a lot:
   
   > The fields are presented with the most significant one first.
   
   That translates to big-endianness to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464975969


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   Also, a [`ByteBuffer`](https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html) (as per its Javadoc) is initially always big-endian.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1917272296

   > @Fokko As a Avro maintainer you can push to people's PRs!
   
   In this case, the syntax is: `git push git@github.com:clesaec/avro.git avro-3918_Uuid:avro-3918_Uuid`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1869561268

   Please add to the specification, with an example of how the same UUID would be serialised as `string` and as `fixed`, so that there is no doubt about the byte order.  Perhaps also notes about:
   
   - whether a UUID field with a *sort order* specified will be sorted differently if the underlying type is changed from string to fixed or vice versa
   - upper vs. lower case distinction not being preserved if UUID is not encoded as string
   - changing the underlying type not being supported in schema evolution
   
   Please add a Java test for whether the byte order is correct, preferably using the same UUID as in the spec example.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1869599360

   > TestUuidConversions is not enough?
   
   It's not enough — it tests whether the encoding and the decoding are consistent with each other in the Java implementation, but if both have the wrong byte order against the spec, then the test might not notice.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464763230


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   Do we want to re-use the buffer? This avoids reallocating the buffer each time.
   
   https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/avro/ValueReaders.java#L396-L402



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464834266


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   ```suggestion
         ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);
         buffer.order(ByteOrder.BIG_ENDIAN);
   ```
   I still think we cannot leave the endianness undefined. 
   
   ![image](https://github.com/apache/avro/assets/1134248/20a2b165-c137-4060-ad57-0e3f03bbc293)
   
   According to Wikipedia, the big-endian format is the most common one (and the other one looks awkward :)
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1442745011


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   Given that UUIDs are 128 bit labels (source with extra references: https://en.wikipedia.org/wiki/Universally_unique_identifier#Binary_wire_format), I think this is the best choice.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450508420


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));

Review Comment:
   This certainly works but given the time it takes to read data over a network / from disk), I'd prefer a more readable version:
   
   ```
   ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
   long mostSigBits = bugger.getLong();
   long leastSigBits = bugger.getLong();
   return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
   ```
   
   The additional benefit is that we can easily adjust the byte order.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464677468


##########
lang/java/avro/src/test/java/org/apache/avro/TestUuidConversions.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericFixed;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.UUID;
+import java.util.stream.Stream;
+
+public class TestUuidConversions {
+
+  private Conversions.UUIDConversion uuidConversion = new Conversions.UUIDConversion();
+
+  private Schema fixed = Schema.createFixed("fixed", "doc", "", Long.BYTES * 2);
+  private Schema fixedUUid = LogicalTypes.uuid().addToSchema(fixed);
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixed(UUID uuid) {
+    GenericFixed value = uuidConversion.toFixed(uuid, fixedUUid, LogicalTypes.uuid());
+    UUID uuid1 = uuidConversion.fromFixed(value, fixedUUid, LogicalTypes.uuid());
+    Assertions.assertEquals(uuid, uuid1);
+  }
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixedReverse(UUID uuid) {
+    Conversions.UUIDConversion bigEndianConversion = new Conversions.UUIDConversion(true);
+    Conversions.UUIDConversion littleEndianConversion = new Conversions.UUIDConversion(false);
+
+    // simulate uuid from other endian source, assume it's big endian (nevermind if
+    // current jvm is big endian, as we simulate).
+    UUID reverserUUID = new UUID(Long.reverseBytes(uuid.getMostSignificantBits()),
+        Long.reverseBytes(uuid.getLeastSignificantBits()));

Review Comment:
   Yes, this point is moot: [RFC-4122 (section 4.1.2)](https://www.ietf.org/rfc/rfc4122.html#section-4.1.2) defines the byte layout.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1915671338

   @clesaec Thanks for the quick reply. If you don't have time, let me know. I'm happy to cherry-pick your work and create a new PR. Otherwise this week is fine as well 👍 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1474096232


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   Correct. This is explained a bit later in the section (the next page in the print version), where it says:
   > In the absence of explicit application or presentation protocol specification to the contrary, [...] the fields are encoded as 16 octets, with the sizes and order of the fields defined above, and with each field encoded with the Most Significant Byte first (known as network byte order).
   
   In other words: unless we explicitly want to document otherwise, everything is in network order (also known as big-endian).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1437795342


##########
doc/content/en/docs/++version++/Specification/_index.md:
##########
@@ -827,7 +827,31 @@ Here, as scale property is stored in value itself it needs more bytes than prece
 ### UUID
 The `uuid` logical type represents a random generated universally unique identifier (UUID).
 
-A `uuid` logical type annotates an Avro `string`. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+A `uuid` logical type annotates an Avro `string`, `fixed` of length 16 or `bytes`. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+
+The following schemas represent a uuid:
+```json
+{
+  "type": "string",
+  "logicalType": "uuid"
+}
+```
+
+```json
+{
+  "type": "fixed",
+  "size": "16",
+  "logicalType": "uuid"
+}
+```
+
+```json
+{
+  "type": "bytes",
+  "logicalType": "uuid"
+}
+```

Review Comment:
   I don't think this adds value since a UUID is always 128 bits. Storing it as bytes will be suboptimal since you first need to encode the length of the bytes which is always the same.



##########
lang/java/avro/src/main/java/org/apache/avro/LogicalTypes.java:
##########
@@ -296,8 +296,12 @@ private Uuid() {
     @Override
     public void validate(Schema schema) {
       super.validate(schema);
-      if (schema.getType() != Schema.Type.STRING) {
-        throw new IllegalArgumentException("Uuid can only be used with an underlying string type");
+      if (schema.getType() != Schema.Type.STRING && schema.getType() != Schema.Type.FIXED
+          && schema.getType() != Schema.Type.BYTES) {
+        throw new IllegalArgumentException("Uuid can only be used with an underlying string, fixed or byte type");
+      }
+      if (schema.getType() == Schema.Type.FIXED && schema.getFixedSize() != 2 * Long.BYTES) {

Review Comment:
   Should we create a new constant with `UUID_BYTES`?



##########
doc/content/en/docs/++version++/Specification/_index.md:
##########
@@ -827,7 +827,31 @@ Here, as scale property is stored in value itself it needs more bytes than prece
 ### UUID
 The `uuid` logical type represents a random generated universally unique identifier (UUID).
 
-A `uuid` logical type annotates an Avro `string`. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+A `uuid` logical type annotates an Avro `string`, `fixed` of length 16 or `bytes`. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+
+The following schemas represent a uuid:
+```json
+{
+  "type": "string",
+  "logicalType": "uuid"
+}
+```
+
+```json
+{
+  "type": "fixed",
+  "size": "16",
+  "logicalType": "uuid"
+}
+```
+
+```json
+{
+  "type": "bytes",
+  "logicalType": "uuid"
+}
+```
+(UUID will be sorted differently if the underlying type is a string, a fixed or a bytes)

Review Comment:
   I would not expect this to be true, but I have to dig into the details.



##########
lang/java/avro/src/test/java/org/apache/avro/TestLogicalType.java:
##########
@@ -208,7 +208,7 @@ void uuidExtendsString() {
     assertEquals(LogicalTypes.uuid(), uuidSchema.getLogicalType());
 
     assertThrows("UUID requires a string", IllegalArgumentException.class,
-        "Uuid can only be used with an underlying string type",
+        "Uuid can only be used with an underlying string, fixed or byte type",

Review Comment:
   The current proposal does not include bytes.
   ```suggestion
           "Uuid can only be used with an underlying string or fixed type",
   ```



##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +80,68 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
+    }
+
+    private long convert(long value) {
+      if (this.isBigEndian) {
+        return value;
+      } else {
+        return Long.reverseBytes(value);
+      }
+    }
+
+    @Override
+    public UUID fromBytes(final ByteBuffer value, final Schema schema, final LogicalType type) {
+      BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(value.array(), null);
+      try {
+        final long mostSigBits = decoder.readLong();

Review Comment:
   In Iceberg we [first read it to a buffer](https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/avro/ValueReaders.java#L255), but I think your version here is more efficient. Let me do some testing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1440180391


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   I'm not sure about this. I would say that we always write BigEndian, similar to Iceberg: https://github.com/apache/iceberg/blob/main/api/src/main/java/org/apache/iceberg/util/UUIDUtil.java#L76-L79



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450511234


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
+    }
+
+    private long convert(long value) {
+      if (this.isBigEndian) {
+        return value;
+      } else {
+        return Long.reverseBytes(value);
+      }
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      final long mostSigBits = this.convert(value.getMostSignificantBits());
+      final long leastSigBits = this.convert(value.getLeastSignificantBits());
+      byte[] result = new byte[2 * Long.BYTES];
+      for (int i = 0; i < Long.BYTES; i++) {
+        result[i] = (byte) ((mostSigBits >> (i * Byte.SIZE)) & 255);
+        result[i + Long.BYTES] = (byte) ((leastSigBits >> (i * Byte.SIZE)) & 255);
+      }
+
+      return new GenericData.Fixed(schema, result);

Review Comment:
   Similar to `fromFixed`, I'd choose an implementation like this:
   
   ```
   ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);
   buffer.putLong(value.getMostSignificantBits());
   buffer.putLong(value.getLeastSignificantBits());
   return new GenericData.Fixed(schema, buffer.array());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450514906


##########
lang/java/avro/src/test/java/org/apache/avro/TestUuidConversions.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericFixed;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.UUID;
+import java.util.stream.Stream;
+
+public class TestUuidConversions {
+
+  private Conversions.UUIDConversion uuidConversion = new Conversions.UUIDConversion();
+
+  private Schema fixed = Schema.createFixed("fixed", "doc", "", Long.BYTES * 2);
+  private Schema fixedUUid = LogicalTypes.uuid().addToSchema(fixed);
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixed(UUID uuid) {
+    GenericFixed value = uuidConversion.toFixed(uuid, fixedUUid, LogicalTypes.uuid());
+    UUID uuid1 = uuidConversion.fromFixed(value, fixedUUid, LogicalTypes.uuid());
+    Assertions.assertEquals(uuid, uuid1);
+  }
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixedReverse(UUID uuid) {
+    Conversions.UUIDConversion bigEndianConversion = new Conversions.UUIDConversion(true);
+    Conversions.UUIDConversion littleEndianConversion = new Conversions.UUIDConversion(false);
+
+    // simulate uuid from other endian source, assume it's big endian (nevermind if
+    // current jvm is big endian, as we simulate).
+    UUID reverserUUID = new UUID(Long.reverseBytes(uuid.getMostSignificantBits()),
+        Long.reverseBytes(uuid.getLeastSignificantBits()));

Review Comment:
   As a UUID is a 128-bit "label", doesn't switching between LittleEndian and BigEndian byte order also switches the order of the long values? I'd expect the most significant 64 bits are in the first half for BE data, but in the last half for LE data.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1916205824

   @Fokko As a Avro maintainer you can push to people's PRs!
   In the top-right corner of this page there is a `Code` dropdown button. Open its `Local` tab and you will see `gh pr checkout 2652` - this is the Github CLI command to use to fetch the branch locally. Then you can make modifications and `git push` them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464590871


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   Having done a bit oof research, network byte order is the way to go. For reference: [RFC4122, section 4.12.2](https://www.ietf.org/rfc/rfc4122.html#section-4.1.2).



##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   Given that UUIDs are 128 bit labels (source with extra references: https://en.wikipedia.org/wiki/Universally_unique_identifier#Binary_wire_format), I think using a fixed byte order of BigEndian is the best choice.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464590871


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   Having done a bit of research, network byte order is the way to go. For reference: [RFC4122, section 4.12.2](https://www.ietf.org/rfc/rfc4122.html#section-4.1.2).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450635170


##########
lang/java/avro/src/test/java/org/apache/avro/TestUuidConversions.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericFixed;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.UUID;
+import java.util.stream.Stream;
+
+public class TestUuidConversions {
+
+  private Conversions.UUIDConversion uuidConversion = new Conversions.UUIDConversion();
+
+  private Schema fixed = Schema.createFixed("fixed", "doc", "", Long.BYTES * 2);
+  private Schema fixedUUid = LogicalTypes.uuid().addToSchema(fixed);
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixed(UUID uuid) {
+    GenericFixed value = uuidConversion.toFixed(uuid, fixedUUid, LogicalTypes.uuid());
+    UUID uuid1 = uuidConversion.fromFixed(value, fixedUUid, LogicalTypes.uuid());
+    Assertions.assertEquals(uuid, uuid1);
+  }
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixedReverse(UUID uuid) {
+    Conversions.UUIDConversion bigEndianConversion = new Conversions.UUIDConversion(true);
+    Conversions.UUIDConversion littleEndianConversion = new Conversions.UUIDConversion(false);
+
+    // simulate uuid from other endian source, assume it's big endian (nevermind if
+    // current jvm is big endian, as we simulate).
+    UUID reverserUUID = new UUID(Long.reverseBytes(uuid.getMostSignificantBits()),
+        Long.reverseBytes(uuid.getLeastSignificantBits()));

Review Comment:
   As uuid is only identifier, without any notion of ordering (you can use ordering property for treemap, but it has no functional meaning), this would have no impact. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1442745011


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -44,6 +44,17 @@
 public class Conversions {
 
   public static class UUIDConversion extends Conversion<UUID> {
+
+    private final boolean isBigEndian;
+
+    public UUIDConversion() {
+      this(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);

Review Comment:
   ~Given that UUIDs are 128 bit labels (source with extra references: https://en.wikipedia.org/wiki/Universally_unique_identifier#Binary_wire_format), I think using a fixed byte order of BigEndian is the best choice.~
   
   As per the [RFC 4122 section 4.1.2](https://www.ietf.org/rfc/rfc4122.html#section-4.1.2), and because we haven't specified anything to the contrary, the byte layout must be in network order (BigEndian).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464674618


##########
doc/content/en/docs/++version++/Specification/_index.md:
##########
@@ -827,7 +827,25 @@ Here, as scale property is stored in value itself it needs more bytes than prece
 ### UUID
 The `uuid` logical type represents a random generated universally unique identifier (UUID).
 
-A `uuid` logical type annotates an Avro `string`. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+A `uuid` logical type annotates an Avro `string` or `fixed` of length 16. The string has to conform with [RFC-4122](https://www.ietf.org/rfc/rfc4122.txt)
+
+The following schemas represent a uuid:
+```json
+{
+  "type": "string",
+  "logicalType": "uuid"
+}
+```
+
+```json
+{
+  "type": "fixed",
+  "size": "16",
+  "logicalType": "uuid"
+}
+```
+
+(UUID will be sorted differently if the underlying type is a string or a fixed)

Review Comment:
   Let's wait until PR #2672 is merged, and take that as documentation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464826558


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
+    }
+
+    private long convert(long value) {
+      if (this.isBigEndian) {
+        return value;
+      } else {
+        return Long.reverseBytes(value);
+      }
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      final long mostSigBits = this.convert(value.getMostSignificantBits());
+      final long leastSigBits = this.convert(value.getLeastSignificantBits());
+      byte[] result = new byte[2 * Long.BYTES];
+      for (int i = 0; i < Long.BYTES; i++) {
+        result[i] = (byte) ((mostSigBits >> (i * Byte.SIZE)) & 255);
+        result[i + Long.BYTES] = (byte) ((leastSigBits >> (i * Byte.SIZE)) & 255);
+      }
+
+      return new GenericData.Fixed(schema, result);

Review Comment:
   This looks good to me 👍 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1464971369


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +69,22 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
+      long mostSigBits = buffer.getLong();
+      long leastSigBits = buffer.getLong();
+      return new UUID(mostSigBits, leastSigBits);
+    }
+
+    @Override
+    public GenericFixed toFixed(final UUID value, final Schema schema, final LogicalType type) {
+      ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);

Review Comment:
   Do we want to explicitly quote the spec here, and specify network byte order (i.e., big-endian)? In #2672 the Avro spec will explicitly mention RFC-4122, which defines the exact byte layout in [section 4.1.2](https://www.ietf.org/rfc/rfc4122.html#section-4.1.2).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1874120829

   @Fokko : Merge this PR if you find it OK; otherwise, copy branch; i won't be able to merge it this afternoon


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1871936557

   @Fokko:  I removed inefficient bytes storage


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1870275935

   I added number encoded control (big endian vs little endian)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1914326153

   @clesaec I know you're busy,  do you have time to rebase this PR? 🙌 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1919925617

   @martin-g @opwvhk Thanks for the pointers here! I was not aware of this 👍 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450508420


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));

Review Comment:
   This certainly works but given the time it takes to read data over a network / from disk), I'd prefer a more readable version:
   
   ```
   ByteBuffer buffer = ByteBuffer.wrap(value.bytes());
   long mostSigBits = bugger.getLong();
   long leastSigBits = bugger.getLong();
   return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
   ```
   
   The additional benefit is that we can easily adjust the byte order without the byte order bug in the original (for LittleEndian order, the meaning of MostSigBits and LeastSigBits is reversed as well as the bit order).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450639250


##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema schema, LogicalType type
     public CharSequence toCharSequence(UUID value, Schema schema, LogicalType type) {
       return value.toString();
     }
+
+    @Override
+    public UUID fromFixed(final GenericFixed value, final Schema schema, final LogicalType type) {
+      long mostSigBits = 0;
+      long leastSigBits = 0;
+      byte[] bytes = value.bytes();
+      for (int i = 0; i < Long.BYTES; i++) {
+        mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+        leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE * i);
+      }
+
+      return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));

Review Comment:
   That's sounds good, and even convert function could be useless with that.
   I let you do all modifications you can, as I can't work much on it from now. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: Add UUID with `fixed[16]` [avro]

Posted by "sk- (via GitHub)" <gi...@apache.org>.
sk- commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-2100968752

   Hi @clesaec, could you expand on the reason you removed support for bytes with logical type uuid in https://github.com/apache/avro/pull/2652/commits/c9f4deadcb07d12ec8f11805f72e33c3c3a61b5c.
   
   I understand is more efficient to store them in a fixed bytes. However, in our company we were already using bytes to represent uuids, so it would be greatly beneficial, if we could also use logical types.
   
   Speaking of efficiency, it wouldn't be worse that the already existing string format, which uses even more bytes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1871359420

   Thanks @clesaec for working on this, this is great!
   
   @KalleOlaviNiemitalo Thanks for chiming in here. It would be great to get this on the devlist as well: https://lists.apache.org/thread/h4lvc2lk194om6cjm76wzosm22cop2xw
   
   Or on the Google doc: https://docs.google.com/document/d/16_oSWrEM7AFUCTe0uuraAEHxywezLfoEz5ahzwvhGUk/edit#heading=h.43xuauwfk7ow
   
   You raised some valid points, but I think we don't sort on the actual physical type, but rather on the logical type. Meaning that how you store them, is more of an implementation detail than something that should be visible to the user. Java should just sort on a `java.util.UUID`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1869585565

   - Spec changed, sort order added.
   - upper vs. lower case has no meaning for "no string" type ? (on fixed where every byte can have value from  0x00 to 0xFF, it has undetermine behaviour)
   - Schema evolution; could may be added ? (when logical type is same but not underlying type)
   
   `Java test for whether the byte order is correct, preferably using the same UUID as in the spec example` ?? Can't see uuid example in spec. TestUuidConversions is not enough ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1436463806


##########
lang/java/avro/src/test/java/org/apache/avro/TestUuidConversions.java:
##########
@@ -0,0 +1,42 @@
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericFixed;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.nio.ByteBuffer;
+import java.util.UUID;
+import java.util.stream.Stream;
+
+class TestUuidConversions {

Review Comment:
   ## Unused classes and interfaces
   
   Unused class: TestUuidConversions is not referenced within this codebase. If not used as an external API it should be removed.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/3160)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "clesaec (via GitHub)" <gi...@apache.org>.
clesaec commented on PR #2652:
URL: https://github.com/apache/avro/pull/2652#issuecomment-1914491264

   Pb is not really that I'm busy, it's that as I changed job, I have now not much time for avro.
   I will try to find time this week, but no guarantee. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: Add UUID with `fixed[16]` [avro]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko merged PR #2652:
URL: https://github.com/apache/avro/pull/2652


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3918: add uuid with bytes and fixed [avro]

Posted by "opwvhk (via GitHub)" <gi...@apache.org>.
opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450514906


##########
lang/java/avro/src/test/java/org/apache/avro/TestUuidConversions.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericFixed;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.UUID;
+import java.util.stream.Stream;
+
+public class TestUuidConversions {
+
+  private Conversions.UUIDConversion uuidConversion = new Conversions.UUIDConversion();
+
+  private Schema fixed = Schema.createFixed("fixed", "doc", "", Long.BYTES * 2);
+  private Schema fixedUUid = LogicalTypes.uuid().addToSchema(fixed);
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixed(UUID uuid) {
+    GenericFixed value = uuidConversion.toFixed(uuid, fixedUUid, LogicalTypes.uuid());
+    UUID uuid1 = uuidConversion.fromFixed(value, fixedUUid, LogicalTypes.uuid());
+    Assertions.assertEquals(uuid, uuid1);
+  }
+
+  @ParameterizedTest
+  @MethodSource("uuidData")
+  void uuidFixedReverse(UUID uuid) {
+    Conversions.UUIDConversion bigEndianConversion = new Conversions.UUIDConversion(true);
+    Conversions.UUIDConversion littleEndianConversion = new Conversions.UUIDConversion(false);
+
+    // simulate uuid from other endian source, assume it's big endian (nevermind if
+    // current jvm is big endian, as we simulate).
+    UUID reverserUUID = new UUID(Long.reverseBytes(uuid.getMostSignificantBits()),
+        Long.reverseBytes(uuid.getLeastSignificantBits()));

Review Comment:
   As a UUID is a 128-bit "label", switching between LittleEndian and BigEndian byte order also switches the order of the long values: the most significant 64 bits are in the first half for BE data, but in the last half for LE data.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org