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

Fix APPLY query generation #4180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/LinqToDB/Linq/Builder/DistinctBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, Meth
{
sequence.SelectQuery.Select.OptimizeDistinct = true;
}
else
else if (sequence.SelectQuery.ParentSelect != null)
{
sequence.ConvertToIndex(null, 0, ConvertFlags.All);
}
Expand Down
69 changes: 68 additions & 1 deletion Tests/Linq/Linq/JoinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,6 @@ public void Issue1455Test2([DataSources(TestProvName.AllClickHouse)] string cont
}
#endregion


[ActiveIssue(1224, Configurations = new[]
{
TestProvName.AllAccess,
Expand All @@ -2921,5 +2920,73 @@ public void FullJoinCondition_Regression([DataSources(ProviderName.InformixDB2,
}
}

[Table]
private class Issue4160Person
{
[Column] public string Code { get; set; } = default!;

public static readonly Issue4160Person[] Data = new[]
{
new Issue4160Person() { Code = "SD" },
new Issue4160Person() { Code = "SD" },
new Issue4160Person() { Code = "SH" },
};
}

[Table]
private class Issue4160City
{
[Column] public string Code { get; set; } = default!;
[Column] public string? Name { get; set; }

public static readonly Issue4160City[] Data = new[]
{
new Issue4160City() { Code = "SD", Name = "SYDNEY" },
new Issue4160City() { Code = "SD", Name = "SUNDAY" },
new Issue4160City() { Code = "SH", Name = "SYDHIP" }
};
}

[Test]
public void Issue4160Test1([DataSources] string context)
{
using var db = GetDataContext(context);
using var persons = db.CreateLocalTable(Issue4160Person.Data);
using var cities = db.CreateLocalTable(Issue4160City.Data);

var query = (
from pe in persons
select new
{
Value = (from cc in cities
where cc.Code == pe.Code
select cc.Name).FirstOrDefault()
}).Distinct();

var data = query.ToList();

Assert.That(data.Count, Is.EqualTo(2));
// TODO: disable is_empty field generation for this query as it is not needed
//Assert.That(query.GetSelectQuery().Select.Columns.Count, Is.EqualTo(1));
}

[ActiveIssue(Details = "Currently fails for providers without APPLY support. We should enable conversion to JOIN for it.")]
[Test]
public void Issue4160Test2([DataSources] string context)
{
using var db = GetDataContext(context);
using var persons = db.CreateLocalTable(Issue4160Person.Data);
using var cities = db.CreateLocalTable(Issue4160City.Data);

var data = (
from pe in persons
from cc in cities.Where(cc => cc.Code == pe.Code).Take(1).DefaultIfEmpty()
select new
{
Value = cc.Name
}).Distinct().ToList();

Assert.That(data.Count, Is.EqualTo(2));
}
}
}
Loading