Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-2825: [csharp] Resolve: C# Logical Types throw exception on unkn… #2751

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lang/csharp/src/apache/codegen/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Avro.codegen": {
"commandName": "Project",
"commandLineArgs": "-s C:\\Users\\Thomas.Bruns\\source\\repos\\AzureDevOps\\TQL.Kafka.Samples\\TQL.Kafka.Samples\\TQL.Kafka.Samples.Messages\\v2\\cdc_customers_dbo_tblcustomers.avsc .\\tab"
TomBruns marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
8 changes: 8 additions & 0 deletions lang/csharp/src/apache/main/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Avro.main": {
"commandName": "Project",
"commandLineArgs": "C:\\Users\\Thomas.Bruns\\source\\repos\\AzureDevOps\\TQL.Kafka.Samples\\TQL.Kafka.Samples\\TQL.Kafka.Samples.Messages\\v2\\cdc_customers_dbo_tblcustomers.avsc"
TomBruns marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 1 addition & 1 deletion lang/csharp/src/apache/main/Schema/LogicalSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private LogicalSchema(Schema baseSchema, string logicalTypeName, PropertyMap pr
{
BaseSchema = baseSchema ?? throw new ArgumentNullException(nameof(baseSchema));
LogicalTypeName = logicalTypeName;
LogicalType = LogicalTypeFactory.Instance.GetFromLogicalSchema(this);
LogicalType = LogicalTypeFactory.Instance.GetFromLogicalSchema(this, true);
}

/// <summary>
Expand Down
22 changes: 11 additions & 11 deletions lang/csharp/src/apache/main/Util/LogicalTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private LogicalTypeFactory()
{ TimeMicrosecond.LogicalTypeName, new TimeMicrosecond() },
{ TimestampMillisecond.LogicalTypeName, new TimestampMillisecond() },
{ TimestampMicrosecond.LogicalTypeName, new TimestampMicrosecond() },
{ Uuid.LogicalTypeName, new Uuid() }
{ Uuid.LogicalTypeName, new Uuid() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comma plz

};
}

Expand All @@ -67,22 +67,22 @@ public void Register(LogicalType logicalType)
/// <returns>A <see cref="LogicalType" />.</returns>
public LogicalType GetFromLogicalSchema(LogicalSchema schema, bool ignoreInvalidOrUnknown = false)
{
try
{
if (!_logicalTypes.TryGetValue(schema.LogicalTypeName, out LogicalType logicalType))
throw new AvroTypeException("Logical type '" + schema.LogicalTypeName + "' is not supported.");
LogicalType logicalType = null;

if (_logicalTypes.TryGetValue(schema.LogicalTypeName, out logicalType))
{
logicalType.ValidateSchema(schema);

return logicalType;
}
catch (AvroTypeException)
else if (ignoreInvalidOrUnknown)
{
logicalType = new UnknownLogicalType(schema);
}
else
{
if (!ignoreInvalidOrUnknown)
throw;
throw new AvroTypeException("Logical type '" + schema.LogicalTypeName + "' is not supported.");
}

return null;
return logicalType;
}
}
}
57 changes: 57 additions & 0 deletions lang/csharp/src/apache/main/Util/UnknownLogicalType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Avro.Util
{
public class UnknownLogicalType : LogicalType

Check warning on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType'

Check warning on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType'

Check failure on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType'

Check failure on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType'

Check failure on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType'

Check failure on line 7 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType'
{
public LogicalSchema Schema { get; }

Check warning on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

Check warning on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

Check failure on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

Check failure on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

Check failure on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

Check failure on line 9 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.Schema'

public UnknownLogicalType(LogicalSchema schema) : base(schema.LogicalTypeName)

Check warning on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'

Check warning on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'

Check failure on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'

Check failure on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'

Check failure on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'

Check failure on line 11 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.UnknownLogicalType(LogicalSchema)'
{
this.Schema = schema;
}

public override object ConvertToBaseValue(object logicalValue, LogicalSchema schema)

Check warning on line 16 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToBaseValue(object, LogicalSchema)'

Check failure on line 16 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToBaseValue(object, LogicalSchema)'

Check failure on line 16 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToBaseValue(object, LogicalSchema)'
{
throw new NotImplementedException();
}

public override object ConvertToLogicalValue(object baseValue, LogicalSchema schema)

Check warning on line 21 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToLogicalValue(object, LogicalSchema)'

Check failure on line 21 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToLogicalValue(object, LogicalSchema)'

Check failure on line 21 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.ConvertToLogicalValue(object, LogicalSchema)'
{
throw new NotImplementedException();
}

public override Type GetCSharpType(bool nullible)

Check warning on line 26 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.GetCSharpType(bool)'

Check failure on line 26 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.GetCSharpType(bool)'

Check failure on line 26 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.GetCSharpType(bool)'
Copy link

@a-kalashnikov a-kalashnikov Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps you meant nullable instead of nullible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your comment but that name pre-existed in many files in this codebase so I choose to be consistent.

{
// handle all Primitive Types
switch (this.Schema.BaseSchema.Name)
{
case @"string":
return typeof(System.String);
case @"boolean":
return typeof(System.Boolean);
case @"int":
return typeof(System.Int32);
case @"long":
return typeof(System.Int64);
case @"float":
return typeof(System.Single);
case @"double":
return typeof(System.Double);
case @"bytes":
return typeof(System.Byte[]);
default:
return typeof(System.Object);
}
}

public override bool IsInstanceOfLogicalType(object logicalValue)

Check warning on line 50 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Missing XML comment for publicly visible type or member 'UnknownLogicalType.IsInstanceOfLogicalType(object)'

Check failure on line 50 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.IsInstanceOfLogicalType(object)'

Check failure on line 50 in lang/csharp/src/apache/main/Util/UnknownLogicalType.cs

View workflow job for this annotation

GitHub Actions / test

Missing XML comment for publicly visible type or member 'UnknownLogicalType.IsInstanceOfLogicalType(object)'
{
// => throw new NotImplementedException();
return true;
}

}
}
5 changes: 4 additions & 1 deletion lang/csharp/src/apache/test/AvroGen/AvroGenSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,10 @@ public void NotSupportedSchema(string schema, Type expectedException)
string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
System.IO.File.WriteAllText(schemaFileName, schema);

Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary<string, string>(), false), Is.EqualTo(1));
// We now support unknown logical types
//Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary<string, string>(), false), Is.EqualTo(1));
TomBruns marked this conversation as resolved.
Show resolved Hide resolved
Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary<string, string>(), false), Is.EqualTo(0));

}
finally
{
Expand Down
70 changes: 68 additions & 2 deletions lang/csharp/src/apache/test/Schema/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using Avro.Util;

namespace Avro.Test
{
Expand Down Expand Up @@ -548,12 +549,77 @@
testToString(sc);
}

// Make sure unknown type is carried thru to LogicalTypeName
[TestCase("{\"type\": \"int\", \"logicalType\": \"unknown\"}", "unknown")]
public void TestUnknownLogical(string s, string unknownType)
{
var err = Assert.Throws<AvroTypeException>(() => Schema.Parse(s));
//var err = Assert.Throws<AvroTypeException>(() => Schema.Parse(s));
//Assert.AreEqual("Logical type '" + unknownType + "' is not supported.", err.Message);
TomBruns marked this conversation as resolved.
Show resolved Hide resolved

Assert.AreEqual("Logical type '" + unknownType + "' is not supported.", err.Message);
var schema = Schema.Parse(s);
Assert.IsInstanceOf(typeof(LogicalSchema), schema);

var logicalSchema = schema as LogicalSchema;
Assert.IsInstanceOf(typeof(UnknownLogicalType), logicalSchema.LogicalType);
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

Assert.AreEqual(logicalSchema.LogicalTypeName, unknownType);
}

/*
{
"fields": [
{
"default": 0,
"name": "firstField",
"type": "int"
},
{
"default": null,
"name": "secondField",
"type": [
"null",
{
"logicalType": "varchar",
"maxLength": 65,
"type": "string"
}
]
}
],
"name": "sample_schema",
"type": "record"
}
*/

// Before Change will throw Avro.AvroTypeException: 'Logical type 'varchar' is not supported.'
// Per AVRO Spec (v1.8.0 - v1.11.1) ... Logical Types Section
// Language implementations must ignore unknown logical types when reading, and should use the underlying Avro type.
[TestCase("{\"fields\": [{\"default\": 0,\"name\": \"firstField\",\"type\": \"int\"},{\"default\": null,\"name\": \"secondField\",\"type\": [\"null\",{\"logicalType\": \"varchar\",\"maxLength\": 65,\"type\": \"string\"}]}],\"name\": \"sample_schema\",\"type\": \"record\"}")]
public void TestUnknownLogicalType(string schemaText)
{
var schema = Avro.Schema.Parse(schemaText);
Assert.IsNotNull(schema);

var secondField = ((RecordSchema)schema).Fields.FirstOrDefault(f => f.Name == @"secondField");
Assert.IsNotNull(secondField);

var secondFieldSchema = ((Field)secondField).Schema;
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
Assert.IsNotNull(secondFieldSchema);

var secondFieldUnionSchema = (UnionSchema)secondFieldSchema;
Assert.IsNotNull(secondFieldUnionSchema);

var props = secondFieldUnionSchema.Schemas.Where(s => s.Props != null).ToList();
Assert.IsNotNull(props);
Assert.IsTrue(props.Count == 1);

var prop = props[0];
// Confirm that the unknown logical type is ignored and the underlying AVRO type is used
Assert.IsTrue(prop.Name == @"string");
var logicalSchema = prop as LogicalSchema;
Assert.IsInstanceOf(typeof(UnknownLogicalType), logicalSchema.LogicalType);
TomBruns marked this conversation as resolved.
Show resolved Hide resolved

Assert.AreEqual(logicalSchema.LogicalTypeName, @"varchar");
}

[TestCase("{\"type\": \"map\", \"values\": \"long\"}", "long")]
Expand Down
Loading