**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
{
    const auto * select_query = getAggregatingQuery();
    if (!select_query->having())
        return false;
    ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns);
    getRootActionsForHaving(select_query->having(), only_types, step.actions());
    step.required_output.push_back(select_query->having()->getColumnName());
    return true;
}
void SelectQueryExpressionAnalyzer::appendSelect(ExpressionActionsChain & chain, bool only_types)
{
    const auto * select_query = getSelectQuery();
    ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns);
    getRootActions(select_query->select(), only_types, step.actions());
    for (const auto & child : select_query->select()->children)
    {
        if (const auto * function = typeid_cast<const ASTFunction *>(child.get());
            function
            && function->is_window_function)
        {
            
            
            continue;
        }
        step.required_output.push_back(child->getColumnName());
    }
}
ActionsDAGPtr SelectQueryExpressionAnalyzer::appendOrderBy(ExpressionActionsChain & chain, bool only_types, bool optimize_read_in_order,