Skip to content

Commit effdcbb

Browse files
committed
Use composable functions for modifiers instead of adding them as arguments to every query function
1 parent 7b1062a commit effdcbb

16 files changed

Lines changed: 235 additions & 434 deletions

CHANGELOG.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,3 @@
33
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

55
## [Unreleased]
6-
7-
### Added
8-
9-
- Proximity query support via `EF.Functions.Match()` with `Pdb.Proximity()`, `Pdb.ProximityRegex()`, and `Pdb.ProximityArray()`
10-
11-
### Changed
12-
13-
- Updated solution, workflow, and docs references to the `ParadeDB.EntityFrameworkCore` project and example paths

src/ParadeDB.EntityFrameworkCore/Extensions/ParadeDbFunctionsExtensions.cs

Lines changed: 0 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.EntityFrameworkCore;
33
using Microsoft.EntityFrameworkCore.Diagnostics;
44
using Microsoft.EntityFrameworkCore.Query;
5-
using ParadeDB.EntityFrameworkCore.Modifiers;
65

76
namespace ParadeDB.EntityFrameworkCore.Extensions;
87

@@ -25,70 +24,6 @@ string[] values
2524
) =>
2625
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
2726

28-
[DbFunction]
29-
public static bool MatchDisjunction<TProperty>(
30-
this DbFunctions _,
31-
TProperty property,
32-
string value,
33-
[NotParameterized] Fuzzy fuzzy
34-
) =>
35-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
36-
37-
[DbFunction]
38-
public static bool MatchDisjunction<TProperty>(
39-
this DbFunctions _,
40-
TProperty property,
41-
string[] values,
42-
[NotParameterized] Fuzzy fuzzy
43-
) =>
44-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
45-
46-
[DbFunction]
47-
public static bool MatchDisjunction<TProperty>(
48-
this DbFunctions _,
49-
TProperty property,
50-
string value,
51-
[NotParameterized] Boost boost
52-
) =>
53-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
54-
55-
[DbFunction]
56-
public static bool MatchDisjunction<TProperty>(
57-
this DbFunctions _,
58-
TProperty property,
59-
string[] values,
60-
[NotParameterized] Boost boost
61-
) =>
62-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
63-
64-
[DbFunction]
65-
public static bool MatchDisjunction<TProperty>(
66-
this DbFunctions _,
67-
TProperty property,
68-
string value,
69-
[NotParameterized] Const @const
70-
) =>
71-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
72-
73-
[DbFunction]
74-
public static bool MatchDisjunction<TProperty>(
75-
this DbFunctions _,
76-
TProperty property,
77-
string[] values,
78-
[NotParameterized] Const @const
79-
) =>
80-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
81-
82-
[DbFunction]
83-
public static bool MatchDisjunction<TProperty>(
84-
this DbFunctions _,
85-
TProperty property,
86-
string value,
87-
[NotParameterized] Fuzzy fuzzy,
88-
[NotParameterized] Boost boost
89-
) =>
90-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchDisjunction)));
91-
9227
[DbFunction]
9328
public static bool MatchConjunction<TProperty>(
9429
this DbFunctions _,
@@ -105,70 +40,6 @@ string[] values
10540
) =>
10641
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
10742

108-
[DbFunction]
109-
public static bool MatchConjunction<TProperty>(
110-
this DbFunctions _,
111-
TProperty property,
112-
string value,
113-
[NotParameterized] Fuzzy fuzzy
114-
) =>
115-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
116-
117-
[DbFunction]
118-
public static bool MatchConjunction<TProperty>(
119-
this DbFunctions _,
120-
TProperty property,
121-
string[] values,
122-
[NotParameterized] Fuzzy fuzzy
123-
) =>
124-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
125-
126-
[DbFunction]
127-
public static bool MatchConjunction<TProperty>(
128-
this DbFunctions _,
129-
TProperty property,
130-
string value,
131-
[NotParameterized] Boost boost
132-
) =>
133-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
134-
135-
[DbFunction]
136-
public static bool MatchConjunction<TProperty>(
137-
this DbFunctions _,
138-
TProperty property,
139-
string[] values,
140-
[NotParameterized] Boost boost
141-
) =>
142-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
143-
144-
[DbFunction]
145-
public static bool MatchConjunction<TProperty>(
146-
this DbFunctions _,
147-
TProperty property,
148-
string value,
149-
[NotParameterized] Const @const
150-
) =>
151-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
152-
153-
[DbFunction]
154-
public static bool MatchConjunction<TProperty>(
155-
this DbFunctions _,
156-
TProperty property,
157-
string[] values,
158-
[NotParameterized] Const @const
159-
) =>
160-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
161-
162-
[DbFunction]
163-
public static bool MatchConjunction<TProperty>(
164-
this DbFunctions _,
165-
TProperty property,
166-
string value,
167-
[NotParameterized] Fuzzy fuzzy,
168-
[NotParameterized] Boost boost
169-
) =>
170-
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MatchConjunction)));
171-
17243
[DbFunction]
17344
public static bool Phrase<TProperty>(this DbFunctions _, TProperty property, string value) =>
17445
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
@@ -177,54 +48,6 @@ public static bool Phrase<TProperty>(this DbFunctions _, TProperty property, str
17748
public static bool Phrase<TProperty>(this DbFunctions _, TProperty property, string[] values) =>
17849
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
17950

180-
[DbFunction]
181-
public static bool Phrase<TProperty>(
182-
this DbFunctions _,
183-
TProperty property,
184-
string value,
185-
[NotParameterized] Boost boost
186-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
187-
188-
[DbFunction]
189-
public static bool Phrase<TProperty>(
190-
this DbFunctions _,
191-
TProperty property,
192-
string[] values,
193-
[NotParameterized] Boost boost
194-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
195-
196-
[DbFunction]
197-
public static bool Phrase<TProperty>(
198-
this DbFunctions _,
199-
TProperty property,
200-
string value,
201-
[NotParameterized] Const @const
202-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
203-
204-
[DbFunction]
205-
public static bool Phrase<TProperty>(
206-
this DbFunctions _,
207-
TProperty property,
208-
string[] values,
209-
[NotParameterized] Const @const
210-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
211-
212-
[DbFunction]
213-
public static bool Phrase<TProperty>(
214-
this DbFunctions _,
215-
TProperty property,
216-
string value,
217-
[NotParameterized] Slop slop
218-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
219-
220-
[DbFunction]
221-
public static bool Phrase<TProperty>(
222-
this DbFunctions _,
223-
TProperty property,
224-
string[] values,
225-
[NotParameterized] Slop slop
226-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Phrase)));
227-
22851
[DbFunction]
22952
public static bool Term<TProperty>(this DbFunctions _, TProperty property, string value) =>
23053
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
@@ -233,63 +56,6 @@ public static bool Term<TProperty>(this DbFunctions _, TProperty property, strin
23356
public static bool Term<TProperty>(this DbFunctions _, TProperty property, string[] values) =>
23457
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
23558

236-
[DbFunction]
237-
public static bool Term<TProperty>(
238-
this DbFunctions _,
239-
TProperty property,
240-
string value,
241-
[NotParameterized] Fuzzy fuzzy
242-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
243-
244-
[DbFunction]
245-
public static bool Term<TProperty>(
246-
this DbFunctions _,
247-
TProperty property,
248-
string[] values,
249-
[NotParameterized] Fuzzy fuzzy
250-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
251-
252-
[DbFunction]
253-
public static bool Term<TProperty>(
254-
this DbFunctions _,
255-
TProperty property,
256-
string value,
257-
[NotParameterized] Boost boost
258-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
259-
260-
[DbFunction]
261-
public static bool Term<TProperty>(
262-
this DbFunctions _,
263-
TProperty property,
264-
string[] values,
265-
[NotParameterized] Boost boost
266-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
267-
268-
[DbFunction]
269-
public static bool Term<TProperty>(
270-
this DbFunctions _,
271-
TProperty property,
272-
string value,
273-
[NotParameterized] Const @const
274-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
275-
276-
[DbFunction]
277-
public static bool Term<TProperty>(
278-
this DbFunctions _,
279-
TProperty property,
280-
string[] values,
281-
[NotParameterized] Const @const
282-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
283-
284-
[DbFunction]
285-
public static bool Term<TProperty>(
286-
this DbFunctions _,
287-
TProperty property,
288-
string value,
289-
[NotParameterized] Fuzzy fuzzy,
290-
[NotParameterized] Boost boost
291-
) => throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Term)));
292-
29359
[DbFunction]
29460
public static float Score<TProperty>(this DbFunctions _, TProperty property) =>
29561
throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Score)));

src/ParadeDB.EntityFrameworkCore/Internal/Query/ParadeDbEvaluatableExpressionFilterPlugin.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ public bool IsEvaluatableExpression(Expression expression)
99
{
1010
if (
1111
expression is MethodCallExpression call
12-
&& typeof(PdbQuery).IsAssignableFrom(call.Method.ReturnType)
12+
&& (
13+
typeof(PdbQuery).IsAssignableFrom(call.Method.ReturnType)
14+
|| (
15+
call.Method.DeclaringType == typeof(Pdb)
16+
&& call.Method.Name
17+
is nameof(Pdb.Boost)
18+
or nameof(Pdb.Const)
19+
or nameof(Pdb.Fuzzy)
20+
or nameof(Pdb.Slop)
21+
&& call.Arguments.Count > 1
22+
)
23+
)
1324
)
1425
{
1526
return false;

src/ParadeDB.EntityFrameworkCore/Internal/Query/ParadeDbMethodCallTranslatorPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public ParadeDbMethodCallTranslatorPlugin(ISqlExpressionFactory sqlExpressionFac
99
{
1010
Translators =
1111
[
12+
new ModifierTranslator(sqlExpressionFactory),
1213
new OperatorTranslator(sqlExpressionFactory),
1314
new ScoreTranslator(sqlExpressionFactory),
1415
new SnippetTranslator(sqlExpressionFactory),
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Linq.Expressions;
2+
using System.Reflection;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Diagnostics;
5+
using Microsoft.EntityFrameworkCore.Query;
6+
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
7+
using Microsoft.EntityFrameworkCore.Storage;
8+
using ParadeDB.EntityFrameworkCore.Internal.Storage;
9+
using ParadeDB.EntityFrameworkCore.Modifiers;
10+
11+
namespace ParadeDB.EntityFrameworkCore.Internal.Query.Translators;
12+
13+
internal sealed class ModifierTranslator : IMethodCallTranslator
14+
{
15+
private readonly ISqlExpressionFactory _sqlExpressionFactory;
16+
17+
public ModifierTranslator(ISqlExpressionFactory sqlExpressionFactory)
18+
{
19+
_sqlExpressionFactory = sqlExpressionFactory;
20+
}
21+
22+
public SqlExpression? Translate(
23+
SqlExpression? instance,
24+
MethodInfo method,
25+
IReadOnlyList<SqlExpression> arguments,
26+
IDiagnosticsLogger<DbLoggerCategory.Query> logger
27+
)
28+
{
29+
if (method.DeclaringType != typeof(Pdb) || arguments.Count < 2)
30+
{
31+
return null;
32+
}
33+
34+
var typeMapping = GetTypeMapping(method.Name, arguments);
35+
36+
if (typeMapping is null)
37+
{
38+
return null;
39+
}
40+
41+
return _sqlExpressionFactory.MakeUnary(
42+
ExpressionType.Convert,
43+
_sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]),
44+
typeMapping.ClrType,
45+
typeMapping
46+
);
47+
}
48+
49+
private static RelationalTypeMapping? GetTypeMapping(
50+
string methodName,
51+
IReadOnlyList<SqlExpression> arguments
52+
) =>
53+
methodName switch
54+
{
55+
nameof(Pdb.Boost) when arguments[1] is SqlConstantExpression { Value: float factor } =>
56+
new PdbModifierTypeMapping<Boost>(new Boost(factor)),
57+
nameof(Pdb.Const) when arguments[1] is SqlConstantExpression { Value: float score } =>
58+
new PdbModifierTypeMapping<Const>(new Const(score)),
59+
nameof(Pdb.Fuzzy) when arguments[1] is SqlConstantExpression { Value: int distance } =>
60+
new PdbModifierTypeMapping<Fuzzy>(
61+
new Fuzzy(
62+
distance,
63+
arguments.Count > 2
64+
&& arguments[2] is SqlConstantExpression { Value: true },
65+
arguments.Count > 3 && arguments[3] is SqlConstantExpression { Value: true }
66+
)
67+
),
68+
nameof(Pdb.Slop) when arguments[1] is SqlConstantExpression { Value: int slop } =>
69+
new PdbModifierTypeMapping<Slop>(new Slop(slop)),
70+
_ => null,
71+
};
72+
}

0 commit comments

Comments
 (0)