This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 82a2bc8b03 AVRO-4140: [C++] Support uuid to annotate fixed[16] (#3379)
82a2bc8b03 is described below

commit 82a2bc8b034de34626e2ab8bf091234122474d50
Author: Gang Wu <[email protected]>
AuthorDate: Mon May 19 16:08:00 2025 +0800

    AVRO-4140: [C++] Support uuid to annotate fixed[16] (#3379)
---
 lang/c++/impl/Node.cc        |  4 ++--
 lang/c++/test/SchemaTests.cc | 22 ++++++++++++++++++----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/lang/c++/impl/Node.cc b/lang/c++/impl/Node.cc
index 017da22b8e..0a5fcfc1b0 100644
--- a/lang/c++/impl/Node.cc
+++ b/lang/c++/impl/Node.cc
@@ -228,9 +228,9 @@ void Node::setLogicalType(LogicalType logicalType) {
             }
             break;
         case LogicalType::UUID:
-            if (type_ != AVRO_STRING) {
+            if (type_ != AVRO_STRING && (type_ != AVRO_FIXED || fixedSize() != 
16)) {
                 throw Exception("UUID logical type can only annotate "
-                                "STRING type");
+                                "STRING type or FIXED type of length 16");
             }
             break;
         case LogicalType::CUSTOM:
diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc
index 39e2224af6..2aa39d4146 100644
--- a/lang/c++/test/SchemaTests.cc
+++ b/lang/c++/test/SchemaTests.cc
@@ -333,6 +333,7 @@ const char *roundTripSchemas[] = {
     R"({"type":"long","logicalType":"local-timestamp-nanos"})",
     R"({"type":"fixed","name":"test","size":12,"logicalType":"duration"})",
     R"({"type":"string","logicalType":"uuid"})",
+    R"({"type":"fixed","name":"test","size":16,"logicalType":"uuid"})",
 
     // namespace with '$' in it.
     R"({
@@ -393,7 +394,9 @@ const char *malformedLogicalTypes[] = {
     // and scale is integrated in bytes.
     R"({"type":"bytes","logicalType": "big-decimal","precision": 9})",
     R"({"type":"bytes","logicalType": "big-decimal","scale": 2})",
-    R"({"type":"bytes","logicalType": "big-decimal","precision": 9,"scale": 
2})"};
+    R"({"type":"bytes","logicalType": "big-decimal","precision": 9,"scale": 
2})",
+    
R"({"type":"fixed","logicalType":"uuid","size":12,"name":"invalid_uuid_size"})",
+};
 const char *schemasToCompact[] = {
     // Schema without any whitespace
     R"({"type":"record","name":"Test","fields":[]})",
@@ -508,7 +511,8 @@ static void testLogicalTypes() {
     const char *localTimestampMicrosType = R"({"type": "long", "logicalType": 
"local-timestamp-micros"})";
     const char *localTimestampNanosType = R"({"type": "long", "logicalType": 
"local-timestamp-nanos"})";
     const char *durationType = R"({"type": "fixed","size": 12,"name": 
"durationType","logicalType": "duration"})";
-    const char *uuidType = R"({"type": "string","logicalType": "uuid"})";
+    const char *uuidStringType = R"({"type": "string","logicalType": "uuid"})";
+    const char *uuidFixedType = R"({"type": "fixed", "size": 16, "name": 
"uuidFixedType", "logicalType": "uuid"})";
     // AVRO-2923 Union with LogicalType
     const char *unionType = R"([{"type":"string", 
"logicalType":"uuid"},"null"]})";
     {
@@ -631,14 +635,24 @@ static void testLogicalTypes() {
         BOOST_CHECK(datum.logicalType().type() == LogicalType::DURATION);
     }
     {
-        BOOST_TEST_CHECKPOINT(uuidType);
-        ValidSchema schema = compileJsonSchemaFromString(uuidType);
+        BOOST_TEST_CHECKPOINT(uuidStringType);
+        ValidSchema schema = compileJsonSchemaFromString(uuidStringType);
         BOOST_CHECK(schema.root()->type() == AVRO_STRING);
         LogicalType logicalType = schema.root()->logicalType();
         BOOST_CHECK(logicalType.type() == LogicalType::UUID);
         GenericDatum datum(schema);
         BOOST_CHECK(datum.logicalType().type() == LogicalType::UUID);
     }
+    {
+        BOOST_TEST_CHECKPOINT(uuidFixedType);
+        ValidSchema schema = compileJsonSchemaFromString(uuidFixedType);
+        BOOST_CHECK(schema.root()->type() == AVRO_FIXED);
+        BOOST_CHECK(schema.root()->fixedSize() == 16);
+        LogicalType logicalType = schema.root()->logicalType();
+        BOOST_CHECK(logicalType.type() == LogicalType::UUID);
+        GenericDatum datum(schema);
+        BOOST_CHECK(datum.logicalType().type() == LogicalType::UUID);
+    }
     {
         BOOST_TEST_CHECKPOINT(unionType);
         ValidSchema schema = compileJsonSchemaFromString(unionType);

Reply via email to