**Describe the bug**

The query:
```
SELECT
    (current_database = currentDatabase()) AND (type = 0),
    ((current_database = currentDatabase()) AND (type = NULL)) AND (type = NULL) AND ((current_database = currentDatabase()) AND (type = 1048575)) AND (type = NULL) AND (event_date >= yesterday())
FROM system.query_log
ARRAY JOIN ProfileEvents
WHERE ProfileEvents AND (current_database = currentDatabase())
ORDER BY event_time DESC NULLS FIRST
LIMIT 0
```
crashes the server.

Most likely introduced here: #16618
{
    if (hasPrewhere())
    {
        const ExpressionActionsChain::Step & step = *chain.steps.at(0);
        prewhere_info->remove_prewhere_column = step.can_remove_required_output.at(0);
        NameSet columns_to_remove;
        for (size_t i = 1; i < step.required_output.size(); ++i)
        {
            if (step.can_remove_required_output[i])
                columns_to_remove.insert(step.required_output[i]);
        }
        columns_to_remove_after_prewhere = std::move(columns_to_remove);
    }
    else if (hasFilter())
    {
        
        filter_info->do_remove_column = chain.steps.at(0)->can_remove_required_output.at(0);
    }
    if (hasWhere())
        remove_where_filter = chain.steps.at(where_step_num)->can_remove_required_output.at(0);
}
void ExpressionAnalysisResult::removeExtraColumns() const