**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
*  throw out unnecessary columns based on the entire query. In unnecessary parts of the query, we will not execute subqueries.
        */
    const ASTSelectQuery & query = *query_analyzer.getSelectQuery();
    const Context & context = query_analyzer.context;
    const Settings & settings = context.getSettingsRef();
    const ConstStoragePtr & storage = query_analyzer.storage();
    bool finalized = false;
    size_t where_step_num = 0;
    auto finalize_chain = [&](ExpressionActionsChain & chain)
    {
        chain.finalize();
        if (!finalized)
        {
            finalize(chain, where_step_num);
            finalized = true;
        }
        chain.clear();
    };
    if (storage)
    {
        query_analyzer.makeSetsForIndex(query.where());
        query_analyzer.makeSetsForIndex(query.prewhere());
    }
    {
        ExpressionActionsChain chain(context);
        Names additional_required_columns_after_prewhere;
        if (storage && (query.sampleSize() || settings.parallel_replicas_count > 1))
        {
            Names columns_for_sampling = metadata_snapshot->getColumnsRequiredForSampling();
            additional_required_columns_after_prewhere.insert(additional_required_columns_after_prewhere.end(),