Skip to content

Commit ccef0d4

Browse files
committed
Add Math functions to translate Floor and Ceiling
1 parent 458197e commit ccef0d4

2 files changed

Lines changed: 403 additions & 353 deletions

File tree

src/EFCore.Jet/Query/ExpressionTranslators/Internal/JetMathTranslator.cs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public JetMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
155155
)
156156
},
157157
true,
158-
new[] {true},
158+
new[] { true },
159159
method.ReturnType)),
160160

161161
// Arcsin(X) = Atn(X / Sqr(-X * X + 1))
@@ -185,21 +185,27 @@ public JetMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
185185
)
186186
},
187187
true,
188-
new[] {true},
188+
new[] { true },
189189
method.ReturnType),
190190

191191
// Logn(x) = Log(x) / Log(n)
192192
nameof(Math.Log10) => _sqlExpressionFactory.Divide(
193-
_sqlExpressionFactory.Function("LOG", new[] {arguments[0]}, true, new[] {true}, method.ReturnType),
193+
_sqlExpressionFactory.Function("LOG", new[] { arguments[0] }, true, new[] { true }, method.ReturnType),
194194
_sqlExpressionFactory.Constant(Math.Log(10))
195195
),
196196

197197
// Math.Log(x, n) //Logn(x) = Log(x) / Log(n)
198198
nameof(Math.Log) => _sqlExpressionFactory.Divide(
199-
_sqlExpressionFactory.Function("LOG", new[] {arguments[0]}, true, new[] {true}, method.ReturnType),
200-
_sqlExpressionFactory.Function("LOG", new[] {arguments[1]}, true, new[] {true}, method.ReturnType)
199+
_sqlExpressionFactory.Function("LOG", new[] { arguments[0] }, true, new[] { true }, method.ReturnType),
200+
_sqlExpressionFactory.Function("LOG", new[] { arguments[1] }, true, new[] { true }, method.ReturnType)
201201
),
202202

203+
nameof(Math.Floor) => CreateFix(arguments, method.ReturnType),
204+
nameof(Math.Ceiling) => CreateCeiling(arguments, method.ReturnType),
205+
206+
207+
208+
203209
_ => null,
204210
};
205211
}
@@ -258,5 +264,44 @@ public JetMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
258264

259265
return null;
260266
}
267+
268+
private SqlExpression CreateCeiling(IReadOnlyList<SqlExpression> arguments, Type methodReturnType)
269+
{
270+
SqlFunctionExpression fixExpression = (SqlFunctionExpression)CreateFix(arguments, methodReturnType);
271+
var addoneexp = _sqlExpressionFactory.Add(fixExpression, _sqlExpressionFactory.Constant(1));
272+
return _sqlExpressionFactory.Case(
273+
new[]
274+
{
275+
new CaseWhenClause(
276+
_sqlExpressionFactory.Equal(
277+
fixExpression,
278+
arguments[0]),
279+
fixExpression)
280+
},
281+
addoneexp);
282+
}
283+
284+
private SqlExpression CreateFix(IReadOnlyList<SqlExpression> arguments, Type methodReturnType)
285+
{
286+
var typeMapping = arguments.Count == 1
287+
? ExpressionExtensions.InferTypeMapping(arguments[0])
288+
: ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]);
289+
290+
var newArguments = new SqlExpression[arguments.Count];
291+
newArguments[0] = _sqlExpressionFactory.ApplyTypeMapping(arguments[0], typeMapping);
292+
293+
if (arguments.Count == 2)
294+
{
295+
newArguments[1] = _sqlExpressionFactory.ApplyTypeMapping(arguments[1], typeMapping);
296+
}
297+
298+
return _sqlExpressionFactory.Function(
299+
"FIX",
300+
newArguments,
301+
nullable: true,
302+
argumentsPropagateNullability: newArguments.Select(_ => true).ToArray(),
303+
methodReturnType,
304+
typeMapping);
305+
}
261306
}
262307
}

0 commit comments

Comments
 (0)