using System.Diagnostics.CodeAnalysis;
using Npgsql.EntityFrameworkCore.PostgreSQL.Extensions.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
using static Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Statics;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal;
///
/// 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.
///
public class NpgsqlQueryableMethodTranslatingExpressionVisitor : RelationalQueryableMethodTranslatingExpressionVisitor
{
private readonly RelationalQueryCompilationContext _queryCompilationContext;
private readonly NpgsqlTypeMappingSource _typeMappingSource;
private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory;
private readonly Version _postgresVersion;
private readonly bool _isRedshift;
private RelationalTypeMapping? _ordinalityTypeMapping;
#region MethodInfos
private static readonly MethodInfo Like2MethodInfo =
typeof(DbFunctionsExtensions).GetRuntimeMethod(
nameof(DbFunctionsExtensions.Like), [typeof(DbFunctions), typeof(string), typeof(string)])!;
// ReSharper disable once InconsistentNaming
private static readonly MethodInfo ILike2MethodInfo
= typeof(NpgsqlDbFunctionsExtensions).GetRuntimeMethod(
nameof(NpgsqlDbFunctionsExtensions.ILike), [typeof(DbFunctions), typeof(string), typeof(string)])!;
private static readonly MethodInfo MatchesLQuery =
typeof(LTree).GetRuntimeMethod(nameof(LTree.MatchesLQuery), [typeof(string)])!;
#endregion
///
/// 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.
///
public NpgsqlQueryableMethodTranslatingExpressionVisitor(
QueryableMethodTranslatingExpressionVisitorDependencies dependencies,
RelationalQueryableMethodTranslatingExpressionVisitorDependencies relationalDependencies,
RelationalQueryCompilationContext queryCompilationContext,
INpgsqlSingletonOptions npgsqlSingletonOptions)
: base(dependencies, relationalDependencies, queryCompilationContext)
{
_queryCompilationContext = queryCompilationContext;
_typeMappingSource = (NpgsqlTypeMappingSource)relationalDependencies.TypeMappingSource;
_sqlExpressionFactory = (NpgsqlSqlExpressionFactory)relationalDependencies.SqlExpressionFactory;
_postgresVersion = npgsqlSingletonOptions.PostgresVersion;
_isRedshift = npgsqlSingletonOptions.UseRedshift;
}
///
/// 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.
///
protected NpgsqlQueryableMethodTranslatingExpressionVisitor(NpgsqlQueryableMethodTranslatingExpressionVisitor parentVisitor)
: base(parentVisitor)
{
_queryCompilationContext = parentVisitor._queryCompilationContext;
_typeMappingSource = parentVisitor._typeMappingSource;
_sqlExpressionFactory = parentVisitor._sqlExpressionFactory;
_postgresVersion = parentVisitor._postgresVersion;
_isRedshift = parentVisitor._isRedshift;
}
///
/// 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.
///
protected override QueryableMethodTranslatingExpressionVisitor CreateSubqueryVisitor()
=> new NpgsqlQueryableMethodTranslatingExpressionVisitor(this);
///
/// 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.
///
protected override ShapedQueryExpression? TranslatePrimitiveCollection(
SqlExpression sqlExpression,
IProperty? property,
string tableAlias)
{
if (_isRedshift)
{
AddTranslationErrorDetails("Redshift does not support unnest, which is required for most forms of querying of JSON arrays.");
return null;
}
var elementClrType = sqlExpression.Type.GetSequenceType();
var elementTypeMapping = (RelationalTypeMapping?)sqlExpression.TypeMapping?.ElementTypeMapping;
// If this is a collection property, get the element's nullability out of metadata. Otherwise, this is a parameter property, in
// which case we only have the CLR type (note that we cannot produce different SQLs based on the nullability of an *element* in
// a parameter collection - our caching mechanism only supports varying by the nullability of the parameter itself (i.e. the
// collection).
// TODO: if property is non-null, GetElementType() should never be null, but we have #31469 for shadow properties
var isElementNullable = property?.GetElementType() is null
? elementClrType.IsNullableType()
: property.GetElementType()!.IsNullable;
// We support two kinds of primitive collections: the standard one with PostgreSQL arrays (where we use the unnest function), and
// a special case for geometry collections, where we use
SelectExpression selectExpression;
// TODO: Parameters have no type mapping. We can check whether the expression type is one of the NTS geometry collection types,
// though in a perfect world we'd actually infer this. In other words, when the type mapping of the element is inferred further on,
// we'd replace the unnest expression with ST_Dump. We could even have a special expression type which means "indeterminate, must be
// inferred".
#pragma warning disable EF1001 // SelectExpression constructors are pubternal
switch (sqlExpression.TypeMapping)
{
case { StoreTypeNameBase: "geometry" or "geography" }:
{
// TODO: For geometry collection support (not yet supported), see #2850.
selectExpression = new SelectExpression(
[new TableValuedFunctionExpression(tableAlias, "ST_Dump", [sqlExpression])],
new ColumnExpression("geom", tableAlias, elementClrType.UnwrapNullableType(), elementTypeMapping, isElementNullable),
identifier: [], // TODO
_queryCompilationContext.SqlAliasManager);
break;
}
// Scalar/primitive collection mapped to a PostgreSQL array (typical and default case)
case NpgsqlArrayTypeMapping or NpgsqlMultirangeTypeMapping or null:
{
// Note that for unnest we have a special expression type extending TableValuedFunctionExpression, adding the ability to provide
// an explicit column name for its output (SELECT * FROM unnest(array) AS f(foo)).
// This is necessary since when the column name isn't explicitly specified, it is automatically identical to the table alias
// (f above); since the table alias may get uniquified by EF, this would break queries.
// TODO: When we have metadata to determine if the element is nullable, pass that here to SelectExpression
// Note also that with PostgreSQL unnest, the output ordering is guaranteed to be the same as the input array. However, we still
// need to add an explicit ordering on the ordinality column, since once the unnest is joined into a select, its "natural"
// orderings is lost and an explicit ordering is needed again (see #3207).
var (ordinalityColumn, ordinalityComparer) = GenerateOrdinalityIdentifier(tableAlias);
selectExpression = new SelectExpression(
[new PgUnnestExpression(tableAlias, sqlExpression, "value")],
new ColumnExpression(
"value",
tableAlias,
elementClrType.UnwrapNullableType(),
elementTypeMapping,
isElementNullable),
identifier: [(ordinalityColumn, ordinalityComparer)],
_queryCompilationContext.SqlAliasManager);
selectExpression.AppendOrdering(new OrderingExpression(ordinalityColumn, ascending: true));
break;
}
// Scalar/primitive collection mapped to a JSON array, like in other providers.
// Happens for scalar collections nested within JSON documents, or if the user explicitly mapped to JSON instead of
// the default PG array.
// Translate to SELECT element::int FROM jsonb_array_elements_text(...) WITH ORDINALITY
case NpgsqlJsonTypeMapping { ElementTypeMapping: not null, StoreType: var storeType }:
{
var (ordinalityColumn, ordinalityComparer) = GenerateOrdinalityIdentifier(tableAlias);
SqlExpression elementProjection = new ColumnExpression(
"element",
tableAlias,
typeof(string),
_typeMappingSource.FindMapping(typeof(string)),
isElementNullable);
// If the projected type is anything other than a text, apply a cast (jsonb_array_elements_text returns text)
if (!elementTypeMapping!.StoreType.Equals("text", StringComparison.OrdinalIgnoreCase))
{
elementProjection = _sqlExpressionFactory.Convert(
elementProjection,
elementClrType.UnwrapNullableType(),
elementTypeMapping);
}
selectExpression = new SelectExpression(
[
new PgTableValuedFunctionExpression(
tableAlias,
storeType switch
{
"jsonb" => "jsonb_array_elements_text",
"json" => "json_array_elements_text",
_ => throw new UnreachableException()
},
[sqlExpression],
columnInfos: [new("element")],
withOrdinality: true)
],
elementProjection,
identifier: [(ordinalityColumn, ordinalityComparer)],
_queryCompilationContext.SqlAliasManager);
selectExpression.AppendOrdering(new OrderingExpression(ordinalityColumn, ascending: true));
break;
}
default:
throw new UnreachableException();
}
#pragma warning restore EF1001 // SelectExpression constructors are pubternal
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), elementClrType.MakeNullable());
if (elementClrType != shaperExpression.Type)
{
Check.DebugAssert(
elementClrType.MakeNullable() == shaperExpression.Type,
"expression.Type must be nullable of targetType");
shaperExpression = Expression.Convert(shaperExpression, elementClrType);
}
return new ShapedQueryExpression(selectExpression, shaperExpression);
}
///
/// 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.
///
protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpression jsonQueryExpression)
{
// Calculate the table alias for the jsonb_to_recordset function based on the last named path segment
// (or the JSON column name if there are none)
var lastNamedPathSegment = jsonQueryExpression.Path.LastOrDefault(ps => ps.PropertyName is not null);
var tableAlias =
_queryCompilationContext.SqlAliasManager.GenerateTableAlias(
lastNamedPathSegment.PropertyName ?? jsonQueryExpression.JsonColumn.Name);
// TODO: This relies on nested JSON columns flowing across the type mapping of the top-most containing JSON column, check this.
var functionName = jsonQueryExpression.JsonColumn switch
{
{ TypeMapping.StoreType: "jsonb" } => "jsonb_to_recordset",
{ TypeMapping.StoreType: "json" } => "json_to_recordset",
{ TypeMapping: null } => throw new UnreachableException("Missing type mapping on JSON column"),
_ => throw new UnreachableException()
};
var jsonTypeMapping = jsonQueryExpression.JsonColumn.TypeMapping!;
Check.DebugAssert(jsonTypeMapping is NpgsqlStructuralJsonTypeMapping, "JSON column has a non-JSON mapping");
// We now add all of projected entity's the properties and navigations into the jsonb_to_recordset's AS clause, which defines the
// names and types of columns to come out of the JSON fragments.
var columnInfos = new List();
// We're only interested in properties which actually exist in the JSON, filter out uninteresting shadow keys
foreach (var property in jsonQueryExpression.StructuralType.GetPropertiesInHierarchy())
{
if (property.GetJsonPropertyName() is string jsonPropertyName)
{
columnInfos.Add(
new PgTableValuedFunctionExpression.ColumnInfo
{
Name = jsonPropertyName, TypeMapping = property.GetRelationalTypeMapping()
});
}
}
switch (jsonQueryExpression.StructuralType)
{
case IEntityType entityType:
foreach (var navigation in entityType.GetNavigationsInHierarchy()
.Where(n => n.ForeignKey.IsOwnership
&& n.TargetEntityType.IsMappedToJson()
&& n.ForeignKey.PrincipalToDependent == n))
{
var jsonNavigationName = navigation.TargetEntityType.GetJsonPropertyName();
Check.DebugAssert(jsonNavigationName is not null, $"No JSON property name for navigation {navigation.Name}");
columnInfos.Add(
new PgTableValuedFunctionExpression.ColumnInfo { Name = jsonNavigationName, TypeMapping = jsonTypeMapping });
}
break;
case IComplexType complexType:
foreach (var complexProperty in complexType.GetComplexProperties())
{
var jsonPropertyName = complexProperty.ComplexType.GetJsonPropertyName();
Check.DebugAssert(jsonPropertyName is not null, $"No JSON property name for complex property {complexProperty.Name}");
columnInfos.Add(
new PgTableValuedFunctionExpression.ColumnInfo { Name = jsonPropertyName, TypeMapping = jsonTypeMapping });
}
break;
default:
throw new UnreachableException();
}
// json_to_recordset requires the nested JSON document - it does not accept a path within a containing JSON document (like SQL
// Server OPENJSON or SQLite json_each). So we wrap json_to_recordset around a JsonScalarExpression which will extract the nested
// document.
var jsonScalarExpression = new JsonScalarExpression(
jsonQueryExpression.JsonColumn, jsonQueryExpression.Path, typeof(string), jsonTypeMapping, jsonQueryExpression.IsNullable);
// Construct the json_to_recordset around the JsonScalarExpression, and wrap it in a SelectExpression
var jsonToRecordSetExpression = new PgTableValuedFunctionExpression(
tableAlias, functionName, [jsonScalarExpression], columnInfos, withOrdinality: true);
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var selectExpression = CreateSelect(
jsonQueryExpression,
jsonToRecordSetExpression,
"ordinality",
typeof(int),
_typeMappingSource.FindMapping(typeof(int))!);
#pragma warning restore EF1001 // Internal EF Core API usage.
return new ShapedQueryExpression(
selectExpression,
new RelationalStructuralTypeShaperExpression(
jsonQueryExpression.StructuralType,
new ProjectionBindingExpression(
selectExpression,
new ProjectionMember(),
typeof(ValueBuffer)),
false));
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateAll(ShapedQueryExpression source, LambdaExpression predicate)
{
if ((source.TryExtractArray(out var array, ignoreOrderings: true)
|| source.TryConvertToArray(out array, ignoreOrderings: true))
&& source.QueryExpression is SelectExpression { Tables: [{ Alias: var tableAlias }] }
&& TranslateLambdaExpression(source, predicate) is { } translatedPredicate)
{
switch (translatedPredicate)
{
case PgBinaryExpression
{
OperatorType: PgExpressionType.Contains,
Left: { TypeMapping: NpgsqlHstoreTypeMapping } hstore,
Right: ColumnExpression key
}
when key.TableAlias == tableAlias
// Rewriting All to ?& removes the iterated key's scope. If the hstore depends on that key
// (e.g. tags.Remove(key)), keep the correlated subquery instead.
&& !new OuterReferenceFindingExpressionVisitor(tableAlias).ContainsReferenceToMainTable(hstore):
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.Contains(hstore, array));
// Pattern match for: new[] { "a", "b", "c" }.All(p => EF.Functions.Like(e.SomeText, p)),
// which we translate to WHERE s.""SomeText"" LIKE ALL (ARRAY['a','b','c'])
case LikeExpression
{
Match: var match,
Pattern: ColumnExpression pattern,
EscapeChar: SqlConstantExpression { Value: "" }
}
when pattern.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.All(match, array, PgAllOperatorType.Like));
}
// Pattern match for: new[] { "a", "b", "c" }.All(p => EF.Functions.Like(e.SomeText, p)),
// which we translate to WHERE s.""SomeText"" LIKE ALL (ARRAY['a','b','c'])
case PgILikeExpression
{
Match: var match,
Pattern: ColumnExpression pattern,
EscapeChar: SqlConstantExpression { Value: "" }
}
when pattern.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.All(match, array, PgAllOperatorType.ILike));
}
// Pattern match for: e.SomeArray.All(p => ints.Contains(p)) over non-column,
// using array containment (<@)
case PgAnyExpression
{
Item: ColumnExpression sourceColumn,
Array: var otherArray
}
when sourceColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.ContainedBy(array, otherArray));
}
// Pattern match for: new[] { 4, 5 }.All(p => e.SomeArray.Contains(p)) over column,
// using array containment (<@)
case PgBinaryExpression
{
OperatorType: PgExpressionType.Contains,
Left: var otherArray,
Right: PgNewArrayExpression { Expressions: [ColumnExpression sourceColumn] }
}
when sourceColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.ContainedBy(array, otherArray));
}
}
}
return base.TranslateAll(source, predicate);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateAny(ShapedQueryExpression source, LambdaExpression? predicate)
{
if (source.QueryExpression is SelectExpression { Tables: [{ Alias: var tableAlias }] })
{
// Pattern match: x.JsonArray.Any()
// Translation: jsonb_array_length(x.json_array) > 0 instead of EXISTS (SELECT 1 FROM FROM jsonb_array_elements(x.Array))
if (predicate is null && source.TryExtractJsonArray(out var jsonArray, out _, out _, ignoreOrderings: true))
{
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.GreaterThan(
_sqlExpressionFactory.Function(
jsonArray.TypeMapping!.StoreType switch
{
"jsonb" => "jsonb_array_length",
"json" => "json_array_length",
_ => throw new UnreachableException()
},
[jsonArray],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(int)),
_sqlExpressionFactory.Constant(0)));
}
if (source.TryExtractArray(out var array, ignoreOrderings: true)
|| source.TryConvertToArray(out array, ignoreOrderings: true))
{
// Pattern match: x.Array.Any()
// Translation: cardinality(x.array) > 0 instead of EXISTS (SELECT 1 FROM FROM unnest(x.Array))
if (predicate is null)
{
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.GreaterThan(
_sqlExpressionFactory.Function(
"cardinality",
[array],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(int)),
_sqlExpressionFactory.Constant(0)));
}
if (TranslateLambdaExpression(source, predicate) is not SqlExpression translatedPredicate)
{
return null;
}
switch (translatedPredicate)
{
// Pattern match: new[] { "a", "b", "c" }.Any(p => EF.Functions.Like(e.SomeText, p))
// Translation: s.SomeText LIKE ANY (ARRAY['a','b','c'])
case LikeExpression
{
Match: var match,
Pattern: ColumnExpression pattern,
EscapeChar: SqlConstantExpression { Value: "" }
}
when pattern.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source, _sqlExpressionFactory.Any(match, array, PgAnyOperatorType.Like));
}
// Pattern match: new[] { "a", "b", "c" }.Any(p => EF.Functions.Like(e.SomeText, p))
// Translation: s.SomeText LIKE ANY (ARRAY['a','b','c'])
case PgILikeExpression
{
Match: var match,
Pattern: ColumnExpression pattern,
EscapeChar: SqlConstantExpression { Value: "" }
}
when pattern.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source, _sqlExpressionFactory.Any(match, array, PgAnyOperatorType.ILike));
}
case PgBinaryExpression
{
OperatorType: PgExpressionType.Contains,
Left: { TypeMapping: NpgsqlHstoreTypeMapping } hstore,
Right: ColumnExpression key
}
when key.TableAlias == tableAlias
// Rewriting Any to ?| removes the iterated key's scope. If the hstore depends on that key
// (e.g. tags.Remove(key)), keep the correlated subquery instead.
&& !new OuterReferenceFindingExpressionVisitor(tableAlias).ContainsReferenceToMainTable(hstore):
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.Overlaps(hstore, array));
// Array overlap over non-column
// Pattern match: e.SomeArray.Any(p => ints.Contains(p))
// Translation: @ints && s.SomeArray
case PgAnyExpression
{
Item: ColumnExpression sourceColumn,
Array: var otherArray
}
when sourceColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.Overlaps(array, otherArray));
}
// Array overlap over column
// Pattern match: new[] { 4, 5 }.Any(p => e.SomeArray.Contains(p))
// Translation: s.SomeArray && ARRAY[4, 5]
case PgBinaryExpression
{
OperatorType: PgExpressionType.Contains,
Left: var otherArray,
Right: PgNewArrayExpression { Expressions: [ColumnExpression sourceColumn] }
}
when sourceColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.Overlaps(array, otherArray));
}
#region LTree translations
// Pattern match: new[] { "q1", "q2" }.Any(q => e.SomeLTree.MatchesLQuery(q))
// Translation: s.SomeLTree ? ARRAY['q1','q2']
case PgBinaryExpression
{
OperatorType: PgExpressionType.LTreeMatches,
Left: var ltree,
Right: SqlUnaryExpression { OperatorType: ExpressionType.Convert, Operand: ColumnExpression lqueryColumn }
}
when lqueryColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
PgExpressionType.LTreeMatchesAny,
ltree,
_sqlExpressionFactory.ApplyTypeMapping(array, _typeMappingSource.FindMapping("lquery[]")),
typeof(bool),
typeMapping: _typeMappingSource.FindMapping(typeof(bool))));
}
// Pattern match: new[] { "t1", "t2" }.Any(t => t.IsAncestorOf(e.SomeLTree))
// Translation: ARRAY['t1','t2'] @> s.SomeLTree
// Pattern match: new[] { "t1", "t2" }.Any(t => t.IsDescendantOf(e.SomeLTree))
// Translation: ARRAY['t1','t2'] <@ s.SomeLTree
case PgBinaryExpression
{
OperatorType: (PgExpressionType.Contains or PgExpressionType.ContainedBy) and var operatorType,
Left: ColumnExpression ltreeColumn,
// Contains/ContainedBy can happen for non-LTree types too, so check that
Right: { TypeMapping: NpgsqlLTreeTypeMapping } ltree
}
when ltreeColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
operatorType,
_sqlExpressionFactory.ApplyDefaultTypeMapping(array),
ltree,
typeof(bool),
typeMapping: _typeMappingSource.FindMapping(typeof(bool))));
}
// Pattern match: new[] { "t1", "t2" }.Any(t => t.MatchesLQuery(lquery))
// Translation: ARRAY['t1','t2'] ~ lquery
// Pattern match: new[] { "t1", "t2" }.Any(t => t.MatchesLTxtQuery(ltxtquery))
// Translation: ARRAY['t1','t2'] @ ltxtquery
case PgBinaryExpression
{
OperatorType: PgExpressionType.LTreeMatches,
Left: ColumnExpression ltreeColumn,
Right: var lquery
}
when ltreeColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
PgExpressionType.LTreeMatches,
_sqlExpressionFactory.ApplyDefaultTypeMapping(array),
lquery,
typeof(bool),
typeMapping: _typeMappingSource.FindMapping(typeof(bool))));
}
// Any within Any (i.e. intersection)
// Pattern match: ltrees.Any(t => lqueries.Any(q => t.MatchesLQuery(q)))
// Translate: ltrees ? lqueries
case PgBinaryExpression
{
OperatorType: PgExpressionType.LTreeMatchesAny,
Left: ColumnExpression ltreeColumn,
Right: var lqueries
}
when ltreeColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
PgExpressionType.LTreeMatchesAny,
_sqlExpressionFactory.ApplyDefaultTypeMapping(array),
lqueries,
typeof(bool),
typeMapping: _typeMappingSource.FindMapping(typeof(bool))));
}
#endregion LTree translations
}
}
}
// Pattern match: x.Array1.Intersect(x.Array2).Any()
// Translation: x.Array1 && x.Array2
if (predicate is null
&& source.QueryExpression is SelectExpression
{
Tables:
[
IntersectExpression
{
Source1:
{
Projection: [{ Expression: ColumnExpression projectedColumn1 }],
Tables: [var table1],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Limit: null,
Offset: null
} source1,
Source2:
{
Projection: [{ Expression: ColumnExpression projectedColumn2 }],
Tables: [var table2],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Limit: null,
Offset: null
} source2
}
],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Limit: null,
Offset: null
}
&& projectedColumn1.TableAlias == table1.Alias
&& projectedColumn2.TableAlias == table2.Alias
// Unlike INTERSECT, array overlap doesn't match null elements.
&& (!projectedColumn1.IsNullable || !projectedColumn2.IsNullable)
&& (table1 is PgUnnestExpression { Array: { TypeMapping: not NpgsqlMultirangeTypeMapping } array1 }
|| (projectedColumn1 is { Name: "Value", IsNullable: false }
&& source1.TryConvertToArray(out array1, ignoreOrderings: true)))
&& (table2 is PgUnnestExpression { Array: { TypeMapping: not NpgsqlMultirangeTypeMapping } array2 }
|| (projectedColumn2 is { Name: "Value", IsNullable: false }
&& source2.TryConvertToArray(out array2, ignoreOrderings: true)))
&& (array1.TypeMapping is not null || array2.TypeMapping is not null))
{
return BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.Overlaps(array1, array2));
}
return base.TranslateAny(source, predicate);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateContains(ShapedQueryExpression source, Expression item)
{
switch (source)
{
// Contains over array, e.g. x = ANY(y.Array), or y.Array @> ARRAY[x]
// Note that most other simplifications convert ValuesExpression to unnest over array constructor, but we avoid doing that
// here for Contains, since the relational translation for ValuesExpression is better.
case var _ when source.TryExtractArray(out var array, ignoreOrderings: true)
&& TranslateExpression(item, applyDefaultTypeMapping: false) is SqlExpression translatedItem:
{
(translatedItem, array) = _sqlExpressionFactory.ApplyTypeMappingsOnItemAndArray(translatedItem, array);
// We special-case null constant item and use array_position instead, since it does
// nulls correctly (but doesn't use indexes).
// TODO: Better just translate to ANY and handle in nullability processing?
// TODO: once lambda-based caching is implemented, move this to NpgsqlSqlNullabilityProcessor
// (https://github.com/dotnet/efcore/issues/17598) and do for parameters as well.
if (translatedItem is SqlConstantExpression { Value: null })
{
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.IsNotNull(
_sqlExpressionFactory.Function(
"array_position",
[array, translatedItem],
nullable: true,
argumentsPropagateNullability: FalseArrays[2],
typeof(int))));
}
return array switch
{
// For array columns which have a GIN index, we translate to array containment (with @>) which uses that index.
ColumnExpression { Column: IColumnBase column }
when column.PropertyMappings.Any(
m => m.Property.GetContainingIndexes().Any(
i => i.Properties.Count > 0
&& i.Properties[0] == m.Property
&& i.GetMethod()?.Equals("GIN", StringComparison.OrdinalIgnoreCase) == true))
=> BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.Contains(
array,
_sqlExpressionFactory.NewArrayOrConstant([translatedItem], array.Type, array.TypeMapping))),
// For constant arrays (new[] { 1, 2, 3 }) or inline arrays (new[] { 1, param, 3 }), don't do anything PG-specific for since
// the general EF Core mechanism is fine for that case: item IN (1, 2, 3).
SqlConstantExpression or PgNewArrayExpression
=> base.TranslateContains(source, item),
// Similar to ParameterExpression below, but when a bare subquery is present inside ANY(), PostgreSQL just compares
// against each of its resulting rows (just like IN). To "extract" the array result of the scalar subquery, we need
// to add an explicit cast (see #1803). This is handled in the SQL generator (VisitArrayAny) rather than via
// SqlUnaryExpression(Convert), since it's a same-type cast that would be removed by EF's simplifier.
ScalarSubqueryExpression subqueryExpression
=> BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.Any(
translatedItem,
subqueryExpression,
PgAnyOperatorType.Equal)),
// For ParameterExpression, and for all other cases - e.g. array returned from some function -
// translate to e.SomeText = ANY (@p). This is superior to the general solution which will expand
// parameters to constants, since non-PG SQL does not support arrays.
// Note that this will allow indexes on the item to be used.
_ => BuildSimplifiedShapedQuery(source, _sqlExpressionFactory.Any(translatedItem, array, PgAnyOperatorType.Equal))
};
}
// Contains over JSON array: array ? 'foo' for text, array @> item for bool/numeric
case var _ when source.TryExtractJsonArray(out var array, out _, out var isElementNullable, ignoreOrderings: true)
&& TranslateExpression(item, applyDefaultTypeMapping: false) is SqlExpression translatedItem:
{
(translatedItem, array) = _sqlExpressionFactory.ApplyTypeMappingsOnItemAndArray(translatedItem, array);
// TODO: Null semantics - need to coalesce NULL on the item side to 'null'::jsonb.
// TODO: For constant/parameter, use JsonValueReaderWriter to produce the JSON value client-side
return BuildSimplifiedShapedQuery(
source,
_sqlExpressionFactory.Contains(
array,
_sqlExpressionFactory.Function(
"to_jsonb",
[translatedItem is SqlConstantExpression { Value: string }
? _sqlExpressionFactory.Convert(translatedItem, typeof(string), _typeMappingSource.FindMapping(typeof(string)))
: translatedItem],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(string),
_typeMappingSource.FindMapping("jsonb"))));
}
}
return base.TranslateContains(source, item);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateCount(ShapedQueryExpression source, LambdaExpression? predicate)
{
// Simplify x.Array.Count() => cardinality(x.Array) instead of SELECT COUNT(*) FROM unnest(x.Array)
if (predicate is null)
{
switch (source)
{
case var _ when source.TryExtractArray(out var array, ignoreOrderings: true):
{
var translation = _sqlExpressionFactory.Function(
"cardinality",
[array],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(int));
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
return source.Update(
new SelectExpression(translation, _queryCompilationContext.SqlAliasManager),
Expression.Convert(
new ProjectionBindingExpression(source.QueryExpression, new ProjectionMember(), typeof(int?)),
typeof(int)));
#pragma warning restore EF1001
}
case var _ when source.TryExtractJsonArray(out var jsonArray, out _, out _, ignoreOrderings: true):
{
var translation = _sqlExpressionFactory.Function(
jsonArray.TypeMapping!.StoreType switch
{
"jsonb" => "jsonb_array_length",
"json" => "json_array_length",
_ => throw new UnreachableException()
},
[jsonArray],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(int));
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
return source.Update(
new SelectExpression(translation, _queryCompilationContext.SqlAliasManager),
Expression.Convert(
new ProjectionBindingExpression(source.QueryExpression, new ProjectionMember(), typeof(int?)),
typeof(int)));
#pragma warning restore EF1001
}
}
}
return base.TranslateCount(source, predicate);
}
///
/// 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.
///
protected override ShapedQueryExpression TranslateConcat(ShapedQueryExpression source1, ShapedQueryExpression source2)
{
// Simplify x.Array.Concat(y.Array) => x.Array || y.Array instead of:
// SELECT u.value FROM unnest(x.Array) UNION ALL SELECT u.value FROM unnest(y.Array)
if (source1.TryExtractArray(out var array1, out var projectedColumn1)
&& source2.TryExtractArray(out var array2, out var projectedColumn2))
{
Check.DebugAssert(projectedColumn1.Type == projectedColumn2.Type, "projectedColumn1.Type == projectedColumn2.Type");
Check.DebugAssert(
projectedColumn1.TypeMapping is not null || projectedColumn2.TypeMapping is not null,
"Concat with no type mapping on either side (operation should be client-evaluated over parameters/constants");
// TODO: Conflicting type mappings from both sides?
var inferredTypeMapping = projectedColumn1.TypeMapping ?? projectedColumn2.TypeMapping;
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var tableAlias = ((SelectExpression)source1.QueryExpression).Tables.Single().Alias!;
var selectExpression = new SelectExpression(
[new PgUnnestExpression(
tableAlias, _sqlExpressionFactory.MakePostgresBinary(PgExpressionType.ArrayConcat, array1, array2), "value")],
new ColumnExpression("value", tableAlias, projectedColumn1.Type, inferredTypeMapping, projectedColumn1.IsNullable || projectedColumn2.IsNullable),
identifier: [GenerateOrdinalityIdentifier(tableAlias)],
_queryCompilationContext.SqlAliasManager);
#pragma warning restore EF1001 // Internal EF Core API usage.
// TODO: Simplify by using UpdateQueryExpression after https://github.com/dotnet/efcore/issues/31511
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), source1.ShaperExpression.Type.MakeNullable());
if (source1.ShaperExpression.Type != shaperExpression.Type)
{
Check.DebugAssert(
source1.ShaperExpression.Type.MakeNullable() == shaperExpression.Type,
"expression.Type must be nullable of targetType");
shaperExpression = Expression.Convert(shaperExpression, source1.ShaperExpression.Type);
}
return new ShapedQueryExpression(selectExpression, shaperExpression);
}
return base.TranslateConcat(source1, source2);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateElementAtOrDefault(
ShapedQueryExpression source,
Expression index,
bool returnDefault)
{
if (!returnDefault && TranslateExpression(index) is { } translatedIndex)
{
#pragma warning disable EF1001 // SelectExpression constructors are pubternal
switch (source)
{
// Simplify x.Array[1] => x.Array[1] (using the PG array subscript operator) instead of a subquery with LIMIT/OFFSET
// Note that we have unnest over multiranges, not just arrays - but multiranges don't support subscripting/slicing.
case var _ when source.TryExtractArray(out var array, out var projectedColumn):
{
return source.UpdateQueryExpression(
new SelectExpression(
_sqlExpressionFactory.ArrayIndex(
array,
// Note that PostgreSQL arrays are 1-based, so adjust the index.
GenerateOneBasedIndexExpression(translatedIndex), projectedColumn.IsNullable),
_queryCompilationContext.SqlAliasManager));
}
case var _ when source.TryExtractJsonArray(out var jsonArray, out var projectedElement, out var isElementNullable):
{
var (json, path) = jsonArray is JsonScalarExpression innerJsonScalarExpression
? (innerJsonScalarExpression.Json, innerJsonScalarExpression.Path.Append(new(translatedIndex)).ToArray())
: (jsonArray, [new(translatedIndex)]);
var translation = new JsonScalarExpression(
json,
path,
projectedElement.Type,
projectedElement.TypeMapping,
isElementNullable);
return source.UpdateQueryExpression(new SelectExpression(translation, _queryCompilationContext.SqlAliasManager));
}
}
#pragma warning restore EF1001 // SelectExpression constructors are pubternal
}
return base.TranslateElementAtOrDefault(source, index, returnDefault);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateFirstOrDefault(
ShapedQueryExpression source,
LambdaExpression? predicate,
Type returnType,
bool returnDefault)
{
// Some LTree translations (see LTreeQueryTest)
// Note that preprocessing normalizes FirstOrDefault(predicate) to Where(predicate).FirstOrDefault(), so the source's
// select expression should already contain our predicate.
if ((source.TryExtractArray(out var array, ignorePredicate: true)
|| source.TryConvertToArray(out array, ignorePredicate: true))
&& source.QueryExpression is SelectExpression { Tables: [{ Alias: var tableAlias }], Predicate: var translatedPredicate }
&& translatedPredicate is null ^ predicate is null)
{
if (translatedPredicate is null)
{
translatedPredicate = TranslateLambdaExpression(source, predicate!);
if (translatedPredicate is null)
{
return null;
}
}
switch (translatedPredicate)
{
// Pattern match: new[] { "t1", "t2" }.FirstOrDefault(t => t.IsAncestorOf(e.SomeLTree))
// Translation: ARRAY['t1','t2'] ?@> e.SomeLTree
// Pattern match: new[] { "t1", "t2" }.FirstOrDefault(t => t.IsDescendant(e.SomeLTree))
// Translation: ARRAY['t1','t2'] ?<@ e.SomeLTree
case PgBinaryExpression
{
OperatorType: (PgExpressionType.Contains or PgExpressionType.ContainedBy) and var operatorType,
Left: ColumnExpression ltreeColumn,
// Contains/ContainedBy can happen for non-LTree types too, so check that
Right: { TypeMapping: NpgsqlLTreeTypeMapping } ltree
}
when ltreeColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
operatorType == PgExpressionType.Contains
? PgExpressionType.LTreeFirstAncestor
: PgExpressionType.LTreeFirstDescendent,
_sqlExpressionFactory.ApplyDefaultTypeMapping(array),
ltree,
typeof(LTree),
_typeMappingSource.FindMapping(typeof(LTree))));
}
// Pattern match: new[] { "t1", "t2" }.FirstOrDefault(t => t.MatchesLQuery(lquery))
// Translation: ARRAY['t1','t2'] ?~ e.lquery
// Pattern match: new[] { "t1", "t2" }.FirstOrDefault(t => t.MatchesLQuery(ltxtquery))
// Translation: ARRAY['t1','t2'] ?@ e.ltxtquery
case PgBinaryExpression
{
OperatorType: PgExpressionType.LTreeMatches,
Left: ColumnExpression ltreeColumn,
Right: var lquery
}
when ltreeColumn.TableAlias == tableAlias:
{
return BuildSimplifiedShapedQuery(
source,
new PgBinaryExpression(
PgExpressionType.LTreeFirstMatches,
_sqlExpressionFactory.ApplyDefaultTypeMapping(array),
lquery,
typeof(LTree),
_typeMappingSource.FindMapping(typeof(LTree))));
}
}
}
return base.TranslateFirstOrDefault(source, predicate, returnType, returnDefault);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateSkip(ShapedQueryExpression source, Expression count)
{
// Translate Skip over array to the PostgreSQL slice operator (array.Skip(2) -> array[3,])
// Note that we have unnest over multiranges, not just arrays - but multiranges don't support subscripting/slicing.
if (source.TryExtractArray(out var array, out var projectedColumn)
&& TranslateExpression(count) is { } translatedCount)
{
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var tableAlias = ((SelectExpression)source.QueryExpression).Tables[0].Alias!;
var selectExpression = new SelectExpression(
[
new PgUnnestExpression(
tableAlias,
_sqlExpressionFactory.ArraySlice(
array,
lowerBound: GenerateOneBasedIndexExpression(translatedCount),
upperBound: null,
// isColumnNullable: /*projectedColumn.IsNullable*/ true, // TODO: This fails because of a shaper check
projectedColumn.IsNullable),
"value"),
],
new ColumnExpression("value", tableAlias, projectedColumn.Type, projectedColumn.TypeMapping, projectedColumn.IsNullable),
identifier: [GenerateOrdinalityIdentifier(tableAlias)],
_queryCompilationContext.SqlAliasManager);
#pragma warning restore EF1001
// TODO: Simplify by using UpdateQueryExpression after https://github.com/dotnet/efcore/issues/31511
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), source.ShaperExpression.Type.MakeNullable());
if (source.ShaperExpression.Type != shaperExpression.Type)
{
Check.DebugAssert(
source.ShaperExpression.Type.MakeNullable() == shaperExpression.Type,
"expression.Type must be nullable of targetType");
shaperExpression = Expression.Convert(shaperExpression, source.ShaperExpression.Type);
}
return new ShapedQueryExpression(selectExpression, shaperExpression);
}
return base.TranslateSkip(source, count);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateTake(ShapedQueryExpression source, Expression count)
{
// Translate Take over array to the PostgreSQL slice operator (array.Take(2) -> array[,2])
// Note that we have unnest over multiranges, not just arrays - but multiranges don't support subscripting/slicing.
if (source.TryExtractArray(out var array, out var projectedColumn)
&& TranslateExpression(count) is { } translatedCount)
{
PgArraySliceExpression sliceExpression;
// If Skip has been called before, an array slice expression is already there; try to integrate this Take into it.
// Note that we need to take the Skip (lower bound) into account for the Take (upper bound), since the slice upper bound
// operates on the original array (Skip hasn't yet taken place).
if (array is PgArraySliceExpression existingSliceExpression)
{
if (existingSliceExpression is
{
LowerBound: SqlConstantExpression { Value: int lowerBoundValue } lowerBound,
UpperBound: null
})
{
sliceExpression = existingSliceExpression.Update(
existingSliceExpression.Array,
existingSliceExpression.LowerBound,
translatedCount is SqlConstantExpression { Value: int takeCount }
? _sqlExpressionFactory.Constant(lowerBoundValue + takeCount - 1, lowerBound.TypeMapping)
: _sqlExpressionFactory.Subtract(
_sqlExpressionFactory.Add(lowerBound, translatedCount),
_sqlExpressionFactory.Constant(1, lowerBound.TypeMapping)));
}
else
{
// For any other case, we allow relational to translate with normal querying. For non-constant lower bounds, we could
// duplicate them into the upper bound, but that could cause expensive double evaluation.
return base.TranslateTake(source, count);
}
}
else
{
sliceExpression = _sqlExpressionFactory.ArraySlice(
array,
lowerBound: null,
upperBound: translatedCount,
projectedColumn.IsNullable);
}
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var tableAlias = ((SelectExpression)source.QueryExpression).Tables[0].Alias!;
var selectExpression = new SelectExpression(
[new PgUnnestExpression(tableAlias, sliceExpression, "value")],
new ColumnExpression("value", tableAlias, projectedColumn.Type, projectedColumn.TypeMapping, projectedColumn.IsNullable),
[GenerateOrdinalityIdentifier(tableAlias)],
_queryCompilationContext.SqlAliasManager);
#pragma warning restore EF1001 // Internal EF Core API usage.
// TODO: Simplify by using UpdateQueryExpression after https://github.com/dotnet/efcore/issues/31511
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), source.ShaperExpression.Type.MakeNullable());
if (source.ShaperExpression.Type != shaperExpression.Type)
{
Check.DebugAssert(
source.ShaperExpression.Type.MakeNullable() == shaperExpression.Type,
"expression.Type must be nullable of targetType");
shaperExpression = Expression.Convert(shaperExpression, source.ShaperExpression.Type);
}
return new ShapedQueryExpression(selectExpression, shaperExpression);
}
return base.TranslateTake(source, count);
}
///
/// 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.
///
protected override ShapedQueryExpression? TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
{
// Simplify x.Array.Where(i => i != 3) => array_remove(x.Array, 3) instead of subquery
if (predicate.Body is BinaryExpression
{
NodeType: ExpressionType.NotEqual,
Left: var left,
Right: var right
}
&& (left == predicate.Parameters[0] ? right : right == predicate.Parameters[0] ? left : null) is Expression itemToFilterOut
&& source.TryExtractArray(out var array, out var projectedColumn)
&& TranslateExpression(itemToFilterOut) is SqlExpression translatedItemToFilterOut)
{
var simplifiedTranslation = _sqlExpressionFactory.Function(
"array_remove",
[array, translatedItemToFilterOut],
nullable: true,
argumentsPropagateNullability: [true, false],
array.Type,
array.TypeMapping);
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
var tableAlias = ((SelectExpression)source.QueryExpression).Tables[0].Alias!;
var selectExpression = new SelectExpression(
[new PgUnnestExpression(tableAlias, simplifiedTranslation, "value")],
new ColumnExpression("value", tableAlias, projectedColumn.Type, projectedColumn.TypeMapping, projectedColumn.IsNullable),
[GenerateOrdinalityIdentifier(tableAlias)],
_queryCompilationContext.SqlAliasManager);
#pragma warning restore EF1001 // Internal EF Core API usage.
// TODO: Simplify by using UpdateQueryExpression after https://github.com/dotnet/efcore/issues/31511
Expression shaperExpression = new ProjectionBindingExpression(
selectExpression, new ProjectionMember(), source.ShaperExpression.Type.MakeNullable());
if (source.ShaperExpression.Type != shaperExpression.Type)
{
Check.DebugAssert(
source.ShaperExpression.Type.MakeNullable() == shaperExpression.Type,
"expression.Type must be nullable of targetType");
shaperExpression = Expression.Convert(shaperExpression, source.ShaperExpression.Type);
}
return new ShapedQueryExpression(selectExpression, shaperExpression);
}
return base.TranslateWhere(source, predicate);
}
///
/// 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.
///
protected override bool IsNaturallyOrdered(SelectExpression selectExpression)
=> selectExpression is { Tables: [PgUnnestExpression unnest, ..] }
&& (selectExpression.Orderings is []
|| selectExpression.Orderings is
[{ Expression: ColumnExpression { Name: "ordinality", TableAlias: var orderingTableAlias } }]
&& orderingTableAlias == unnest.Alias);
#region ExecuteUpdate
///
/// 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.
///
protected override bool IsValidSelectExpressionForExecuteUpdate(
SelectExpression selectExpression,
TableExpressionBase targetTable,
[NotNullWhen(true)] out TableExpression? tableExpression)
{
if (!base.IsValidSelectExpressionForExecuteUpdate(selectExpression, targetTable, out tableExpression))
{
return false;
}
// PostgreSQL doesn't support referencing the main update table from anywhere except for the UPDATE WHERE clause.
// This specifically makes it impossible to have joins which reference the main table in their predicate (ON ...).
// Because of this, we detect all such inner joins and lift their predicates to the main WHERE clause (where a reference to the
// main table is allowed) - see NpgsqlQuerySqlGenerator.VisitUpdate.
// For any other type of join which contains a reference to the main table, we return false to trigger a subquery pushdown instead.
OuterReferenceFindingExpressionVisitor? visitor = null;
for (var i = 0; i < selectExpression.Tables.Count; i++)
{
var table = selectExpression.Tables[i];
if (ReferenceEquals(table, tableExpression))
{
continue;
}
visitor ??= new OuterReferenceFindingExpressionVisitor(tableExpression.Alias);
// For inner joins, if the predicate contains a reference to the main table, NpgsqlQuerySqlGenerator will lift the predicate
// to the WHERE clause; so we only need to check the inner join's table (i.e. subquery) for such a reference.
// Cross join and cross/outer apply (lateral joins) don't have predicates, so just check the entire join for a reference to
// the main table, and switch to subquery syntax if one is found.
// Left join does have a predicate, but it isn't possible to lift it to the main WHERE clause; so also check the entire
// join.
if (table is InnerJoinExpression innerJoin)
{
table = innerJoin.Table;
}
if (visitor.ContainsReferenceToMainTable(table))
{
return false;
}
}
return true;
}
#pragma warning disable EF9002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
///
/// 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.
///
protected override bool TrySerializeScalarToJson(
JsonScalarExpression target,
SqlExpression value,
[NotNullWhen(true)] out SqlExpression? jsonValue)
{
var jsonTypeMapping = ((ColumnExpression)target.Json).TypeMapping!;
if (IsSqlNull(value))
{
jsonValue = JsonNull(jsonTypeMapping);
return true;
}
var coalesceToJsonNull = !SupportsJsonSetLax(jsonTypeMapping) && MayReturnNull(value);
if (
// The base implementation doesn't handle serializing arbitrary SQL expressions to JSON, since that's
// database-specific. In PostgreSQL we simply do this by wrapping any expression in to_jsonb().
!base.TrySerializeScalarToJson(target, value, out jsonValue)
// In addition, for string, numeric and bool, the base implementation simply returns the value as-is, since most databases allow
// passing these native types directly to their JSON partial update function. In PostgreSQL, jsonb_set() always requires jsonb,
// so we wrap those expression with to_jsonb() as well.
|| jsonValue.TypeMapping?.StoreType is not "jsonb" and not "json")
{
switch (value.TypeMapping!.StoreType)
{
case "jsonb" or "json":
jsonValue = coalesceToJsonNull ? CoalesceToJsonNull(value, jsonTypeMapping) : value;
return true;
case "bytea":
value = _sqlExpressionFactory.Function(
"encode",
[value, _sqlExpressionFactory.Constant("base64")],
nullable: true,
argumentsPropagateNullability: [true, true],
typeof(string),
_typeMappingSource.FindMapping(typeof(string))!
);
break;
}
// We now have a scalar value expression that needs to be passed to jsonb_set(), but jsonb_set() requires a json/jsonb
// argument, not e.g. text or int. So we need to wrap the argument in to_jsonb/to_json.
// Note that for structural types we always already get a jsonb/json value and have already exited above (no need for
// to_jsonb/to_json).
// One exception is if the value expression happens to be a JsonScalarExpression (e.g. copy scalar property from within
// one JSON document into another). For that case, rather than do to_jsonb(x.JsonbDoc ->> 'SomeProperty') - which extracts
// a jsonb property as text only to reconvert it back to jsonb - we just change the type mapping on the JsonScalarExpression
// to json/jsonb, in order to generate x.JsonbDoc -> 'SomeProperty' (no text extraction).
if (value is JsonScalarExpression jsonScalarValue
&& jsonScalarValue.Json.TypeMapping?.StoreType == jsonTypeMapping.StoreType)
{
jsonValue = new JsonScalarExpression(
jsonScalarValue.Json,
jsonScalarValue.Path,
jsonScalarValue.Type,
jsonTypeMapping,
jsonScalarValue.IsNullable);
if (coalesceToJsonNull)
{
jsonValue = CoalesceToJsonNull(jsonValue, jsonTypeMapping);
}
return true;
}
var valueForJsonScalar = NeedsJsonScalarTypeInference(value) ? CastForJsonScalarTypeInference(value, target) : value;
var toJson = _sqlExpressionFactory.Function(
jsonTypeMapping.StoreType switch
{
"jsonb" => "to_jsonb",
"json" => "to_json",
_ => throw new UnreachableException()
},
// Make sure PG interprets constant/nullable parameter values correctly by adding explicit typing based on the target property's
// type mapping.
// Note that we can only be here for scalar properties, for structural types we always already get a jsonb/json value
// and don't need to add to_jsonb/to_json.
[valueForJsonScalar],
nullable: true,
argumentsPropagateNullability: [true],
typeof(string),
jsonTypeMapping);
jsonValue = toJson;
}
if (coalesceToJsonNull)
{
jsonValue = CoalesceToJsonNull(jsonValue, jsonTypeMapping);
}
return true;
}
#pragma warning restore EF9002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
///
/// 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.
///
protected override SqlExpression? GenerateJsonPartialUpdateSetter(
Expression target,
SqlExpression value,
ref SqlExpression? existingSetterValue)
{
var (jsonColumn, path) = target switch
{
JsonScalarExpression j => ((ColumnExpression)j.Json, j.Path),
JsonQueryExpression j => (j.JsonColumn, j.Path),
_ => throw new UnreachableException(),
};
var jsonTypeMapping = jsonColumn.TypeMapping!;
var isNullConstant = IsJsonNull(value);
var isConstant = value is SqlConstantExpression;
var valueMayBeNull = !isNullConstant && MayReturnNull(value);
var supportsJsonSetLax = SupportsJsonSetLax(jsonTypeMapping);
value = isNullConstant
? JsonNull(jsonTypeMapping)
: supportsJsonSetLax || isConstant || !valueMayBeNull
? value
: CoalesceToJsonNull(value, jsonTypeMapping);
var useJsonSetLax = supportsJsonSetLax && (valueMayBeNull || isNullConstant);
var jsonSetTarget = existingSetterValue ?? jsonColumn;
// Hack: Rendering of JSONPATH strings happens in value generation. We can have a special expression for modify to hold the
// IReadOnlyList (just like Json{Scalar,Query}Expression), but instead we do the slight hack of packaging it
// as a constant argument; it will be unpacked and handled in SQL generation.
var jsonPath = _sqlExpressionFactory.Constant(path, RelationalTypeMapping.NullMapping);
var jsonSet = useJsonSetLax
? _sqlExpressionFactory.Function(
"jsonb_set_lax",
[jsonSetTarget, jsonPath, value],
nullable: true,
argumentsPropagateNullability: [true, true, false],
typeof(string),
jsonTypeMapping)
: _sqlExpressionFactory.Function(
jsonTypeMapping.StoreType switch
{
"jsonb" => "jsonb_set",
"json" => "json_set",
_ => throw new UnreachableException()
},
[jsonSetTarget, jsonPath, value],
nullable: true,
argumentsPropagateNullability: [true, true, !isNullConstant],
typeof(string),
jsonTypeMapping);
if (existingSetterValue is null)
{
return jsonSet;
}
else
{
existingSetterValue = jsonSet;
return null;
}
}
private SqlExpression JsonNull(RelationalTypeMapping jsonTypeMapping)
=> new SqlUnaryExpression(
ExpressionType.Convert,
_sqlExpressionFactory.Constant("null"),
jsonTypeMapping.ClrType,
jsonTypeMapping);
private SqlExpression CoalesceToJsonNull(SqlExpression value, RelationalTypeMapping jsonTypeMapping)
=> _sqlExpressionFactory.Coalesce(value, JsonNull(jsonTypeMapping), jsonTypeMapping);
private bool SupportsJsonSetLax(RelationalTypeMapping jsonTypeMapping)
=> jsonTypeMapping.StoreType == "jsonb" && !_isRedshift && _postgresVersion.AtLeast(16);
private static bool IsNullConstant(SqlExpression expression)
=> expression is SqlConstantExpression { Value: null or DBNull };
private static bool IsSqlNull(SqlExpression expression)
=> IsNullConstant(expression)
|| expression is SqlFragmentExpression { Sql: "NULL" };
private static bool IsJsonNull(SqlExpression expression)
=> IsSqlNull(expression)
|| expression is SqlFunctionExpression
{
Name: "to_jsonb" or "to_json",
Arguments: [var argument]
} && IsSqlNull(argument);
private static bool NeedsJsonScalarTypeInference(SqlExpression expression)
=> expression is SqlConstantExpression;
private static SqlExpression CastForJsonScalarTypeInference(SqlExpression expression, JsonScalarExpression target)
=> new SqlUnaryExpression(ExpressionType.Convert, expression, typeof(object), target.TypeMapping ?? expression.TypeMapping);
private static bool MayReturnNull(SqlExpression expression)
=> expression switch
{
SqlConstantExpression { Value: null or DBNull } => true,
SqlFragmentExpression { Sql: "NULL" } => true,
SqlParameterExpression { IsNullable: true } => true,
ColumnExpression { IsNullable: true } => true,
JsonScalarExpression { IsNullable: true } => true,
PgJsonTraversalExpression => true,
SqlFunctionExpression { IsNullable: true } => true,
_ => false
};
#endregion ExecuteUpdate
///
/// 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.
///
protected override bool IsValidSelectExpressionForExecuteDelete(SelectExpression selectExpression)
// The default relational behavior is to allow only single-table expressions, and the only permitted feature is a predicate.
// Here we extend this to also inner joins to tables, which we generate via the PostgreSQL-specific USING construct.
=> selectExpression is
{
Orderings: [],
Offset: null,
Limit: null,
GroupBy: [],
Having: null
}
&& selectExpression.Tables[0] is TableExpression && selectExpression.Tables.Skip(1).All(t => t is InnerJoinExpression);
// PostgreSQL unnest is guaranteed to return output rows in the same order as its input array,
// https://www.postgresql.org/docs/current/functions-array.html.
///
protected override bool IsOrdered(SelectExpression selectExpression)
=> base.IsOrdered(selectExpression)
|| selectExpression.Tables is
[PgTableValuedFunctionExpression { Name: "unnest" or "jsonb_to_recordset" or "json_to_recordset" }];
private (ColumnExpression, ValueComparer) GenerateOrdinalityIdentifier(string tableAlias)
{
_ordinalityTypeMapping ??= _typeMappingSource.FindMapping("int")!;
return (new ColumnExpression("ordinality", tableAlias, typeof(int), _ordinalityTypeMapping, nullable: false),
_ordinalityTypeMapping.Comparer);
}
///
/// PostgreSQL array indexing is 1-based. If the index happens to be a constant, just increment it. Otherwise, append a +1 in the
/// SQL.
///
private SqlExpression GenerateOneBasedIndexExpression(SqlExpression expression)
=> expression is SqlConstantExpression constant
? _sqlExpressionFactory.Constant(Convert.ToInt32(constant.Value) + 1, constant.TypeMapping)
: _sqlExpressionFactory.Add(expression, _sqlExpressionFactory.Constant(1));
#pragma warning disable EF1001 // SelectExpression constructors are currently internal
private ShapedQueryExpression BuildSimplifiedShapedQuery(ShapedQueryExpression source, SqlExpression translation)
=> source.Update(
new SelectExpression(translation, _queryCompilationContext.SqlAliasManager),
Expression.Convert(
new ProjectionBindingExpression(translation, new ProjectionMember(), typeof(bool?)), typeof(bool)));
#pragma warning restore EF1001
private sealed class OuterReferenceFindingExpressionVisitor(string mainTableAlias) : ExpressionVisitor
{
private bool _containsReference;
public bool ContainsReferenceToMainTable(Expression expression)
{
_containsReference = false;
Visit(expression);
return _containsReference;
}
[return: NotNullIfNotNull("expression")]
public override Expression? Visit(Expression? expression)
{
if (_containsReference)
{
return expression;
}
if (expression is ColumnExpression { TableAlias: var tableAlias } && tableAlias == mainTableAlias)
{
_containsReference = true;
return expression;
}
return base.Visit(expression);
}
}
}