|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using EntityFrameworkCore.Ydb.Utilities; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using Microsoft.EntityFrameworkCore.Diagnostics; |
| 8 | +using Microsoft.EntityFrameworkCore.Query; |
| 9 | +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
| 10 | +using ExpressionExtensions = Microsoft.EntityFrameworkCore.Query.ExpressionExtensions; |
| 11 | + |
| 12 | +namespace EntityFrameworkCore.Ydb.Query.Internal.Translators; |
| 13 | + |
| 14 | +public class YdbMathTranslator : IMethodCallTranslator |
| 15 | +{ |
| 16 | + private static readonly Dictionary<MethodInfo, string> SupportedMethods = new() |
| 17 | + { |
| 18 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(double)])!, "Abs" }, |
| 19 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(float)])!, "Abs" }, |
| 20 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(int)])!, "Abs" }, |
| 21 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(long)])!, "Abs" }, |
| 22 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(sbyte)])!, "Abs" }, |
| 23 | + { typeof(Math).GetMethod(nameof(Math.Abs), [typeof(short)])!, "Abs" }, |
| 24 | + { typeof(Math).GetMethod(nameof(Math.Acos), [typeof(double)])!, "Acos" }, |
| 25 | + { typeof(Math).GetMethod(nameof(Math.Acosh), [typeof(double)])!, "Acosh" }, |
| 26 | + { typeof(Math).GetMethod(nameof(Math.Asin), [typeof(double)])!, "Asin" }, |
| 27 | + { typeof(Math).GetMethod(nameof(Math.Asinh), [typeof(double)])!, "Asinh" }, |
| 28 | + { typeof(Math).GetMethod(nameof(Math.Atan), [typeof(double)])!, "Atan" }, |
| 29 | + { typeof(Math).GetMethod(nameof(Math.Atan2), [typeof(double), typeof(double)])!, "Atan2" }, |
| 30 | + { typeof(Math).GetMethod(nameof(Math.Atanh), [typeof(double)])!, "Atanh" }, |
| 31 | + { typeof(Math).GetMethod(nameof(Math.Ceiling), [typeof(double)])!, "Ceil" }, |
| 32 | + { typeof(Math).GetMethod(nameof(Math.Cos), [typeof(double)])!, "Cos" }, |
| 33 | + { typeof(Math).GetMethod(nameof(Math.Cosh), [typeof(double)])!, "Cosh" }, |
| 34 | + { typeof(Math).GetMethod(nameof(Math.Exp), [typeof(double)])!, "Exp" }, |
| 35 | + { typeof(Math).GetMethod(nameof(Math.Floor), [typeof(double)])!, "Floor" }, |
| 36 | + { typeof(Math).GetMethod(nameof(Math.Log), [typeof(double)])!, "Log" }, |
| 37 | + { typeof(Math).GetMethod(nameof(Math.Log2), [typeof(double)])!, "Log2" }, |
| 38 | + { typeof(Math).GetMethod(nameof(Math.Log10), [typeof(double)])!, "Log10" }, |
| 39 | + { typeof(Math).GetMethod(nameof(Math.Pow), [typeof(double), typeof(double)])!, "Pow" }, |
| 40 | + { typeof(Math).GetMethod(nameof(Math.Round), [typeof(double)])!, "Round" }, |
| 41 | + { typeof(Math).GetMethod(nameof(Math.Sign), [typeof(double)])!, "Sign" }, |
| 42 | + { typeof(Math).GetMethod(nameof(Math.Sign), [typeof(float)])!, "Sign" }, |
| 43 | + { typeof(Math).GetMethod(nameof(Math.Sign), [typeof(long)])!, "Sign" }, |
| 44 | + { typeof(Math).GetMethod(nameof(Math.Sign), [typeof(sbyte)])!, "Sign" }, |
| 45 | + { typeof(Math).GetMethod(nameof(Math.Sign), [typeof(short)])!, "Sign" }, |
| 46 | + { typeof(Math).GetMethod(nameof(Math.Sin), [typeof(double)])!, "Sin" }, |
| 47 | + { typeof(Math).GetMethod(nameof(Math.Sinh), [typeof(double)])!, "Sinh" }, |
| 48 | + { typeof(Math).GetMethod(nameof(Math.Sqrt), [typeof(double)])!, "Sqrt" }, |
| 49 | + { typeof(Math).GetMethod(nameof(Math.Tan), [typeof(double)])!, "Tan" }, |
| 50 | + { typeof(Math).GetMethod(nameof(Math.Tanh), [typeof(double)])!, "Tanh" }, |
| 51 | + { typeof(Math).GetMethod(nameof(Math.Truncate), [typeof(double)])!, "Trunc" }, |
| 52 | + { typeof(MathF).GetMethod(nameof(MathF.Acos), [typeof(float)])!, "Acos" }, |
| 53 | + { typeof(MathF).GetMethod(nameof(MathF.Acosh), [typeof(float)])!, "Acosh" }, |
| 54 | + { typeof(MathF).GetMethod(nameof(MathF.Asin), [typeof(float)])!, "Asin" }, |
| 55 | + { typeof(MathF).GetMethod(nameof(MathF.Asinh), [typeof(float)])!, "Asinh" }, |
| 56 | + { typeof(MathF).GetMethod(nameof(MathF.Atan), [typeof(float)])!, "Atan" }, |
| 57 | + { typeof(MathF).GetMethod(nameof(MathF.Atan2), [typeof(float), typeof(float)])!, "Atan2" }, |
| 58 | + { typeof(MathF).GetMethod(nameof(MathF.Atanh), [typeof(float)])!, "Atanh" }, |
| 59 | + { typeof(MathF).GetMethod(nameof(MathF.Ceiling), [typeof(float)])!, "Ceil" }, |
| 60 | + { typeof(MathF).GetMethod(nameof(MathF.Cos), [typeof(float)])!, "Cos" }, |
| 61 | + { typeof(MathF).GetMethod(nameof(MathF.Cosh), [typeof(float)])!, "Cosh" }, |
| 62 | + { typeof(MathF).GetMethod(nameof(MathF.Exp), [typeof(float)])!, "Exp" }, |
| 63 | + { typeof(MathF).GetMethod(nameof(MathF.Floor), [typeof(float)])!, "Floor" }, |
| 64 | + { typeof(MathF).GetMethod(nameof(MathF.Log), [typeof(float)])!, "Log" }, |
| 65 | + { typeof(MathF).GetMethod(nameof(MathF.Log10), [typeof(float)])!, "Log10" }, |
| 66 | + { typeof(MathF).GetMethod(nameof(MathF.Log2), [typeof(float)])!, "Log2" }, |
| 67 | + { typeof(MathF).GetMethod(nameof(MathF.Pow), [typeof(float), typeof(float)])!, "Pow" }, |
| 68 | + { typeof(MathF).GetMethod(nameof(MathF.Round), [typeof(float)])!, "Round" }, |
| 69 | + { typeof(MathF).GetMethod(nameof(MathF.Sin), [typeof(float)])!, "Sin" }, |
| 70 | + { typeof(MathF).GetMethod(nameof(MathF.Sinh), [typeof(float)])!, "Sinh" }, |
| 71 | + { typeof(MathF).GetMethod(nameof(MathF.Sqrt), [typeof(float)])!, "Sqrt" }, |
| 72 | + { typeof(MathF).GetMethod(nameof(MathF.Tan), [typeof(float)])!, "Tan" }, |
| 73 | + { typeof(MathF).GetMethod(nameof(MathF.Tanh), [typeof(float)])!, "Tanh" }, |
| 74 | + { typeof(MathF).GetMethod(nameof(MathF.Truncate), [typeof(float)])!, "Trunc" }, |
| 75 | + }; |
| 76 | + |
| 77 | + private static readonly List<MethodInfo> _roundWithDecimalMethods = |
| 78 | + [ |
| 79 | + typeof(Math).GetMethod(nameof(Math.Round), [typeof(double), typeof(int)])!, |
| 80 | + typeof(MathF).GetMethod(nameof(MathF.Round), [typeof(float), typeof(int)])! |
| 81 | + ]; |
| 82 | + |
| 83 | + private static readonly List<MethodInfo> _logWithBaseMethods = |
| 84 | + [ |
| 85 | + typeof(Math).GetMethod(nameof(Math.Log), [typeof(double), typeof(double)])!, |
| 86 | + typeof(MathF).GetMethod(nameof(MathF.Log), [typeof(float), typeof(float)])! |
| 87 | + ]; |
| 88 | + |
| 89 | + private readonly ISqlExpressionFactory _sqlExpressionFactory; |
| 90 | + |
| 91 | + public YdbMathTranslator(ISqlExpressionFactory sqlExpressionFactory) |
| 92 | + => _sqlExpressionFactory = sqlExpressionFactory; |
| 93 | + |
| 94 | + public virtual SqlExpression? Translate( |
| 95 | + SqlExpression? instance, |
| 96 | + MethodInfo method, |
| 97 | + IReadOnlyList<SqlExpression> arguments, |
| 98 | + IDiagnosticsLogger<DbLoggerCategory.Query> logger |
| 99 | + ) |
| 100 | + { |
| 101 | + if (SupportedMethods.TryGetValue(method, out var sqlFunctionName)) |
| 102 | + { |
| 103 | + var typeMapping = ExpressionExtensions.InferTypeMapping(arguments.ToArray()); |
| 104 | + var newArguments = arguments |
| 105 | + .Select(a => _sqlExpressionFactory.ApplyTypeMapping(a, typeMapping)) |
| 106 | + .ToList(); |
| 107 | + |
| 108 | + return _sqlExpressionFactory.Function( |
| 109 | + "Math::" + sqlFunctionName, |
| 110 | + newArguments, |
| 111 | + nullable: true, |
| 112 | + argumentsPropagateNullability: newArguments.Select(_ => true).ToList(), |
| 113 | + method.ReturnType, |
| 114 | + typeMapping |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + if (_roundWithDecimalMethods.Contains(method)) |
| 119 | + { |
| 120 | + return _sqlExpressionFactory.Function( |
| 121 | + "Math::Round", |
| 122 | + arguments, |
| 123 | + nullable: true, |
| 124 | + argumentsPropagateNullability: ArrayUtil.TrueArrays[2], |
| 125 | + method.ReturnType, |
| 126 | + arguments[0].TypeMapping); |
| 127 | + } |
| 128 | + |
| 129 | + return null; |
| 130 | + } |
| 131 | +} |
0 commit comments