Skip to content

Commit

Permalink
Change upper/lower case to align with PG conventions (#2397)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Jun 6, 2022
1 parent 28999d5 commit d9a236c
Show file tree
Hide file tree
Showing 32 changed files with 590 additions and 590 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ SqlExpression Upper()

case "DayOfWeek":
// Unlike DateTime.DayOfWeek, NodaTime's IsoDayOfWeek enum doesn't exactly correspond to PostgreSQL's
// values returned by DATE_PART('dow', ...): in NodaTime Sunday is 7 and not 0, which is None.
// values returned by date_part('dow', ...): in NodaTime Sunday is 7 and not 0, which is None.
// So we generate a CASE WHEN expression to translate PostgreSQL's 0 to 7.
var getValueExpression = GetDatePartExpression(instance, "dow", true);
// TODO: Can be simplified once https://github.com/aspnet/EntityFrameworkCore/pull/16726 is in
Expand Down Expand Up @@ -331,16 +331,16 @@ SqlExpression Upper()
}

/// <summary>
/// Constructs the DATE_PART expression.
/// Constructs the date_part expression.
/// </summary>
/// <param name="e">The member expression.</param>
/// <param name="partName">The name of the DATE_PART to construct.</param>
/// <param name="floor">True if the result should be wrapped with FLOOR(...); otherwise, false.</param>
/// <param name="partName">The name of the date_part to construct.</param>
/// <param name="floor">True if the result should be wrapped with floor(...); otherwise, false.</param>
/// <returns>
/// The DATE_PART expression.
/// The date_part expression.
/// </returns>
/// <remarks>
/// DATE_PART returns doubles, which we floor and cast into ints
/// date_part returns doubles, which we floor and cast into ints
/// This also gets rid of sub-second components when retrieving seconds.
/// </remarks>
private SqlExpression GetDatePartExpression(
Expand All @@ -358,7 +358,7 @@ private SqlExpression GetDatePartExpressionDouble(
bool floor = false)
{
var result = _sqlExpressionFactory.Function(
"DATE_PART",
"date_part",
new[] { _sqlExpressionFactory.Constant(partName), instance },
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
Expand All @@ -367,7 +367,7 @@ private SqlExpression GetDatePartExpressionDouble(
if (floor)
{
result = _sqlExpressionFactory.Function(
"FLOOR",
"floor",
new[] { result },
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public NpgsqlNodaTimeMethodCallTranslator(

static PostgresFunctionExpression IntervalPart(string datePart, SqlExpression parameter)
=> PostgresFunctionExpression.CreateWithNamedArguments(
"MAKE_INTERVAL",
"make_interval",
new[] { parameter },
new[] { datePart },
nullable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ public class NpgsqlMathTranslator : IMethodCallTranslator
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Tan), new[] { typeof(float) })!, "tan" },

// 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(MathF).GetRuntimeMethod(nameof(MathF.Max), new[] { typeof(float), typeof(float) })!, "greatest" },
{ typeof(BigInteger).GetRuntimeMethod(nameof(BigInteger.Max), new[] { typeof(BigInteger), typeof(BigInteger) })!, "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" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Max), new[] { typeof(float), typeof(float) })!, "GREATEST" },
{ typeof(BigInteger).GetRuntimeMethod(nameof(BigInteger.Max), new[] { typeof(BigInteger), typeof(BigInteger) })!, "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(MathF).GetRuntimeMethod(nameof(MathF.Min), new[] { typeof(float), typeof(float) })!, "least" },
{ typeof(BigInteger).GetRuntimeMethod(nameof(BigInteger.Min), new[] { typeof(BigInteger), typeof(BigInteger) })!, "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(MathF).GetRuntimeMethod(nameof(MathF.Min), new[] { typeof(float), typeof(float) })!, "LEAST" },
{ typeof(BigInteger).GetRuntimeMethod(nameof(BigInteger.Min), new[] { typeof(BigInteger), typeof(BigInteger) })!, "LEAST" },
};

private static readonly IEnumerable<MethodInfo> TruncateMethodInfos = new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public NpgsqlNetworkTranslator(
nameof(NpgsqlNetworkDbFunctionsExtensions.ContainsOrContainedBy)
=> _sqlExpressionFactory.MakePostgresBinary(PostgresExpressionType.NetworkContainsOrContainedBy, arguments[1], arguments[2]),

nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseNot) => new SqlUnaryExpression(ExpressionType.Not,
arguments[1],
arguments[1].Type,
arguments[1].TypeMapping),
nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseNot)
=> new SqlUnaryExpression(ExpressionType.Not, arguments[1], arguments[1].Type, arguments[1].TypeMapping),

nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseAnd) => _sqlExpressionFactory.And(arguments[1], arguments[2]),
nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseOr) => _sqlExpressionFactory.Or(arguments[1], arguments[2]),
nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseAnd)
=> _sqlExpressionFactory.And(arguments[1], arguments[2]),
nameof(NpgsqlNetworkDbFunctionsExtensions.BitwiseOr)
=> _sqlExpressionFactory.Or(arguments[1], arguments[2]),

// Add/Subtract accept inet + int, so we can't use the default type mapping inference logic which assumes
// same-typed operands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
return averageInputType == typeof(float)
? _sqlExpressionFactory.Convert(
_sqlExpressionFactory.AggregateFunction(
"AVG",
"avg",
new[] { averageSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -52,7 +52,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
averageSqlExpression.Type,
averageSqlExpression.TypeMapping)
: _sqlExpressionFactory.AggregateFunction(
"AVG",
"avg",
new[] { averageSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -68,7 +68,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
return _sqlExpressionFactory.Convert(
_sqlExpressionFactory.ApplyDefaultTypeMapping(
_sqlExpressionFactory.AggregateFunction(
"COUNT",
"count",
new[] { countSqlExpression },
nullable: false,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -82,7 +82,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
var longCountSqlExpression = (source.Selector as SqlExpression) ?? _sqlExpressionFactory.Fragment("*");
return _sqlExpressionFactory.ApplyDefaultTypeMapping(
_sqlExpressionFactory.AggregateFunction(
"COUNT",
"count",
new[] { longCountSqlExpression },
nullable: false,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -94,7 +94,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
|| methodInfo == QueryableMethods.MaxWithSelector)
&& source.Selector is SqlExpression maxSqlExpression:
return _sqlExpressionFactory.AggregateFunction(
"MAX",
"max",
new[] { maxSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -107,7 +107,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
|| methodInfo == QueryableMethods.MinWithSelector)
&& source.Selector is SqlExpression minSqlExpression:
return _sqlExpressionFactory.AggregateFunction(
"MIN",
"min",
new[] { minSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -129,7 +129,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
{
return _sqlExpressionFactory.Convert(
_sqlExpressionFactory.AggregateFunction(
"SUM",
"sum",
new[] { sumSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -143,7 +143,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
{
return _sqlExpressionFactory.Convert(
_sqlExpressionFactory.AggregateFunction(
"SUM",
"sum",
new[] { sumSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand All @@ -154,7 +154,7 @@ public NpgsqlQueryableAggregateMethodTranslator(
}

return _sqlExpressionFactory.AggregateFunction(
"SUM",
"sum",
new[] { sumSqlExpression },
nullable: true,
argumentsPropagateNullability: FalseArrays[1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public NpgsqlStringMethodTranslator(
_whitespace = _sqlExpressionFactory.Constant(
@" \t\n\r", // TODO: Complete this
typeMappingSource.EStringTypeMapping);
_textTypeMapping = (RelationalTypeMapping)typeMappingSource.FindMapping(typeof(string), model)!;
_textTypeMapping = typeMappingSource.FindMapping(typeof(string), model)!;
}

public virtual SqlExpression? Translate(
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpressio
case ScalarSubqueryExpression:
var storeType = sqlUnaryExpression.TypeMapping.StoreType switch
{
"integer" => "INT",
"integer" => "int",
"timestamp with time zone" => "timestamptz",
"timestamp without time zone" => "timestamp",
var s => s
Expand Down Expand Up @@ -631,7 +631,7 @@ public virtual Expression VisitJsonPathTraversal(PostgresJsonTraversalExpression
Sql.Append(",");
}
}
Sql.Append("]::TEXT[]");
Sql.Append("]::text[]");
}

return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
ExpressionType.Subtract, sqlLeft!, sqlRight!, _typeMappingSource.FindMapping(typeof(int)))!;

return PostgresFunctionExpression.CreateWithNamedArguments(
"MAKE_INTERVAL",
"make_interval",
new[] { subtraction },
new[] { "days" },
nullable: true,
Expand Down
6 changes: 3 additions & 3 deletions test/EFCore.PG.FunctionalTests/BuiltInDataTypesNpgsqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,13 +912,13 @@ public void Sum_Conversions()
_ = context.Set<MappedDataTypes>().Sum(m => m.ShortAsSmallint);

AssertSql(
@"SELECT COALESCE(SUM(m.""LongAsBigint""), 0.0)::bigint
@"SELECT COALESCE(sum(m.""LongAsBigint""), 0.0)::bigint
FROM ""MappedDataTypes"" AS m",
//
@"SELECT COALESCE(SUM(m.""Int""), 0)::int
@"SELECT COALESCE(sum(m.""Int""), 0)::int
FROM ""MappedDataTypes"" AS m",
//
@"SELECT COALESCE(SUM(m.""ShortAsSmallint""::INT), 0)::INT
@"SELECT COALESCE(sum(m.""ShortAsSmallint""::int), 0)::int
FROM ""MappedDataTypes"" AS m");
}

Expand Down
Loading

0 comments on commit d9a236c

Please sign in to comment.