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.
norm: push CASE WHEN condition into subquery so ApplyJoin is possible
Fixes cockroachdb#80169 Fixes cockroachdb#71908 Previously, some queries which follow the pattern: `SELECT CASE WHEN <cond> THEN <subquery> END FROM <table>` would error out with a `could not decorrelate subquery` error. The reason is that the subquery should only run in cases where `<cond>` is true or when it is leak-proof (when running the subquery when `<cond>` is false couldn't have side-effects, e.g. erroring out due to overflow during a CAST). When the subquery is not leak-proof, it cannot be pulled above the CASE expression into an apply join (which is a necessary first step in executing a subquery expression). To address this, this patch introduces a new normalization rule which attempts to push the WHEN clause condition into the THEN clause subquery and remove the CASE expression entirely (replace the CASE with the subquery). The preconditions for this normalization are: 1. There is a single WHEN clause. 2. There is an ELSE NULL clause (either explicitly specified or implicit). 3. The WHEN clause condition is not volatile (for example, the result is the same no matter how many times it is evaluated). 4. The WHEN clause condition does not cause any side-effects, like writing rows to a table. 5. The relational expressions in the THEN clause are only of the following types: (Select, Project, Limit, Offset, RecursiveCTE, InnerJoin, Scan, WithScan, ScalarGroupBy, Window). 6. There are no aggregate functions which produce a non-null value when the input is empty, such as COUNT. 7. There are no projected expressions above an aggregation in the subquery operation tree. If these conditions are met, a new Select is placed above each Scan or WithScan operation using the WHEN condition as a filter and the CASE expression is replaced by the subquery. Release note (sql change): This patch is adding support for some queries which asyncpg generates internally, which previously would error out with the message, "could not decorrelate subquery".
- Loading branch information
Mark Sirek
committed
May 24, 2022
1 parent
12e772d
commit 388e4fa
Showing
4 changed files
with
1,264 additions
and
0 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
Oops, something went wrong.