Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thoughts on caching argumentsPropagateNullability arrays? #34422

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -62,15 +62,15 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"AVG",
new[] { averageSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(double)),
averageSqlExpression.Type,
averageSqlExpression.TypeMapping)
: _sqlExpressionFactory.Function(
"AVG",
new[] { averageSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
averageSqlExpression.Type,
averageSqlExpression.TypeMapping);

Expand All @@ -85,7 +85,7 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"COUNT",
new[] { countSqlExpression },
nullable: false,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(int));

case nameof(Queryable.LongCount)
Expand All @@ -97,7 +97,7 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"COUNT",
new[] { longCountSqlExpression },
nullable: false,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(long));

case nameof(Queryable.Max)
Expand All @@ -109,7 +109,7 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"MAX",
new[] { maxSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
maxSqlExpression.Type,
maxSqlExpression.TypeMapping);

Expand All @@ -122,7 +122,7 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"MIN",
new[] { minSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
minSqlExpression.Type,
minSqlExpression.TypeMapping);

Expand All @@ -138,15 +138,15 @@ public QueryableAggregateMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"SUM",
new[] { sumSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(double)),
sumInputType,
sumSqlExpression.TypeMapping)
: _sqlExpressionFactory.Function(
"SUM",
new[] { sumSqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
sumInputType,
sumSqlExpression.TypeMapping);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public virtual SqlExpression Coalesce(SqlExpression left, SqlExpression right, R
[left, right],
nullable: true,
// COALESCE is handled separately since it's only nullable if *all* arguments are null
argumentsPropagateNullability: [false, false],
argumentsPropagateNullability: Statics.FalseArrays[2],
resultType,
inferredTypeMapping)
};
Expand Down
26 changes: 26 additions & 0 deletions src/EFCore.Relational/Statics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

internal static class Statics
{
internal static readonly bool[][] TrueArrays =
[
[],
[true],
[true, true],
[true, true, true],
];

internal static readonly bool[][] FalseArrays =
[
[],
[false],
[false, false],
[false, false, false],
];

internal static IReadOnlyList<bool> FalseTrue = [false, true];
internal static IReadOnlyList<bool> TrueFalse = [true, false];
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public SqlServerGeometryCollectionMethodTranslator(
},
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(typeof(Geometry), instance.TypeMapping!.StoreType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public SqlServerGeometryMethodTranslator(
},
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(method.ReturnType, storeType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SqlServerLineStringMethodTranslator(
},
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(method.ReturnType, instance.TypeMapping!.StoreType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public SqlServerNetTopologySuiteAggregateMethodTranslator(
$"{typeMapping.StoreType}::{functionName}",
new[] { sqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(method.ReturnType, typeMapping.StoreType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public SqlServerPolygonMemberTranslator(
new[] { _sqlExpressionFactory.Constant(1) },
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
returnType,
_typeMappingSource.FindMapping(returnType, storeType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SqlServerPolygonMethodTranslator(
},
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(method.ReturnType, storeType));
}
Expand All @@ -81,7 +81,7 @@ public SqlServerPolygonMethodTranslator(
},
nullable: true,
instancePropagatesNullability: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
method.ReturnType,
_typeMappingSource.FindMapping(method.ReturnType, storeType));
}
Expand Down
26 changes: 26 additions & 0 deletions src/EFCore.SqlServer.NTS/Statics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Utilities;

internal static class Statics
{
internal static readonly bool[][] TrueArrays =
[
[],
[true],
[true, true],
[true, true, true],
];

internal static readonly bool[][] FalseArrays =
[
[],
[false],
[false, false],
[false, false, false],
];

internal static IReadOnlyList<bool> FalseTrue = [false, true];
internal static IReadOnlyList<bool> TrueFalse = [true, false];
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
"DATALENGTH",
new[] { sqlExpression },
nullable: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
isBinaryMaxDataType ? typeof(long) : typeof(int));

return isBinaryMaxDataType
Expand Down Expand Up @@ -393,11 +393,11 @@ StartsEndsWithContains.StartsWith or StartsEndsWithContains.EndsWith
"LEN",
new[] { translatedPattern },
nullable: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
typeof(int))
},
nullable: true,
argumentsPropagateNullability: new[] { true, true },
argumentsPropagateNullability: Statics.TrueArrays[2],
typeof(string),
stringTypeMapping),
translatedPattern))),
Expand Down Expand Up @@ -430,7 +430,7 @@ SqlExpression CharIndexGreaterThanZero()
"CHARINDEX",
new[] { translatedPattern, translatedInstance },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
argumentsPropagateNullability: Statics.TrueArrays[2],
typeof(int)),
_sqlExpressionFactory.Constant(0));
}
Expand Down Expand Up @@ -627,7 +627,7 @@ private Expression TranslateByteArrayElementAccess(Expression array, Expression
Dependencies.SqlExpressionFactory.Constant(1)
},
nullable: true,
argumentsPropagateNullability: new[] { true, true, true },
argumentsPropagateNullability: Statics.TrueArrays[3],
typeof(byte[])),
resultType)
: QueryCompilationContext.NotTranslatedExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SqlServerByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"CHARINDEX",
[value, source],
nullable: true,
argumentsPropagateNullability: [true, true],
argumentsPropagateNullability: Statics.TrueArrays[2],
typeof(int)),
_sqlExpressionFactory.Constant(0));
}
Expand All @@ -68,7 +68,7 @@ public SqlServerByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"SUBSTRING",
[arguments[0], _sqlExpressionFactory.Constant(1), _sqlExpressionFactory.Constant(1)],
nullable: true,
argumentsPropagateNullability: [true, true, true],
argumentsPropagateNullability: Statics.TrueArrays[3],
typeof(byte[])),
method.ReturnType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public SqlServerConvertTranslator(ISqlExpressionFactory sqlExpressionFactory)
=> SupportedMethods.Contains(method)
? _sqlExpressionFactory.Function(
"CONVERT",
new[] { _sqlExpressionFactory.Fragment(TypeMapping[method.Name]), arguments[0] },
[_sqlExpressionFactory.Fragment(TypeMapping[method.Name]), arguments[0]],
nullable: true,
argumentsPropagateNullability: new[] { false, true },
argumentsPropagateNullability: Statics.FalseTrue,
method.ReturnType)
: null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SqlServerDataLengthFunctionTranslator(ISqlExpressionFactory sqlExpression
"DATALENGTH",
arguments.Skip(1),
nullable: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
typeof(long));

return _sqlExpressionFactory.Convert(result, method.ReturnType.UnwrapNullableType());
Expand All @@ -91,7 +91,7 @@ public SqlServerDataLengthFunctionTranslator(ISqlExpressionFactory sqlExpression
"DATALENGTH",
arguments.Skip(1),
nullable: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
method.ReturnType.UnwrapNullableType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SqlServerDateOnlyMemberTranslator(ISqlExpressionFactory sqlExpressionFact
"DATEPART",
new[] { _sqlExpressionFactory.Fragment(datePart), instance! },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
argumentsPropagateNullability: Statics.FalseTrue,
returnType)
: null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SqlServerDateTimeMemberTranslator(
"CONVERT",
new[] { sqlExpressionFactory.Fragment("date"), instance! },
nullable: true,
argumentsPropagateNullability: [false, true],
argumentsPropagateNullability: Statics.FalseTrue,
returnType,
declaringType == typeof(DateTime)
? instance!.TypeMapping
Expand All @@ -63,7 +63,7 @@ public class SqlServerDateTimeMemberTranslator(
"CONVERT",
new[] { sqlExpressionFactory.Fragment("time"), instance! },
nullable: true,
argumentsPropagateNullability: [false, true],
argumentsPropagateNullability: Statics.FalseTrue,
returnType),

nameof(DateTime.Now)
Expand Down Expand Up @@ -107,7 +107,7 @@ public class SqlServerDateTimeMemberTranslator(
typeof(DateTime))
},
nullable: true,
argumentsPropagateNullability: [false, true],
argumentsPropagateNullability: Statics.FalseTrue,
returnType),

_ => null
Expand All @@ -118,7 +118,7 @@ SqlExpression DatePart(string part)
"DATEPART",
arguments: [sqlExpressionFactory.Fragment(part), instance!],
nullable: true,
argumentsPropagateNullability: new[] { false, true },
argumentsPropagateNullability: Statics.FalseTrue,
returnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public SqlServerIsDateFunctionTranslator(ISqlExpressionFactory sqlExpressionFact
"ISDATE",
new[] { arguments[1] },
nullable: true,
argumentsPropagateNullability: new[] { true },
argumentsPropagateNullability: Statics.TrueArrays[1],
MethodInfo.ReturnType),
MethodInfo.ReturnType)
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public SqlServerIsNumericFunctionTranslator(ISqlExpressionFactory sqlExpressionF
"ISNUMERIC",
new[] { arguments[1] },
nullable: false,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(int)),
_sqlExpressionFactory.Constant(1))
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public SqlServerLongCountMethodTranslator(ISqlExpressionFactory sqlExpressionFac
"COUNT_BIG",
new[] { sqlExpression },
nullable: false,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(long)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public SqlServerMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
"ROUND",
new[] { argument, _sqlExpressionFactory.Constant(0), _sqlExpressionFactory.Constant(1) },
nullable: true,
argumentsPropagateNullability: new[] { true, false, false },
argumentsPropagateNullability: [true, false, false],
resultType);

if (argument.Type == typeof(float))
Expand All @@ -176,7 +176,7 @@ public SqlServerMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
"ROUND",
new[] { argument, digits },
nullable: true,
argumentsPropagateNullability: new[] { true, true },
argumentsPropagateNullability: Statics.TrueArrays[2],
resultType);

if (argument.Type == typeof(float))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
"CONVERT",
new[] { _sqlExpressionFactory.Fragment(storeType), instance },
nullable: true,
argumentsPropagateNullability: new[] { false, true },
argumentsPropagateNullability: Statics.FalseTrue,
typeof(string),
_typeMappingSource.GetMapping(storeType)),
_sqlExpressionFactory.Constant(string.Empty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public SqlServerStatisticsAggregateMethodTranslator(
source,
enumerableArgumentIndex: 0,
nullable: true,
argumentsPropagateNullability: new[] { false },
argumentsPropagateNullability: Statics.FalseArrays[1],
typeof(double),
_doubleTypeMapping);
}
Expand Down
Loading