Skip to content

Query: GroupJoin generates LEFT-JOIN and doesn't return all results #6360

Closed

Description

@Hajisharifi

Steps to reproduce (SQL Server Database)

CREATE TABLE Table_1
(
ID1 int NOT NULL PRIMARY KEY,
ID2 int NULL
);
GO
CREATE TABLE Table_2
(
ID2 int NOT NULL PRIMARY KEY,
MyData int NOT NULL,
MyOtherDate nvarchar(50) NULL
/* I have multi column... */
);
GO
INSERT Table_1 (ID1, ID2) VALUES (11, 21);
INSERT Table_1 (ID1, ID2) VALUES (12, 21);
INSERT Table_1 (ID1, ID2) VALUES (13, 22);
INSERT Table_2 (ID2, MyData, MyOtherDate) VALUES (21, 11111, NULL);
INSERT Table_2 (ID2, MyData, MyOtherDate) VALUES (22, 22222, NULL);
GO

Steps to reproduce (C# Code1)

[Table("Table_1")]
public class Table_1
{
[Key] public int ID1 { get; set; }
public int? ID2 { get; set; }
}
[Table("Table_2")]
public class Table_2
{
[Key] public int ID2 { get; set; }
public int? MyData { get; set; }
public string MyOtherDate { get; set; }
}

//EFCore1-Return only two rows(?!!!)
//SELECT b.[ID2], b.[MyData], b.[MyOtherDate], a.[ID1]
//FROM [Table_1] AS a LEFT JOIN [Table_2] AS b ON a.[ID2] = b.[ID2] ORDER BY a.[ID2]
var x = db.Table_1
.GroupJoin(db.Table_2,
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
MyData = b.Select(c => c.MyData).FirstOrDefault()
})
.ToList();

Steps to reproduce (Alternative Code2)

//EFCore1-ERROR
//System.Data.SqlClient.SqlException
//The column prefix 't20' does not match with a table name or alias name used in the query.
//No column name was specified for column 1 of 't3'.
//Invalid column name 'ID2'.
//Invalid column name 'MyData'.
var y = db.Table_1
.Join(db.Table_2.DefaultIfEmpty(),
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
b.MyData
})
.ToList();

Steps to reproduce (Detect another Error)

//EFCore1-ERROR
//System.InvalidOperationException
//Operation is not valid due to the current state of the object.
var z = db.Table_1
.Join(db.Table_2.Select(r => new { r.ID2, r.MyData }),
a => a.ID2,
b => b.ID2, (a, b) => new
{
a.ID1,
b.MyData
})
.ToList();

The issue

GroupJoin in Code1 don't return all rows
Row[0]=11,1111
Row[1]=13,2222
Row[2]=12,1111 (don't return)

auto TSQL generate by EFCore1 (return three row):
ssms-result

Join+DefaultIfEmpty in Code2 throw an exception.
Exception type: System.Data.SqlClient.SqlException
Exception message:
The column prefix 't20' does not match with a table name or alias name used in the query.
No column name was specified for column 1 of 't3'.
Invalid column name 'ID2'.
Invalid column name 'MyData'.

Further technical details

"Microsoft.EntityFrameworkCore": "1.0.0"

Sorry for my bad English.
Thanks.

Activity

  1. added this to the 1.0.1 milestone on Aug 19, 2016
  2. assigned and unassigned on Aug 19, 2016
  3. smitpatel commented on Aug 19, 2016

    @smitpatel
    Contributor

    Same root cause as #6318
    The projection includes columns from second join table before first hence the logic of outer key changed failed.

  4. smitpatel commented on Aug 19, 2016

    @smitpatel
    Contributor

    Issue here is:
    For the case of streaming group join, we query data sorted by outer key selector. If outer element has projection from something other than outer key selector then we check for change in outer element while inner key remains the same. For the rest of cases, we assume that change in outer element will be recorded by change in outer key selector. Though that optimization fails for following query:

    var query = context.Orders
        .GroupJoin(context.Customers,
            o => o.CustomerID,
            c => c.CustomerID,
            (o, cs) => new
            {
                o.OrderID,
                Name = cs.Select(c => c.ContactName).FirstOrDefault()
            }).ToList();

    In above query, customer has one-to-many fk to orders so multiple orders can have same customer. While doing group join with orders being outer, change in outer element (different orders) can have same key which due to our optimization we assume to be same element. Therefore number of groups generated are not same as number of unique outer elements, rather it is equal to number of unique keys.
    Therefore in this issue, we see 2 groups instead of 3.
    In #6318 & #6232 , it causes to return same outer element multiple times, even though the element is changed.

  5. changed the title [-]The GroupJoin generate LEFT-JOIN and don't return all result ![/-] [+]The GroupJoin generate LEFT-JOIN and don't return all result[/+] on Aug 23, 2016
  6. changed the title [-]The GroupJoin generate LEFT-JOIN and don't return all result[/-] [+]GroupJoin generate LEFT-JOIN and don't return all result[/+] on Aug 23, 2016
  7. changed the title [-]GroupJoin generate LEFT-JOIN and don't return all result[/-] [+]GroupJoin generates LEFT-JOIN and don't return all result[/+] on Aug 23, 2016
  8. Eilon commented on Aug 26, 2016

    @Eilon
    Contributor

    Approved for 1.0.1.

  9. smitpatel commented on Aug 27, 2016

    @smitpatel
    Contributor

    fixed via #6387

  10. changed the title [-]GroupJoin generates LEFT-JOIN and don't return all result[/-] [+]Query: GroupJoin generates LEFT-JOIN and don't return all result[/+] on Sep 13, 2016
  11. changed the title [-]Query: GroupJoin generates LEFT-JOIN and don't return all result[/-] [+]Query: GroupJoin generates LEFT-JOIN and doesn't return all results[/+] on Sep 13, 2016
  12. 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

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions