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: SubQueryMemberPushDown optimization is too aggressive, which may lead to invalid sql or data corruption #8505

Closed
maumar opened this issue May 17, 2017 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented May 17, 2017

Part of QM optimization is SubQueryMemberPushDownExpressionVisitor, which pushes down member from outside subquery into it. This way we get much simpler QMs and are able to translate more queries fully to sql. Problem is that we are doing it too aggresively, e.g. query:

from l1 in ctx.LevelOne
select (from l3 in ctx.LevelThree
		orderby l3.Id
		select l3).Distinct().Skip(1).FirstOrDefault().Name

with the following QM (before optimization):

from Level1 l1 in DbSet<Level1>
select 
    (from Level3 l3 in DbSet<Level3>
    order by [l3].Id asc
    select [l3])
    .Distinct()
    .Skip(1)
    .FirstOrDefault().Name

will get "optimized" to:

from Level1 l1 in DbSet<Level1>
select 
    (from Level3 l3 in DbSet<Level3>
    order by [l3].Id asc
    select [l3].Name)
    .Distinct()
    .Skip(1)
    .FirstOrDefault()

which is wrong. This produces invalid sql, but even if the sql would be valid, results would be incorrect - Distinct is now applied on the Name, rather than entity itself, which will produce unexpected results if the Name has duplicated values

We should not perform the pushdown if Distinct is present in the subquery ( other operators also? Intersect, Except, Union)

@maumar maumar self-assigned this May 19, 2017
@maumar maumar changed the title Query: SubQueryMemberPushDown optimization is too aggresive, which may lead to invalid sql or data corruption Query: SubQueryMemberPushDown optimization is too aggressive, which may lead to invalid sql or data corruption May 19, 2017
maumar added a commit that referenced this issue May 20, 2017
…essive, which may lead to invalid sql or data corruption

Problem was that we were performing subquery member pushdown incorrectly in some cases, e.g. when subquery had Distinct result operator:

from Level1 l1 in DbSet<Level1>
select
    (from Level3 l3 in DbSet<Level3>
    select [l3])
    .Distinct()
    .FirstOrDefault().Name

previously we were translating this into:

from Level1 l1 in DbSet<Level1>
select
    (from Level3 l3 in DbSet<Level3>
    select [l3].Name)
    .Distinct()
    .FirstOrDefault()

Fix is to introduce a subquery, if Distinct result operator is present in a subquery:

from Level1 l1 in DbSet<Level1>
select
    (from subquery in (from Level3 l3 in DbSet<Level3>
                      select [l3])
                     .Distinct()
     select [subquery].Name)
   .FirstOrDefault()
@ajcvickers ajcvickers added this to the 2.0.0-preview2 milestone May 22, 2017
maumar added a commit that referenced this issue May 22, 2017
…essive, which may lead to invalid sql or data corruption

Problem was that we were performing subquery member pushdown incorrectly in some cases, e.g. when subquery had Distinct result operator:

from Level1 l1 in DbSet<Level1>
select
    (from Level3 l3 in DbSet<Level3>
    select [l3])
    .Distinct()
    .FirstOrDefault().Name

previously we were translating this into:

from Level1 l1 in DbSet<Level1>
select
    (from Level3 l3 in DbSet<Level3>
    select [l3].Name)
    .Distinct()
    .FirstOrDefault()

Fix is to introduce a subquery, if Distinct result operator is present in a subquery:

from Level1 l1 in DbSet<Level1>
select
    (from subquery in (from Level3 l3 in DbSet<Level3>
                      select [l3])
                     .Distinct()
     select [subquery].Name)
   .FirstOrDefault()
@ajcvickers
Copy link
Contributor

@maumar Do you think you can get this merged today?

@maumar
Copy link
Contributor Author

maumar commented May 26, 2017

I hope so, hit a bunch of issues yesterday and today but I think I finally got it

@maumar
Copy link
Contributor Author

maumar commented May 26, 2017

fixed in 3f8baeb

@maumar maumar closed this as completed May 26, 2017
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 26, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview2, 2.0.0 Oct 15, 2022
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. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants