-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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 incorrect plan property derivations #15154
Fix incorrect plan property derivations #15154
Conversation
The underlying cause is that a local gathering exchange (non-merging) does not preserve grouping and sorting properties, only constants. |
Can you add a test with the query you were showing me earlier today? |
Yep, need to try to understand if there’s a more minimized version that can be made and what assertions need to be made on that version to make it a little less of a regression test scenario. |
There is also the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per @raunaqmorarka , we already have isEffectivelySingleStream
check, so I'm not sure why there would be a problem here
@@ -661,10 +661,11 @@ public ActualProperties visitExchange(ExchangeNode node, List<ActualProperties> | |||
if (inputProperty.isEffectivelySingleStream() && node.getOrderingScheme().isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please improve commit message with description what was wrong and example preferrably
|
@martint The check for The stream:
Hence any property of that single stream should be held across exchange. |
I've updated the PR description with more details including an example query and the previous incorrect results. @sopel39 - your assumptions about |
@raunaqmorarka - This fix triggers a regression test added by #8535 which suggests there may be other bugs introduced by #6634. I may need some help root causing the test failures to identify the necessary related fix(es). Seems like the same issue you mentioned in this comment. |
Maybe that's true, but even method javadoc suggest otherwise:
Could you describe why for unpartitioned stream this method will return |
Because The last two methods in particular should make it obvious what will happen for streams partitioned on constants or not partitioned at all, and none of the logic guarantees that there will actually be only a single stream. |
da8b9bb
to
4ef4e65
Compare
I've pushed a new commit. In addition to the initial This bug has been present in Presto/Trino since Presto version 0.101, introduced in commit c5a3bfc - but became much more likely to cause correctness issues after the changes introduced in #6634 |
Window nodes do not emit output rows based on their orderBy specificiation and therefore should not attach a sorting property as party of PropertyDerivations.
LocalExchanges should not propagate grouping or ordering local properties from one side to another.
4ef4e65
to
eff37ff
Compare
@martint / @raunaqmorarka - After fixing invalid |
There are still a few issues we need to sort out with this fix, so my suggestion for the short term would be to add a config/session property to disable the optimization added in #6634 and turn off the optimization by default. |
The properties for WindowNode are a little more complicated. Currently, it guarantees that the output will be sorted by the partition-by + order-by columns. However, if the input is pre-grouped, it will be only grouped by the pre-grouped columns and sorted by the remaining partition-by + order by columns. |
@pettyjamesm I still don't see a problem with |
@@ -661,10 +659,11 @@ public ActualProperties visitExchange(ExchangeNode node, List<ActualProperties> | |||
if (inputProperty.isEffectivelySingleStream() && node.getOrderingScheme().isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think isEffectivelySingleStream()
might be incorrectly returning true
here.
This optimization can cause a correctness issue identified in trinodb#15154 Attempting to fix that leads to other resolved issues around properties of WindowNode. So this optimization is being disabled by default until we resolve all the known issues related to it.
Yes, it can be present.
Unpartitioned streams are allowed to be parallelized beyond a single stream, in the case of this particular reproducing query- by fixed arbitrary distribution (round robin) local exchanges between gathering exchange sequences. |
Seems like we really should fix |
I agree that the method name is very misleading, at least from the perspective of someone not familiar with this part of the code base (including me). Maybe the notion of “effectively single stream” is useful in some other context than “actually single stream”, but if so- it probably deserves a different name that makes it more clear. |
Here is my fix proposal (#15194) that fixes underlying issue with |
Fixed via #15203 |
Description
Fix a correctness issue associated with local exchange properties incorrectly propagating non-constant local properties inappropriately.
Example Query:
Before this fix, trino would produce incorrect (but not deterministic) results like the following:
Instead of the expected correct output:
Note that in this example, the query results are in the incorrect order according to the top-level
ORDER BY
and the window function results are incorrect because theWindowNode
in the plan was constructed with apreSortedOrderPrefix=2
essentially declaring that the input was pre-sorted bystudent_id, course_id
which was not. As a result, theWindowOperator
does not think it needs to sort its output (causing the incorrect row order in the results) and attempts to find window frame boundaries in its input (causing incorrect window function results).This bug was caused by commit f57b8a4 introduced as part of #6634 and released in Trino 358, which incorrectly propagated sort and partitioning local properties across the exchange boundary.
As @martint is correctly noted below,
ActualProperties.isEffectivelySingleStream()
does not mean is actually single stream (and is probably poorly named). In the case of a gathering exchange, if multiple sub-streams each produce data in sort order before the local exchange, the rows will not be emitted from the exchange preserving that order unless that exchange is a "merging" exchange (which in this case it is not, because of theExchangeNode#getOrderingScheme().isEmpty()
) check). Only "constant" local properties can safely and correctly be be propagated across local exchanges like this.Release notes
( ) This is not user-visible or docs only and no release notes are required.
(x) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:
I'm not sure exactly how to characterize this for the purposes of release notes since the scope of the potential impact of this bug on query correctness is potentially very broad. Suggestions for the release note welcome.