From 756af098f93f6f2419f122fa6b03e21d7f141c26 Mon Sep 17 00:00:00 2001 From: Andrea Canciani Date: Sun, 26 May 2024 07:48:58 +0200 Subject: [PATCH 1/3] Check arity consistency when constructing `SqlFunctionExpression`s --- .../Properties/RelationalStrings.Designer.cs | 8 ++++++++ .../Properties/RelationalStrings.resx | 3 +++ .../Query/SqlExpressions/SqlFunctionExpression.cs | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs b/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs index 64c5fcd3b60..23ddf163915 100644 --- a/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs +++ b/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs @@ -889,6 +889,14 @@ public static string IncompatibleViewNoRelationship(object? view, object? entity GetString("IncompatibleViewNoRelationship", nameof(view), nameof(entityType), nameof(otherEntityType)), view, entityType, otherEntityType); + /// + /// '{name}' was constructed with {argumentCount} arguments, but the nullability was defined for {argumentNullabilityCount} arguments. + /// + public static string InconsistentNumberOfArguments(object? name, object? argumentCount, object? argumentNullabilityCount) + => string.Format( + GetString("InconsistentNumberOfArguments", nameof(name), nameof(argumentCount), nameof(argumentNullabilityCount)), + name, argumentCount, argumentNullabilityCount); + /// /// Cannot set default value '{value}' of type '{valueType}' on property '{property}' of type '{propertyType}' in entity type '{entityType}'. /// diff --git a/src/EFCore.Relational/Properties/RelationalStrings.resx b/src/EFCore.Relational/Properties/RelationalStrings.resx index e791520a692..b27917d5d26 100644 --- a/src/EFCore.Relational/Properties/RelationalStrings.resx +++ b/src/EFCore.Relational/Properties/RelationalStrings.resx @@ -451,6 +451,9 @@ Cannot use view '{view}' for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and there is no relationship between their primary keys. + + '{name}' was constructed with {argumentCount} arguments, but the nullability was defined for {argumentNullabilityCount} arguments. + Cannot set default value '{value}' of type '{valueType}' on property '{property}' of type '{propertyType}' in entity type '{entityType}'. diff --git a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs index ff3dcb48ce3..ff782b9a80e 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs @@ -213,6 +213,17 @@ public SqlFunctionExpression( IsNullable = nullable; InstancePropagatesNullability = instancePropagatesNullability; ArgumentsPropagateNullability = argumentsPropagateNullability?.ToList(); + + if (Arguments?.Count != ArgumentsPropagateNullability?.Count) + { + throw new InvalidOperationException( + RelationalStrings.InconsistentNumberOfArguments( + name, + Arguments?.Count(), + ArgumentsPropagateNullability?.Count() + ) + ); + } } /// From 1ff7042e7a48838493c704d139ac65bf0d766597 Mon Sep 17 00:00:00 2001 From: Andrea Canciani Date: Sat, 25 May 2024 17:13:35 +0200 Subject: [PATCH 2/3] Fix arity of `random()` translation The Sqlite translation of `random()` was creating an expression in which a `SqlFunction` was defined to have 1 argument, but had an empty `argumentsPropagateNullability` parameter. --- .../Query/Internal/Translators/SqliteRandomTranslator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteRandomTranslator.cs b/src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteRandomTranslator.cs index 5015c54c62b..cc54f6f70b7 100644 --- a/src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteRandomTranslator.cs +++ b/src/EFCore.Sqlite.Core/Query/Internal/Translators/SqliteRandomTranslator.cs @@ -56,7 +56,7 @@ public SqliteRandomTranslator(ISqlExpressionFactory sqlExpressionFactory) _sqlExpressionFactory.Constant(9223372036854780000.0)) }, nullable: false, - argumentsPropagateNullability: Enumerable.Empty(), + argumentsPropagateNullability: [true], method.ReturnType) : null; } From 9f56b59511b22c9839f95e433a9e834fc9e0e340 Mon Sep 17 00:00:00 2001 From: Andrea Canciani Date: Wed, 29 May 2024 14:10:20 +0200 Subject: [PATCH 3/3] Fix arity of `rtrim(X,Y)` translation If its 2-arguments form is being used, the `argumentsPropagateNullability` should have 2 elements as well. --- .../Query/Internal/SqliteGeometryMemberTranslator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Sqlite.NTS/Query/Internal/SqliteGeometryMemberTranslator.cs b/src/EFCore.Sqlite.NTS/Query/Internal/SqliteGeometryMemberTranslator.cs index 73866d306b8..f5465515b17 100644 --- a/src/EFCore.Sqlite.NTS/Query/Internal/SqliteGeometryMemberTranslator.cs +++ b/src/EFCore.Sqlite.NTS/Query/Internal/SqliteGeometryMemberTranslator.cs @@ -105,7 +105,7 @@ public SqliteGeometryMemberTranslator(ISqlExpressionFactory sqlExpressionFactory _sqlExpressionFactory.Constant(" ZM") }, nullable: true, - argumentsPropagateNullability: new[] { true }, + argumentsPropagateNullability: new[] { true, true }, returnType), new[] { @@ -139,7 +139,7 @@ public SqliteGeometryMemberTranslator(ISqlExpressionFactory sqlExpressionFactory _sqlExpressionFactory.Constant(" ZM") }, nullable: true, - argumentsPropagateNullability: new[] { true }, + argumentsPropagateNullability: new[] { true, true }, typeof(string)), new[] {