Skip to content

Commit

Permalink
Add JSON fragment.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Jun 15, 2024
1 parent 47c1ba4 commit d69a4d6
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 89 deletions.
13 changes: 13 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,19 @@ protected override Expression VisitSqlConstant(SqlConstantExpression sqlConstant
return sqlConstantExpression;
}

/// <summary>
/// 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.
/// </summary>
protected override Expression VisitJsonFragment(JsonFragmentExpression jsonFragmentExpression)
{
_sqlBuilder.Append(jsonFragmentExpression.Json);

return jsonFragmentExpression;
}

/// <summary>
/// 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;

/// <summary>
/// An expression that represents a JSON fragment.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public class JsonFragmentExpression(string json) : SqlExpression(typeof(string), CosmosTypeMapping.Default)
{
/// <summary>
/// The JSON string token to print in SQL tree.
/// </summary>
public virtual string Json { get; } = json;

/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
=> this;

/// <inheritdoc />
protected override void Print(ExpressionPrinter expressionPrinter)
=> expressionPrinter.Append(Json);

/// <summary>
/// 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.
/// </summary>
protected virtual bool Equals(JsonFragmentExpression other)
=> base.Equals(other)
&& Json == other.Json;

/// <inheritdoc />
public override bool Equals(object? obj)
=> !ReferenceEquals(null, obj)
&& (ReferenceEquals(this, obj)
|| obj.GetType() == GetType()
&& Equals((JsonFragmentExpression)obj));

/// <inheritdoc />
public override int GetHashCode()
=> HashCode.Combine(base.GetHashCode(), Json);
}
8 changes: 8 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/ISqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ SqlConditionalExpression Condition(
/// </summary>
SqlConstantExpression Constant(object? value, CosmosTypeMapping? typeMapping = null);

/// <summary>
/// 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.
/// </summary>
JsonFragmentExpression JsonLiteral(string json);

/// <summary>
/// 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
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,15 @@ public virtual InExpression In(SqlExpression item, SqlParameterExpression values
public virtual SqlConstantExpression Constant(object? value, CosmosTypeMapping? typeMapping = null)
=> new(Expression.Constant(value), typeMapping);

/// <summary>
/// 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.
/// </summary>
public virtual JsonFragmentExpression JsonLiteral(string json)
=> new(json);

/// <summary>
/// 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
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/SqlExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ShapedQueryExpression shapedQueryExpression
ScalarSubqueryExpression scalarSubqueryExpression => VisitScalarSubquery(scalarSubqueryExpression),
SqlBinaryExpression sqlBinaryExpression => VisitSqlBinary(sqlBinaryExpression),
SqlConstantExpression sqlConstantExpression => VisitSqlConstant(sqlConstantExpression),
JsonFragmentExpression jsonFragmentExpression => VisitJsonFragment(jsonFragmentExpression),
SqlUnaryExpression sqlUnaryExpression => VisitSqlUnary(sqlUnaryExpression),
SqlConditionalExpression sqlConditionalExpression => VisitSqlConditional(sqlConditionalExpression),
SqlParameterExpression sqlParameterExpression => VisitSqlParameter(sqlParameterExpression),
Expand Down Expand Up @@ -144,6 +145,14 @@ ShapedQueryExpression shapedQueryExpression
/// </summary>
protected abstract Expression VisitSqlConstant(SqlConstantExpression sqlConstantExpression);

/// <summary>
/// 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.
/// </summary>
protected abstract Expression VisitJsonFragment(JsonFragmentExpression jsonFragmentExpression);

/// <summary>
/// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public class CosmosVectorSearchTranslator(ISqlExpressionFactory sqlExpressionFac
sqlExpressionFactory.ApplyTypeMapping(arguments[1], vectorMapping),
sqlExpressionFactory.ApplyTypeMapping(arguments[2], vectorMapping),
bruteForce,
sqlExpressionFactory.Constant(vectorType, (CosmosTypeMapping)typeMappingSource.FindMapping(typeof(CosmosVectorType))!)
sqlExpressionFactory.JsonLiteral(
$"{{'distanceFunction':'{vectorType.DistanceFunction.ToString().ToLower()}', 'dataType':'{vectorType.DataType?.ToString().ToLower()}'}}")
],
typeof(double));
}
Expand Down
5 changes: 0 additions & 5 deletions src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ public CosmosTypeMappingSource(TypeMappingSourceDependencies dependencies)
private CoreTypeMapping? FindPrimitiveMapping(in TypeMappingInfo mappingInfo)
{
var clrType = mappingInfo.ClrType!;
if (clrType.IsAssignableTo(typeof(CosmosVectorType)))
{
return new CosmosVectorDistanceCalculationTypeMapping();
}

if ((clrType.IsValueType
&& clrType != typeof(Guid)
&& !clrType.IsEnum)
Expand Down

This file was deleted.

0 comments on commit d69a4d6

Please sign in to comment.