Skip to content

Commit

Permalink
Make all function translations lower-case (#1526)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Oct 9, 2020
1 parent 3d6c638 commit bc23399
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 128 deletions.
Expand Up @@ -47,7 +47,7 @@ public NpgsqlByteArrayMethodTranslator([NotNull] ISqlExpressionFactory sqlExpres
? (SqlExpression)_sqlExpressionFactory.Constant(new[] { (byte)constantValue.Value }, typeMapping)
// Create bytea from non-constant byte: SELECT set_byte('\x00', 0, 8::smallint);
: _sqlExpressionFactory.Function(
"SET_BYTE",
"set_byte",
new[]
{
_sqlExpressionFactory.Constant(new[] { (byte)0 }, typeMapping),
Expand All @@ -61,7 +61,7 @@ public NpgsqlByteArrayMethodTranslator([NotNull] ISqlExpressionFactory sqlExpres

return _sqlExpressionFactory.GreaterThan(
PostgresFunctionExpression.CreateWithArgumentSeparators(
"POSITION",
"position",
new[] { value, source },
new[] { "IN" }, // POSITION(x IN y)
nullable: true,
Expand Down
Expand Up @@ -40,7 +40,7 @@ public NpgsqlDateTimeMemberTranslator([NotNull] NpgsqlSqlExpressionFactory sqlEx
_sqlExpressionFactory.AtTimeZone(Now(), _sqlExpressionFactory.Constant("UTC"), returnType),

nameof(DateTime.Today) => _sqlExpressionFactory.Function(
"DATE_TRUNC",
"date_trunc",
new SqlExpression[] { _sqlExpressionFactory.Constant("day"), Now() },
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
Expand All @@ -60,7 +60,7 @@ public NpgsqlDateTimeMemberTranslator([NotNull] NpgsqlSqlExpressionFactory sqlEx
nameof(DateTime.DayOfWeek) => GetDatePartExpression(instance, "dow", floor: true),

nameof(DateTime.Date) => _sqlExpressionFactory.Function(
"DATE_TRUNC",
"date_trunc",
new[] { _sqlExpressionFactory.Constant("day"), instance },
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
Expand All @@ -80,7 +80,7 @@ public NpgsqlDateTimeMemberTranslator([NotNull] NpgsqlSqlExpressionFactory sqlEx

SqlFunctionExpression Now()
=> _sqlExpressionFactory.Function(
"NOW",
"now",
Array.Empty<SqlExpression>(),
nullable: false,
argumentsPropagateNullability: TrueArrays[0],
Expand All @@ -107,7 +107,7 @@ SqlFunctionExpression Now()
bool floor = false)
{
var result = _sqlExpressionFactory.Function(
"DATE_PART",
"date_part",
new[]
{
_sqlExpressionFactory.Constant(partName),
Expand All @@ -119,7 +119,7 @@ SqlFunctionExpression Now()

if (floor)
result = _sqlExpressionFactory.Function(
"FLOOR",
"floor",
new[] { result },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand Down
Expand Up @@ -24,50 +24,50 @@ public class NpgsqlMathTranslator : IMethodCallTranslator
{
static readonly Dictionary<MethodInfo, string> SupportedMethodTranslations = new Dictionary<MethodInfo, string>
{
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(decimal) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(double) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(float) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(int) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(long) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(short) }), "ABS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(decimal) }), "CEILING" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(double) }), "CEILING" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Floor), new[] { typeof(decimal) }), "FLOOR" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Floor), new[] { typeof(double) }), "FLOOR" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Pow), new[] { typeof(double), typeof(double) }), "POWER" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Exp), new[] { typeof(double) }), "EXP" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Log10), new[] { typeof(double) }), "LOG" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Log), new[] { typeof(double) }), "LN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(decimal) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(double) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(float) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(int) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(long) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Abs), new[] { typeof(short) }), "abs" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(decimal) }), "ceiling" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(double) }), "ceiling" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Floor), new[] { typeof(decimal) }), "floor" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Floor), new[] { typeof(double) }), "floor" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Pow), new[] { typeof(double), typeof(double) }), "power" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Exp), new[] { typeof(double) }), "exp" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Log10), new[] { typeof(double) }), "log" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Log), new[] { typeof(double) }), "ln" },
// Note: PostgreSQL has log(x,y) but only for decimal, whereas .NET has it only for double
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sqrt), new[] { typeof(double) }), "SQRT" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Acos), new[] { typeof(double) }), "ACOS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Asin), new[] { typeof(double) }), "ASIN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Atan), new[] { typeof(double) }), "ATAN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Atan2), new[] { typeof(double), typeof(double) }), "ATAN2" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Cos), new[] { typeof(double) }), "COS" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sin), new[] { typeof(double) }), "SIN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Tan), new[] { typeof(double) }), "TAN" },

{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(double) }), "ROUND" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal) }), "ROUND" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) }), "TRUNC" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Truncate), new[] { typeof(decimal) }), "TRUNC" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sqrt), new[] { typeof(double) }), "sqrt" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Acos), new[] { typeof(double) }), "acos" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Asin), new[] { typeof(double) }), "asin" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Atan), new[] { typeof(double) }), "atan" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Atan2), new[] { typeof(double), typeof(double) }), "atan2" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Cos), new[] { typeof(double) }), "cos" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sin), new[] { typeof(double) }), "sin" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Tan), new[] { typeof(double) }), "tan" },

{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(double) }), "round" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal) }), "round" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) }), "trunc" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Truncate), new[] { typeof(decimal) }), "trunc" },

// https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(decimal), typeof(decimal) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(double), typeof(double) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(float), typeof(float) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(int), typeof(int) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(long), typeof(long) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(short), typeof(short) }), "GREATEST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(decimal), typeof(decimal) }), "greatest" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(double), typeof(double) }), "greatest" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(float), typeof(float) }), "greatest" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(int), typeof(int) }), "greatest" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(long), typeof(long) }), "greatest" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Max), new[] { typeof(short), typeof(short) }), "greatest" },

// https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(decimal), typeof(decimal) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(double), typeof(double) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(float), typeof(float) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(int), typeof(int) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(long), typeof(long) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(short), typeof(short) }), "LEAST" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(decimal), typeof(decimal) }), "least" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(double), typeof(double) }), "least" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(float), typeof(float) }), "least" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(int), typeof(int) }), "least" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(long), typeof(long) }), "least" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Min), new[] { typeof(short), typeof(short) }), "least" },
};

static readonly IEnumerable<MethodInfo> SignMethodInfos = new[]
Expand Down Expand Up @@ -149,7 +149,7 @@ public class NpgsqlMathTranslator : IMethodCallTranslator
return
_sqlExpressionFactory.Convert(
_sqlExpressionFactory.Function(
"SIGN",
"sign",
arguments,
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand All @@ -160,7 +160,7 @@ public class NpgsqlMathTranslator : IMethodCallTranslator

if (method == RoundDecimalTwoParams)
{
return _sqlExpressionFactory.Function("ROUND", new[]
return _sqlExpressionFactory.Function("round", new[]
{
_sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]),
_sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[1])
Expand Down
Expand Up @@ -10,7 +10,7 @@
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal
{
/// <summary>
/// Translates <see cref="M:string.Length"/> to 'LENGTH(text)'.
/// Translates <see cref="M:string.Length"/> to 'length(text)'.
/// </summary>
public class NpgsqlStringMemberTranslator : IMemberTranslator
{
Expand All @@ -26,7 +26,7 @@ public NpgsqlStringMemberTranslator([NotNull] ISqlExpressionFactory sqlExpressio
=> member.Name == nameof(string.Length) && instance?.Type == typeof(string)
? _sqlExpressionFactory.Convert(
_sqlExpressionFactory.Function(
"LENGTH",
"length",
new[] { instance },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand Down
Expand Up @@ -93,7 +93,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator

return _sqlExpressionFactory.Subtract(
_sqlExpressionFactory.Function(
"STRPOS",
"strpos",
new[]
{
_sqlExpressionFactory.ApplyTypeMapping(instance, stringTypeMapping),
Expand All @@ -112,7 +112,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator
var stringTypeMapping = ExpressionExtensions.InferTypeMapping(instance, oldValue, newValue);

return _sqlExpressionFactory.Function(
"REPLACE",
"replace",
new[]
{
_sqlExpressionFactory.ApplyTypeMapping(instance, stringTypeMapping),
Expand All @@ -128,7 +128,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator
if (method == ToLower || method == ToUpper)
{
return _sqlExpressionFactory.Function(
method == ToLower ? "LOWER" : "UPPER",
method == ToLower ? "lower" : "upper",
new[] { instance },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand All @@ -143,7 +143,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator
? new[] { instance, GenerateOneBasedIndexExpression(arguments[0]) }
: new[] { instance, GenerateOneBasedIndexExpression(arguments[0]), arguments[1] };
return _sqlExpressionFactory.Function(
"SUBSTRING",
"substring",
args,
nullable: true,
argumentsPropagateNullability: TrueArrays[args.Length],
Expand All @@ -159,7 +159,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator
_sqlExpressionFactory.IsNull(argument),
_sqlExpressionFactory.Equal(
_sqlExpressionFactory.Function(
"BTRIM",
"btrim",
new[]
{
argument,
Expand Down Expand Up @@ -193,7 +193,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator
}

return _sqlExpressionFactory.Function(
isTrimStart ? "LTRIM" : isTrimEnd ? "RTRIM" : "BTRIM",
isTrimStart ? "ltrim" : isTrimEnd ? "rtrim" : "btrim",
new[]
{
instance,
Expand All @@ -216,7 +216,7 @@ public class NpgsqlStringMethodTranslator : IMethodCallTranslator

var strposCheck = _sqlExpressionFactory.GreaterThan(
_sqlExpressionFactory.Function(
"STRPOS",
"strpos",
new[]
{
_sqlExpressionFactory.ApplyTypeMapping(instance, stringTypeMapping),
Expand Down Expand Up @@ -334,12 +334,12 @@ SqlExpression TranslateStartsEndsWith(SqlExpression instance, SqlExpression patt
// For StartsWith we also first run a LIKE to quickly filter out most non-matching results (sargable, but imprecise
// because of wildchars).
SqlExpression leftRight = _sqlExpressionFactory.Function(
startsWith ? "LEFT" : "RIGHT",
startsWith ? "left" : "right",
new[]
{
instance,
_sqlExpressionFactory.Function(
"LENGTH",
"length",
new[] { pattern },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand Down
Expand Up @@ -133,7 +133,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)
(sqlOperand.TypeMapping == null || sqlOperand.TypeMapping is NpgsqlByteArrayTypeMapping))
{
return _sqlExpressionFactory.Function(
"LENGTH",
"length",
new[] { sqlOperand },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand Down Expand Up @@ -318,7 +318,7 @@ protected override Expression VisitNew(NewExpression newExpression)
{
return TryTranslateArguments(newExpression.Arguments, out var sqlArguments)
? _sqlExpressionFactory.Function(
"MAKE_DATE", sqlArguments, nullable: true, TrueArrays[3], typeof(DateTime))
"make_date", sqlArguments, nullable: true, TrueArrays[3], typeof(DateTime))
: null;
}

Expand All @@ -331,7 +331,7 @@ protected override Expression VisitNew(NewExpression newExpression)
sqlArguments[5] = _sqlExpressionFactory.Convert(sqlArguments[5], typeof(double));

return _sqlExpressionFactory.Function(
"MAKE_TIMESTAMP", sqlArguments, nullable: true, TrueArrays[6], typeof(DateTime));
"make_timestamp", sqlArguments, nullable: true, TrueArrays[6], typeof(DateTime));
}

return null;
Expand Down

0 comments on commit bc23399

Please sign in to comment.