```
2022.10.07 18:44:35.062802 [ 3872117 ] {3f28fa72-4f7d-4d38-b28c-dc814c341a9c} <Error> executeQuery: std::exception. Code: 1001, type: std::out_of_range, e.what() = basic_string (version 22.10.1.1) (from 127.0.0.1:40342) (in query: SELECT * FROM hdfsCluster('test_cluster_two_shards_localhost', '', 'TSV') ORDER BY c1 ASC, c2 ASC, c3 DESC), Stack trace (when copying this message, always include the lines below):

0. ./build_docker/../contrib/libcxx/include/exception:133: std::logic_error::logic_error(char const*) @ 0x5c8ff4c1 in /home/ubuntu/memory-ch/clickhouse
1. std::out_of_range::out_of_range(char const*) @ 0xd0e6528 in /home/ubuntu/memory-ch/clickhouse
2. std::__1::__throw_out_of_range(char const*) @ 0xd0e6471 in /home/ubuntu/memory-ch/clickhouse
3. ? @ 0xd0e63dd in /home/ubuntu/memory-ch/clickhouse
4. ./build_docker/../contrib/libcxx/include/string:0: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long, unsigned long, std::__1::allocator<char> const&) @ 0x5c920058 in /home/ubuntu/memory-ch/clickhouse
5. ./build_docker/../contrib/libcxx/include/string:0: DB::(anonymous namespace)::getPathFromUriAndUriWithoutPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x3c716780 in /home/ubuntu/memory-ch/clickhouse
6. ./build_docker/../src/Storages/HDFS/StorageHDFS.cpp:0: DB::StorageHDFS::getTableStructureFromData(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&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<DB::Context const>) @ 0x3c713ed0 in /home/ubuntu/memory-ch/clickhouse
7. ./build_docker/../src/TableFunctions/TableFunctionHDFSCluster.cpp:80: DB::TableFunctionHDFSCluster::getActualTableStructure(std::__1::shared_ptr<DB::Context const>) const @ 0x3bf59725 in /home/ubuntu/memory-ch/clickhouse
8. ./build_docker/../src/TableFunctions/TableFunctionHDFSCluster.cpp:0: DB::TableFunctionHDFSCluster::getStorage(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&, DB::ColumnsDescription const&, std::__1::shared_ptr<DB::Context 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&) const @ 0x3bf5a4df in /home/ubuntu/memory-ch/clickhouse
9. ./build_docker/../src/TableFunctions/ITableFunctionFileLike.cpp:87: DB::ITableFunctionFileLike::executeImpl(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context const>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, DB::ColumnsDescription) const @ 0x3be93299 in /home/ubuntu/memory-ch/clickhouse
10. ./build_docker/../src/TableFunctions/ITableFunction.cpp:0: DB::ITableFunction::execute(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context const>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, DB::ColumnsDescription, bool) const @ 0x3c9985c8 in /home/ubuntu/memory-ch/clickhouse
11. ./build_docker/../src/Interpreters/Context.cpp:1248: DB::Context::executeTableFunction(std::__1::shared_ptr<DB::IAST> const&) @ 0x3f4bd7fc in /home/ubuntu/memory-ch/clickhouse
12. ./build_docker/../src/Interpreters/JoinedTables.cpp:0: DB::JoinedTables::getLeftTableStorage() @ 0x411be9b1 in /home/ubuntu/memory-ch/clickhouse
13. ./build_docker/../src/Interpreters/InterpreterSelectQuery.cpp:0: DB::InterpreterSelectQuery::InterpreterSelectQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context> const&, std::__1::optional<DB::Pipe>, std::__1::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::__1::shared_ptr<DB::PreparedSets>) @ 0x40f9346f in /home/ubuntu/memory-ch/clickhouse
14. ./build_docker/../src/Interpreters/InterpreterSelectQuery.cpp:0: DB::InterpreterSelectQuery::InterpreterSelectQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context> const&, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) @ 0x40f8ed96 in /home/ubuntu/memory-ch/clickhouse
15. ./build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:313: DB::InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(std::__1::shared_ptr<DB::IAST> const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) @ 0x410de59c in /home/ubuntu/memory-ch/clickhouse
16. ./build_docker/../src/Interpreters/InterpreterSelectWithUnionQuery.cpp:146: DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context>, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) @ 0x410d7565 in /home/ubuntu/memory-ch/clickhouse
17. ./build_docker/../contrib/libcxx/include/vector:399: std::__1::__unique_if<DB::InterpreterSelectWithUnionQuery>::__unique_single std::__1::make_unique<DB::InterpreterSelectWithUnionQuery, std::__1::shared_ptr<DB::IAST>&, std::__1::shared_ptr<DB::Context>&, DB::SelectQueryOptions const&>(std::__1::shared_ptr<DB::IAST>&, std::__1::shared_ptr<DB::Context>&, DB::SelectQueryOptions const&) @ 0x40e78078 in /home/ubuntu/memory-ch/clickhouse
18. ./build_docker/../src/Interpreters/InterpreterFactory.cpp:0: DB::InterpreterFactory::get(std::__1::shared_ptr<DB::IAST>&, std::__1::shared_ptr<DB::Context>, DB::SelectQueryOptions const&) @ 0x40e7315c in /home/ubuntu/memory-ch/clickhouse
19. ./build_docker/../src/Interpreters/executeQuery.cpp:0: DB::executeQueryImpl(char const*, char const*, std::__1::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) @ 0x41ce6aa1 in /home/ubuntu/memory-ch/clickhouse
20. ./build_docker/../src/Interpreters/executeQuery.cpp:1104: DB::executeQuery(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum) @ 0x41cdc8bd in /home/ubuntu/memory-ch/clickhouse
21. ./build_docker/../src/Server/TCPHandler.cpp:362: DB::TCPHandler::runImpl() @ 0x4508a9e4 in /home/ubuntu/memory-ch/clickhouse
22. ./build_docker/../src/Server/TCPHandler.cpp:1879: DB::TCPHandler::run() @ 0x450c945e in /home/ubuntu/memory-ch/clickhouse
23. ./build_docker/../contrib/poco/Net/src/TCPServerConnection.cpp:57: Poco::Net::TCPServerConnection::start() @ 0x50c6365e in /home/ubuntu/memory-ch/clickhouse
24. ./build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:312: Poco::Net::TCPServerDispatcher::run() @ 0x50c649ef in /home/ubuntu/memory-ch/clickhouse
25. ./build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:0: Poco::PooledThread::run() @ 0x513802ec in /home/ubuntu/memory-ch/clickhouse
26. ./build_docker/../contrib/poco/Foundation/src/Thread.cpp:56: Poco::(anonymous namespace)::RunnableHolder::run() @ 0x5137bd42 in /home/ubuntu/memory-ch/clickhouse
27. ./build_docker/../contrib/poco/Foundation/src/Thread_POSIX.cpp:360: Poco::ThreadImpl::runnableEntry(void*) @ 0x51377c29 in /home/ubuntu/memory-ch/clickhouse
28. ? @ 0x7ffff7f92609 in ?
29. __clone @ 0x7ffff7eb7163 in ?
```


```
(lldb) bt
* thread #3, name = 'TCPHandler', stop reason = breakpoint 3.1
  * frame #0: 0x000000005c9c2f00 clickhouse`::__cxa_throw(thrown_object=0x000071300014f0c0, tinfo=0x000000005ca0fbc8, dest=(clickhouse`std::logic_error::~logic_error() at stdlib_stdexcept.cpp:22))(void *)) at cxa_exception.cpp:257
    frame #1: 0x000000000d0e64c0 clickhouse`std::__1::__throw_out_of_range(char const*) + 224
    frame #2: 0x000000000d0e63dd clickhouse`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__throw_out_of_range() const + 29
    frame #3: 0x000000005c920058 clickhouse`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(this="", __str=<unavailable>, __pos=18446744073709551615, __n=<unavailable>, __a=<unavailable>) at string:2058:9
    frame #4: 0x000000003c716780 clickhouse`DB::(anonymous namespace)::getPathFromUriAndUriWithoutPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) [inlined] std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::substr(this="", __pos=18446744073709551615, __n=18446744073709551615) const at string:3425:12
    frame #5: 0x000000003c7166f2 clickhouse`DB::(anonymous namespace)::getPathFromUriAndUriWithoutPath(uri="") at StorageHDFS.cpp:124:21
    frame #6: 0x000000003c713ed0 clickhouse`DB::StorageHDFS::getTableStructureFromData(format=<unavailable>, uri="", compression_method=<unavailable>, ctx=std::__1::shared_ptr<const DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7) at StorageHDFS.cpp:190:52
    frame #7: 0x000000003bf59725 clickhouse`DB::TableFunctionHDFSCluster::getActualTableStructure(this=0x0000712000720198, context=std::__1::shared_ptr<const DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7) const at TableFunctionHDFSCluster.cpp:80:16
    frame #8: 0x000000003bf5a4df clickhouse`DB::TableFunctionHDFSCluster::getStorage(this=<unavailable>, (null)=<unavailable>, (null)=<unavailable>, (null)=<unavailable>, context=std::__1::shared_ptr<const DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, table_name=<unavailable>, (null)=<unavailable>) const at TableFunctionHDFSCluster.cpp:112:21
    frame #9: 0x000000003be93299 clickhouse`DB::ITableFunctionFileLike::executeImpl(this=<unavailable>, (null)=<unavailable>, context=std::__1::shared_ptr<const DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, table_name=<unavailable>, (null)=<unavailable>) const at ITableFunctionFileLike.cpp:87:26
    frame #10: 0x000000003c9985c8 clickhouse`DB::ITableFunction::execute(this=<unavailable>, ast_function=<unavailable>, context=std::__1::shared_ptr<const DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, table_name=<unavailable>, cached_columns=<unavailable>, use_global_context=<unavailable>) const at ITableFunction.cpp:36:16
    frame #11: 0x000000003f4bd7fc clickhouse`DB::Context::executeTableFunction(this=<unavailable>, table_expression=std::__1::shared_ptr<DB::IAST>::element_type @ 0x000070f0000f9ac8 strong=3 weak=2) at Context.cpp:1248:35
    frame #12: 0x00000000411be9b1 clickhouse`DB::JoinedTables::getLeftTableStorage(this=0x00007ffff3b0a4c0) at JoinedTables.cpp:209:44
    frame #13: 0x0000000040f9346f clickhouse`DB::InterpreterSelectQuery::InterpreterSelectQuery(this=0x000071c0001a0000, query_ptr_=std::__1::shared_ptr<DB::IAST>::element_type @ 0x00007090000d6248 strong=1 weak=2, context_=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, input_pipe_= Has Value=false , storage_=nullptr, options_=0x000070e000070310, required_result_column_names=size=0, metadata_snapshot_=nullptr, prepared_sets_=nullptr) at InterpreterSelectQuery.cpp:408:33
    frame #14: 0x0000000040f8ed96 clickhouse`DB::InterpreterSelectQuery::InterpreterSelectQuery(this=0x000071c0001a0000, query_ptr_=std::__1::shared_ptr<DB::IAST>::element_type @ 0x00007090000d6248 strong=1 weak=2, context_=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, options_=0x000070e000070310, required_result_column_names_=size=0) at InterpreterSelectQuery.cpp:192:7
    frame #15: 0x00000000410de59c clickhouse`DB::InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(std::__1::shared_ptr<DB::IAST> const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) [inlined] std::__1::__unique_if<DB::InterpreterSelectQuery>::__unique_single std::__1::make_unique<DB::InterpreterSelectQuery, std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context>&, DB::SelectQueryOptions&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&>(__args=<unavailable>, __args=<unavailable>, __args=<unavailable>, __args=<unavailable>) at unique_ptr.h:725:32
    frame #16: 0x00000000410de506 clickhouse`DB::InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(this=0x000070e0000702a0, ast_ptr_=std::__1::shared_ptr<DB::IAST>::element_type @ 0x00007090000d6248 strong=1 weak=2, current_required_result_column_names=size=0) at InterpreterSelectWithUnionQuery.cpp:244:16
    frame #17: 0x00000000410d7565 clickhouse`DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(this=0x000070e0000702a0, query_ptr_=<unavailable>, context_=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, options_=<unavailable>, required_result_column_names=<unavailable>) at InterpreterSelectWithUnionQuery.cpp:146:13
    frame #18: 0x0000000040e78078 clickhouse`std::__1::__unique_if<DB::InterpreterSelectWithUnionQuery>::__unique_single std::__1::make_unique<DB::InterpreterSelectWithUnionQuery, std::__1::shared_ptr<DB::IAST>&, std::__1::shared_ptr<DB::Context>&, DB::SelectQueryOptions const&>(__args=std::__1::shared_ptr<DB::IAST>::element_type @ 0x0000711000126618 strong=2 weak=2, __args=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, __args=0x00007ffff3b0c7e0) at unique_ptr.h:725:32
    frame #19: 0x0000000040e7315c clickhouse`DB::InterpreterFactory::get(query=<unavailable>, context=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, options=<unavailable>) at InterpreterFactory.cpp:128:16
    frame #20: 0x0000000041ce6aa1 clickhouse`DB::executeQueryImpl(begin=<unavailable>, end=<unavailable>, context=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, internal=<unavailable>, stage=<unavailable>, istr=<unavailable>) at executeQuery.cpp:662:27
    frame #21: 0x0000000041cdc8bd clickhouse`DB::executeQuery(query=<unavailable>, context=std::__1::shared_ptr<DB::Context>::element_type @ 0x00007250000f2800 strong=14 weak=7, internal=<unavailable>, stage=<unavailable>) at executeQuery.cpp:1104:30
    frame #22: 0x000000004508a9e4 clickhouse`DB::TCPHandler::runImpl(this=0x000071a0000f0000) at TCPHandler.cpp:362:24
    frame #23: 0x00000000450c945e clickhouse`DB::TCPHandler::run(this=0x000071a0000f0000) at TCPHandler.cpp:1879:9
    frame #24: 0x0000000050c6365e clickhouse`Poco::Net::TCPServerConnection::start(this=<unavailable>) at TCPServerConnection.cpp:43:3
    frame #25: 0x0000000050c649ef clickhouse`Poco::Net::TCPServerDispatcher::run(this=0x0000710000091b00) at TCPServerDispatcher.cpp:115:20
    frame #26: 0x00000000513802ec clickhouse`Poco::PooledThread::run(this=<unavailable>) at ThreadPool.cpp:199:14
    frame #27: 0x000000005137bd42 clickhouse`Poco::(anonymous namespace)::RunnableHolder::run(this=<unavailable>) at Thread.cpp:55:11
    frame #28: 0x0000000051377c29 clickhouse`Poco::ThreadImpl::runnableEntry(pThread=<unavailable>) at Thread_POSIX.cpp:345:27
    frame #29: 0x00007ffff7f92609 libpthread.so.0`start_thread(arg=<unavailable>) at pthread_create.c:477:8
    frame #30: 0x00007ffff7eb7163 libc.so.6`__clone + 67
```
{
            if (context_->getSettingsRef().hdfs_create_new_file_on_insert)
            {
                auto pos = uris[0].find_first_of('.', uris[0].find_last_of('/'));
                size_t index = uris.size();
                String new_uri;
                do
                {
                    new_uri = uris[0].substr(0, pos) + "." + std::to_string(index) + (pos == std::string::npos ? "" : uris[0].substr(pos));
                    ++index;
                }
                while (!hdfsExists(fs.get(), new_uri.c_str()));
                uris.push_back(new_uri);
                current_uri = new_uri;
            }
            else
                throw Exception(
                    ErrorCodes::BAD_ARGUMENTS,
                    "File with path {} already exists. If you want to overwrite it, enable setting hdfs_truncate_on_insert, "
                    "if you want to create new file on each insert, enable setting hdfs_create_new_file_on_insert",
                    path_from_uri);
        }
        return std::make_shared<HDFSSink>(current_uri,
            format_name,
            metadata_snapshot->getSampleBlock(),
            context_,
            chooseCompressionMethod(current_uri, compression_method));
    }
}
void StorageHDFS::truncate(const ASTPtr & , const StorageMetadataPtr &, ContextPtr local_context, TableExclusiveLockHolder &)