-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathOtherBuiltinSystemFunctions.cs
More file actions
202 lines (175 loc) · 9.86 KB
/
OtherBuiltinSystemFunctions.cs
File metadata and controls
202 lines (175 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.SqlObjects;
internal static class OtherBuiltinSystemFunctions
{
private class RRFVisit : SqlBuiltinFunctionVisitor
{
public RRFVisit()
: base("RRF",
true,
new List<Type[]>()
{
new Type[]{typeof(double[])},
new Type[]{typeof(double[]), typeof(double[])}
})
{
}
protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if (methodCallExpression.Arguments.Count != 1 && methodCallExpression.Arguments.Count != 2)
{
throw new DocumentQueryException("Invalid Argument Count.");
}
if (methodCallExpression.Arguments[0] is NewArrayExpression argumentsExpressions)
{
ReadOnlyCollection<Expression> functionListExpression = argumentsExpressions.Expressions;
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>();
foreach (Expression argument in functionListExpression)
{
if (!(argument is MethodCallExpression functionCallExpression))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
"Expressions of type {0} is not supported as an argument to CosmosLinqExtensions.RRF. Supported expressions are method calls to {1}, {2}.",
argument.Type,
nameof(CosmosLinqExtensions.FullTextScore),
nameof(CosmosLinqExtensions.VectorDistance)));
}
if (functionCallExpression.Method.Name != nameof(CosmosLinqExtensions.FullTextScore) &&
functionCallExpression.Method.Name != nameof(CosmosLinqExtensions.VectorDistance))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
"Method {0} is not supported as an argument to CosmosLinqExtensions.RRF. Supported methods are {1}, {2}.",
functionCallExpression.Method.Name,
nameof(CosmosLinqExtensions.FullTextScore),
nameof(CosmosLinqExtensions.VectorDistance)));
}
arguments.Add(ExpressionToSql.VisitNonSubqueryScalarExpression(argument, context));
}
// Append the weight if exists
if (methodCallExpression.Arguments.Count == 2)
{
arguments.Add(ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[1], context));
}
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.RRF, arguments.ToImmutableArray());
}
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, "Method {0} is not supported with the given argument list.", methodCallExpression.Method.Name));
}
protected override SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return null;
}
}
private class FullTextScoreVisit : SqlBuiltinFunctionVisitor
{
public FullTextScoreVisit()
: base("FullTextScore",
true,
new List<Type[]>()
{
new Type[]{typeof(object), typeof(string[])}
})
{
}
protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if (methodCallExpression.Arguments.Count == 2
&& methodCallExpression.Arguments[1] is ConstantExpression stringListArgumentExpression
&& ExpressionToSql.VisitConstant(stringListArgumentExpression, context) is SqlArrayCreateScalarExpression arrayScalarExpressions)
{
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>
{
ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[0], context)
};
arguments.AddRange(arrayScalarExpressions.Items);
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.FullTextScore, arguments.ToImmutableArray());
}
return null;
}
protected override SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return null;
}
}
private class VectorDistanceVisit : SqlBuiltinFunctionVisitor
{
public VectorDistanceVisit()
: base("VectorDistance",
true,
new List<Type[]>()
{
new Type[]{typeof(float[]), typeof(float[]), typeof(bool), typeof(CosmosLinqExtensions.VectorDistanceOptions)},
new Type[]{typeof(sbyte[]), typeof(sbyte[]), typeof(bool), typeof(CosmosLinqExtensions.VectorDistanceOptions)},
new Type[]{typeof(byte[]), typeof(byte[]), typeof(bool), typeof(CosmosLinqExtensions.VectorDistanceOptions)},
})
{
}
protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if (methodCallExpression.Arguments.Count != 4) throw new ArgumentException();
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>
{
ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[0], context),
ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[1], context),
ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[2], context)
};
if (methodCallExpression.Arguments[3] is ConstantExpression optionExpression && optionExpression.Value != null)
{
JsonSerializerOptions options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
string serializedConstant = JsonSerializer.Serialize(
optionExpression.Value,
options);
arguments.Add(CosmosElement.Parse(serializedConstant).Accept(CosmosElementToSqlScalarExpressionVisitor.Singleton));
}
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.VectorDistance, arguments.ToImmutableArray());
}
protected override SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return null;
}
}
private static Dictionary<string, BuiltinFunctionVisitor> FunctionsDefinitions { get; set; }
static OtherBuiltinSystemFunctions()
{
FunctionsDefinitions = new Dictionary<string, BuiltinFunctionVisitor>
{
[nameof(CosmosLinqExtensions.DocumentId)] = new SqlBuiltinFunctionVisitor(
sqlName: "DOCUMENTID",
isStatic: true,
argumentLists: new List<Type[]>()
{
new Type[]{typeof(object)},
}),
[nameof(CosmosLinqExtensions.RRF)] = new RRFVisit(),
[nameof(CosmosLinqExtensions.FullTextScore)] = new FullTextScoreVisit(),
[nameof(CosmosLinqExtensions.VectorDistance)] = new VectorDistanceVisit(),
};
}
public static SqlScalarExpression Visit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return FunctionsDefinitions.TryGetValue(methodCallExpression.Method.Name, out BuiltinFunctionVisitor visitor)
? visitor.Visit(methodCallExpression, context)
: throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}
}
}