KalleOlaviNiemitalo commented on code in PR #1718:
URL: https://github.com/apache/avro/pull/1718#discussion_r910100336


##########
lang/csharp/src/apache/main/Reflect/ReflectDefaultWriter.cs:
##########
@@ -199,6 +199,8 @@ protected override bool Matches(Schema sc, object obj)
                     return false;   // Union directly within another union not 
allowed!
                 case Schema.Type.Fixed:
                     return obj is byte[];
+                case Schema.Type.Logical:
+                    return 
((LogicalSchema)sc).LogicalType.IsInstanceOfLogicalType(obj);

Review Comment:
   OK, `((LogicalSchema)sc).LogicalType` cannot be null here, because the 
LogicalSchema constructor assigns `LogicalType = 
LogicalTypeFactory.Instance.GetFromLogicalSchema(this)` without 
`ignoreInvalidOrUnknown: true`.
   
   Before this PR, ReflectDefaultWriter.Matches always returned false for 
LogicalSchema, causing SpecificWriter.WriteUnion not to choose that schema. Is 
there any `obj` for which SpecificWriter could have succeeeded before, but for 
which it now starts choosing the logical schema? I think not, because 
IsInstanceOfLogicalType can only return true for AvroDecimal, Guid, DateTime, 
or TimeSpan, and none of those types matches any other Schema.Type in this 
`switch`. OK.



##########
lang/csharp/src/apache/test/Reflect/TestLogicalSchema.cs:
##########
@@ -0,0 +1,177 @@
+/**
+ * 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.
+ */
+
+using System;
+using System.IO;
+using Avro.IO;
+using Avro.Reflect;
+using NUnit.Framework;
+
+namespace Avro.test.Reflect
+{
+    public class TestLogicalSchema
+    {
+        [TestCase]
+        public void WriteAndReadObjectsWithLogicalSchemaFields_WithNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = null,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = null,
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = null,
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = null,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = null,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = null,
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = null,
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.IsNull(obj.DateNullableProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);

Review Comment:
   These should truncate to microseconds (one tick of DateTime is 100 
nanoseconds = 0.1 microseconds). The test passes because it compares to 
properties of the same object (`obj.` in both arguments, but one should be 
`result.`).



##########
lang/csharp/src/apache/test/Reflect/TestLogicalSchema.cs:
##########
@@ -0,0 +1,177 @@
+/**
+ * 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.
+ */
+
+using System;
+using System.IO;
+using Avro.IO;
+using Avro.Reflect;
+using NUnit.Framework;
+
+namespace Avro.test.Reflect
+{
+    public class TestLogicalSchema
+    {
+        [TestCase]
+        public void WriteAndReadObjectsWithLogicalSchemaFields_WithNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = null,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = null,
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = null,
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = null,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = null,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = null,
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = null,
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.IsNull(obj.DateNullableProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);
+            Assert.IsNull(result.DateTimeMillisecondNullableProperty);
+            Assert.AreEqual((obj.DateTimeMillisecondProperty.Ticks / 10000) * 
10000, result.DateTimeMillisecondProperty.Ticks);
+            Assert.AreEqual(obj.TimeSpanMicrosecondNullableProperty, 
result.TimeSpanMicrosecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMicrosecondProperty, 
result.TimeSpanMicrosecondProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondNullableProperty, 
result.TimeSpanMillisecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondProperty, 
result.TimeSpanMillisecondProperty);

Review Comment:
   OK, these don't need to truncate, because the values are constant.



##########
lang/csharp/src/apache/test/Reflect/TestLogicalSchema.cs:
##########
@@ -0,0 +1,177 @@
+/**
+ * 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.
+ */
+
+using System;
+using System.IO;
+using Avro.IO;
+using Avro.Reflect;
+using NUnit.Framework;
+
+namespace Avro.test.Reflect
+{
+    public class TestLogicalSchema
+    {
+        [TestCase]
+        public void WriteAndReadObjectsWithLogicalSchemaFields_WithNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = null,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = null,
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = null,
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = null,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = null,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = null,
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = null,
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.IsNull(obj.DateNullableProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);
+            Assert.IsNull(result.DateTimeMillisecondNullableProperty);
+            Assert.AreEqual((obj.DateTimeMillisecondProperty.Ticks / 10000) * 
10000, result.DateTimeMillisecondProperty.Ticks);
+            Assert.AreEqual(obj.TimeSpanMicrosecondNullableProperty, 
result.TimeSpanMicrosecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMicrosecondProperty, 
result.TimeSpanMicrosecondProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondNullableProperty, 
result.TimeSpanMillisecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondProperty, 
result.TimeSpanMillisecondProperty);
+        }
+
+        [TestCase]
+        public void 
WriteAndReadObjectsWithLogicalSchemaFields_WithoutNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = 136.42m,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = Guid.NewGuid(),
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = new DateTime(2022, 05, 26, 14, 57, 24, 
123),
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = DateTime.UtcNow,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = DateTime.UtcNow,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.AreEqual(obj.DateNullableProperty?.Date, 
result.DateProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);

Review Comment:
   The same bugs as in 
WriteAndReadObjectsWithLogicalSchemaFields_WithNullValues.



##########
lang/csharp/src/apache/test/Reflect/TestLogicalSchema.cs:
##########
@@ -0,0 +1,177 @@
+/**
+ * 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.
+ */
+
+using System;
+using System.IO;
+using Avro.IO;
+using Avro.Reflect;
+using NUnit.Framework;
+
+namespace Avro.test.Reflect
+{
+    public class TestLogicalSchema
+    {
+        [TestCase]
+        public void WriteAndReadObjectsWithLogicalSchemaFields_WithNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = null,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = null,
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = null,
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = null,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = null,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = null,
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = null,
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.IsNull(obj.DateNullableProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);
+            Assert.IsNull(result.DateTimeMillisecondNullableProperty);
+            Assert.AreEqual((obj.DateTimeMillisecondProperty.Ticks / 10000) * 
10000, result.DateTimeMillisecondProperty.Ticks);
+            Assert.AreEqual(obj.TimeSpanMicrosecondNullableProperty, 
result.TimeSpanMicrosecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMicrosecondProperty, 
result.TimeSpanMicrosecondProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondNullableProperty, 
result.TimeSpanMillisecondNullableProperty);
+            Assert.AreEqual(obj.TimeSpanMillisecondProperty, 
result.TimeSpanMillisecondProperty);
+        }
+
+        [TestCase]
+        public void 
WriteAndReadObjectsWithLogicalSchemaFields_WithoutNullValues()
+        {
+            //Arrange
+            var obj = new TestObject
+            {
+                AvroDecimalNullableProperty = 136.42m,
+                AvroDecimalProperty = 13.42m,
+                GuidNullableProperty = Guid.NewGuid(),
+                GuidProperty = Guid.NewGuid(),
+                DateNullableProperty = new DateTime(2022, 05, 26, 14, 57, 24, 
123),
+                DateProperty = new DateTime(2022, 05, 26, 14, 57, 24, 123),
+                DateTimeMicrosecondNullableProperty = DateTime.UtcNow,
+                DateTimeMicrosecondProperty = DateTime.UtcNow,
+                DateTimeMillisecondNullableProperty = DateTime.UtcNow,
+                DateTimeMillisecondProperty = DateTime.UtcNow,
+                TimeSpanMicrosecondNullableProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMicrosecondProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondNullableProperty = new TimeSpan(23, 59, 59),
+                TimeSpanMillisecondProperty = new TimeSpan(23, 59, 59),
+            };
+
+            var schema = Schema.Parse(SchemaJson);
+            var writer = new ReflectWriter<TestObject>(schema);
+            var reader = new ReflectReader<TestObject>(schema, schema);
+            var writeStream = new MemoryStream();
+            var writeBinaryEncoder = new BinaryEncoder(writeStream);
+
+            //Act
+            writer.Write(obj, writeBinaryEncoder);
+            var data = writeStream.ToArray();
+
+            var readStream = new MemoryStream(data);
+            var result = reader.Read(null, new BinaryDecoder(readStream));
+
+            //Assert
+            Assert.NotNull(result);
+            Assert.AreEqual(obj.AvroDecimalNullableProperty, 
result.AvroDecimalNullableProperty);
+            Assert.AreEqual(obj.AvroDecimalProperty, 
result.AvroDecimalProperty);
+            Assert.AreEqual(obj.GuidNullableProperty, 
result.GuidNullableProperty);
+            Assert.AreEqual(obj.GuidProperty, result.GuidProperty);
+            Assert.AreEqual(obj.DateNullableProperty?.Date, 
result.DateProperty);
+            Assert.AreEqual(obj.DateProperty.Date, result.DateProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondNullableProperty, 
obj.DateTimeMicrosecondNullableProperty);
+            Assert.AreEqual(obj.DateTimeMicrosecondProperty, 
obj.DateTimeMicrosecondProperty);
+            Assert.AreEqual((obj.TimeSpanMicrosecondNullableProperty?.Ticks / 
10000) * 10000, result.TimeSpanMicrosecondNullableProperty?.Ticks);

Review Comment:
   Should compare DateTimeMillisecondNullableProperty rather than 
TimeSpanMicrosecondNullableProperty, which is compared again below.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to