**Describe what's wrong**

Hierarchical dictionaries with sparse_hashed layout can't be loaded (take too long time). 
Works fine in 21.3. Does not work in 21.11. 

**Does it reproduce on recent release?**

Yes. 21.11.5.33

**How to reproduce**

Dictionary creation
```

drop table if exists default.hierdictsrc;

create table default.hierdictsrc (parent UInt64, child UInt64) Engine=MergeTree ORDER BY tuple();

insert into default.hierdictsrc 
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child from (
		select arrayJoin(range(0,breadth)) as parent, range(0,breadth) as child
)
UNION ALL
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child from (
		select arrayJoin(range(breadth,breadth*breadth)) as parent, range(0,breadth) as child
)
UNION ALL
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child from (
		select arrayJoin(range(breadth*breadth,breadth*breadth*breadth)) as parent, range(0,breadth) as child
)
;

select count(*) from default.hierdictsrc ;
drop dictionary if exists default.hierdict;

CREATE DICTIONARY default.hierdict
(
    `child` UInt64,
    `parent` UInt64 HIERARCHICAL
)
PRIMARY KEY child
SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' DB 'default' TABLE 'hierdictsrc'))
LIFETIME(MIN 300 MAX 86400)
LAYOUT(SPARSE_HASHED());

system reload dictionary default.hierdict;

```

v21.3 Loads it correctly: 
```
server21.3 :) system reload dictionary default.hierdict;

SYSTEM RELOAD DICTIONARY default.hierdict

Query id: 504c1c83-ebab-4445-9654-6b5656104067

[server21.3] 2021.12.09 20:35:06.688338 [ 15173 ] {504c1c83-ebab-4445-9654-6b5656104067} <Debug> executeQuery: (from [::ffff:10.10.10.134]:50834, using production parser) system reload dictionary default.hierdict;
[server21.3] 2021.12.09 20:35:06.688964 [ 15173 ] {504c1c83-ebab-4445-9654-6b5656104067} <Trace> ContextAccess (default): Access granted: SYSTEM RELOAD DICTIONARY ON *.*
[server21.3] 2021.12.09 20:35:06.689516 [ 15173 ] {504c1c83-ebab-4445-9654-6b5656104067} <Trace> ExternalDictionariesLoader: Will load the object 'default.hierdict' in background, force = true, loading_id = 21682
[server21.3] 2021.12.09 20:35:49.375847 [ 15173 ] {504c1c83-ebab-4445-9654-6b5656104067} <Debug> MemoryTracker: Peak memory usage (for query): 0.00 B.
Ok.
```

v21.11 loads infinitely and never finishes. The table gets locked and the only way is to drop table is to restart clickhouse (Engine ordinary). 

```
server21.11 :) system reload dictionary default.hierdict;

SYSTEM RELOAD DICTIONARY default.hierdict

Query id: c766d74a-e2ce-4694-a31c-cdecfd40765d

[server21.11] 2021.12.09 20:31:57.707631 [ 11723 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> executeQuery: (from [::1]:59362) system reload dictionary default.hierdict;
Timeout exceeded while receiving data from server. Waited for 300 seconds, timeout is 300 seconds.
Cancelling query.

```

Last related entries in log 
```
2021.12.09 20:31:57.707631 [ 11723 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> executeQuery: (from [::1]:59362) system reload dictionary default.hierdict;
2021.12.09 20:31:57.708693 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> LOCAL-Session: 32f9972c-c11e-4648-b2f9-972cc11e1648 Authenticating user 'default' from 127.0.0.1:0
2021.12.09 20:31:57.708727 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> LOCAL-Session: 32f9972c-c11e-4648-b2f9-972cc11e1648 Authenticated with global context as user 94309d50-4f52-525
0-31bd-74fecac179db
2021.12.09 20:31:57.708754 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> LOCAL-Session: 32f9972c-c11e-4648-b2f9-972cc11e1648 Creating query context from global context, user_id: 94309d
50-4f52-5250-31bd-74fecac179db, parent context user: <NOT SET>
2021.12.09 20:31:57.709145 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> executeQuery: (internal) SELECT `child`, `parent` FROM `default`.`hierdictsrc`;
2021.12.09 20:31:57.709549 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> default.hierdictsrc (SelectExecutor): Key condition: unknown
2021.12.09 20:31:57.709857 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> default.hierdictsrc (SelectExecutor): Selected 2/2 parts by partition key, 2 parts by primary key, 5001/5001 ma
rks by primary key, 5001 marks to read from 2 ranges
2021.12.09 20:31:57.709986 [ 11747 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> default.hierdictsrc (SelectExecutor): Reading approx. 40960000 rows with 8 streams

....

2021.12.09 20:46:17.689121 [ 11723 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Debug> MemoryTracker: Peak memory usage (for query): 8.51 MiB.
2021.12.09 20:46:17.689923 [ 11723 ] {c766d74a-e2ce-4694-a31c-cdecfd40765d} <Error> executeQuery: Code: 210. DB::NetException: I/O error: Broken pipe, while writing to socket ([::1]:59362). (NETWORK_ERROR) (version 21.11.5.33 (official build)) (from [::1]:59362) (in query: system reload dictionary default.hierdict;), Stack trace (when copying this message, always include the lines below):

0. DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0x9b737d4 in /usr/bin/clickhouse
1. DB::WriteBufferFromPocoSocket::nextImpl() @ 0x11f84a5c in /usr/bin/clickhouse
2. DB::TCPHandler::sendEndOfStream() @ 0x13128029 in /usr/bin/clickhouse
3. DB::TCPHandler::runImpl() @ 0x1311f861 in /usr/bin/clickhouse
4. DB::TCPHandler::run() @ 0x13132fd9 in /usr/bin/clickhouse
5. Poco::Net::TCPServerConnection::start() @ 0x15d6e96f in /usr/bin/clickhouse
6. Poco::Net::TCPServerDispatcher::run() @ 0x15d70d61 in /usr/bin/clickhouse
7. Poco::PooledThread::run() @ 0x15e85709 in /usr/bin/clickhouse
8. Poco::ThreadImpl::runnableEntry(void*) @ 0x15e82e40 in /usr/bin/clickhouse
9. start_thread @ 0x7ea5 in /usr/lib64/libpthread-2.17.so
10. __clone @ 0xfe8dd in /usr/lib64/libc-2.17.so
```




WHen dictionary is using layout HASHED - it works fine in 21.11 too. 

```
drop table if exists default.hierdictsrc;

create table default.hierdictsrc (parent UInt64, child UInt64, lvl UInt8) Engine=MergeTree ORDER BY tuple();

insert into default.hierdictsrc 
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child, 1 as lvl from (
		select arrayJoin(range(0,breadth)) as parent, range(0,breadth) as child
)
UNION ALL
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child, 2 as lvl from (
		select arrayJoin(range(breadth,breadth*breadth)) as parent, range(0,breadth) as child
)
UNION ALL
with 80 as breadth
select parent, arrayJoin(child)+parent*breadth as child, 3 as lvl from (
		select arrayJoin(range(breadth*breadth,breadth*breadth*breadth)) as parent, range(0,breadth) as child
)
;

select count(*) from default.hierdictsrc ;
drop dictionary if exists default.hierdict;

CREATE DICTIONARY default.hierdict
(
    `child` UInt64,
    `parent` UInt64 HIERARCHICAL
)
PRIMARY KEY child
SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' DB 'default' TABLE 'hierdictsrc'))
LIFETIME(MIN 300 MAX 86400)
LAYOUT(HASHED());
system reload dictionary default.hierdict;
```

Result:

```
server21.11 :) system reload dictionary default.hierdict;

SYSTEM RELOAD DICTIONARY default.hierdict

Query id: a8eab9d5-f75f-453d-86d9-41374c677fbb

[server21.11] 2021.12.09 20:56:49.091200 [ 11723 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> executeQuery: (from [::1]:59370) system reload dictionary default.hierdict;
[server21.11] 2021.12.09 20:56:49.091329 [ 11723 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): Access granted: SYSTEM RELOAD DICTIONARY ON *.*
[server21.11] 2021.12.09 20:56:49.091524 [ 11723 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ExternalDictionariesLoader: Will load the object 'default.hierdict' in background, force = true, loading_id = 7
[server21.11] 2021.12.09 20:56:49.091640 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ExternalDictionariesLoader: Start loading object 'default.hierdict'
[server21.11] 2021.12.09 20:56:49.091943 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> LOCAL-Session: 37124aa6-fc39-4b1f-b712-4aa6fc39eb1f Authenticating user 'default' from 127.0.0.1:0
[server21.11] 2021.12.09 20:56:49.091996 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> LOCAL-Session: 37124aa6-fc39-4b1f-b712-4aa6fc39eb1f Authenticated with global context as user 94309d50-4f52-5250-31bd-74fecac179db
[server21.11] 2021.12.09 20:56:49.092026 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> LOCAL-Session: 37124aa6-fc39-4b1f-b712-4aa6fc39eb1f Creating query context from global context, user_id: 94309d50-4f52-5250-31bd-74fecac179db, parent context user: <NOT SET>
[server21.11] 2021.12.09 20:56:49.092074 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): Settings: readonly=0, allow_ddl=true, allow_introspection_functions=true
[server21.11] 2021.12.09 20:56:49.092091 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): List of all grants: GRANT SHOW, SELECT, INSERT, ALTER, CREATE, DROP, TRUNCATE, OPTIMIZE, KILL QUERY, MOVE PARTITION BETWEEN SHARDS, SYSTEM, dictGet, INTROSPECTION, SOURCES ON *.*
[server21.11] 2021.12.09 20:56:49.092104 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): List of all grants including implicit: GRANT SHOW, SELECT, INSERT, ALTER, CREATE, DROP, TRUNCATE, OPTIMIZE, KILL QUERY, MOVE PARTITION BETWEEN SHARDS, SYSTEM, dictGet, INTROSPECTION, SOURCES ON *.*
[server21.11] 2021.12.09 20:56:49.092140 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): Settings: readonly=0, allow_ddl=true, allow_introspection_functions=true
[server21.11] 2021.12.09 20:56:49.092160 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): List of all grants: GRANT SHOW, SELECT, INSERT, ALTER, CREATE, DROP, TRUNCATE, OPTIMIZE, KILL QUERY, MOVE PARTITION BETWEEN SHARDS, SYSTEM, dictGet, INTROSPECTION, SOURCES ON *.*
[server21.11] 2021.12.09 20:56:49.092190 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): List of all grants including implicit: GRANT SHOW, SELECT, INSERT, ALTER, CREATE, DROP, TRUNCATE, OPTIMIZE, KILL QUERY, MOVE PARTITION BETWEEN SHARDS, SYSTEM, dictGet, INTROSPECTION, SOURCES ON *.*
[server21.11] 2021.12.09 20:56:49.092255 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> DictionaryFactory: Created dictionary source 'ClickHouse: default.hierdictsrc' for dictionary 'default.hierdict'
[server21.11] 2021.12.09 20:56:49.092408 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> executeQuery: (internal) SELECT `child`, `parent` FROM `default`.`hierdictsrc`;
[server21.11] 2021.12.09 20:56:49.092711 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ContextAccess (default): Access granted: SELECT(parent, child) ON default.hierdictsrc
[server21.11] 2021.12.09 20:56:49.092778 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> InterpreterSelectQuery: FetchColumns -> Complete
[server21.11] 2021.12.09 20:56:49.092891 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> default.hierdictsrc (SelectExecutor): Key condition: unknown
[server21.11] 2021.12.09 20:56:49.093237 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> default.hierdictsrc (SelectExecutor): Selected 2/2 parts by partition key, 2 parts by primary key, 5001/5001 marks by primary key, 5001 marks to read from 2 ranges
[server21.11] 2021.12.09 20:56:49.093373 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> default.hierdictsrc (SelectExecutor): Reading approx. 40960000 rows with 8 streams
[server21.11] 2021.12.09 20:56:51.649314 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> MemoryTracker: Current memory usage (for query): 1.01 GiB.
[server21.11] 2021.12.09 20:56:54.239137 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> MemoryTracker: Current memory usage (for query): 2.01 GiB.
[server21.11] 2021.12.09 20:56:55.988792 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ExternalDictionariesLoader: Supposed update time for 'default.hierdict' is 2021-12-10 10:26:52 (loaded, lifetime [300, 86400], no errors)
[server21.11] 2021.12.09 20:56:55.988807 [ 11831 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Trace> ExternalDictionariesLoader: Next update time for 'default.hierdict' was set to 2021-12-10 10:26:52
[server21.11] 2021.12.09 20:56:55.988982 [ 11723 ] {a8eab9d5-f75f-453d-86d9-41374c677fbb} <Debug> MemoryTracker: Peak memory usage (for query): 2.01 GiB.
Ok.

0 rows in set. Elapsed: 6.899 sec.
```



**Expected behavior**

Sparse hashed layout dictionary shall work fine as they used to work in 21.3.

ColumnPtr getHierarchy(ColumnPtr key_column, const DataTypePtr & hierarchy_attribute_type) const override;
    ColumnUInt8::Ptr isInHierarchy(
        ColumnPtr key_column,
        ColumnPtr in_key_column,
        const DataTypePtr & key_type) const override;
    ColumnPtr getDescendants(
        ColumnPtr key_column,
        const DataTypePtr & key_type,
        size_t level) const override;
    Pipe read(const Names & column_names, size_t max_block_size, size_t num_streams) const override;
private:
    template <typename Value>
    using CollectionTypeNonSparse = std::conditional_t<
        dictionary_key_type == DictionaryKeyType::Simple,
        HashMap<UInt64, Value>,
        HashMapWithSavedHash<StringRef, Value, DefaultHash<StringRef>>>;
    template <typename Value>
    using CollectionTypeSparse = std::conditional_t<
        dictionary_key_type == DictionaryKeyType::Simple,
        SparseHashMap<UInt64, Value>,
        SparseHashMap<StringRef, Value>>;
    template <typename Value>
    using CollectionType = std::conditional_t<sparse, CollectionTypeSparse<Value>, CollectionTypeNonSparse<Value>>;
    using NullableSet = HashSet<KeyType, DefaultHash<KeyType>>;
    struct Attribute final
    {
        AttributeUnderlyingType type;
        std::optional<NullableSet> is_nullable_set;