Skip to content

Commit

Permalink
Serialize nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Eriksson authored and Stefan Eriksson committed Jan 26, 2024
1 parent e5c24ed commit 7e29b1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ BenchmarkDotNet.Artifacts/
Parquet.sln.DotSettings.user
launchSettings.json
.DS_Store
*.user
20 changes: 18 additions & 2 deletions src/Parquet.Test/Serialisation/ParquetSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ class NullableRecord : Record {
public double? ParentMeterValue { get; set; }

public decimal? ParentResolution { get; set; }

public DateTime? ParentTime { get; set; }

public Interval? ParentInterval { get; set; }

public TimeSpan? ParentTimeSpan { get; set; }

}

[Fact]
Expand All @@ -172,8 +179,12 @@ public async Task Atomics_Nullable_Serde() {
EventName = i % 2 == 0 ? "on" : "off",
MeterValue = i,
ParentId = (i % 4 == 0) ? null : i,
ExternalId = Guid.NewGuid(),
ParentMeterValue = (i % 5 == 0) ? null : (double?)i,
ParentResolution = (i % 6 == 0) ? null : (decimal?)i
ParentResolution = (i % 6 == 0) ? null : (decimal?)i,
ParentTime = (i % 5 == 0) ? null : DateTime.UtcNow.AddSeconds(i),
ParentInterval = (i % 5 == 0) ? null : new Interval(i, i, i),
ParentTimeSpan = (i % 5 == 0) ? null : TimeSpan.FromSeconds(i)
}).ToList();

await Compare(data);
Expand All @@ -187,7 +198,12 @@ public async Task Atomics_Nullable_Serde_Dict() {
["EventName"] = i % 2 == 0 ? "on" : "off",
["MeterValue"] = (double)i,
["ParentId"] = (i % 4 == 0) ? null : i,
["ExternalId"] = Guid.NewGuid()
["ExternalId"] = Guid.NewGuid(),
["ParentMeterValue"] = (i % 5 == 0) ? null : (double?)i,
["ParentResolution"] = (i % 6 == 0) ? null : (decimal?)i,
["ParentTime"] = (i % 5 == 0) ? null : DateTime.UtcNow.AddSeconds(i),
["ParentInterval"] = (i % 5 == 0) ? null : new Interval(i, i, i),
["ParentTimeSpan"] = (i % 5 == 0) ? null : TimeSpan.FromSeconds(i)
}).ToList();

await DictCompare<NullableRecord>(data);
Expand Down
10 changes: 9 additions & 1 deletion src/Parquet/Serialization/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static Field ConstructDataField(string name, string propertyName, Type t
Field r;
bool? isNullable = member == null
? null
: member.IsRequired ? false : null;
: member.IsRequired ? false : IsNullable(t);

if(t == typeof(DateTime) || t == typeof(DateTime?)) {
ParquetTimestampAttribute? tsa = member?.TimestampAttribute;
Expand Down Expand Up @@ -165,6 +165,14 @@ private static Field ConstructDataField(string name, string propertyName, Type t
return r;
}

static bool IsNullable(Type type) {
if(!type.IsValueType)
return true; // ref-type
if(Nullable.GetUnderlyingType(type) != null)
return true; // Nullable<T>
return false; // value-type
}

private static MapField ConstructMapField(string name, string propertyName,
Type tKey, Type tValue,
bool forWriting) {
Expand Down

0 comments on commit 7e29b1e

Please sign in to comment.