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

Query :: projecting FirstOrDefault() on empty collection of value types may cause NRE (generated SQL returns null rather than 0) #6061

Closed
maumar opened this issue Jul 12, 2016 · 1 comment · Fixed by #17593
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Jul 12, 2016

query:

from c in cs
where c.CustomerID.Equals("FISSA")
select new { c.CustomerID, OrderId = c.Orders.OrderBy(o => o.OrderID).Select(o => o.OrderID).FirstOrDefault() }

query plan:

TRACKED: False
(QueryContext queryContext) => IEnumerable<<>f__AnonymousType132<string, int>> _Select(
    source: IEnumerable<ValueBuffer> _ShapedQuery(
        queryContext: queryContext, 
        shaperCommandContext: SelectExpression: 
            SELECT [c].[CustomerID], (
                SELECT TOP(1) [o0].[OrderID]
                FROM [Orders] AS [o0]
                WHERE [c].[CustomerID] = [o0].[CustomerID]
                ORDER BY [o0].[OrderID]
            )
            FROM [Customers] AS [c]
            WHERE [c].[CustomerID] = N'FISSA'
        , 
        shaper: ValueBufferShaper
    )
    , 
    selector: (ValueBuffer c) => new <>f__AnonymousType132<string, int>(
        (string) object c.get_Item(0), 
        (int) object c.get_Item(1)
    )
)

this query will return {FISSA, NULL}, rather than {FISSA, 0}, which causes the error

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 15, 2016
@maumar maumar modified the milestones: 1.2.0, 1.1.0-preview1 Oct 5, 2016
@smitpatel
Copy link
Contributor

Exception:

arting:    Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests
    Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.QueryNavigationsSqlServerTest.Project_first_or_default_on_empty_collection_of_value_types_returns_proper_default [FAIL]
      System.NullReferenceException : Object reference not set to an instance of an object.
      Stack Trace:
           at lambda_method(Closure , ValueBuffer )
           at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
        D:\EntityFramework\src\Microsoft.EntityFrameworkCore\Query\Internal\LinqOperatorProvider.cs(147,0): at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
           at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
           at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
           at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
        D:\EntityFramework\src\Microsoft.EntityFrameworkCore.Specification.Tests\QueryNavigationsTestBase.cs(971,0): at Microsoft.EntityFrameworkCore.Specification.Tests.QueryNavigationsTestBase`1.AssertQuery[TItem](Func`2 efQuery, Func`2 l2oQuery, Boolean assertOrder, Int32 entryCount, Action`2 asserter)
        D:\EntityFramework\src\Microsoft.EntityFrameworkCore.Specification.Tests\QueryNavigationsTestBase.cs(852,0): at Microsoft.EntityFrameworkCore.Specification.Tests.QueryNavigationsTestBase`1.Project_first_or_default_on_empty_collection_of_value_types_returns_proper_default()
        D:\EntityFramework\test\Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests\QueryNavigationsSqlServerTest.cs(1056,0): at Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.QueryNavigationsSqlServerTest.Project_first_or_default_on_empty_collection_of_value_types_returns_proper_default()
  Finished:    Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests

@maumar maumar removed this from the 2.0.0 milestone Apr 13, 2017
@ajcvickers ajcvickers added this to the Backlog milestone Apr 17, 2017
@smitpatel smitpatel assigned smitpatel and unassigned maumar Sep 3, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 3, 2019
@smitpatel smitpatel modified the milestones: Backlog, 3.0.0 Sep 3, 2019
smitpatel added a commit that referenced this issue Sep 3, 2019
smitpatel added a commit that referenced this issue Sep 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants