Skip to content

Commit

Permalink
Improved error reporting on aggregate parameter counts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Jun 25, 2024
1 parent 6008bb4 commit e0dd417
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/AdoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2105,5 +2105,22 @@ public void InSelectStarError()
}
}
}

[TestMethod]
public void MissingCountParameter()
{
using (var con = new Sql4CdsConnection(_localDataSources))
{
try
{
con.Execute("SELECT count() FROM account");
Assert.Fail();
}
catch (Sql4CdsException ex)
{
Assert.AreEqual(174, ex.Number);
}
}
}
}
}
18 changes: 17 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3347,30 +3347,46 @@ private IDataExecutionPlanNodeInternal ConvertGroupByAggregates(IDataExecutionPl
Distinct = aggregate.Expression.UniqueRowFilter == UniqueRowFilter.Distinct
};

converted.SqlExpression = aggregate.Expression.Parameters[0].Clone();
if (aggregate.Expression.Parameters.Count > 0)
converted.SqlExpression = aggregate.Expression.Parameters[0].Clone();

switch (aggregate.Expression.FunctionName.Value.ToUpper())
{
case "AVG":
if (aggregate.Expression.Parameters.Count != 1)
throw new NotSupportedQueryFragmentException(Sql4CdsError.InvalidFunctionParameterCount(aggregate.Expression.FunctionName, 1));

converted.AggregateType = AggregateType.Average;
break;

case "COUNT":
if (aggregate.Expression.Parameters.Count != 1)
throw new NotSupportedQueryFragmentException(Sql4CdsError.InvalidFunctionParameterCount(aggregate.Expression.FunctionName, 1));

if ((converted.SqlExpression is ColumnReferenceExpression countCol && countCol.ColumnType == ColumnType.Wildcard) || (converted.SqlExpression is Literal && !(converted.SqlExpression is NullLiteral)))
converted.AggregateType = AggregateType.CountStar;
else
converted.AggregateType = AggregateType.Count;
break;

case "MAX":
if (aggregate.Expression.Parameters.Count != 1)
throw new NotSupportedQueryFragmentException(Sql4CdsError.InvalidFunctionParameterCount(aggregate.Expression.FunctionName, 1));

converted.AggregateType = AggregateType.Max;
break;

case "MIN":
if (aggregate.Expression.Parameters.Count != 1)
throw new NotSupportedQueryFragmentException(Sql4CdsError.InvalidFunctionParameterCount(aggregate.Expression.FunctionName, 1));

converted.AggregateType = AggregateType.Min;
break;

case "SUM":
if (aggregate.Expression.Parameters.Count != 1)
throw new NotSupportedQueryFragmentException(Sql4CdsError.InvalidFunctionParameterCount(aggregate.Expression.FunctionName, 1));

if (converted.SqlExpression is IntegerLiteral sumLiteral && sumLiteral.Value == "1")
converted.AggregateType = AggregateType.CountStar;
else
Expand Down

0 comments on commit e0dd417

Please sign in to comment.