forked from Giorgi/DuckDB.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuckDBInfinityTests.cs
More file actions
359 lines (297 loc) · 16.2 KB
/
Copy pathDuckDBInfinityTests.cs
File metadata and controls
359 lines (297 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
namespace DuckDB.NET.Test;
public class DuckDBInfinityTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db)
{
#region Assertion Helpers
private static void AssertInfinityDateValues(DuckDBDataReader reader)
{
// Positive infinity
var positiveInfinity = reader.GetFieldValue<DuckDBDateOnly>(0);
positiveInfinity.Should().Be(DuckDBDateOnly.PositiveInfinity);
positiveInfinity.IsPositiveInfinity.Should().BeTrue();
positiveInfinity.IsInfinity.Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(positiveInfinity.ToDuckDBDate()).Should().BeFalse();
// Negative infinity
var negativeInfinity = reader.GetFieldValue<DuckDBDateOnly>(1);
negativeInfinity.Should().Be(DuckDBDateOnly.NegativeInfinity);
negativeInfinity.IsNegativeInfinity.Should().BeTrue();
negativeInfinity.IsInfinity.Should().BeTrue();
reader.GetFieldValue<DuckDBDateOnly>(1).Should().Be(DuckDBDateOnly.NegativeInfinity);
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(negativeInfinity.ToDuckDBDate()).Should().BeFalse();
// isinf() function results
reader.GetBoolean(2).Should().BeTrue("isinf() should return true for positive infinity date");
reader.GetBoolean(3).Should().BeTrue("isinf() should return true for negative infinity date");
// Reading as DateTime throws
var actPositive = () => reader.GetDateTime(0);
actPositive.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
var actNegative = () => reader.GetDateTime(1);
actNegative.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
// Reading as nullable DateTime throws
var actNullablePositive = () => reader.GetFieldValue<DateTime?>(0);
actNullablePositive.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
// Reading as DateOnly throws
var actDateOnlyPositive = () => reader.GetFieldValue<DateOnly>(0);
actDateOnlyPositive.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
var actDateOnlyNegative = () => reader.GetFieldValue<DateOnly>(1);
actDateOnlyNegative.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
// Reading as nullable DateOnly throws
var actNullableDateOnly = () => reader.GetFieldValue<DateOnly?>(0);
actNullableDateOnly.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBDateOnly*");
}
private static void AssertInfinityTimestampValues(DuckDBDataReader reader, DuckDBType duckDBType)
{
bool IsFinite(DuckDBTimestampStruct timestamp)
{
return duckDBType switch
{
DuckDBType.TimestampNs => NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampNs(timestamp),
DuckDBType.TimestampMs => NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampMs(timestamp),
DuckDBType.TimestampS => NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampS(timestamp),
_ => NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestamp(timestamp)
};
}
// Positive infinity
var positiveInfinity = reader.GetFieldValue<DuckDBTimestamp>(0);
positiveInfinity.Should().Be(DuckDBTimestamp.PositiveInfinity);
positiveInfinity.IsPositiveInfinity.Should().BeTrue();
positiveInfinity.IsInfinity.Should().BeTrue();
IsFinite(positiveInfinity.ToDuckDBTimestampStruct()).Should().BeFalse();
// Negative infinity
var negativeInfinity = reader.GetFieldValue<DuckDBTimestamp>(1);
negativeInfinity.Should().Be(DuckDBTimestamp.NegativeInfinity);
negativeInfinity.IsNegativeInfinity.Should().BeTrue();
negativeInfinity.IsInfinity.Should().BeTrue();
IsFinite(negativeInfinity.ToDuckDBTimestampStruct()).Should().BeFalse();
// isinf() function results
reader.GetBoolean(2).Should().BeTrue("isinf() should return true for positive infinity timestamp");
reader.GetBoolean(3).Should().BeTrue("isinf() should return true for negative infinity timestamp");
// Reading as DateTime throws
var actPositive = () => reader.GetDateTime(0);
actPositive.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
var actNegative = () => reader.GetDateTime(1);
actNegative.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
// Reading as nullable DateTime throws
var actNullable = () => reader.GetFieldValue<DateTime?>(0);
actNullable.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
// Reading as DateTimeOffset throws
var actOffsetPositive = () => reader.GetFieldValue<DateTimeOffset>(0);
actOffsetPositive.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
var actOffsetNegative = () => reader.GetFieldValue<DateTimeOffset>(1);
actOffsetNegative.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
// Reading as nullable DateTimeOffset throws
var actNullableOffset = () => reader.GetFieldValue<DateTimeOffset?>(0);
actNullableOffset.Should().Throw<InvalidOperationException>().WithMessage("*infinite*DuckDBTimestamp*");
}
#endregion
#region Native IsFinite Tests
[Fact]
public void duckdb_date_is_finite()
{
var positiveInfinity = DuckDBDate.PositiveInfinity;
var negativeInfinity = DuckDBDate.NegativeInfinity;
var finitePositive = new DuckDBDate { Days = Int32.MaxValue - 1 };
var finiteNegative = new DuckDBDate { Days = -Int32.MaxValue + 1 };
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(finitePositive).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(positiveInfinity).Should().BeFalse();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(finiteNegative).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(negativeInfinity).Should().BeFalse();
}
[Fact]
public void duckdb_timestamp_is_finite()
{
var positiveInfinity = DuckDBTimestampStruct.PositiveInfinity;
var negativeInfinity = DuckDBTimestampStruct.NegativeInfinity;
var finitePositive = new DuckDBTimestampStruct { Micros = Int64.MaxValue - 1 };
var finiteNegative = new DuckDBTimestampStruct { Micros = -Int64.MaxValue + 1 };
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestamp(finitePositive).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestamp(positiveInfinity).Should().BeFalse();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestamp(finiteNegative).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestamp(negativeInfinity).Should().BeFalse();
}
[Fact]
public void duckdb_timestamp_s_is_finite()
{
var positiveInfinity = DuckDBTimestampStruct.PositiveInfinity;
var negativeInfinity = DuckDBTimestampStruct.NegativeInfinity;
var finitePositive = new DuckDBTimestampStruct { Micros = Int64.MaxValue - 1 };
var finiteNegative = new DuckDBTimestampStruct { Micros = -Int64.MaxValue + 1 };
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampS(finitePositive).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampS(positiveInfinity).Should().BeFalse();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampS(finiteNegative).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampS(negativeInfinity).Should().BeFalse();
}
[Fact]
public void duckdb_timestamp_ms_is_finite()
{
var positiveInfinity = DuckDBTimestampStruct.PositiveInfinity;
var negativeInfinity = DuckDBTimestampStruct.NegativeInfinity;
var finitePositive = new DuckDBTimestampStruct { Micros = Int64.MaxValue - 1 };
var finiteNegative = new DuckDBTimestampStruct { Micros = -Int64.MaxValue + 1 };
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampMs(finitePositive).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampMs(positiveInfinity).Should().BeFalse();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampMs(finiteNegative).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampMs(negativeInfinity).Should().BeFalse();
}
[Fact]
public void duckdb_timestamp_ns_is_finite()
{
var positiveInfinity = DuckDBTimestampStruct.PositiveInfinity;
var negativeInfinity = DuckDBTimestampStruct.NegativeInfinity;
var finitePositive = new DuckDBTimestampStruct { Micros = Int64.MaxValue - 1 };
var finiteNegative = new DuckDBTimestampStruct { Micros = -Int64.MaxValue + 1 };
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampNs(finitePositive).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampNs(positiveInfinity).Should().BeFalse();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampNs(finiteNegative).Should().BeTrue();
NativeMethods.DateTimeHelpers.DuckDBIsFiniteTimestampNs(negativeInfinity).Should().BeFalse();
}
#endregion
#region Infinity Date Tests
[Fact]
public void ReadInfinityDate()
{
Command.CommandText = "SELECT 'infinity'::DATE, '-infinity'::DATE, isinf('infinity'::DATE), isinf('-infinity'::DATE)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityDateValues(reader);
}
[Fact]
public void ReadInfinityDateWithParameters()
{
Command.CommandText = "SELECT $1::DATE, $2::DATE, isinf($1::DATE), isinf($2::DATE)";
Command.Parameters.Add(new DuckDBParameter(DuckDBDateOnly.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBDateOnly.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityDateValues(reader);
}
#endregion
#region Infinity Timestamp Tests
[Fact]
public void ReadInfinityTimestamp()
{
Command.CommandText = "SELECT 'infinity'::TIMESTAMP, '-infinity'::TIMESTAMP, isinf('infinity'::TIMESTAMP), isinf('-infinity'::TIMESTAMP)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.Timestamp);
}
[Fact]
public void ReadInfinityTimestampWithParameters()
{
Command.CommandText = "SELECT $1::TIMESTAMP, $2::TIMESTAMP, isinf($1::TIMESTAMP), isinf($2::TIMESTAMP)";
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.Timestamp);
}
[Fact]
public void ReadInfinityTimestampNs()
{
Command.CommandText = "SELECT 'infinity'::TIMESTAMP_NS, '-infinity'::TIMESTAMP_NS, isinf('infinity'::TIMESTAMP_NS), isinf('-infinity'::TIMESTAMP_NS)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampNs);
}
[Fact]
public void ReadInfinityTimestampNsWithParameters()
{
Command.CommandText = "SELECT $1::TIMESTAMP_NS, $2::TIMESTAMP_NS, isinf($1::TIMESTAMP_NS), isinf($2::TIMESTAMP_NS)";
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampNs);
}
[Fact]
public void ReadInfinityTimestampMs()
{
Command.CommandText = "SELECT 'infinity'::TIMESTAMP_MS, '-infinity'::TIMESTAMP_MS, isinf('infinity'::TIMESTAMP_MS), isinf('-infinity'::TIMESTAMP_MS)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampMs);
}
[Fact]
public void ReadInfinityTimestampMsWithParameters()
{
Command.CommandText = "SELECT $1::TIMESTAMP_MS, $2::TIMESTAMP_MS, isinf($1::TIMESTAMP_MS), isinf($2::TIMESTAMP_MS)";
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampMs);
}
[Fact]
public void ReadInfinityTimestampS()
{
Command.CommandText = "SELECT 'infinity'::TIMESTAMP_S, '-infinity'::TIMESTAMP_S, isinf('infinity'::TIMESTAMP_S), isinf('-infinity'::TIMESTAMP_S)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampS);
}
[Fact]
public void ReadInfinityTimestampSWithParameters()
{
Command.CommandText = "SELECT $1::TIMESTAMP_S, $2::TIMESTAMP_S, isinf($1::TIMESTAMP_S), isinf($2::TIMESTAMP_S)";
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampS);
}
[Fact]
public void ReadInfinityTimestampTz()
{
Command.CommandText = "SELECT 'infinity'::TIMESTAMPTZ, '-infinity'::TIMESTAMPTZ, isinf('infinity'::TIMESTAMPTZ), isinf('-infinity'::TIMESTAMPTZ)";
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampTz);
}
[Fact]
public void ReadInfinityTimestampTzWithParameters()
{
Command.CommandText = "SELECT $1::TIMESTAMPTZ, $2::TIMESTAMPTZ, isinf($1::TIMESTAMPTZ), isinf($2::TIMESTAMPTZ)";
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.PositiveInfinity));
Command.Parameters.Add(new DuckDBParameter(DuckDBTimestamp.NegativeInfinity));
using var reader = Command.ExecuteReader();
reader.Read();
AssertInfinityTimestampValues(reader, DuckDBType.TimestampTz);
}
#endregion
#region Mixed Infinity and Finite Values
[Fact]
public void ReadMixedInfinityDatesAsDuckDBDateOnly()
{
Command.CommandText = "SELECT * FROM (VALUES ('infinity'::DATE), ('-infinity'::DATE), ('2024-01-15'::DATE)) AS t(d)";
using var reader = Command.ExecuteReader();
reader.Read();
var positiveInfinity = reader.GetFieldValue<DuckDBDateOnly>(0);
positiveInfinity.IsPositiveInfinity.Should().BeTrue();
reader.Read();
var negativeInfinity = reader.GetFieldValue<DuckDBDateOnly>(0);
negativeInfinity.IsNegativeInfinity.Should().BeTrue();
reader.Read();
var finiteDate = reader.GetFieldValue<DuckDBDateOnly>(0);
finiteDate.IsInfinity.Should().BeFalse();
finiteDate.Year.Should().Be(2024);
finiteDate.Month.Should().Be(1);
finiteDate.Day.Should().Be(15);
}
[Fact]
public void ReadMixedInfinityTimestampsAsDuckDBTimestamp()
{
Command.CommandText = "SELECT * FROM (VALUES ('infinity'::TIMESTAMP), ('-infinity'::TIMESTAMP), ('2024-01-15 12:30:45'::TIMESTAMP)) AS t(ts)";
using var reader = Command.ExecuteReader();
reader.Read();
var positiveInfinity = reader.GetFieldValue<DuckDBTimestamp>(0);
positiveInfinity.IsPositiveInfinity.Should().BeTrue();
reader.Read();
var negativeInfinity = reader.GetFieldValue<DuckDBTimestamp>(0);
negativeInfinity.IsNegativeInfinity.Should().BeTrue();
reader.Read();
var finiteTimestamp = reader.GetFieldValue<DuckDBTimestamp>(0);
finiteTimestamp.IsInfinity.Should().BeFalse();
finiteTimestamp.Date.Year.Should().Be(2024);
finiteTimestamp.Date.Month.Should().Be(1);
finiteTimestamp.Date.Day.Should().Be(15);
}
#endregion
}