Skip to content

Commit 1761c68

Browse files
authored
.Net: [MEVD] Support MemoryExtensions.Contains in LINQ filters (#13263)
This makes the provider filters compatible with C# 14 (see #12504, and dotnet/runtime#109757 on the runtime side). Note that this changes the entire repo to build with dotnet SDK 10.0 (rc2) - that's probably a good idea regardless to test with the latest, ensure compatibility etc. (but if this is problematic, let me know - it's only truly necessary for test coverage and can be reverted). I'm not happy to have the implementation duplicated across the implementations: we have FilterTranslationPreprocessor for common LINQ filter preprocessing, and that would be the ideal place for this. However, rewriting MemoryExtensions.Contains to Enumerable.Contains would require doing MethodInfo.MakeGenericMethod which is incompatible with NativeAOT. I considered have a fake, non-invocable MethodInfo implementation to represent the closed generic MethodInfo to Enumerable.Contains (MethodInfo and Type are abstract, so we can create custom implementations compatible with NativeAOT), but that's quite involved/complicated. The better story here is probably to have a base class implementation for the provider filter processors, which recognizes specific patterns and dispatches to abstract handlers which specific implementations override; it would take care of recognizing the various Contains variants. Note also that we already have duplication around Enumerable.Contains and List.Contains (the instance method), so this just adds another variant (for now). I'll think about cleaning this up. Fixes #12504
1 parent 5fec3be commit 1761c68

16 files changed

Lines changed: 393 additions & 5 deletions

File tree

‎dotnet/src/VectorData/AzureAISearch/AzureAISearchFilterTranslator.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,42 @@ private void TranslateMethodCall(MethodCallExpression methodCall)
187187
this.TranslateContains(source, item);
188188
return;
189189

190+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
191+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
192+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
193+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
194+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
195+
// it's null.
196+
case { Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
197+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
198+
&& (contains.Arguments.Count is 2
199+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
200+
&& TryUnwrapSpanImplicitCast(spanArg, out var source):
201+
this.TranslateContains(source, item);
202+
return;
203+
190204
default:
191205
throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}");
192206
}
207+
208+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
209+
{
210+
if (expression is UnaryExpression
211+
{
212+
NodeType: ExpressionType.Convert,
213+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
214+
Operand: var unwrapped
215+
}
216+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
217+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
218+
{
219+
result = unwrapped;
220+
return true;
221+
}
222+
223+
result = null;
224+
return false;
225+
}
193226
}
194227

195228
private void TranslateContains(Expression source, Expression item)

‎dotnet/src/VectorData/Common/SqlFilterTranslator.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,42 @@ private void TranslateMethodCall(MethodCallExpression methodCall, bool isSearchC
239239
this.TranslateContains(source, item);
240240
return;
241241

242+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
243+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
244+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
245+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
246+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
247+
// it's null.
248+
case { Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
249+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
250+
&& (contains.Arguments.Count is 2
251+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
252+
&& TryUnwrapSpanImplicitCast(spanArg, out var source):
253+
this.TranslateContains(source, item);
254+
return;
255+
242256
default:
243257
throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}");
244258
}
259+
260+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
261+
{
262+
if (expression is UnaryExpression
263+
{
264+
NodeType: ExpressionType.Convert,
265+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
266+
Operand: var unwrapped
267+
}
268+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
269+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
270+
{
271+
result = unwrapped;
272+
return true;
273+
}
274+
275+
result = null;
276+
return false;
277+
}
245278
}
246279

247280
private void TranslateContains(Expression source, Expression item)

‎dotnet/src/VectorData/CosmosMongoDB/CosmosMongoFilterTranslator.cs‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ private BsonDocument TranslateNot(UnaryExpression not)
155155
}
156156

157157
private BsonDocument TranslateMethodCall(MethodCallExpression methodCall)
158-
=> methodCall switch
158+
{
159+
return methodCall switch
159160
{
160161
// Enumerable.Contains()
161162
{ Method.Name: nameof(Enumerable.Contains), Arguments: [var source, var item] } contains
@@ -171,11 +172,45 @@ private BsonDocument TranslateMethodCall(MethodCallExpression methodCall)
171172
},
172173
Object: Expression source,
173174
Arguments: [var item]
174-
} when declaringType.GetGenericTypeDefinition() == typeof(List<>) => this.TranslateContains(source, item),
175+
} when declaringType.GetGenericTypeDefinition() == typeof(List<>)
176+
=> this.TranslateContains(source, item),
177+
178+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
179+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
180+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
181+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
182+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
183+
// it's null.
184+
{ Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
185+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
186+
&& (contains.Arguments.Count is 2
187+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
188+
&& TryUnwrapSpanImplicitCast(spanArg, out var source)
189+
=> this.TranslateContains(source, item),
175190

176191
_ => throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}")
177192
};
178193

194+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
195+
{
196+
if (expression is UnaryExpression
197+
{
198+
NodeType: ExpressionType.Convert,
199+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
200+
Operand: var unwrapped
201+
}
202+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
203+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
204+
{
205+
result = unwrapped;
206+
return true;
207+
}
208+
209+
result = null;
210+
return false;
211+
}
212+
}
213+
179214
private BsonDocument TranslateContains(Expression source, Expression item)
180215
{
181216
switch (source)

‎dotnet/src/VectorData/CosmosNoSql/CosmosNoSqlFilterTranslator.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,42 @@ private void TranslateMethodCall(MethodCallExpression methodCall)
230230
this.TranslateContains(source, item);
231231
return;
232232

233+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
234+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
235+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
236+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
237+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
238+
// it's null.
239+
case { Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
240+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
241+
&& (contains.Arguments.Count is 2
242+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
243+
&& TryUnwrapSpanImplicitCast(spanArg, out var source):
244+
this.TranslateContains(source, item);
245+
return;
246+
233247
default:
234248
throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}");
235249
}
250+
251+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
252+
{
253+
if (expression is UnaryExpression
254+
{
255+
NodeType: ExpressionType.Convert,
256+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
257+
Operand: var unwrapped
258+
}
259+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
260+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
261+
{
262+
result = unwrapped;
263+
return true;
264+
}
265+
266+
result = null;
267+
return false;
268+
}
236269
}
237270

238271
private void TranslateContains(Expression source, Expression item)

‎dotnet/src/VectorData/Directory.Build.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
44

55
<PropertyGroup>
6+
<LangVersion>latest</LangVersion>
67
<NoWarn>$(NoWarn);MEVD9000,MEVD9001</NoWarn> <!-- Experimental MEVD connector-facing APIs -->
78
</PropertyGroup>
89

‎dotnet/src/VectorData/MongoDB/MongoFilterTranslator.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ private BsonDocument TranslateNot(UnaryExpression not)
161161
}
162162

163163
private BsonDocument TranslateMethodCall(MethodCallExpression methodCall)
164-
=> methodCall switch
164+
{
165+
return methodCall switch
165166
{
166167
// Enumerable.Contains()
167168
{ Method.Name: nameof(Enumerable.Contains), Arguments: [var source, var item] } contains
@@ -179,9 +180,42 @@ private BsonDocument TranslateMethodCall(MethodCallExpression methodCall)
179180
Arguments: [var item]
180181
} when declaringType.GetGenericTypeDefinition() == typeof(List<>) => this.TranslateContains(source, item),
181182

183+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
184+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
185+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
186+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
187+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
188+
// it's null.
189+
{ Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
190+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
191+
&& (contains.Arguments.Count is 2
192+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
193+
&& TryUnwrapSpanImplicitCast(spanArg, out var source)
194+
=> this.TranslateContains(source, item),
195+
182196
_ => throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}")
183197
};
184198

199+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
200+
{
201+
if (expression is UnaryExpression
202+
{
203+
NodeType: ExpressionType.Convert,
204+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
205+
Operand: var unwrapped
206+
}
207+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
208+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
209+
{
210+
result = unwrapped;
211+
return true;
212+
}
213+
214+
result = null;
215+
return false;
216+
}
217+
}
218+
185219
private BsonDocument TranslateContains(Expression source, Expression item)
186220
{
187221
switch (source)

‎dotnet/src/VectorData/Pinecone/PineconeFilterTranslator.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ private Metadata TranslateNot(UnaryExpression not)
158158
}
159159

160160
private Metadata TranslateMethodCall(MethodCallExpression methodCall)
161-
=> methodCall switch
161+
{
162+
return methodCall switch
162163
{
163164
// Enumerable.Contains()
164165
{ Method.Name: nameof(Enumerable.Contains), Arguments: [var source, var item] } contains
@@ -176,9 +177,42 @@ private Metadata TranslateMethodCall(MethodCallExpression methodCall)
176177
Arguments: [var item]
177178
} when declaringType.GetGenericTypeDefinition() == typeof(List<>) => this.TranslateContains(source, item),
178179

180+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
181+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
182+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
183+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
184+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
185+
// it's null.
186+
{ Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
187+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
188+
&& (contains.Arguments.Count is 2
189+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
190+
&& TryUnwrapSpanImplicitCast(spanArg, out var source)
191+
=> this.TranslateContains(source, item),
192+
179193
_ => throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}")
180194
};
181195

196+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
197+
{
198+
if (expression is UnaryExpression
199+
{
200+
NodeType: ExpressionType.Convert,
201+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
202+
Operand: var unwrapped
203+
}
204+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
205+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
206+
{
207+
result = unwrapped;
208+
return true;
209+
}
210+
211+
result = null;
212+
return false;
213+
}
214+
}
215+
182216
private Metadata TranslateContains(Expression source, Expression item)
183217
{
184218
switch (source)

‎dotnet/src/VectorData/Qdrant/QdrantFilterTranslator.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ private Filter TranslateNot(Expression expression)
267267
#endregion Logical operators
268268

269269
private Filter TranslateMethodCall(MethodCallExpression methodCall)
270-
=> methodCall switch
270+
{
271+
return methodCall switch
271272
{
272273
// Enumerable.Contains()
273274
{ Method.Name: nameof(Enumerable.Contains), Arguments: [var source, var item] } contains
@@ -286,9 +287,42 @@ private Filter TranslateMethodCall(MethodCallExpression methodCall)
286287
} when declaringType.GetGenericTypeDefinition() == typeof(List<>)
287288
=> this.TranslateContains(source, item),
288289

290+
// C# 14 made changes to overload resolution to prefer Span-based overloads when those exist ("first-class spans");
291+
// this makes MemoryExtensions.Contains() be resolved rather than Enumerable.Contains() (see above).
292+
// MemoryExtensions.Contains() also accepts a Span argument for the source, adding an implicit cast we need to remove.
293+
// See https://github.com/dotnet/runtime/issues/109757 for more context.
294+
// Note that MemoryExtensions.Contains has an optional 3rd ComparisonType parameter; we only match when
295+
// it's null.
296+
{ Method.Name: nameof(MemoryExtensions.Contains), Arguments: [var spanArg, var item, ..] } contains
297+
when contains.Method.DeclaringType == typeof(MemoryExtensions)
298+
&& (contains.Arguments.Count is 2
299+
|| (contains.Arguments.Count is 3 && contains.Arguments[2] is ConstantExpression { Value: null }))
300+
&& TryUnwrapSpanImplicitCast(spanArg, out var source)
301+
=> this.TranslateContains(source, item),
302+
289303
_ => throw new NotSupportedException($"Unsupported method call: {methodCall.Method.DeclaringType?.Name}.{methodCall.Method.Name}")
290304
};
291305

306+
static bool TryUnwrapSpanImplicitCast(Expression expression, [NotNullWhen(true)] out Expression? result)
307+
{
308+
if (expression is UnaryExpression
309+
{
310+
NodeType: ExpressionType.Convert,
311+
Method: { Name: "op_Implicit", DeclaringType: { IsGenericType: true } implicitCastDeclaringType },
312+
Operand: var unwrapped
313+
}
314+
&& implicitCastDeclaringType.GetGenericTypeDefinition() is var genericTypeDefinition
315+
&& (genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>)))
316+
{
317+
result = unwrapped;
318+
return true;
319+
}
320+
321+
result = null;
322+
return false;
323+
}
324+
}
325+
292326
private Filter TranslateContains(Expression source, Expression item)
293327
{
294328
switch (source)

0 commit comments

Comments
 (0)