• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Giorgi / DuckDB.NET / 13181745983

06 Feb 2025 02:31PM UTC coverage: 89.958% (+0.02%) from 89.937%
13181745983

push

github

Giorgi
Merge branch 'develop'

1081 of 1235 branches covered (87.53%)

Branch coverage included in aggregate %.

322 of 345 new or added lines in 20 files covered. (93.33%)

2 existing lines in 1 file now uncovered.

2108 of 2310 relevant lines covered (91.26%)

760472.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.61
/DuckDB.NET.Data/DataChunk/Reader/DateTimeVectorDataReader.cs
1
using System;
2
using DuckDB.NET.Data.Extensions;
3
using DuckDB.NET.Native;
4

5
namespace DuckDB.NET.Data.DataChunk.Reader;
6

7
internal sealed class DateTimeVectorDataReader : VectorDataReaderBase
8
{
9
    private static readonly Type DateTimeType = typeof(DateTime);
3✔
10
    private static readonly Type DateTimeNullableType = typeof(DateTime?);
3✔
11

12
    private static readonly Type DateTimeOffsetType = typeof(DateTimeOffset);
3✔
13
    private static readonly Type DateTimeOffsetNullableType = typeof(DateTimeOffset?);
3✔
14

15
#if NET6_0_OR_GREATER
16
    private static readonly Type DateOnlyType = typeof(DateOnly);
3✔
17
    private static readonly Type DateOnlyNullableType = typeof(DateOnly?);
3✔
18

19
    private static readonly Type TimeOnlyType = typeof(TimeOnly);
3✔
20
    private static readonly Type TimeOnlyNullableType = typeof(TimeOnly?);
3✔
21
#endif
22

23
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
2,211✔
24
    {
25
    }
2,211✔
26

27
    protected override T GetValidValue<T>(ulong offset, Type targetType)
28
    {
29
        if (DuckDBType == DuckDBType.Date)
405✔
30
        {
31
            var dateOnly = GetDateOnly(offset);
33✔
32

33
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
33✔
34
            {
35
                var dateTime = (DateTime)dateOnly;
9✔
36
                return (T)(object)dateTime;
9✔
37
            }
38

39
#if NET6_0_OR_GREATER
40
            if (targetType == DateOnlyType || targetType == DateOnlyNullableType)
24✔
41
            {
42
                var dateTime = (DateOnly)dateOnly;
9✔
43
                return (T)(object)dateTime;
9✔
44
            }
45
#endif
46
            return (T)(object)dateOnly;
15✔
47
        }
48

49
        if (DuckDBType == DuckDBType.Time)
372✔
50
        {
51
            var timeOnly = GetTimeOnly(offset);
42✔
52

53
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
42!
54
            {
55
                var dateTime = (DateTime)timeOnly;
×
56
                return (T)(object)dateTime;
×
57
            }
58

59
#if NET6_0_OR_GREATER
60
            if (targetType == TimeOnlyType || targetType == TimeOnlyNullableType)
42✔
61
            {
62
                var dateTime = (TimeOnly)timeOnly;
18✔
63
                return (T)(object)dateTime;
18✔
64
            }
65
#endif
66
            return (T)(object)timeOnly;
24✔
67
        }
68

69
        if (DuckDBType == DuckDBType.TimeTz)
330✔
70
        {
71
            var timeTz = GetTimeTz(offset);
42✔
72

73
            if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
42✔
74
            {
75
                var dateTimeOffset = new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
36✔
76
                return (T)(object)dateTimeOffset;
36✔
77
            }
78

79
            return (T)(object)timeTz;
6✔
80
        }
81

82
        return DuckDBType switch
288!
83
        {
288✔
84
            DuckDBType.Timestamp or DuckDBType.TimestampS or 
288✔
85
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or 
288✔
86
            DuckDBType.TimestampNs => ReadTimestamp<T>(offset, targetType),
288✔
87
            _ => base.GetValidValue<T>(offset, targetType)
×
88
        };
288✔
89
    }
90

91
    private T ReadTimestamp<T>(ulong offset, Type targetType)
92
    {
93
        var (timestamp, additionalTicks) = GetFieldData<DuckDBTimestampStruct>(offset).ToDuckDBTimestamp(DuckDBType);
288✔
94

95
        if (targetType == DateTimeType || targetType == DateTimeNullableType)
288✔
96
        {
97
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
96✔
98
            return (T)(object)dateTime;
96✔
99
        }
100

101
        if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
192✔
102
        {
103
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
168✔
104
            var dateTimeOffset = new DateTimeOffset(dateTime, TimeSpan.Zero);
168✔
105
            return (T)(object)dateTimeOffset;
168✔
106
        }
107

108
        return (T)(object)timestamp;
24✔
109
    }
110

111
    internal override object GetValue(ulong offset, Type targetType)
112
    {
113
        return DuckDBType switch
770,562!
114
        {
770,562✔
115
            DuckDBType.Date => GetDate(offset, targetType),
753,593✔
116
            DuckDBType.Time => GetTime(offset, targetType),
831✔
117
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
475✔
118
            DuckDBType.Timestamp or DuckDBType.TimestampS or 
770,562✔
119
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
770,562✔
120
            DuckDBType.TimestampNs => GetDateTime(offset, targetType),
15,663✔
121
            _ => base.GetValue(offset, targetType)
×
122
        };
770,562✔
123
    }
124

125
    private DuckDBTimeTz GetTimeTz(ulong offset)
126
    {
127
        var data = GetFieldData<DuckDBTimeTzStruct>(offset);
517✔
128

129
        return NativeMethods.DateTimeHelpers.DuckDBFromTimeTz(data);
517✔
130
    }
131

132
    private DuckDBTimeOnly GetTimeOnly(ulong offset)
133
    {
134
        return NativeMethods.DateTimeHelpers.DuckDBFromTime(GetFieldData<DuckDBTime>(offset));
873✔
135
    }
136

137
    private DuckDBDateOnly GetDateOnly(ulong offset)
138
    {
139
        return NativeMethods.DateTimeHelpers.DuckDBFromDate(GetFieldData<DuckDBDate>(offset));
753,626✔
140
    }
141

142
    private object GetDate(ulong offset, Type targetType)
143
    {
144
        var dateOnly = GetDateOnly(offset);
753,593✔
145
        if (targetType == DateTimeType)
753,593✔
146
        {
147
            return (DateTime)dateOnly;
752,881✔
148
        }
149

150
#if NET6_0_OR_GREATER
151
        if (targetType == DateOnlyType)
712✔
152
        {
153
            return (DateOnly)dateOnly;
471✔
154
        }
155
#endif
156

157
        return dateOnly;
241✔
158
    }
159

160
    private object GetTime(ulong offset, Type targetType)
161
    {
162
        var timeOnly = GetTimeOnly(offset);
831✔
163
        if (targetType == DateTimeType)
831!
164
        {
165
            return (DateTime)timeOnly;
×
166
        }
167

168
#if NET6_0_OR_GREATER
169
        if (targetType == TimeOnlyType)
831✔
170
        {
171
            return (TimeOnly)timeOnly;
620✔
172
        }
173
#endif
174

175
        return timeOnly;
211✔
176
    }
177

178
    private object GetDateTime(ulong offset, Type targetType)
179
    {
180
        var (timestamp, additionalTicks) = GetFieldData<DuckDBTimestampStruct>(offset).ToDuckDBTimestamp(DuckDBType);
15,663✔
181

182
        if (targetType == typeof(DateTime))
15,663✔
183
        {
184
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
15,639✔
185

186
            return dateTime;
15,639✔
187
        }
188

189
        if (targetType == DateTimeOffsetType)
24!
190
        {
NEW
191
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
×
NEW
192
            return new DateTimeOffset(dateTime, TimeSpan.Zero);
×
193
        }
194

195
        return timestamp;
24✔
196
    }
197

198
    private object GetDateTimeOffset(ulong offset, Type targetType)
199
    {
200
        var timeTz = GetTimeTz(offset);
475✔
201

202
        if (targetType == typeof(DateTimeOffset))
475✔
203
        {
204
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
469✔
205
        }
206

207
        return timeTz;
6✔
208
    }
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc