forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: do not remap columns under a grouping operator
The `TryRemapOuterCols` rules attempt to replace an outer column reference within a relational expression by a non-outer column. The transformation is valid because the rule only fires when a parent expression filters all rows for which the outer and non-outer columns are not equal. For example: ``` SELECT * FROM xy INNER JOIN (SELECT a + x FROM ab) ON x = a; ``` In the above query, the reference to `x` in the subquery could be replaced with `x`, and the rows for which `a = x` does not hold would be later removed by the join. However, the rule also performs this transformation for GroupBy and DistinctOn, and in particular, the replacement column could be one of the `ConstAgg` columns. This could cause incorrect results because input rows that satisfy the equality and rows that don't could be aggregated together (note that de-duplication is a special form of aggregation). Note that the transformation is correct when the replacement column is one of the grouping columns; this is because the grouping then separates rows that satisfy the equality from those that don't. The groups that don't satisfy the equality are then filtered later in the query, preserving the correct result. This also applies to the distinct variants of set operators (Union, Except, Intersect), since they de-duplicate across all columns. This patch fixes the bug by restricting the rule to only make column replacements under a GroupBy or DistinctOn for columns that are part of the grouping column set. Fixes cockroachdb#130001 Release note (bug fix): Fixed a bug introduced in v23.1 that can cause incorrect results when: 1. The query contains a correlated subquery. 2. The correlated subquery has a GroupBy or DistinctOn operator with an outer-column reference in its input. 3. The correlated subquery is in the input of a Select or Join operator 4. The Select or Join has a filter that sets the outer-column reference from (2) equal to a non-outer column in the input of the grouping operator. 5. The grouping column set does not include the replacement column, and functionally determines the replacement column.
- Loading branch information
1 parent
f09d877
commit f38e791
Showing
5 changed files
with
213 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters