Skip to content

QueryRewrite: when comparing entity with composite key to null, only check it's first key property, rather than all of them #15080

Description

@maumar

Query:

from g1 in gs
join g2 in gs on g1.HasSoulPatch equals true into grouping
from g2 in grouping.DefaultIfEmpty()
orderby g2.Nickname
select g2 != null
  ? new
  {
	  g2.Nickname,
	  Five = 5
  }
  : null

Expected:

SELECT CASE
    WHEN [t].[Nickname] IS NOT NULL
    THEN CAST(1 AS bit) ELSE CAST(0 AS bit)
END, [t].[Nickname]
FROM [Gears] AS [g1]
LEFT JOIN (
    SELECT [g2].*
    FROM [Gears] AS [g2]
    WHERE [g2].[Discriminator] IN (N'Officer', N'Gear')
) AS [t] ON [g1].[HasSoulPatch] = CAST(1 AS bit)
WHERE [g1].[Discriminator] IN (N'Officer', N'Gear')
ORDER BY [t].[Nickname]

current:

SELECT CASE
    WHEN [t].[Nickname] IS NOT NULL OR [t].[SquadId] IS NOT NULL
    THEN CAST(1 AS bit) ELSE CAST(0 AS bit)
END, [t].[Nickname]
FROM [Gears] AS [g1]
LEFT JOIN (
    SELECT [g2].*
    FROM [Gears] AS [g2]
    WHERE [g2].[Discriminator] IN (N'Officer', N'Gear')
) AS [t] ON [g1].[HasSoulPatch] = CAST(1 AS bit)
WHERE [g1].[Discriminator] IN (N'Officer', N'Gear')
ORDER BY [t].[Nickname]");

Tests affected:
Select_null_propagation_negative4
Select_null_propagation_negative5

Activity

  1. self-assigned this
    on Mar 19, 2019
  2. added this to the 3.0.0 milestone on Mar 19, 2019
  3. assigned and unassigned on May 29, 2019
  4. ajcvickers commented on Jun 27, 2019

    @ajcvickers
    Contributor

    @maumar What if one key value is null and the other is non-null? This should be treated as a null key, but the first property may not be null.

  5. modified the milestones: 3.0.0, Backlog on Jun 28, 2019
  6. 20 remaining items

  7. added a commit that references this issue on Mar 31, 2020
    b7360ca
  8. added a commit that references this issue on Mar 31, 2020
    bb88898
  9. modified the milestones: 5.0.0, on Mar 31, 2020
  10. modified the milestones: , 5.0.0 on Apr 1, 2020
  11. modified the milestones: 5.0.0, on Apr 20, 2020
  12. modified the milestones: , 5.0.0 on Nov 7, 2020
  13. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions