**Describe the bug**
I use MaterializeMySQL to replicate data from MySQL to ClickHouse. It's crashed in DDL stage. It seems that clickhouse accesses an invalid address.

**How to reproduce**
just create the following table in MySQL, and then create MaterializeMySQL table in ClickHouse.
CREATE TABLE `test1` (
  `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `id` bigint NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint NOT NULL,
   PRIMARY KEY (`id`),
  UNIQUE KEY `code_id` (`code`, `tenant_id`),
  UNIQUE KEY `name_id` (`name`, `tenant_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


* Which ClickHouse server version to use
I have tested version 20.10.6.27 and 20.12.3.3. Both have this problem.



* `CREATE TABLE` statements for all tables involved
In MySQL side:
CREATE TABLE `test1` (
  `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `id` bigint NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint NOT NULL,
   PRIMARY KEY (`id`),
  UNIQUE KEY `code_id` (`code`, `tenant_id`),
  UNIQUE KEY `name_id` (`name`, `tenant_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

**Expected behavior**
create MaterializeMySQL table success in ClickHouse side.

**Error message and/or stacktrace**

stack of version 20.10.6.27
Cannot access memory at address 0x7f69ca4a2128
Cannot access memory at address 0x7f69ca4a2120
Failed to read a valid object file image from memory.
Core was generated by `./clickhouse-server'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000015f106cc in std::__1::shared_ptr<DB::IAST>::shared_ptr (__r=..., this=0x9640) at ../contrib/libcxx/include/memory:4057
4057	../contrib/libcxx/include/memory: No such file or directory.
[Current thread is 1 (LWP 10227)]
(gdb) bt
#0  0x0000000015f106cc in std::__1::shared_ptr<DB::IAST>::shared_ptr (__r=..., this=0x9640) at ../contrib/libcxx/include/memory:4057
#1  std::__1::allocator<std::__1::shared_ptr<DB::IAST> >::construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (__p=0x9640, this=0x7f6809975d10)
    at ../contrib/libcxx/include/memory:1876
#2  std::__1::allocator_traits<std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::__construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (
    __p=0x9640, __a=...) at ../contrib/libcxx/include/memory:1768
#3  std::__1::allocator_traits<std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (
    __p=0x9640, __a=...) at ../contrib/libcxx/include/memory:1595
#4  std::__1::__split_buffer<std::__1::shared_ptr<DB::IAST>, std::__1::allocator<std::__1::shared_ptr<DB::IAST> >&>::__construct_at_end<std::__1::__wrap_iter<std::__1::shared_ptr<DB::IAST>*> > (this=0x7f67f7de7460, __first=..., __last=...) at ../contrib/libcxx/include/__split_buffer:275
#5  0x0000000015f0ec4e in std::__1::vector<std::__1::shared_ptr<DB::IAST>, std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::insert<std::__1::__wrap_iter<std::__1::shared_ptr<DB::IAST>*> > (this=0x7f6809975d00, __position=..., __first=..., __last=...) at ../contrib/libcxx/include/vector:2009
#6  0x0000000015edb55a in DB::MySQLInterpreter::getKeys (columns_define=0x7f6809975c98, indices_define=0x7f6809975b58, context=..., columns=...)
    at ../src/Interpreters/MySQL/InterpretersMySQLDDLQuery.cpp:177
#7  0x0000000015eeffb3 in DB::MySQLInterpreter::InterpreterCreateImpl::getRewrittenQueries (create_query=..., context=..., mapped_to_database=..., mysql_database=...)
    at ../src/Interpreters/MySQL/InterpretersMySQLDDLQuery.cpp:365
#8  0x0000000015d5623f in DB::MySQLInterpreter::InterpreterMySQLDDLQuery<DB::MySQLInterpreter::InterpreterCreateImpl>::execute (this=0x7f67f7dec7b0)
    at ../src/Interpreters/MySQL/InterpretersMySQLDDLQuery.h:68
#9  0x0000000015d53782 in DB::InterpreterExternalDDLQuery::execute (this=0x7f6809971c40) at ../src/Interpreters/InterpreterExternalDDLQuery.cpp:64
#10 0x000000001427afc6 in DB::executeQueryImpl (
    begin=0x7f6809893600 "/*Materialize MySQL step 1: execute MySQL DDL for dump data*/ EXTERNAL DDL FROM MySQL(ch23, ch23)  CREATE TABLE `test1` (\n  `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n  `name` v"..., end=0x7f68098937ff "", context=..., internal=true, stage=DB::QueryProcessingStage::Complete, 
    has_query_tail=true, istr=0x0) at ../src/Interpreters/executeQuery.cpp:422
#11 0x000000001427ece1 in DB::executeQuery (query=..., context=..., internal=true, stage=DB::QueryProcessingStage::Complete, may_have_embedded_data=false)
    at ../src/Interpreters/executeQuery.cpp:718
#12 0x0000000015796c28 in DB::tryToExecuteQuery (query_to_execute=..., query_context=..., database=..., comment=...)
    at ../src/Databases/MySQL/MaterializeMySQLSyncThread.cpp:62
#13 0x000000001579f9d6 in DB::dumpDataForTables(mysqlxx::Pool::Entry &, DB::MaterializeMetadata &, const DB::String &, const DB::String &, const DB::String &, const DB::Context &, const std::__1::function<bool()> &) (connection=..., master_info=..., query_prefix=..., database_name=..., mysql_database_name=..., context=..., is_cancelled=...)
    at ../src/Databases/MySQL/MaterializeMySQLSyncThread.cpp:277
#14 0x00000000157a2737 in DB::MaterializeMySQLSyncThread::<lambda()>::operator()(void) const (__closure=0x7f67f7df4028)
    at ../src/Databases/MySQL/MaterializeMySQLSyncThread.cpp:327
#15 0x00000000157b7f99 in std::__1::__invoke<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>&> (__f=...)
    at ../contrib/libcxx/include/type_traits:3519
#16 std::__1::__invoke_void_return_wrapper<void>::__call<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>&>(void) ()





stack of version 20.12.3.3
@
Cannot access memory at address 0x7f6a12d5e128
Cannot access memory at address 0x7f6a12d5e120
Failed to read a valid object file image from memory.
Core was generated by `./clickhouse-server'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000098a60e8 in std::__1::shared_ptr<DB::IAST>::shared_ptr (__r=..., 
    this=0xac1e080 <llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::InterleavedLoadCombine>()+128>) at ../contrib/libcxx/include/new:220
220	../contrib/libcxx/include/new: No such file or directory.
[Current thread is 1 (LWP 44708)]
(gdb) bt
#0  0x00000000098a60e8 in std::__1::shared_ptr<DB::IAST>::shared_ptr (__r=..., 
    this=0xac1e080 <llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::InterleavedLoadCombine>()+128>) at ../contrib/libcxx/include/new:220
#1  std::__1::allocator<std::__1::shared_ptr<DB::IAST> >::construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (
    __p=0xac1e080 <llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::InterleavedLoadCombine>()+128>, this=0x7f689ae15090) at ../contrib/libcxx/include/memory:1876
#2  std::__1::allocator_traits<std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::__construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (
    __p=0xac1e080 <llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::InterleavedLoadCombine>()+128>, __a=...) at ../contrib/libcxx/include/memory:1768
#3  std::__1::allocator_traits<std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::construct<std::__1::shared_ptr<DB::IAST>, std::__1::shared_ptr<DB::IAST>&> (
    __p=0xac1e080 <llvm::Pass* llvm::callDefaultCtor<(anonymous namespace)::InterleavedLoadCombine>()+128>, __a=...) at ../contrib/libcxx/include/memory:1595
#4  std::__1::__split_buffer<std::__1::shared_ptr<DB::IAST>, std::__1::allocator<std::__1::shared_ptr<DB::IAST> >&>::__construct_at_end<std::__1::__wrap_iter<std::__1::shared_ptr<DB::IAST>*> > (__last=..., __first=..., this=0x7f6891bf4de0) at ../contrib/libcxx/include/__split_buffer:275
#5  std::__1::vector<std::__1::shared_ptr<DB::IAST>, std::__1::allocator<std::__1::shared_ptr<DB::IAST> > >::insert<std::__1::__wrap_iter<std::__1::shared_ptr<DB::IAST>*> > (this=0x7f689ae15080, __position=..., __first=..., __last=...) at ../contrib/libcxx/include/vector:2009
#6  0x000000000989e4f8 in DB::MySQLInterpreter::getKeys (columns_define=0x7f6a113018b8, indices_define=<optimized out>, context=..., columns=...)
    at ../src/Interpreters/MySQL/InterpretersMySQLDDLQuery.cpp:176
#7  0x00000000098a09c4 in DB::MySQLInterpreter::InterpreterCreateImpl::getRewrittenQueries (create_query=..., context=..., mapped_to_database=..., mysql_database=...)
    at ../src/Interpreters/MySQL/InterpretersMySQLDDLQuery.cpp:365
#8  0x0000000009833d85 in DB::MySQLInterpreter::InterpreterMySQLDDLQuery<DB::MySQLInterpreter::InterpreterCreateImpl>::execute (this=0x7f6891bf5320)
    at ../contrib/libcxx/include/memory:3474
#9  DB::InterpreterExternalDDLQuery::execute (this=<optimized out>) at ../src/Interpreters/InterpreterExternalDDLQuery.cpp:64
#10 0x0000000008eb3a63 in DB::executeQueryImpl (begin=<optimized out>, end=<optimized out>, context=..., internal=<optimized out>, 
    stage=DB::QueryProcessingStage::Complete, has_query_tail=true, istr=0x0) at ../src/Interpreters/executeQuery.cpp:482
#11 0x0000000008eb595e in DB::executeQuery (query=..., context=..., internal=internal@entry=true, stage=stage@entry=DB::QueryProcessingStage::Complete, 
    may_have_embedded_data=may_have_embedded_data@entry=false) at ../src/Interpreters/executeQuery.cpp:813
#12 0x0000000009682381 in DB::tryToExecuteQuery (query_to_execute=..., query_context=..., database=..., comment=...) at ../contrib/libcxx/include/string:1526
#13 0x000000000968d41d in DB::dumpDataForTables(mysqlxx::Pool::Entry &, DB::MaterializeMetadata &, const DB::String &, const DB::String &, const DB::String &, const DB::Context &, const std::__1::function<bool()> &) (connection=..., master_info=..., query_prefix=..., database_name=..., mysql_database_name=..., context=..., is_cancelled=...)
    at ../contrib/libcxx/include/string:1526
#14 0x000000000968eaa0 in operator() (__closure=<optimized out>) at ../contrib/libcxx/include/new:220
#15 std::__1::__invoke<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>&> (__f=...) at ../contrib/libcxx/include/type_traits:3519
#16 std::__1::__invoke_void_return_wrapper<void>::__call<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>&> ()
    at ../contrib/libcxx/include/__functional_base:348
#17 std::__1::__function::__alloc_func<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>, std::__1::allocator<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()> >, void()>::operator() (this=<optimized out>) at ../contrib/libcxx/include/functional:1540
#18 std::__1::__function::__func<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()>, std::__1::allocator<DB::MaterializeMySQLSyncThread::prepareSynchronized(const String&)::<lambda()> >, void()>::operator()(void) (this=<optimized out>) at ../contrib/libcxx/include/functional:1714
--Type <RET> for more, q to quit, c to continue without paging--
#19 0x0000000009ac0275 in std::__1::__function::__value_func<void ()>::operator()() const (this=<optimized out>) at ../src/Databases/MySQL/MaterializeMetadata.cpp:197
#20 std::__1::function<void ()>::operator()() const (this=<optimized out>) at ../contrib/libcxx/include/functional:2473
#21 DB::commitMetadata(std::__1::function<void ()> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (function=..., persistent_tmp_path=..., persistent_path=...)
    at ../src/Databases/MySQL/MaterializeMetadata.cpp:197
#22 0x0000000009ac269e in DB::MaterializeMetadata::transaction(DB::MySQLReplication::Position const&, std::__1::function<void ()> const&) (this=this@entry=0x7f6891bf85c0, 
    position=..., fun=...) at ../src/Databases/MySQL/MaterializeMetadata.cpp:231
#23 0x0000000009683bf5 in DB::MaterializeMySQLSyncThread::prepareSynchronized (this=0x7f68925d5ca8, mysql_version=...) at ../contrib/libcxx/include/new:220
#24 0x000000000968b834 in DB::MaterializeMySQLSyncThread::synchronization (this=0x7f68925d5ca8, mysql_version=...)
    at ../src/Databases/MySQL/MaterializeMySQLSyncThread.cpp:169
#25 0x000000000968bd8b in operator() (__closure=0x7f6891bf9c20) at ../src/Databases/MySQL/MaterializeMySQLSyncThread.cpp:223
#26 std::__1::__invoke_constexpr<DB::MaterializeMySQLSyncThread::startSynchronization()::<lambda()>&> (__f=...) at ../contrib/libcxx/include/type_traits:3525
#27 std::__1::__apply_tuple_impl<DB::MaterializeMySQLSyncThread::startSynchronization()::<lambda()>&, std::__1::tuple<>&> (__t=<synthetic pointer>..., __f=...)
    at ../contrib/libcxx/include/tuple:1415
#28 std::__1::apply<DB::MaterializeMySQLSyncThread::startSynchronization()::<lambda()>&, std::__1::tuple<>&> (__t=<synthetic pointer>..., __f=...)
    at ../contrib/libcxx/include/tuple:1424
#29 operator() (this=<optimized out>) at ../src/Common/ThreadPool.h:178
#30 0x00000000039e7425 in std::__1::__function::__value_func<void ()>::operator()() const (this=0x7f6891bf9e90) at ../contrib/libcxx/include/atomic:676
#31 std::__1::function<void ()>::operator()() const (this=0x7f6891bf9e90) at ../contrib/libcxx/include/functional:2473
#32 ThreadPoolImpl<std::__1::thread>::worker (this=0x7f6a1122e700, thread_it=...) at ../src/Common/ThreadPool.cpp:236

**Additional context**
If I remove the 'UNIQUE' and it works fine. 


@zhang2014  Please take a look when you are free.
{
    if (rename_query.exchange)
        throw Exception("Cannot execute exchange for external ddl query.", ErrorCodes::NOT_IMPLEMENTED);
}
ASTs InterpreterRenameImpl::getRewrittenQueries(
    const InterpreterRenameImpl::TQuery & rename_query, const Context & context, const String & mapped_to_database, const String & mysql_database)
{
    ASTRenameQuery::Elements elements;
    for (const auto & rename_element : rename_query.elements)
    {
        const auto & to_database =  resolveDatabase(rename_element.to.database, mysql_database, mapped_to_database, context);
        const auto & from_database =  resolveDatabase(rename_element.from.database, mysql_database, mapped_to_database, context);
        if ((from_database == mapped_to_database || to_database == mapped_to_database) && to_database != from_database)
            throw Exception("Cannot rename with other database for external ddl query.", ErrorCodes::NOT_IMPLEMENTED);
        if (from_database == mapped_to_database)
        {
            elements.push_back(ASTRenameQuery::Element());
            elements.back().from.database = mapped_to_database;
            elements.back().from.table = rename_element.from.table;
            elements.back().to.database = mapped_to_database;
            elements.back().to.table = rename_element.to.table;
        }
    }
    if (elements.empty())
        return ASTs{};
    auto rewritten_query = std::make_shared<ASTRenameQuery>();
    rewritten_query->elements = elements;
    return ASTs{rewritten_query};
}
void InterpreterAlterImpl::validate(const InterpreterAlterImpl::TQuery & , const Context & )