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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0a5d4c9  AVRO-2923: GenericDatum::logicalType() api should return 
correct type with union (#949)
0a5d4c9 is described below

commit 0a5d4c99c21d588d83e2eca2e658b0533c65d391
Author: Yang <[email protected]>
AuthorDate: Thu Aug 26 10:27:49 2021 -0400

    AVRO-2923: GenericDatum::logicalType() api should return correct type with 
union (#949)
    
    * AVRO-2923: logicalType() api should return correct type with union
    
    logicalType() should behave like type() to return correct logical type 
corresponing to one of the constituent types of the union
    
    * adding test
---
 lang/c++/api/GenericDatum.hh |  8 +++++++-
 lang/c++/test/SchemaTests.cc | 13 +++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/lang/c++/api/GenericDatum.hh b/lang/c++/api/GenericDatum.hh
index 378d215..f58fd94 100644
--- a/lang/c++/api/GenericDatum.hh
+++ b/lang/c++/api/GenericDatum.hh
@@ -551,7 +551,13 @@ inline Type GenericDatum::type() const {
 }
 
 inline LogicalType GenericDatum::logicalType() const {
-    return logicalType_;
+    return (type_ == AVRO_UNION) ?
+#if __cplusplus >= 201703L
+        std::any_cast<GenericUnion>(&value_)->datum().logicalType() :
+#else
+        boost::any_cast<GenericUnion>(&value_)->datum().logicalType() :
+#endif
+        logicalType_;
 }
 
 template<typename T>
diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc
old mode 100644
new mode 100755
index 32adc47..3195eab
--- a/lang/c++/test/SchemaTests.cc
+++ b/lang/c++/test/SchemaTests.cc
@@ -351,6 +351,10 @@ static void testLogicalTypes() {
         \"type\": \"string\",\n\
         \"logicalType\": \"uuid\"\n\
     }";
+    // AVRO-2923 Union with LogicalType
+    const char* unionType = "[\n\
+        {\"type\":\"string\", \"logicalType\":\"uuid\"},\"null\"\n\
+    ]";
     {
         BOOST_TEST_CHECKPOINT(bytesDecimalType);
         ValidSchema schema1 = compileJsonSchemaFromString(bytesDecimalType);
@@ -436,6 +440,15 @@ static void testLogicalTypes() {
         GenericDatum datum(schema);
         BOOST_CHECK(datum.logicalType().type() == LogicalType::UUID);
     }
+    {
+        BOOST_TEST_CHECKPOINT(unionType);
+        ValidSchema schema = compileJsonSchemaFromString(unionType);
+        BOOST_CHECK(schema.root()->type() == AVRO_UNION);
+        LogicalType logicalType = schema.root()->logicalType();
+        BOOST_CHECK(logicalType.type() == LogicalType::NONE);
+        GenericDatum datum(schema);
+        BOOST_CHECK(datum.logicalType().type() == LogicalType::UUID);
+    }
 }
 
 static void testMalformedLogicalTypes(const char *schema) {

Reply via email to