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

Fixed handling of DateTime64 columns #90

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Restore dependencies
run: cd src && dotnet restore
- name: Build
run: cd src && dotnet build --no-restore
- name: Test
run: cd src && dotnet test --no-build --verbosity normal
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0;net9.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Clone()
Assert.Equal(7, p2.Scale);
Assert.Equal(ClickHouseParameterMode.Interpolate, p2.ParameterMode);

p2.TimeZone = TimeZoneInfo.Local;
p2.TimeZone = DateTimeZone.Local;
p2.Size = 35;
p2.ArrayRank = 3;
p2.StringEncoding = Encoding.ASCII;
Expand All @@ -77,7 +77,7 @@ public void Clone()
Assert.Same(collection, p2.Collection);

AssertParametersEqual(p2, p3);
Assert.Equal(TimeZoneInfo.Local, p3.TimeZone);
Assert.Equal(DateTimeZone.Local, p3.TimeZone);
Assert.Equal(35, p3.Size);
Assert.Equal(3, p3.ArrayRank);
Assert.Equal(Encoding.ASCII, p3.StringEncoding);
Expand Down Expand Up @@ -111,7 +111,7 @@ public void CopyTo()
SourceColumn = "aaaaa",
SourceColumnNullMapping = true,
StringEncoding = Encoding.BigEndianUnicode,
TimeZone = TimeZoneInfo.Utc,
TimeZone = DateTimeZone.Utc,
Value = 123.456m
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0;net9.0</TargetFrameworks>

<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" Condition="'$(TargetFramework)' != 'netcoreapp3.1'" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6" Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" Condition="'$(TargetFramework)' != 'netcoreapp3.1'" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
31 changes: 17 additions & 14 deletions src/Octonica.ClickHouseClient.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public async Task ReadDateTimeWithTimezoneScalar()
{
await using var connection = await OpenConnectionAsync();

var tzName = TimeZoneHelper.GetTimeZoneId(TimeZoneInfo.Local);
var tzName = TimeZoneHelper.GetTimeZoneId(DateTimeZone.Local);

await using var cmd = connection.CreateCommand($"SELECT toDateTime('2015-04-21 14:59:44', '{tzName}')");

Expand Down Expand Up @@ -443,7 +443,7 @@ public async Task ReadDateTime64WithTimezoneScalar()
{
await using var connection = await OpenConnectionAsync();

var tzName = TimeZoneHelper.GetTimeZoneId(TimeZoneInfo.Local);
var tzName = TimeZoneHelper.GetTimeZoneId(DateTimeZone.Local);

await using var cmd = connection.CreateCommand($"SELECT cast('2015-04-21 14:59:44.123456789' AS DateTime64(9,'{tzName}'))");

Expand All @@ -453,13 +453,15 @@ public async Task ReadDateTime64WithTimezoneScalar()
Assert.Equal(new DateTime(2015, 4, 21, 14, 59, 44).Add(TimeSpan.FromMilliseconds(123.4567)), resultDateTime);
}

// ...

[Theory]
[MemberData(nameof(ParameterModes))]
public async Task ReadDateTime64ParameterScalar(ClickHouseParameterMode parameterMode)
{
await using var connection = await OpenConnectionAsync(parameterMode);
var timeZone = connection.GetServerTimeZone();
var unixEpochOffset = timeZone.GetUtcOffset(DateTime.UnixEpoch);
var unixEpochOffset = timeZone.GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UnixEpoch));
var minDate = new DateTime(1900, 1, 1);
var maxDate = new DateTime(2299, 12, 31, 23, 59, 59, 999);

Expand All @@ -471,22 +473,23 @@ public async Task ReadDateTime64ParameterScalar(ClickHouseParameterMode paramete
new DateTime(1931, 3, 5, 7, 9, 23).Add(TimeSpan.FromMilliseconds(123.45)),
new DateTime(1984, 4, 21, 14, 59, 44).Add(TimeSpan.FromMilliseconds(123.4567)),
new DateTime(2032, 3, 18, 12, 0, 59).Add(TimeSpan.FromMilliseconds(987.6543)),
new DateTime(1968, 04, 16, 0, 0, 0),
new DateTime(1970, 1, 1),
new DateTime(1970, 1, 1) + unixEpochOffset,
minDate + timeZone.GetUtcOffset(minDate),
maxDate + timeZone.GetUtcOffset(maxDate)
minDate + timeZone.GetUtcOffset(Instant.FromDateTimeUtc(minDate)),
maxDate + timeZone.GetUtcOffset(Instant.FromDateTimeUtc(maxDate))
};

await using var cmd = connection.CreateCommand("SELECT {v}");
var parameter = new ClickHouseParameter("v") {ClickHouseDbType = ClickHouseDbType.DateTime64};
var parameter = new ClickHouseParameter("v") { ClickHouseDbType = ClickHouseDbType.DateTime64 };
cmd.Parameters.Add(parameter);

for (int precision = 0; precision < 10; precision++)
{
parameter.Precision = (byte) precision;
var div = TimeSpan.TicksPerSecond / (long) Math.Pow(10, precision);
parameter.Precision = (byte)precision;
var div = TimeSpan.TicksPerSecond / (long)Math.Pow(10, precision);
if (div == 0)
div = -(long) Math.Pow(10, precision) / TimeSpan.TicksPerSecond;
div = -(long)Math.Pow(10, precision) / TimeSpan.TicksPerSecond;

foreach (var value in values)
{
Expand Down Expand Up @@ -522,11 +525,11 @@ public async Task ReadDateTimeParameterWithTimezoneScalar(ClickHouseParameterMod
var value = valueShort.Add(TimeSpan.FromMilliseconds(123.4567));

const string targetTzCode = "Asia/Magadan";
var targetTz = TimeZoneHelper.GetTimeZoneInfo(targetTzCode);
var targetTz = TimeZoneHelper.GetDateTimeZone(targetTzCode);

await using var connection = await OpenConnectionAsync(parameterMode);
await using var cmd = connection.CreateCommand($"SELECT toTimeZone({{d}}, '{targetTzCode}')");
var parameter = new ClickHouseParameter("d") {Value = value, TimeZone = TimeZoneHelper.GetTimeZoneInfo("Pacific/Niue"), Precision = 4};
var parameter = new ClickHouseParameter("d") {Value = value, TimeZone = TimeZoneHelper.GetDateTimeZone("Pacific/Niue"), Precision = 4};
cmd.Parameters.Add(parameter);
var deltaOffset = targetTz.GetUtcOffset(valueShort) - parameter.TimeZone.GetUtcOffset(valueShort);

Expand Down Expand Up @@ -1200,8 +1203,8 @@ UNION ALL SELECT tuple(3, null, cast('2007-01-11 05:32:48' as DateTime))) T
case 2:
Assert.Equal(2, value.Item1);
Assert.Equal("two", value.Item2);
var tz = TimeZoneHelper.GetTimeZoneInfo("Asia/Yekaterinburg");
dt = TimeZoneInfo.ConvertTime(new DateTime(2019, 12, 11, 16, 55, 54), tz, connection.GetServerTimeZone());
var tz = TimeZoneHelper.GetDateTimeZone("Asia/Yekaterinburg");
dt = DateTimeZone.ConvertTime(new DateTime(2019, 12, 11, 16, 55, 54), tz, connection.GetServerTimeZone());
Assert.Equal(dt, value.Item3);
break;

Expand Down Expand Up @@ -1256,7 +1259,7 @@ UNION ALL SELECT tuple(3, null, cast('2007-01-11 05:32:48' as DateTime))) T
Assert.Equal(2, value.number);
Assert.Equal("two", value.str);
dt = new DateTime(2019, 12, 11, 16, 55, 54);
dt = TimeZoneInfo.ConvertTime(dt, TimeZoneHelper.GetTimeZoneInfo("Asia/Yekaterinburg"), connection.GetServerTimeZone());
dt = DateTimeZone.ConvertTime(dt, TimeZoneHelper.GetDateTimeZone("Asia/Yekaterinburg"), connection.GetServerTimeZone());
Assert.Equal(dt, value.date);
break;

Expand Down
5 changes: 3 additions & 2 deletions src/Octonica.ClickHouseClient/ClickHouseConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Octonica.ClickHouseClient.Protocol;
using Octonica.ClickHouseClient.Types;
using Octonica.ClickHouseClient.Utils;
using NodaTime;

namespace Octonica.ClickHouseClient
{
Expand Down Expand Up @@ -439,14 +440,14 @@ private async ValueTask<ClickHouseColumnWriter> CreateColumnWriter(string insert
/// </summary>
/// <returns>The default timezone of the ClickHouse server.</returns>
/// <exception cref="ClickHouseException">Throws <see cref="ClickHouseException"/> if the connection is not open.</exception>
public TimeZoneInfo GetServerTimeZone()
public DateTimeZone GetServerTimeZone()
{
var connectionState = _connectionState;
var serverInfo = connectionState.TcpClient?.ServerInfo;
if (serverInfo == null || connectionState.State != ConnectionState.Open)
throw new ClickHouseException(ClickHouseErrorCodes.ConnectionClosed, "The connection is closed.");

return TimeZoneHelper.GetTimeZoneInfo(serverInfo.Timezone);
return TimeZoneHelper.GetDateTimeZone(serverInfo.Timezone);
}

/// <summary>
Expand Down
13 changes: 4 additions & 9 deletions src/Octonica.ClickHouseClient/ClickHouseParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Octonica.ClickHouseClient.Protocol;
using Octonica.ClickHouseClient.Types;
using Octonica.ClickHouseClient.Utils;
using NodaTime;

namespace Octonica.ClickHouseClient
{
Expand All @@ -38,23 +39,17 @@ public sealed class ClickHouseParameter : DbParameter, ICloneable
{
// https://github.com/ClickHouse/ClickHouse/blob/master/docs/en/query_language/syntax.md
private static readonly Regex ParameterNameRegex = new Regex("^[a-zA-Z_][0-9a-zA-Z_]*$");

private string _parameterName;

private object? _value;
private int _size;
private TimeZoneInfo? _timeZone;

private DateTimeZone? _timeZone;
private bool? _forcedNullable;
private ClickHouseDbType? _forcedType;
private byte? _forcedScale;
private byte? _forcedPrecision;
private int? _forcedArrayRank;

private IntermediateClickHouseTypeInfo? _valueTypeInfo;

private string? _sourceColumn;

internal string Id { get; private set; }

/// <summary>
Expand Down Expand Up @@ -225,7 +220,7 @@ public override byte Scale
/// <see cref="ClickHouseClient.ClickHouseDbType.DateTimeOffset"/>, <see cref="ClickHouseClient.ClickHouseDbType.DateTime2"/>
/// and <see cref="ClickHouseClient.ClickHouseDbType.DateTime64"/>).
/// </summary>
public TimeZoneInfo? TimeZone
public DateTimeZone? TimeZone
{
get => _timeZone;
set
Expand Down Expand Up @@ -587,7 +582,7 @@ private class ParameterColumnTypeDescriptorAdapter : IClickHouseColumnTypeDescri

public byte? Scale => _parameter._forcedScale;

public TimeZoneInfo? TimeZone => _parameter.TimeZone;
public DateTimeZone? TimeZone => _parameter.TimeZone;

public int? ArrayRank => _parameter._forcedArrayRank;

Expand Down
3 changes: 2 additions & 1 deletion src/Octonica.ClickHouseClient/ClickHouseTableColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NodaTime;

namespace Octonica.ClickHouseClient
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public class ClickHouseTableColumn : IClickHouseColumnDescriptor
/// <summary>
/// Gets or sets the time zone. This value is applied to ClickHouse types DateTime and DateTime64.
/// </summary>
public TimeZoneInfo? TimeZone { get; set; }
public DateTimeZone? TimeZone { get; set; }

/// <summary>
/// Gets or sets the rank (a number of dimensions) of an array.
Expand Down
14 changes: 5 additions & 9 deletions src/Octonica.ClickHouseClient/Octonica.ClickHouseClient.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
<Version Condition="'$(ClickHouseClientVersion)' != ''">$(ClickHouseClientVersion)</Version>
<Version Condition="'$(Version)' == ''">2.2.10</Version>
<AssemblyVersion Condition="'$(AssemblyVersion)' == ''">$(Version).0</AssemblyVersion>
<Version Condition="'$(ClickHouseClientVersionSuffix)' != ''">$(Version)$(ClickHouseClientVersionSuffix)</Version>

<Company>Octonica</Company>
<Copyright>© 2019 – 2024 Octonica</Copyright>
<Product>Octonica.ClickHouseClient</Product>
Expand All @@ -24,10 +21,9 @@
<RepositoryUrl>https://github.com/Octonica/ClickHouseClient.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.6" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageReference Include="NodaTime" Version="3.2.0" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Octonica.ClickHouseClient.Exceptions;
using Octonica.ClickHouseClient.Protocol;
using Octonica.ClickHouseClient.Utils;
using NodaTime;

namespace Octonica.ClickHouseClient.Types
{
Expand Down Expand Up @@ -403,7 +404,7 @@ public IClickHouseColumnTypeInfo GetTypeInfo(IClickHouseColumnTypeDescriptor typ
return GetTypeInfo(typeName);
}

internal static IntermediateClickHouseTypeInfo GetTypeFromValue(Type valueType, bool valueCanBeNull, TimeZoneInfo? timeZone)
internal static IntermediateClickHouseTypeInfo GetTypeFromValue(Type valueType, bool valueCanBeNull, DateTimeZone? timeZone)
{
if (valueType == typeof(string))
return new IntermediateClickHouseTypeInfo(ClickHouseDbType.String, "String", valueCanBeNull, 0);
Expand Down Expand Up @@ -542,7 +543,7 @@ internal static IntermediateClickHouseTypeInfo GetTypeFromValue(Type valueType,
}

[return: NotNullIfNotNull("timeZone")]
private static string? GetTimeZoneCode(TimeZoneInfo? timeZone)
private static string? GetTimeZoneCode(DateTimeZone? timeZone)
{
if (timeZone == null)
return null;
Expand Down
Loading