```
CREATE TABLE a
(
    `number` UInt64,
    `x` MATERIALIZED x
)
ENGINE = MergeTree
ORDER BY number;
-- that should fail!

insert into a values (1);
Received exception from server (version 21.7.1):
Code: 47. DB::Exception: Received from localhost:9000. DB::Exception: Missing columns: 'x' while processing query: 'CAST(x, 'UInt8') AS x', required columns: 'x' 'x'. 
```
removeUnneededColumnsFromSelectClause(select_query, required_result_columns, remove_duplicates);
    
    executeScalarSubqueries(query, getContext(), subquery_depth, result.scalars, select_options.only_analyze);
    TreeOptimizer::apply(
        query, result.aliases, source_columns_set, tables_with_columns, getContext(), result.metadata_snapshot, result.rewrite_subqueries);
    
    getArrayJoinedColumns(query, result, select_query, result.source_columns, source_columns_set);
    setJoinStrictness(*select_query, settings.join_default_strictness, settings.any_join_distinct_right_table_keys,
                        result.analyzed_join->table_join);
    collectJoinedColumns(*result.analyzed_join, *select_query, tables_with_columns, result.aliases);
    
    if (settings.optimize_respect_aliases && result.metadata_snapshot)
    {
        replaceAliasColumnsInQuery(query, result.metadata_snapshot->getColumns(), result.getArrayJoinSourceNameSet(), getContext());
    }
    result.aggregates = getAggregates(query, *select_query);
    result.window_function_asts = getWindowFunctions(query, *select_query);
    result.collectUsedColumns(query, true);
    result.ast_join = select_query->join();
    if (result.optimize_trivial_count)
        result.optimize_trivial_count = settings.optimize_trivial_count_query &&
            !select_query->groupBy() && !select_query->having() &&
            !select_query->sampleSize() && !select_query->sampleOffset() && !select_query->final() &&
            (tables_with_columns.size() < 2 || isLeft(result.analyzed_join->kind()));
    return std::make_shared<const TreeRewriterResult>(result);
}
TreeRewriterResultPtr TreeRewriter::analyze(