Skip to content
Closed
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"
Comment thread
TomBruns marked this conversation as resolved.
Outdated
}
}
}
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"
Comment thread
TomBruns marked this conversation as resolved.
Outdated
}
}
}
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
Copy Markdown
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
{
public LogicalSchema Schema { get; }

public UnknownLogicalType(LogicalSchema schema) : base(schema.LogicalTypeName)
{
this.Schema = schema;
}

public override object ConvertToBaseValue(object logicalValue, LogicalSchema schema)
{
throw new NotImplementedException();
}
Comment on lines +52 to +73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These casts are almost no-ops, because the result of each cast is immediately converted back to object. I think this method could just return logicalValue; always regardless of schema.Name.

The casts have two effects, but I don't think those effects matter here, because the logical value should be the same as the base value, which should be an instance of one of the Avro standard types:

  • If the logicalValue is not compatible with the expected type, then it throws InvalidCastException.
  • In some cases, the .NET runtime allows unboxing from a type that does not match exactly. For example, casting from Object to Int32 works even if the Object is actually a boxed UInt32 or a boxed enum whose underlying type is Int32 or UInt32.


public override object ConvertToLogicalValue(object baseValue, LogicalSchema schema)
{
throw new NotImplementedException();
}

public override Type GetCSharpType(bool nullible)
Copy link
Copy Markdown

@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
Copy Markdown
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)
{
// => 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));
Comment thread
TomBruns marked this conversation as resolved.
Outdated
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 @@ public void TestLogicalPrimitive(string s, string baseType, string logicalType)
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);
Comment thread
TomBruns marked this conversation as resolved.
Outdated

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);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed

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;
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
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);
Comment thread
TomBruns marked this conversation as resolved.

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

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