-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: build ANY expressions as regular subqueries within UDFs
To support execution of ANY expressions within UDFs, the optimizer builds them as regular subqueries with a novel transformation. The transformation simulates the peculiar behavior of ANY expressions. For example, consider the expression: i <comp> ANY (<subquery>) The logic for evaluating this comparison is: 1. If the subquery results in zero rows, then the expression evaluates to false, even if i is NULL. 2. Otherwise, if the comparison between i and any value returned by the subquery is true, then the expression evaluates to true. 3. Otherwise, if any values returned by the subquery are NULL, then the expression evaluates to NULL. 4. Otherwise, if i is NULL, then the expression evaluates to NULL. 5. Otherwise, the expression evaluates to false. We use the following transformation to express this logic: i = ANY (SELECT a FROM t) => SELECT count > 0 AND (bool_or OR (null_count > 0 AND NULL)) FROM ( SELECT count(*) AS count, bool_or(cmp) AS bool_or, count(*) FILTER (is_null) AS null_count FROM ( SELECT a = i AS cmp, a IS NULL AS is_null FROM ( SELECT a FROM t ) ) ) By constructing ANY expressions within UDFs as subqueries, we avoid adding complexity to the execution engine, which is not currently capable of evaluating ANY expressions within the context of a UDF. We only perform this transformation when building ANY expressions within a UDF because this transformation may lead to query plans with slow apply-joins. In the future, if we can eliminate the apply-joins we may be able to apply this transformation universally, and remove existing logic for ANY expression evaluation in the execution engine. Fixes #87291 Release note: None
- Loading branch information
Showing
5 changed files
with
467 additions
and
9 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
Oops, something went wrong.