Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class SqliteDatabaseModelFactory : DatabaseModelFactory

private static readonly HashSet<string> _floatTypes = new(StringComparer.OrdinalIgnoreCase) { "SINGLE" };

private static readonly HashSet<string> _halfTypes = new(StringComparer.OrdinalIgnoreCase) { "HALF", "FLOAT16" };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLOAT16 seems inconsistent with the other floating-point types. I'd go with just HALF.

Copy link
Copy Markdown
Contributor

@bricelam bricelam Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, the philosophy behind these names was to use names specified in the SQL standard, if available, or at least one generic/.NET name to allow some way of preserving the .NET type in the schema. But, the default EF Core mapping will always adhere to the STRICT rules.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx !
Updated SqliteDatabaseModelFactory to drop FLOAT16 and keep only HALF as the explicit type hint (per your suggestion).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roji Hi! I addressed the last feedback (removed FLOAT16 and kept only HALF). Could you please take another look and merge when you have a chance? Thanks!

Thanks again @bricelam for the review!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. @roji All checks are green and the PR has an approving review. Could you check when you have a moment? Thanks!


private static readonly HashSet<string> _decimalTypes = new(StringComparer.OrdinalIgnoreCase) { "DECIMAL" };

private static readonly HashSet<string> _ushortTypes = new(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -130,6 +132,7 @@ public class SqliteDatabaseModelFactory : DatabaseModelFactory
.Concat(_floatTypes.Select(t => KeyValuePair.Create(t, typeof(float))))
.Concat(_decimalTypes.Select(t => KeyValuePair.Create(t, typeof(decimal))))
.Concat(_timeOnlyTypes.Select(t => KeyValuePair.Create(t, typeof(TimeOnly))))
.Concat(_halfTypes.Select(t => KeyValuePair.Create(t, typeof(Half))))
.Concat(_ushortTypes.Select(t => KeyValuePair.Create(t, typeof(ushort))))
.Concat(_uintTypes.Select(t => KeyValuePair.Create(t, typeof(uint))))
.Concat(_ulongTypes.Select(t => KeyValuePair.Create(t, typeof(ulong))))
Expand Down Expand Up @@ -437,6 +440,11 @@ private void ParseClrDefaults(DatabaseTable table)
// Ignored
}
}
else if (type == typeof(Half)
&& double.TryParse(defaultValueSql, NumberStyles.Float, CultureInfo.InvariantCulture, out var halfValue))
{
column.DefaultValue = (Half)halfValue;
}
else if (defaultValueSql.StartsWith('\'')
&& defaultValueSql.EndsWith('\''))
{
Expand Down Expand Up @@ -819,6 +827,19 @@ protected virtual void InferClrTypes(DbConnection connection, DatabaseTable tabl
continue;
}

if (_halfTypes.Contains(baseColumnType))
{
if (min >= (double)Half.MinValue
&& max <= (double)Half.MaxValue)
{
column["ClrType"] = typeof(Half);

continue;
}

_logger.OutOfRangeWarning(column.Name, table.Name, "Half");
}

if (defaultClrTpe != typeof(double))
{
column["ClrType"] = typeof(double);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;

namespace Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class SqliteHalfTypeMapping : RelationalTypeMapping
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static SqliteHalfTypeMapping Default { get; } = new(SqliteTypeMappingSource.RealTypeName);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqliteHalfTypeMapping(string storeType)
: base(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(typeof(Half)),
storeType))
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected SqliteHalfTypeMapping(RelationalTypeMappingParameters parameters)
: base(parameters)
{
}

/// <summary>
/// Creates a copy of this mapping.
/// </summary>
/// <param name="parameters">The parameters for this mapping.</param>
/// <returns>The newly created mapping.</returns>
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new SqliteHalfTypeMapping(parameters);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
=> ((float)(Half)value).ToString("R", CultureInfo.InvariantCulture);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static readonly HashSet<string> SpatialiteTypes
{ typeof(decimal), SqliteDecimalTypeMapping.Default },
{ typeof(double), Real },
{ typeof(float), new FloatTypeMapping(RealTypeName) },
{ typeof(Half), SqliteHalfTypeMapping.Default },
{ typeof(Guid), SqliteGuidTypeMapping.Default },
{ typeof(JsonTypePlaceholder), SqliteJsonTypeMapping.Default }
};
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ public virtual void Bind()
var value1 = (double)(float)value;
BindDouble(value1);
}
#if NET6_0_OR_GREATER
else if (type == typeof(Half))
{
var value1 = (double)(Half)value;
BindDouble(value1);
}
#endif
else if (type == typeof(Guid))
{
var guid = (Guid)value;
Expand Down Expand Up @@ -251,6 +258,9 @@ public virtual void Bind()
{ typeof(decimal), SqliteType.Text },
{ typeof(double), SqliteType.Real },
{ typeof(float), SqliteType.Real },
#if NET6_0_OR_GREATER
{ typeof(Half), SqliteType.Real },
#endif
{ typeof(Guid), SqliteType.Text },
{ typeof(int), SqliteType.Integer },
{ typeof(long), SqliteType.Integer },
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteValueReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ public virtual string GetString(int ordinal)
return (T)(object)GetFloat(ordinal);
}

#if NET6_0_OR_GREATER
if (typeof(T) == typeof(Half))
{
return (T)(object)(Half)GetDouble(ordinal);
}
#endif

if (typeof(T) == typeof(Guid))
{
return (T)(object)GetGuid(ordinal);
Expand Down Expand Up @@ -347,6 +354,13 @@ public virtual string GetString(int ordinal)
return (T)(object)GetFloat(ordinal);
}

#if NET6_0_OR_GREATER
if (type == typeof(Half))
{
return (T)(object)(Half)GetDouble(ordinal);
}
#endif

if (type == typeof(Guid))
{
return (T)(object)GetGuid(ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class SqliteTypeMappingSourceTest : RelationalTypeMappingSourceTestBase
InlineData("TEXT", typeof(TimeSpan), DbType.Time),
InlineData("TEXT", typeof(decimal), DbType.Decimal),
InlineData("REAL", typeof(float), DbType.Single),
InlineData("REAL", typeof(Half), null),
InlineData("REAL", typeof(double), DbType.Double),
InlineData("INTEGER", typeof(ByteEnum), DbType.Byte),
InlineData("INTEGER", typeof(ShortEnum), DbType.Int16),
Expand All @@ -50,6 +51,7 @@ public class SqliteTypeMappingSourceTest : RelationalTypeMappingSourceTestBase
InlineData("TEXT", typeof(TimeSpan?), DbType.Time),
InlineData("TEXT", typeof(decimal?), DbType.Decimal),
InlineData("REAL", typeof(float?), DbType.Single),
InlineData("REAL", typeof(Half?), null),
InlineData("REAL", typeof(double?), DbType.Double),
InlineData("INTEGER", typeof(ByteEnum?), DbType.Byte),
InlineData("INTEGER", typeof(ShortEnum?), DbType.Int16),
Expand Down Expand Up @@ -176,6 +178,9 @@ public void Does_mappings_for_store_type(string storeType, Type clrType, DbType?
InlineData("REAL", typeof(float), DbType.Single),
InlineData("UNREALISTIC", typeof(float), DbType.Single),
InlineData("RUBBISH", typeof(float), DbType.Single),
InlineData("REAL", typeof(Half), null),
InlineData("UNREALISTIC", typeof(Half), null),
InlineData("RUBBISH", typeof(Half), null),
InlineData("REAL", typeof(double), DbType.Double),
InlineData("UNREALISTIC", typeof(double), DbType.Double),
InlineData("RUBBISH", typeof(double), DbType.Double),
Expand Down Expand Up @@ -245,6 +250,9 @@ public void Does_mappings_for_store_type(string storeType, Type clrType, DbType?
InlineData("REAL", typeof(float?), DbType.Single),
InlineData("UNREALISTIC", typeof(float?), DbType.Single),
InlineData("RUBBISH", typeof(float?), DbType.Single),
InlineData("REAL", typeof(Half?), null),
InlineData("UNREALISTIC", typeof(Half?), null),
InlineData("RUBBISH", typeof(Half?), null),
InlineData("REAL", typeof(double?), DbType.Double),
InlineData("UNREALISTIC", typeof(double?), DbType.Double),
InlineData("RUBBISH", typeof(double?), DbType.Double),
Expand Down
13 changes: 12 additions & 1 deletion test/EFCore.Sqlite.Tests/Storage/SqliteTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ protected override DbCommand CreateTestCommand()

[ConditionalTheory, InlineData(typeof(SqliteDateTimeOffsetTypeMapping), typeof(DateTimeOffset)),
InlineData(typeof(SqliteDateTimeTypeMapping), typeof(DateTime)), InlineData(typeof(SqliteDecimalTypeMapping), typeof(decimal)),
InlineData(typeof(SqliteGuidTypeMapping), typeof(Guid)), InlineData(typeof(SqliteULongTypeMapping), typeof(ulong))]
InlineData(typeof(SqliteGuidTypeMapping), typeof(Guid)), InlineData(typeof(SqliteHalfTypeMapping), typeof(Half)),
InlineData(typeof(SqliteULongTypeMapping), typeof(ulong))]
public override void Create_and_clone_with_converter(Type mappingType, Type type)
=> base.Create_and_clone_with_converter(mappingType, type);

Expand Down Expand Up @@ -138,6 +139,16 @@ public override void ULong_literal_generated_correctly()
Test_GenerateSqlLiteral_helper(typeMapping, long.MaxValue + 1ul, "-9223372036854775808");
}

[ConditionalFact]
public void Half_literal_generated_correctly()
{
var typeMapping = SqliteHalfTypeMapping.Default;

Test_GenerateSqlLiteral_helper(typeMapping, Half.MinValue, "-65504");
Test_GenerateSqlLiteral_helper(typeMapping, Half.MaxValue, "65504");
Test_GenerateSqlLiteral_helper(typeMapping, (Half)3.14f, "3.140625");
}

protected override DbContextOptions ContextOptions { get; }
= new DbContextOptionsBuilder()
.UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkSqlite().BuildServiceProvider(validateScopes: true))
Expand Down
19 changes: 19 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,25 @@ public void GetFloat_throws_when_closed()
public void GetFloat_throws_when_non_query()
=> X_throws_when_non_query(r => r.GetFloat(0));

#if NET6_0_OR_GREATER
[Fact]
public void GetFieldValue_of_Half_works()
=> GetX_works(
"SELECT 3;",
r => r.GetFieldValue<Half>(0),
(Half)3f);

[Fact]
public void GetFieldValue_of_Half_throws_when_null()
=> GetX_throws_when_null(r => r.GetFieldValue<Half>(0));

[Fact]
public void GetFieldValue_of_NullableHalf_works()
=> GetFieldValue_works(
"SELECT 3.14;",
(Half?)3.14f);
#endif

[Theory,
InlineData("2.0", 2.0),
InlineData("9e999", double.PositiveInfinity),
Expand Down