Do not optimize any(arrayJoin()) -> arrayJoin() under optimize_move_functions_out_of_any

Otherwise the following query will be optimized incorrectly:

    SELECT
        *,
        any(arrayJoin([[], []]))
    FROM numbers(1)
    GROUP BY number

And the result will be:

    ┌─number─┬─arrayJoin(array(array(), array()))─┐
    │      0 │ []                                 │
    │      0 │ []                                 │
    └────────┴────────────────────────────────────┘

While should be:

    ┌─number─┬─any(arrayJoin(array(array(), array())))─┐
    │      0 │ []                                      │
    └────────┴─────────────────────────────────────────┘
This commit is contained in:
Azat Khuzhin 2020-08-13 22:03:11 +03:00
parent 604b918d44
commit 17b27b6c28
3 changed files with 20 additions and 0 deletions

View File

@ -20,6 +20,21 @@ bool extractIdentifiers(const ASTFunction & func, std::unordered_set<ASTPtr *> &
{
if (const auto * arg_func = arg->as<ASTFunction>())
{
/// arrayJoin() is special and should not be optimized (think about
/// it as a an aggregate function), otherwise wrong result will be
/// produced:
/// SELECT *, any(arrayJoin([[], []])) FROM numbers(1) GROUP BY number
/// ┌─number─┬─arrayJoin(array(array(), array()))─┐
/// │ 0 │ [] │
/// │ 0 │ [] │
/// └────────┴────────────────────────────────────┘
/// While should be:
/// ┌─number─┬─any(arrayJoin(array(array(), array())))─┐
/// │ 0 │ [] │
/// └────────┴─────────────────────────────────────────┘
if (arg_func->name == "arrayJoin")
return false;
if (arg_func->name == "lambda")
return false;

View File

@ -28,3 +28,5 @@ SELECT
x
FROM numbers(1, 2)
6 6
arrayJoin
0 []

View File

@ -30,3 +30,6 @@ ANALYZE SELECT anyLast(number * 3) AS x, x FROM numbers(1, 2);
SELECT anyLast(number * 3) AS x, x FROM numbers(1, 2);
SELECT any(anyLast(number)) FROM numbers(1); -- { serverError 184 }
SELECT 'arrayJoin';
SELECT *, any(arrayJoin([[], []])) FROM numbers(1) GROUP BY number;