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

Giorgi / DuckDB.NET / 21786556530

07 Feb 2026 08:39PM UTC coverage: 89.155% (-0.07%) from 89.223%
21786556530

push

github

Giorgi
Added support for clearing in-progress adapter

Requires DuckDB 1.5

1199 of 1393 branches covered (86.07%)

Branch coverage included in aggregate %.

6 of 8 new or added lines in 1 file covered. (75.0%)

193 existing lines in 43 files now uncovered.

2336 of 2572 relevant lines covered (90.82%)

557295.56 hits per line

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

92.0
/DuckDB.NET.Data/DataChunk/Reader/DateTimeVectorDataReader.cs
1
namespace DuckDB.NET.Data.DataChunk.Reader;
2

3
internal sealed class DateTimeVectorDataReader : VectorDataReaderBase
4
{
5
    private static readonly Type DateTimeType = typeof(DateTime);
3✔
6
    private static readonly Type DateTimeNullableType = typeof(DateTime?);
3✔
7

8
    private static readonly Type DateTimeOffsetType = typeof(DateTimeOffset);
3✔
9
    private static readonly Type DateTimeOffsetNullableType = typeof(DateTimeOffset?);
3✔
10

11
    private static readonly Type DateOnlyType = typeof(DateOnly);
3✔
12
    private static readonly Type DateOnlyNullableType = typeof(DateOnly?);
3✔
13

14
    private static readonly Type TimeOnlyType = typeof(TimeOnly);
3✔
15
    private static readonly Type TimeOnlyNullableType = typeof(TimeOnly?);
3✔
16

17
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
2,382✔
18
    {
19
    }
2,382✔
20

21
    protected override T GetValidValue<T>(ulong offset, Type targetType)
22
    {
23
        if (DuckDBType == DuckDBType.Date)
654✔
24
        {
25
            var (dateOnly, isFinite) = GetDateOnly(offset);
84✔
26

27
            if (!isFinite)
84✔
28
            {
29
                if (targetType == DateTimeType || targetType == DateTimeNullableType)
48✔
30
                {
31
                    ThrowInfinityDateException();
12✔
32
                }
33

34
                if (targetType == DateOnlyType || targetType == DateOnlyNullableType)
36✔
35
                {
36
                    ThrowInfinityDateException();
12✔
37
                }
38

39
                return (T)(object)dateOnly;
24✔
40
            }
41

42
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
36✔
43
            {
44
                var dateTime = (DateTime)dateOnly;
9✔
45
                return (T)(object)dateTime;
9✔
46
            }
47

48
            if (targetType == DateOnlyType || targetType == DateOnlyNullableType)
27✔
49
            {
50
                var dateTime = (DateOnly)dateOnly;
9✔
51
                return (T)(object)dateTime;
9✔
52
            }
53
            return (T)(object)dateOnly;
18✔
54
        }
55

56
        if (DuckDBType == DuckDBType.Time)
570✔
57
        {
58
            var timeOnly = GetTimeOnly(offset);
42✔
59

60
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
42!
61
            {
UNCOV
62
                var dateTime = (DateTime)timeOnly;
×
UNCOV
63
                return (T)(object)dateTime;
×
64
            }
65

66
            if (targetType == TimeOnlyType || targetType == TimeOnlyNullableType)
42✔
67
            {
68
                var dateTime = (TimeOnly)timeOnly;
18✔
69
                return (T)(object)dateTime;
18✔
70
            }
71
            return (T)(object)timeOnly;
24✔
72
        }
73

74
        if (DuckDBType == DuckDBType.TimeTz)
528✔
75
        {
76
            var timeTz = GetTimeTz(offset);
42✔
77

78
            if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
42✔
79
            {
80
                var dateTimeOffset = new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
36✔
81
                return (T)(object)dateTimeOffset;
36✔
82
            }
83

84
            return (T)(object)timeTz;
6✔
85
        }
86

87
        return DuckDBType switch
486!
88
        {
486✔
89
            DuckDBType.Timestamp or DuckDBType.TimestampS or
486✔
90
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
486✔
91
            DuckDBType.TimestampNs => ReadTimestamp<T>(offset, targetType),
486✔
UNCOV
92
            _ => base.GetValidValue<T>(offset, targetType)
×
93
        };
486✔
94
    }
95

96
    private T ReadTimestamp<T>(ulong offset, Type targetType)
97
    {
98
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
486✔
99

100
        if (!timestampStruct.IsFinite(DuckDBType))
486✔
101
        {
102
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
186✔
103
            {
104
                ThrowInfinityTimestampException();
60✔
105
            }
106

107
            if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
126✔
108
            {
109
                ThrowInfinityTimestampException();
60✔
110
            }
111

112
            var infinityTimestamp = DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
66✔
113
            return (T)(object)infinityTimestamp;
66✔
114
        }
115

116
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
300✔
117

118
        if (targetType == DateTimeType || targetType == DateTimeNullableType)
300✔
119
        {
120
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
102✔
121
            return (T)(object)dateTime;
102✔
122
        }
123

124
        if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
198✔
125
        {
126
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
171✔
127
            var dateTimeOffset = new DateTimeOffset(dateTime, TimeSpan.Zero);
171✔
128
            return (T)(object)dateTimeOffset;
171✔
129
        }
130

131
        return (T)(object)timestamp;
27✔
132
    }
133

134
    internal override object GetValue(ulong offset, Type targetType)
135
    {
136
        return DuckDBType switch
762,026!
137
        {
762,026✔
138
            DuckDBType.Date => GetDate(offset, targetType),
744,767✔
139
            DuckDBType.Time => GetTime(offset, targetType),
795✔
140
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
708✔
141
            DuckDBType.Timestamp or DuckDBType.TimestampS or
762,026✔
142
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
762,026✔
143
            DuckDBType.TimestampNs => GetDateTime(offset, targetType),
15,756✔
UNCOV
144
            _ => base.GetValue(offset, targetType)
×
145
        };
762,026✔
146
    }
147

148
    private DuckDBTimeTz GetTimeTz(ulong offset)
149
    {
150
        var data = GetFieldData<DuckDBTimeTzStruct>(offset);
750✔
151

152
        return NativeMethods.DateTimeHelpers.DuckDBFromTimeTz(data);
750✔
153
    }
154

155
    private DuckDBTimeOnly GetTimeOnly(ulong offset)
156
    {
157
        return NativeMethods.DateTimeHelpers.DuckDBFromTime(GetFieldData<DuckDBTime>(offset));
837✔
158
    }
159

160
    private (DuckDBDateOnly dateOnly, bool IsFinite) GetDateOnly(ulong offset)
161
    {
162
        var date = GetFieldData<DuckDBDate>(offset);
744,851✔
163
        var isFinite = NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(date);
744,851✔
164
        return (DuckDBDateOnly.FromDuckDBDate(date), isFinite);
744,851✔
165
    }
166

167
    private object GetDate(ulong offset, Type targetType)
168
    {
169
        var (dateOnly, isFinite) = GetDateOnly(offset);
744,767✔
170

171
        if (!isFinite)
744,767✔
172
        {
173
            if (targetType == DateTimeType)
18✔
174
            {
175
                ThrowInfinityDateException();
6✔
176
            }
177

178
            if (targetType == DateOnlyType)
12✔
179
            {
180
                ThrowInfinityDateException();
6✔
181
            }
182

183
            return dateOnly;
6✔
184
        }
185

186
        if (targetType == DateTimeType)
744,749✔
187
        {
188
            return (DateTime)dateOnly;
744,224✔
189
        }
190

191
        if (targetType == DateOnlyType)
525✔
192
        {
193
            return (DateOnly)dateOnly;
373✔
194
        }
195

196
        return dateOnly;
152✔
197
    }
198

199
    private object GetTime(ulong offset, Type targetType)
200
    {
201
        var timeOnly = GetTimeOnly(offset);
795✔
202
        if (targetType == DateTimeType)
795!
203
        {
UNCOV
204
            return (DateTime)timeOnly;
×
205
        }
206

207
        if (targetType == TimeOnlyType)
795✔
208
        {
209
            return (TimeOnly)timeOnly;
493✔
210
        }
211

212
        return timeOnly;
302✔
213
    }
214

215
    private object GetDateTime(ulong offset, Type targetType)
216
    {
217
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
15,756✔
218

219
        if (!timestampStruct.IsFinite(DuckDBType))
15,756✔
220
        {
221
            if (targetType == typeof(DateTime))
72✔
222
            {
223
                ThrowInfinityTimestampException();
30✔
224
            }
225

226
            if (targetType == DateTimeOffsetType)
42✔
227
            {
228
                ThrowInfinityTimestampException();
30✔
229
            }
230

231
            return DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
12✔
232
        }
233

234
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
15,684✔
235

236
        if (targetType == typeof(DateTime))
15,684✔
237
        {
238
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
15,648✔
239

240
            return dateTime;
15,648✔
241
        }
242

243
        if (targetType == DateTimeOffsetType)
36!
244
        {
UNCOV
245
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
×
UNCOV
246
            return new DateTimeOffset(dateTime, TimeSpan.Zero);
×
247
        }
248

249
        return timestamp;
36✔
250
    }
251

252
    private object GetDateTimeOffset(ulong offset, Type targetType)
253
    {
254
        var timeTz = GetTimeTz(offset);
708✔
255

256
        if (targetType == typeof(DateTimeOffset))
708✔
257
        {
258
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
702✔
259
        }
260

261
        return timeTz;
6✔
262
    }
263

264
    private static void ThrowInfinityDateException()
265
    {
266
        throw new InvalidOperationException(
36✔
267
            "Cannot convert infinite date value to DateTime or DateOnly. " +
36✔
268
            "Use DuckDBDateOnly to read this value and check IsInfinity, IsPositiveInfinity, or IsNegativeInfinity before converting to .NET types.");
36✔
269
    }
270

271
    private static void ThrowInfinityTimestampException()
272
    {
273
        throw new InvalidOperationException(
180✔
274
            "Cannot convert infinite timestamp value to DateTime or DateTimeOffset. " +
180✔
275
            "Use DuckDBTimestamp to read this value and check IsInfinity, IsPositiveInfinity, or IsNegativeInfinity before converting to .NET types.");
180✔
276
    }
277
}
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

© 2026 Coveralls, Inc