You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/04/19 18:28:52 UTC

[avro] branch branch-1.11 updated: AVRO-3841: Try exact schema match first in union type (#1635)

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

mgrigorov pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new 30a5c2ee7 AVRO-3841: Try exact schema match first in union type (#1635)
30a5c2ee7 is described below

commit 30a5c2ee75249778e5a7f2584ca1b47523c3253c
Author: Zoltan Csizmadia <zc...@gmail.com>
AuthorDate: Tue Apr 19 13:28:33 2022 -0500

    AVRO-3841: Try exact schema match first in union type (#1635)
    
    * Try exact schema match
    
    * Fix formatting
    
    * Add tests for exception
    
    Co-authored-by: Zoltan Csizmadia <Cs...@valassis.com>
    (cherry picked from commit a852aa433111499f0479a8efd8149e7dcfb1b77a)
---
 lang/csharp/src/apache/main/Schema/UnionSchema.cs  | 17 ++++-
 .../csharp/src/apache/test/Generic/GenericTests.cs | 12 ++++
 .../apache/test/Specific/DoubleLongUnionRecord.cs  | 73 ++++++++++++++++++++++
 .../src/apache/test/Specific/SpecificTests.cs      | 32 ++++++++++
 4 files changed, 132 insertions(+), 2 deletions(-)

diff --git a/lang/csharp/src/apache/main/Schema/UnionSchema.cs b/lang/csharp/src/apache/main/Schema/UnionSchema.cs
index 7e1575684..af9ba7583 100644
--- a/lang/csharp/src/apache/main/Schema/UnionSchema.cs
+++ b/lang/csharp/src/apache/main/Schema/UnionSchema.cs
@@ -127,8 +127,21 @@ namespace Avro
         {
             if (s is UnionSchema) throw new AvroException("Cannot find a match against union schema");
             // Try exact match.
-            //for (int i = 0; i < Count; i++) if (Schemas[i].Equals(s)) return i; // removed this for performance's sake
-            for (int i = 0; i < Count; i++) if (Schemas[i].CanRead(s)) return i;
+            // CanRead might find a compatible schema which can read. e.g. double and long
+            for (int i = 0; i < Count; i++)
+            {
+                if (Schemas[i].Equals(s))
+                {
+                    return i;
+                }
+            }
+            for (int i = 0; i < Count; i++)
+            {
+                if (Schemas[i].CanRead(s))
+                {
+                    return i;
+                }
+            }
             return -1;
         }
 
diff --git a/lang/csharp/src/apache/test/Generic/GenericTests.cs b/lang/csharp/src/apache/test/Generic/GenericTests.cs
index 8e0a86cdf..b87ce69f8 100644
--- a/lang/csharp/src/apache/test/Generic/GenericTests.cs
+++ b/lang/csharp/src/apache/test/Generic/GenericTests.cs
@@ -128,6 +128,18 @@ namespace Avro.Test.Generic
         [TestCase("[\"int\", \"long\"]", 100L)]
         [TestCase("[\"float\", \"double\"]", 100.75)]
         [TestCase("[\"float\", \"double\"]", 23.67f)]
+        [TestCase("[\"float\", \"int\"]", 0)]
+        [TestCase("[\"float\", \"int\"]", 0.0f)]
+        [TestCase("[\"float\", \"int\"]", 100)]
+        [TestCase("[\"float\", \"int\"]", 100.0f)]
+        [TestCase("[\"float\", \"int\"]", -100)]
+        [TestCase("[\"float\", \"int\"]", -100.0f)]
+        [TestCase("[\"double\", \"long\"]", 0L)]
+        [TestCase("[\"double\", \"long\"]", 0.0)]
+        [TestCase("[\"double\", \"long\"]", 100L)]
+        [TestCase("[\"double\", \"long\"]", 100.0)]
+        [TestCase("[\"double\", \"long\"]", -100L)]
+        [TestCase("[\"double\", \"long\"]", -100.0)]
         [TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", new float[] { 23.67f, 22.78f })]
         [TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", 100.89)]
         [TestCase("[{\"type\": \"array\", \"items\": \"string\"}, \"string\"]", "a")]
diff --git a/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs b/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs
new file mode 100644
index 000000000..97b94be7e
--- /dev/null
+++ b/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs
@@ -0,0 +1,73 @@
+/**
+ * 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.
+ */
+// ------------------------------------------------------------------------------
+// <auto-generated>
+//    Generated by avrogen, version 1.11.0.0
+//    Changes to this file may cause incorrect behavior and will be lost if code
+//    is regenerated
+// </auto-generated>
+// ------------------------------------------------------------------------------
+namespace Avro.Test.Specific
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Text;
+    using Avro;
+    using Avro.Specific;
+
+    public partial class DoubleLongUnionRecord : ISpecificRecord
+    {
+        public static Schema _SCHEMA = Avro.Schema.Parse("{\"type\":\"record\",\"name\":\"DoubleLongUnionRecord\",\"namespace\":\"Avro.Test.Specific\",\"fields\":[{\"name" +
+                "\":\"Property\",\"type\":[\"double\",\"long\"]}]}");
+        private object _Property;
+        public virtual Schema Schema
+        {
+            get
+            {
+                return DoubleLongUnionRecord._SCHEMA;
+            }
+        }
+        public object Property
+        {
+            get
+            {
+                return this._Property;
+            }
+            set
+            {
+                this._Property = value;
+            }
+        }
+        public virtual object Get(int fieldPos)
+        {
+            switch (fieldPos)
+            {
+                case 0: return this.Property;
+                default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
+            };
+        }
+        public virtual void Put(int fieldPos, object fieldValue)
+        {
+            switch (fieldPos)
+            {
+                case 0: this.Property = (System.Object)fieldValue; break;
+                default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
+            };
+        }
+    }
+}
diff --git a/lang/csharp/src/apache/test/Specific/SpecificTests.cs b/lang/csharp/src/apache/test/Specific/SpecificTests.cs
index 178f25544..1aa3c3a03 100644
--- a/lang/csharp/src/apache/test/Specific/SpecificTests.cs
+++ b/lang/csharp/src/apache/test/Specific/SpecificTests.cs
@@ -272,6 +272,38 @@ namespace Avro.Test
             Assert.AreEqual(EnumType.DEFAULT, rec2.enumType);
         }
 
+        [TestCase(0L)]
+        [TestCase(100L)]
+        [TestCase(-100L)]
+        [TestCase(0.0)]
+        [TestCase(100.0)]
+        [TestCase(-100.0)]
+        public void TestDoubleLongUnion(object value)
+        {
+            var testRecord = new DoubleLongUnionRecord();
+            testRecord.Property = value;
+
+            // serialize
+            var stream = serialize(DoubleLongUnionRecord._SCHEMA, testRecord);
+
+            // deserialize
+            var rec2 = deserialize<DoubleLongUnionRecord>(stream, DoubleLongUnionRecord._SCHEMA, DoubleLongUnionRecord._SCHEMA);
+            Assert.AreEqual(value, rec2.Property);
+            Assert.AreEqual(value.GetType(), rec2.Property.GetType());
+        }
+
+        [TestCase(0)]
+        [TestCase(100)]
+        [TestCase(-100)]
+        [TestCase(0.0f)]
+        [TestCase(100.0f)]
+        [TestCase(-100.0f)]
+        [TestCase("0")]
+        [TestCase("100")]
+        public void TestDoubleLongUnionNoMatchException(object value)
+        {
+            Assert.Throws<AvroException>(() => serialize(DoubleLongUnionRecord._SCHEMA, new DoubleLongUnionRecord() { Property = value }));
+        }
 
         [Test]
         public void TestArrayWithReservedWords()