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: JOIN/Left-outer-join queries wrapped in client projection could be simplified to ShapedQueries to reduce client evaluation #7648

Closed
maumar opened this issue Feb 17, 2017 · 3 comments
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 Feb 17, 2017

The following query:

from l2_outer in
    (from l2_inner in ctx.LevelTwo
     join l1_inner in ctx.LevelOne on l2_inner.Level1_Required_Id equals l1_inner.Id
     select l2_inner).Take(2)
join l1_outer in ctx.LevelOne on l2_outer.Level1_Required_Id equals l1_outer.Id
select l2_outer.Name;

produces the following query plan:

TRACKED: False
(QueryContext queryContext) => IEnumerable<string> _Select(
    source: IEnumerable<TransparentIdentifier<ValueBuffer, ValueBuffer>> _Join(
        outer: IEnumerable<ValueBuffer> _Select(
            source: IEnumerable<TransparentIdentifier<ValueBuffer, ValueBuffer>> _ShapedQuery(
                queryContext: queryContext, 
                shaperCommandContext: SelectExpression: 
                    SELECT [t].[Level1_Required_Id], [t].[Name]
                    FROM (
                        SELECT TOP(@__p_0) [l2_inner].*
                        FROM [Level2] AS [l2_inner]
                        INNER JOIN [Level1] AS [l1_inner] ON [l2_inner].[Level1_Required_Id] = [l1_inner].[Id]
                    ) AS [t]
                , 
                shaper: CompositeShaper`3
            )
            , 
            selector: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0) => t0.Outer
        )
        , 
        inner: IEnumerable<ValueBuffer> _ShapedQuery(
            queryContext: queryContext, 
            shaperCommandContext: SelectExpression: 
                SELECT [l1_outer].[Id]
                FROM [Level1] AS [l1_outer]
            , 
            shaper: ValueBufferShaper
        )
        , 
        outerKeySelector: (ValueBuffer l2_outer) => int TryReadValue(l2_outer, 0, null), 
        innerKeySelector: (ValueBuffer l1_outer) => int TryReadValue(l1_outer, 0, null), 
        resultSelector: (ValueBuffer l2_outer | ValueBuffer l1_outer) => TransparentIdentifier<ValueBuffer, ValueBuffer> CreateTransparentIdentifier(
            outer: l2_outer, 
            inner: l1_outer
        )
    )
    , 
    selector: (TransparentIdentifier<ValueBuffer, ValueBuffer> t0) => string TryReadValue(t0.Outer, 1, null)
)

Currently we produce client join here because the outer element (which is a join itself) has a client projection. However, we only project Outer element of that translated join, so it could be simplified to ShapedQuery.

This in turn would allow outer join to get simplified - currently we don't try to translate joins if one of the elements has a client projection (see RelationalQueryModelVisitor.CanFlattenJoin)

@smitpatel
Copy link
Contributor

What would be the SelectExpression for server translated join?

@maumar
Copy link
Contributor Author

maumar commented Feb 17, 2017

SELECT [t].[Level1_Required_Id], [t].[Name]
FROM (
    SELECT TOP(@__p_0) [l2_inner].*
    FROM [Level2] AS [l2_inner]
    INNER JOIN [Level1] AS [l1_inner] ON [l2_inner].[Level1_Required_Id] = [l1_inner].[Id]
) AS [t]
LEFT OUTER JOIN [Level1] as [l1_outer] ON [t].[Level1_Required_Id] = [l1_outer].[Id]

and then client projection on top of that

@rowanmiller rowanmiller added this to the 2.0.0 milestone Feb 22, 2017
@maumar
Copy link
Contributor Author

maumar commented Mar 28, 2017

Fixed in 74b7e4d

@maumar maumar closed this as completed Mar 28, 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 Mar 28, 2017
@ajcvickers ajcvickers changed the title Query/Cleanup : JOIN/LOJ queries wrapped in client projection could be simplified to ShapedQueries to reduce client evaluation Query: JOIN/Left-outer-join queries wrapped in client projection could be simplified to ShapedQueries to reduce client evaluation May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 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

6 participants