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

Chore/datetime kind #553

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 17 additions & 3 deletions src/Parquet.Test/Serialisation/SchemaReflectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public int[]? IntArray { get; set; }

public bool MarkerField;

Check warning on line 26 in src/Parquet.Test/Serialisation/SchemaReflectorTest.cs

View workflow job for this annotation

GitHub Actions / Build NuGet

Field 'SchemaReflectorTest.PocoClass.MarkerField' is never assigned to, and will always have its default value false

Check warning on line 26 in src/Parquet.Test/Serialisation/SchemaReflectorTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

Field 'SchemaReflectorTest.PocoClass.MarkerField' is never assigned to, and will always have its default value false

Check warning on line 26 in src/Parquet.Test/Serialisation/SchemaReflectorTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

Field 'SchemaReflectorTest.PocoClass.MarkerField' is never assigned to, and will always have its default value false

Check warning on line 26 in src/Parquet.Test/Serialisation/SchemaReflectorTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

Field 'SchemaReflectorTest.PocoClass.MarkerField' is never assigned to, and will always have its default value false
}

[Fact]
Expand Down Expand Up @@ -323,8 +323,11 @@
[ParquetTimestamp]
public DateTime TimestampDate { get; set; }

[ParquetTimestamp(useLogicalTimestamp: true, isAdjustedToUTC: false)]
public DateTime LogicalLocalTimestampDate { get; set; }

[ParquetTimestamp(useLogicalTimestamp: true)]
public DateTime LogicalTimestampDate { get; set; }
public DateTime LogicalUtcTimestampDate { get; set; }

[ParquetTimestamp]
public DateTime? NullableTimestampDate { get; set; }
Expand Down Expand Up @@ -387,11 +390,22 @@
}

[Fact]
public void Type_DateTime_LogicalTimestamp() {
public void Type_DateTime_LogicalLocalTimestamp() {
ParquetSchema s = typeof(DatesPoco).GetParquetSchema(true);

DataField df = s.FindDataField(nameof(DatesPoco.LogicalLocalTimestampDate));
Assert.True(df is DateTimeDataField);
Assert.False(((DateTimeDataField)df).IsAdjustedToUTC);
Assert.Equal(DateTimeFormat.Timestamp, ((DateTimeDataField)df).DateTimeFormat);
}

[Fact]
public void Type_DateTime_LogicalUtcTimestamp() {
ParquetSchema s = typeof(DatesPoco).GetParquetSchema(true);

DataField df = s.FindDataField(nameof(DatesPoco.LogicalTimestampDate));
DataField df = s.FindDataField(nameof(DatesPoco.LogicalUtcTimestampDate));
Assert.True(df is DateTimeDataField);
Assert.True(((DateTimeDataField)df).IsAdjustedToUTC);
Assert.Equal(DateTimeFormat.Timestamp, ((DateTimeDataField)df).DateTimeFormat);
}

Expand Down
76 changes: 76 additions & 0 deletions src/Parquet.Test/Types/LogicalTimestampTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Parquet.Serialization;
using Parquet.Serialization.Attributes;
using Xunit;

namespace Parquet.Test.Types {
public class LogicalTimestampTest {
internal class V1 {
public DateTime DateTimeUtc { get; set; }

public DateTime DateTimeLocal { get; set; }
}

internal class V2 {
[ParquetTimestamp(useLogicalTimestamp: true, isAdjustedToUTC: true)]

public DateTime DateTimeUtc { get; set; }

[ParquetTimestamp(useLogicalTimestamp: true, isAdjustedToUTC: false)]
public DateTime DateTimeLocal { get; set; }
}

[Fact]
public async Task SerializeDeserializeV1() {
var utc = new DateTime(2006, 1, 2, 15, 4, 5, DateTimeKind.Utc);
// TODO: should this be unspecified?
var local = new DateTime(2006, 1, 2, 15, 4, 5, DateTimeKind.Local);

var expected = new V1 {
DateTimeUtc = utc,
DateTimeLocal = local,
};

using var ms = new MemoryStream();
await ParquetSerializer.SerializeAsync([expected], ms);

ms.Position = 0;
IList<V1> actuals = await ParquetSerializer.DeserializeAsync<V1>(ms);
Assert.Single(actuals);
V1 actual = actuals.First();
Assert.Equal(utc, actual.DateTimeUtc);
Assert.Equal(DateTimeKind.Utc, actual.DateTimeUtc.Kind);
// Kind is changed from Local to UTC
Assert.Equal(local, actual.DateTimeUtc);
Assert.Equal(DateTimeKind.Utc, actual.DateTimeUtc.Kind);
}

[Fact]
public async Task SerializeDeserializeLogicalTimestamp() {
var utc = new DateTime(2006, 1, 2, 15, 4, 5, DateTimeKind.Utc);
// TODO: should this be unspecified?
var local = new DateTime(2006, 1, 2, 15, 4, 5, DateTimeKind.Local);

var expected = new V2 {
DateTimeUtc = utc,
DateTimeLocal = local,
};

using var ms = new MemoryStream();
await ParquetSerializer.SerializeAsync([expected], ms);

ms.Position = 0;
IList<V2> actuals = await ParquetSerializer.DeserializeAsync<V2>(ms);
Assert.Single(actuals);
V2 actual = actuals.First();
Assert.Equal(utc, actual.DateTimeUtc);
Assert.Equal(DateTimeKind.Utc, actual.DateTimeUtc.Kind);
Assert.Equal(local, actual.DateTimeLocal);
Assert.Equal(DateTimeKind.Local, actual.DateTimeLocal.Kind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public class ParquetTimestampAttribute : Attribute {
/// </summary>
/// <param name="resolution"></param>
/// <param name="useLogicalTimestamp"></param>
public ParquetTimestampAttribute(ParquetTimestampResolution resolution = ParquetTimestampResolution.Milliseconds, bool useLogicalTimestamp = false) {
/// <param name="isAdjustedToUTC"></param>
public ParquetTimestampAttribute(ParquetTimestampResolution resolution = ParquetTimestampResolution.Milliseconds, bool useLogicalTimestamp = false, bool isAdjustedToUTC = true) {
Resolution = resolution;
UseLogicalTimestamp = useLogicalTimestamp;
IsAdjustedToUTC = isAdjustedToUTC;
}

/// <summary>
Expand Down
Loading