Skip to content

Commit a3f1b45

Browse files
committed
a
1 parent 6219d9d commit a3f1b45

4 files changed

Lines changed: 55 additions & 31 deletions

File tree

src/DataStax.AstraDB.DataApi/SerDes/TableInsertManyResultConverter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,28 @@ private object DeserializeValue(JsonElement element, PrimaryKeySchema schema, Js
197197
case "decimal":
198198
return element.GetDecimal();
199199
case "double":
200+
if (element.ValueKind == JsonValueKind.String) // ugly but TableInsertResultConverter won't exist in the future anyways
201+
{
202+
return element.GetString() switch
203+
{
204+
"NaN" => double.NaN,
205+
"Infinity" => double.PositiveInfinity,
206+
"-Infinity" => double.NegativeInfinity,
207+
_ => throw new JsonException($"Unexpected string value '{element.GetString()}' for double type")
208+
};
209+
}
200210
return element.GetDouble();
201211
case "float":
212+
if (element.ValueKind == JsonValueKind.String)
213+
{
214+
return element.GetString() switch
215+
{
216+
"NaN" => float.NaN,
217+
"Infinity" => float.PositiveInfinity,
218+
"-Infinity" => float.NegativeInfinity,
219+
_ => throw new JsonException($"Unexpected string value '{element.GetString()}' for float type")
220+
};
221+
}
202222
return element.GetSingle();
203223
case "boolean":
204224
return element.GetBoolean();

src/DataStax.AstraDB.DataApi/Utils/TypeUtilities.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ public static DataApiType GetDataApiTypeFromUnderlyingType(Type propertyType)
122122
{
123123
if (genericArguments.Length == 2)
124124
{
125-
return DataApiType.Map(GetDataApiTypeFromUnderlyingType(genericArguments[0]), GetDataApiTypeFromUnderlyingType(genericArguments[1]));
125+
return DataApiType.Map(GetDataApiType(genericArguments[0]), GetDataApiType(genericArguments[1]));
126126
}
127127
}
128128
else if (genericTypeDefinition == typeof(List<>))
129129
{
130-
return DataApiType.List(GetDataApiTypeFromUnderlyingType(genericArguments[0]));
130+
return DataApiType.List(GetDataApiType(genericArguments[0]));
131131
}
132132
else if (genericTypeDefinition == typeof(HashSet<>))
133133
{
134-
return DataApiType.Set(GetDataApiTypeFromUnderlyingType(genericArguments[0]));
134+
return DataApiType.Set(GetDataApiType(genericArguments[0]));
135135
}
136136
else
137137
{

test/DataStax.AstraDB.DataApi.IntegrationTests/TestObjects.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,8 @@ public class TypesTester
470470
public class DoubleFloatTypeTest
471471
{
472472
[ColumnPrimaryKey()]
473-
public int Id { get; set; }
474-
public double DoubleValue { get; set; }
475-
public float FloatValue { get; set; }
476-
public double? NullableDouble { get; set; }
477-
public float? NullableFloat { get; set; }
473+
public double? DoubleValue { get; set; }
474+
public float? FloatValue { get; set; }
475+
public Dictionary<float, double> FloatDoubleMap { get; set; }
476+
public List<float> FloatList { get; set; }
478477
}
479-

test/DataStax.AstraDB.DataApi.IntegrationTests/Tests/AdditionalTableTests.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -525,36 +525,42 @@ public async Task Test_DoubleAndFloatConverters()
525525

526526
List<DoubleFloatTypeTest> rows = new List<DoubleFloatTypeTest>
527527
{
528-
new() { Id = 0, DoubleValue = 123.456, FloatValue = 78.9f, NullableDouble = 999.999, NullableFloat = 111.111f },
529-
new() { Id = 1, DoubleValue = double.NaN, FloatValue = float.NaN, NullableDouble = double.NaN, NullableFloat = float.NaN },
530-
new() { Id = 2, DoubleValue = double.PositiveInfinity, FloatValue = float.PositiveInfinity, NullableDouble = null, NullableFloat = null },
531-
new() { Id = 3, DoubleValue = double.NegativeInfinity, FloatValue = float.NegativeInfinity, NullableDouble = 0.0, NullableFloat = 0.0f }
528+
new() { DoubleValue = 123.456, FloatValue = 78.9f, FloatDoubleMap = new Dictionary<float, double> { { 1.1f, 2.2 } }, FloatList = new List<float> { 3.3f, 4.4f } },
529+
new() { DoubleValue = double.NaN, FloatValue = float.NaN, FloatDoubleMap = new Dictionary<float, double> { { float.NaN, double.NaN } }, FloatList = new List<float> { float.NaN } },
530+
new() { DoubleValue = double.PositiveInfinity, FloatValue = null, FloatDoubleMap = new Dictionary<float, double> { { float.PositiveInfinity, double.NegativeInfinity } }, FloatList = new List<float> { float.PositiveInfinity } },
531+
new() { DoubleValue = double.NegativeInfinity, FloatValue = float.NegativeInfinity, FloatDoubleMap = new Dictionary<float, double> { { 0.0f, 0.0 } }, FloatList = new List<float> { 0.0f } }
532532
};
533533

534534
var result = await table.InsertManyAsync(rows);
535535
Assert.Equal(4, result.InsertedCount);
536536

537-
var row0 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.Id, 0));
538-
Assert.Equal(123.456, row0.DoubleValue, 5);
539-
Assert.Equal(78.9f, row0.FloatValue, 5);
540-
541-
var row1 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.Id, 1));
542-
Assert.True(double.IsNaN(row1.DoubleValue));
543-
Assert.True(float.IsNaN(row1.FloatValue));
544-
545-
var row2 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.Id, 2));
546-
Assert.True(double.IsPositiveInfinity(row2.DoubleValue));
547-
Assert.True(float.IsPositiveInfinity(row2.FloatValue));
548-
Assert.Null(row2.NullableDouble);
549-
Assert.Null(row2.NullableFloat);
550-
551-
var row3 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.Id, 3));
552-
Assert.True(double.IsNegativeInfinity(row3.DoubleValue));
553-
Assert.True(float.IsNegativeInfinity(row3.FloatValue));
537+
var row0 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.DoubleValue, 123.456));
538+
Assert.Equal(123.456, row0.DoubleValue.Value, 5);
539+
Assert.Equal(78.9f, row0.FloatValue.Value, 5);
540+
Assert.Equal(new Dictionary<float, double> { { 1.1f, 2.2 } }, row0.FloatDoubleMap);
541+
Assert.Equal(new List<float> { 3.3f, 4.4f }, row0.FloatList);
542+
543+
var row1 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.DoubleValue, double.NaN));
544+
Assert.True(double.IsNaN(row1.DoubleValue.Value));
545+
Assert.True(float.IsNaN(row1.FloatValue.Value));
546+
Assert.Equal(new Dictionary<float, double> { { float.NaN, double.NaN } }, row1.FloatDoubleMap);
547+
Assert.Equal(new List<float> { float.NaN }, row1.FloatList);
548+
549+
var row2 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.DoubleValue, double.PositiveInfinity));
550+
Assert.True(double.IsPositiveInfinity(row2.DoubleValue.Value));
551+
Assert.Null(row2.FloatValue);
552+
Assert.Equal(new Dictionary<float, double> { { float.PositiveInfinity, double.NegativeInfinity } }, row2.FloatDoubleMap);
553+
Assert.Equal(new List<float> { float.PositiveInfinity }, row2.FloatList);
554+
555+
var row3 = await table.FindOneAsync(Builders<DoubleFloatTypeTest>.TableFilter.Eq(x => x.DoubleValue, double.NegativeInfinity));
556+
Assert.True(double.IsNegativeInfinity(row3.DoubleValue.Value));
557+
Assert.True(float.IsNegativeInfinity(row3.FloatValue.Value));
558+
Assert.Equal(new Dictionary<float, double> { { 0.0f, 0.0 } }, row3.FloatDoubleMap);
559+
Assert.Equal(new List<float> { 0.0f }, row3.FloatList);
554560
}
555561
finally
556562
{
557-
await fixture.Database.DropTableAsync(tableName);
563+
await fixture.Database.DropTableAsync(tableName, new() { IfExists = true });
558564
}
559565
}
560566
}

0 commit comments

Comments
 (0)