Skip to content

Commit e75f2b1

Browse files
committed
split CosmosVectorType into two annotations (distance function and dimensions) so that we can keep CVT pubternal
1 parent 1b4bf13 commit e75f2b1

File tree

5 files changed

+118
-40
lines changed

5 files changed

+118
-40
lines changed

Diff for: src/EFCore.Cosmos/Extensions/CosmosPropertyBuilderExtensions.cs

+41-15
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public static PropertyBuilder IsVectorProperty(
121121
DistanceFunction distanceFunction,
122122
int dimensions)
123123
{
124-
propertyBuilder.Metadata.SetVectorType(CreateVectorType(distanceFunction, dimensions));
124+
propertyBuilder.Metadata.SetVectorDistanceFunction(ValidateVectorDistanceFunction(distanceFunction));
125+
propertyBuilder.Metadata.SetVectorDimensions(dimensions);
126+
125127
return propertyBuilder;
126128
}
127129

@@ -168,36 +170,66 @@ public static PropertyBuilder<TProperty> IsVectorProperty<TProperty>(
168170
int dimensions,
169171
bool fromDataAnnotation = false)
170172
{
171-
if (!propertyBuilder.CanSetIsVectorProperty(distanceFunction, dimensions, fromDataAnnotation))
173+
if (!propertyBuilder.CanSetVectorPropertyDistanceFunction(distanceFunction, fromDataAnnotation)
174+
|| !propertyBuilder.CanSetVectorPropertyDimensions(dimensions, fromDataAnnotation))
172175
{
173176
return null;
174177
}
175178

176-
propertyBuilder.Metadata.SetVectorType(CreateVectorType(distanceFunction, dimensions), fromDataAnnotation);
179+
180+
propertyBuilder.Metadata.SetVectorDistanceFunction(ValidateVectorDistanceFunction(distanceFunction), fromDataAnnotation);
181+
propertyBuilder.Metadata.SetVectorDimensions(dimensions, fromDataAnnotation);
177182

178183
return propertyBuilder;
179184
}
180185

181186
/// <summary>
182-
/// Returns a value indicating whether the vector type can be set.
187+
/// Returns a value indicating whether the vector distance function can be set.
183188
/// </summary>
184189
/// <remarks>
185190
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
186191
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
187192
/// </remarks>
188193
/// <param name="propertyBuilder">The builder for the property being configured.</param>
189194
/// <param name="distanceFunction">The distance function for a vector comparisons.</param>
190-
/// <param name="dimensions">The number of dimensions in the vector.</param>
191195
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
192-
/// <returns><see langword="true" /> if the vector type can be set.</returns>
193-
public static bool CanSetIsVectorProperty(
196+
/// <returns><see langword="true" /> if the vector distance function can be set.</returns>
197+
public static bool CanSetVectorPropertyDistanceFunction(
194198
this IConventionPropertyBuilder propertyBuilder,
195199
DistanceFunction distanceFunction,
200+
bool fromDataAnnotation = false)
201+
=> propertyBuilder.CanSetAnnotation(
202+
CosmosAnnotationNames.VectorDistanceFunction,
203+
ValidateVectorDistanceFunction(distanceFunction),
204+
fromDataAnnotation);
205+
206+
private static DistanceFunction ValidateVectorDistanceFunction(DistanceFunction distanceFunction)
207+
=> Enum.IsDefined(distanceFunction)
208+
? distanceFunction
209+
: throw new ArgumentException(
210+
CoreStrings.InvalidEnumValue(
211+
distanceFunction,
212+
nameof(distanceFunction),
213+
typeof(DistanceFunction)));
214+
215+
/// <summary>
216+
/// Returns a value indicating whether the vector dimensions can be set.
217+
/// </summary>
218+
/// <remarks>
219+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
220+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
221+
/// </remarks>
222+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
223+
/// <param name="dimensions">The number of dimensions in the vector.</param>
224+
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
225+
/// <returns><see langword="true" /> if the vector dimensions can be set.</returns>
226+
public static bool CanSetVectorPropertyDimensions(
227+
this IConventionPropertyBuilder propertyBuilder,
196228
int dimensions,
197229
bool fromDataAnnotation = false)
198230
=> propertyBuilder.CanSetAnnotation(
199-
CosmosAnnotationNames.VectorType,
200-
CreateVectorType(distanceFunction, dimensions),
231+
CosmosAnnotationNames.VectorDimensions,
232+
dimensions,
201233
fromDataAnnotation);
202234

203235
/// <summary>
@@ -233,12 +265,6 @@ public static PropertyBuilder<TProperty> IsETagConcurrency<TProperty>(
233265
this PropertyBuilder<TProperty> propertyBuilder)
234266
=> (PropertyBuilder<TProperty>)IsETagConcurrency((PropertyBuilder)propertyBuilder);
235267

236-
private static CosmosVectorType CreateVectorType(DistanceFunction distanceFunction, int dimensions)
237-
=> Enum.IsDefined(distanceFunction)
238-
? new CosmosVectorType(distanceFunction, dimensions)
239-
: throw new ArgumentException(
240-
CoreStrings.InvalidEnumValue(distanceFunction, nameof(distanceFunction), typeof(DistanceFunction)));
241-
242268
/// <summary>
243269
/// Enables full-text search for this property using a specified language.
244270
/// </summary>

Diff for: src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs

+61-19
Original file line numberDiff line numberDiff line change
@@ -84,46 +84,88 @@ public static void SetJsonPropertyName(this IMutableProperty property, string? n
8484
=> property.FindAnnotation(CosmosAnnotationNames.PropertyName)?.GetConfigurationSource();
8585

8686
/// <summary>
87-
/// Returns the definition of the vector stored in this property.
87+
/// Returns the distance function of the vector stored in this property.
8888
/// </summary>
8989
/// <param name="property">The property.</param>
90-
/// <returns>Returns the definition of the vector stored in this property.</returns>
91-
public static CosmosVectorType? GetVectorType(this IReadOnlyProperty property)
92-
=> (CosmosVectorType?)property[CosmosAnnotationNames.VectorType];
90+
/// <returns>Returns the distance function of the vector stored in this property.</returns>
91+
public static DistanceFunction? GetVectorDistanceFunction(this IReadOnlyProperty property)
92+
=> (DistanceFunction?)property[CosmosAnnotationNames.VectorDistanceFunction];
9393

9494
/// <summary>
95-
/// Sets the definition of the vector stored in this property.
95+
/// Returns the dimensions of the vector stored in this property.
9696
/// </summary>
9797
/// <param name="property">The property.</param>
98-
/// <param name="vectorType">The type of vector stored in the property.</param>
99-
public static void SetVectorType(this IMutableProperty property, CosmosVectorType? vectorType)
100-
=> property.SetOrRemoveAnnotation(CosmosAnnotationNames.VectorType, vectorType);
98+
/// <returns>Returns the dimensions of the vector stored in this property.</returns>
99+
public static int? GetVectorDimensions(this IReadOnlyProperty property)
100+
=> (int?)property[CosmosAnnotationNames.VectorDimensions];
101101

102102
/// <summary>
103-
/// Sets the definition of the vector stored in this property.
103+
/// Sets the distance function of the vector stored in this property.
104104
/// </summary>
105105
/// <param name="property">The property.</param>
106-
/// <param name="vectorType">The type of vector stored in the property.</param>
106+
/// <param name="distanceFunction">The distance function of the vector stored in the property.</param>
107+
public static void SetVectorDistanceFunction(this IMutableProperty property, DistanceFunction? distanceFunction)
108+
=> property.SetOrRemoveAnnotation(CosmosAnnotationNames.VectorDistanceFunction, distanceFunction);
109+
110+
/// <summary>
111+
/// Sets the dimensions of the vector stored in this property.
112+
/// </summary>
113+
/// <param name="property">The property.</param>
114+
/// <param name="dimensions">The dimensions of the vector stored in the property.</param>
115+
public static void SetVectorDimensions(this IMutableProperty property, int? dimensions)
116+
=> property.SetOrRemoveAnnotation(CosmosAnnotationNames.VectorDimensions, dimensions);
117+
118+
/// <summary>
119+
/// Sets the distance function of the vector stored in this property.
120+
/// </summary>
121+
/// <param name="property">The property.</param>
122+
/// <param name="distanceFunction">The distance function of the vector stored in the property.</param>
107123
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
108124
/// <returns>The configured value.</returns>
109-
public static CosmosVectorType? SetVectorType(
125+
public static DistanceFunction? SetVectorDistanceFunction(
110126
this IConventionProperty property,
111-
CosmosVectorType? vectorType,
127+
DistanceFunction? distanceFunction,
112128
bool fromDataAnnotation = false)
113-
=> (CosmosVectorType?)property.SetOrRemoveAnnotation(
114-
CosmosAnnotationNames.VectorType,
115-
vectorType,
129+
=> (DistanceFunction?)property.SetOrRemoveAnnotation(
130+
CosmosAnnotationNames.VectorDistanceFunction,
131+
distanceFunction,
116132
fromDataAnnotation)?.Value;
117133

118134
/// <summary>
119-
/// Gets the <see cref="ConfigurationSource" /> for the definition of the vector stored in this property.
135+
/// Sets the dimensions of the vector stored in this property.
136+
/// </summary>
137+
/// <param name="property">The property.</param>
138+
/// <param name="dimensions">The dimensions of the vector stored in the property.</param>
139+
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
140+
/// <returns>The configured value.</returns>
141+
public static int? SetVectorDimensions(
142+
this IConventionProperty property,
143+
int? dimensions,
144+
bool fromDataAnnotation = false)
145+
=> (int?)property.SetOrRemoveAnnotation(
146+
CosmosAnnotationNames.VectorDimensions,
147+
dimensions,
148+
fromDataAnnotation)?.Value;
149+
150+
/// <summary>
151+
/// Gets the <see cref="ConfigurationSource" /> for the distance function of the vector stored in this property.
152+
/// </summary>
153+
/// <param name="property">The property.</param>
154+
/// <returns>
155+
/// The <see cref="ConfigurationSource" /> for the distance function of the vector stored in this property.
156+
/// </returns>
157+
public static ConfigurationSource? GetVectorDistanceFunctionConfigurationSource(this IConventionProperty property)
158+
=> property.FindAnnotation(CosmosAnnotationNames.VectorDistanceFunction)?.GetConfigurationSource();
159+
160+
/// <summary>
161+
/// Gets the <see cref="ConfigurationSource" /> for the dimensions of the vector stored in this property.
120162
/// </summary>
121163
/// <param name="property">The property.</param>
122164
/// <returns>
123-
/// The <see cref="ConfigurationSource" /> for the definition of the vector stored in this property.
165+
/// The <see cref="ConfigurationSource" /> for the dimensions of the vector stored in this property.
124166
/// </returns>
125-
public static ConfigurationSource? GetVectorTypeConfigurationSource(this IConventionProperty property)
126-
=> property.FindAnnotation(CosmosAnnotationNames.VectorType)?.GetConfigurationSource();
167+
public static ConfigurationSource? GetVectorDimensionsConfigurationSource(this IConventionProperty property)
168+
=> property.FindAnnotation(CosmosAnnotationNames.VectorDimensions)?.GetConfigurationSource();
127169

128170
/// <summary>
129171
/// Returns the value indicating whether full-text search is enabled for this property.

Diff for: src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ protected virtual void ValidateIndexes(
584584
string.Join(",", index.Properties.Select(e => e.Name))));
585585
}
586586

587-
if (index.Properties[0].GetVectorType() == null)
587+
if (index.Properties[0].GetVectorDistanceFunction() == null
588+
|| index.Properties[0].GetVectorDimensions() == null)
588589
{
589590
throw new InvalidOperationException(
590591
CosmosStrings.VectorIndexOnNonVector(
@@ -638,8 +639,8 @@ protected override void ValidatePropertyMapping(
638639
{
639640
foreach (var property in entityType.GetDeclaredProperties())
640641
{
641-
var cosmosVectorType = property.GetVectorType();
642-
if (cosmosVectorType is not null)
642+
if (property.GetVectorDistanceFunction() is not null
643+
&& property.GetVectorDimensions() is not null)
643644
{
644645
// Will throw if the data type is not set and cannot be inferred.
645646
CosmosVectorType.CreateDefaultVectorDataType(property.ClrType);

Diff for: src/EFCore.Cosmos/Metadata/Internal/CosmosAnnotationNames.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ public static class CosmosAnnotationNames
9191
/// any release. You should only use it directly in your code with extreme caution and knowing that
9292
/// doing so can result in application failures when updating to a new Entity Framework Core release.
9393
/// </summary>
94-
public const string VectorType = Prefix + "VectorType";
94+
public const string VectorDistanceFunction = Prefix + "VectorDistanceFunction";
95+
96+
/// <summary>
97+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
98+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
99+
/// any release. You should only use it directly in your code with extreme caution and knowing that
100+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
101+
/// </summary>
102+
public const string VectorDimensions = Prefix + "VectorDimensions";
95103

96104
/// <summary>
97105
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to

Diff for: src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ public CosmosTypeMappingSource(TypeMappingSourceDependencies dependencies)
5050
// directly.
5151
=> base.FindMapping(property) switch
5252
{
53-
CosmosTypeMapping mapping when property.FindAnnotation(CosmosAnnotationNames.VectorType)?.Value is CosmosVectorType vectorType
54-
=> new CosmosVectorTypeMapping(mapping, vectorType),
53+
CosmosTypeMapping mapping
54+
when property.GetVectorDistanceFunction() is DistanceFunction distanceFunction && property.GetVectorDimensions() is int dimensions
55+
=> new CosmosVectorTypeMapping(mapping, new CosmosVectorType(distanceFunction, dimensions)),
5556
var other => other
5657
};
5758

0 commit comments

Comments
 (0)