Repository navigation
Conversation
|
@tuespetre, |
| { | ||
| Check.NotNull(predicate, nameof(predicate)); | ||
|
|
||
| if (_limit != null || _offset != null) |
There was a problem hiding this comment.
AddToPredicate is supposed to be a simple function. This logic should not be here at all.
There was a problem hiding this comment.
I placed this logic here to be consistent with the several other 'simple' methods (and even setters) that use similar logic to ensure that a SelectExpression is pushed down into a subquery when appropriate: set_Limit, set_Offset, SetProjectionExpression, etc., but also because it is applicable in several places.
| SELECT CASE | ||
| WHEN NOT EXISTS ( | ||
| SELECT [t].[c0] | ||
| SELECT 1 |
There was a problem hiding this comment.
can we avoid this here? (previous query was better)
There was a problem hiding this comment.
@maumar, the previous query generated for this test was incorrect but the test was not producing the proper conditions to show how it was incorrect.
After changing the test from cs.OrderBy(c => c.ContactName).Skip(5).Take(10).All(p => p.CustomerID.Length == 5) to cs.OrderBy(c => c.ContactName).Skip(5).Take(10).All(p => !p.CustomerID.StartsWith("A"))
Old query, returns 1:
SELECT CASE
WHEN NOT EXISTS (
SELECT [t].[c0]
FROM (
SELECT 1 AS [c0], ROW_NUMBER() OVER(ORDER BY [c].[ContactName]) AS [__RowNumber__]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] LIKE N'A' + N'%' AND (CHARINDEX(N'A', [c].[CustomerID]) = 1)
) AS [t]
WHERE ([t].[__RowNumber__] > 5) AND ([t].[__RowNumber__] <= (15)))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
New query, returns 0:
SELECT CASE
WHEN NOT EXISTS (
SELECT 1
FROM (
SELECT [t0].*
FROM (
SELECT [c].*, ROW_NUMBER() OVER(ORDER BY [c].[ContactName]) AS [__RowNumber__]
FROM [Customers] AS [c]
) AS [t0]
WHERE ([t0].[__RowNumber__] > 5) AND ([t0].[__RowNumber__] <= (15))
) AS [t]
WHERE [t].[CustomerID] LIKE N'A' + N'%' AND (CHARINDEX(N'A', [t].[CustomerID]) = 1))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
2253b57 to
bb73b98
Compare
bb73b98 to
d7f5c64
Compare
|
🆙📅, rebased onto latest |
d7f5c64 to
d5174d4
Compare
|
@tuespetre I understand the original thing was not perfect and much more restrictive but is this approach better than that in some way? Can we improve lifting scenarios by applying appropriate conditions in Further I am working refactoring SelectExpression to make it more consistent and referentially integral, which also removes |
To be clear, this PR doesn't change which subqueries are lifted, just which
Tighter SQL statements, especially when
I am really interested to see what you are doing with that, especially in relation to By the time you get to translate the ...and you have to both successfully translate That's not necessarily this PR's problem, but something to consider alongside the work you are doing |
The pushdowns which were happening during LiftSubquery are happening at later stage now. If you don't pushdown a subquery then you are basically lifting it up. This is certainly improvement over generated SQL. My point is can we include some logic inside Taking simple example from tests added: AssertQuery<OrderDetail>(ods =>
ods.OrderBy(d => d.Quantity).ThenBy(d => d.OrderID).Take(2).OrderBy(d => d.ProductID).ThenBy(d => d.Quantity),
entryCount: 2); The generated query model is as follows: Now in this case if we don't lift the subquery and have the SelectExpression inside it appear in a subquery select then while adding outer order by we don't need to do push down. (btw above query works correctly in current dev) Having pushdown inside orderby/where means that we made a flat select expression for a subquery model instead of making a subquery select expression as it was required.
So which approach would be better for us? After changes to SelectExpression 1 may be more complex than 2. But if 2 cannot solve everything for us then 1 is also fine. While discussing in team, based on the Sql which are improving, there is another way to do this in QueryOptimizer. In this case since we have only select in outer query model, we can combine it with subquery and flatten the query. Optimizing at query model level makes things much easier while translating. Would you like to tackle this issue in query optimizer? As for group by query you posted, lets discuss on group by PR. |
|
@smitpatel good call on the pattern of the improved queries. I may not tackle this in the optimizer right now but since you have pointed that out I would say I am okay with backing off from this. If it's agreeable to you I will amend the commit to include the new test cases as well as the fix to the incorrect query that I discovered. |
|
| SELECT CASE | ||
| WHEN NOT EXISTS ( | ||
| SELECT [t].[c0] | ||
| SELECT 1 |
There was a problem hiding this comment.
@smitpatel this one that @maumar commented on previously
There was a problem hiding this comment.
Test changed. Original SQL looks fine.
| Sql); | ||
| } | ||
|
|
||
| public override void Skip_Skip_Take() |
There was a problem hiding this comment.
@smitpatel IIRC this test would also fail without a fix
There was a problem hiding this comment.
This could potentially fail. Though looking at generated SQL. It doesn't look very good given it has * at top level select. We should have proper column projected out. The reason we never generate SelectExpression with * at top level is you cannot accurately bind the valuebuffer to properties with star projection.
| WHERE LEN([c].[CustomerID]) <> 5 | ||
| ORDER BY [c].[ContactName] | ||
| OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY) | ||
| FROM ( |
There was a problem hiding this comment.
What is wrong with the original query here? The test itself was changed causing different SQL.
There was a problem hiding this comment.
I changed the All predicate within the test to demonstrate the issue. The predicate that was used before was hiding the issue. A subquery with Limit/Offset has to be pushed down before adding another predicate.
EDIT: In other words, if you changed the data in the database so that there actually were some customers with a customer ID that was not 5 characters long, the original query would have produced incorrect results
There was a problem hiding this comment.
Good catch. It indeed is incorrect query. The modifying AddToPredicate does not seem to be right way to fix it.
This situation arises because of multiple result operators. And it should be fixed in same manner as done in #7578 . i.e. Result operators should be taking care of other result operators which may come before. Do you want to submit a fix for it in HandleAll?
There was a problem hiding this comment.
Yes, do you have any objections to also fixing the .Skip().Skip() issue and including the new test cases in the same PR?
There was a problem hiding this comment.
No objection as long as they are localized in handling result operators.
There was a problem hiding this comment.
OK, great. Thanks!
|
We can certainly use the tests here. Also the improvement around |
|
@smitpatel we'll see how the other PRs go; besides those, what would be left in this PR is not really substantial so I am going to close it. Thanks! 🐢 |
Fixes #7766