Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special-case coalescing of SUM in SqlNullability is causing issues #28158

Open
roji opened this issue Jun 4, 2022 · 1 comment
Open

Special-case coalescing of SUM in SqlNullability is causing issues #28158

roji opened this issue Jun 4, 2022 · 1 comment
Assignees
Labels
area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented Jun 4, 2022

In my eternal quest to translate all aggregate functions on the planet (and beyond), I'm adding support for Sum and Average over TimeSpan for PostgreSQL, where that's supported (npgsql/efcore.pg#2339).

For sum, I ran into trouble because of the following code in SqlNullabilityProcessor:

if (sqlFunctionExpression.IsBuiltIn
    && string.Equals(sqlFunctionExpression.Name, "SUM", StringComparison.OrdinalIgnoreCase))
{
    nullable = false;

    return _sqlExpressionFactory.Coalesce(
        sqlFunctionExpression.Update(instance, arguments),
        _sqlExpressionFactory.Constant(0, sqlFunctionExpression.TypeMapping),
        sqlFunctionExpression.TypeMapping);
}

This fails because it attempts to coalesce TimeSpan (non-numeric) to zero.

Replacing the 0 with Activator.CreateInstance(sqlFunctionExpression.Type) makes the problem go away. However, since I'm translating EF.Functions.Sum, the return type of that function is nullable (TimeSpan?), since that's how SQL sum behaves (compare with #26634). So the COALESCE introduced here is unwanted.

It generally seems better to handle this kind of thing in the method translator (but I don't have the context) - is that possible?

/cc @maumar @smitpatel

@roji
Copy link
Member Author

roji commented Jul 9, 2022

@maumar I've implemented a hacky workaround on my side for this, i.e. I just remove the bad coalesce node in NpgsqlSqlNullabilityProcessor.

This means this is no longer blocking and can be punted out if needed (though I believe it should still be done).

@ajcvickers ajcvickers added the punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. label Aug 11, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants