**Describe what's wrong**

When inserting data to a table with `Object('JSON')` datatype, querying it might raise an error from MergeTreeThread.


After calling `OPTIMIZE TABLE test_json FINAL` the issue goes away.

**Does it reproduce on recent release?**

It reproduces on `22.3.2 revision 54455.`


**How to reproduce**

Create a table with an `Object('JSON')` datatype:

```
create table production.test_json (data Object('JSON')) ENGINE = MergeTree() ORDER BY tuple();
```

and inserted some data into it:

```
insert into test_json SELECT replace(toString(m), '\'', '"') as data FROM (SELECT properties as m from production.events_v4 where notEmpty(properties) limit 100000000)
```

When you describe the table, we can see it has `data.reason`:

```
│ data.reason               │ String │              │                    │         │                  │                │            1 │
```

However, querying for it raises an error:

```
ec2.internal :) select data.reason from test_json where data.reason <> '' limit 10;

SELECT data.reason
FROM test_json
WHERE data.reason != ''
LIMIT 10

Query id: 91e682c1-2650-4f3b-99d8-5eafdd0e2839

[ip-172-31-90-127] 2022.03.18 13:29:50.471519 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Debug> executeQuery: (from 127.0.0.1:60630) select data.reason from test_json where data.reason <> '' limit 10;
[ip-172-31-90-127] 2022.03.18 13:29:50.471953 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Trace> ContextAccess (default): Access granted: SELECT(`data.reason`) ON production.test_json
[ip-172-31-90-127] 2022.03.18 13:29:50.472015 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Trace> ContextAccess (default): Access granted: SELECT(`data.reason`) ON production.test_json
[ip-172-31-90-127] 2022.03.18 13:29:50.472041 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Trace> InterpreterSelectQuery: FetchColumns -> Complete
[ip-172-31-90-127] 2022.03.18 13:29:50.472134 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Debug> production.test_json (463749c6-4417-490e-939b-66e00b80baa2) (SelectExecutor): Key condition: unknown
[ip-172-31-90-127] 2022.03.18 13:29:50.472401 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Debug> production.test_json (463749c6-4417-490e-939b-66e00b80baa2) (SelectExecutor): Selected 8/8 parts by partition key, 8 parts by primary key, 12453/12453 marks by primary key, 12453 marks to read from 8 ranges
[ip-172-31-90-127] 2022.03.18 13:29:50.472457 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Debug> production.test_json (463749c6-4417-490e-939b-66e00b80baa2) (SelectExecutor): Reading approx. 102000000 rows with 2 streams
[ip-172-31-90-127] 2022.03.18 13:29:50.489145 [ 311286 ] {91e682c1-2650-4f3b-99d8-5eafdd0e2839} <Error> executeQuery: Code: 49. DB::Exception: Can't adjust last granule because it has 8161 rows, but try to subtract 65505 rows.: While executing MergeTreeThread. (LOGICAL_ERROR) (version 22.3.2.1) (from 127.0.0.1:60630) (in query: select data.reason from test_json where data.reason <> '' limit 10;), 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) @ 0xa4dde1a in /usr/bin/clickhouse
1. DB::Exception::Exception<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(int, 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> >&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) @ 0xbeaf7d9 in /usr/bin/clickhouse
2. DB::MergeTreeRangeReader::ReadResult::adjustLastGranule() @ 0x1577c250 in /usr/bin/clickhouse
3. DB::MergeTreeRangeReader::startReadingChain(unsigned long, std::__1::deque<DB::MarkRange, std::__1::allocator<DB::MarkRange> >&) @ 0x15780044 in /usr/bin/clickhouse
4. DB::MergeTreeRangeReader::read(unsigned long, std::__1::deque<DB::MarkRange, std::__1::allocator<DB::MarkRange> >&) @ 0x1577e815 in /usr/bin/clickhouse
5. DB::MergeTreeBaseSelectProcessor::readFromPartImpl() @ 0x15774028 in /usr/bin/clickhouse
6. DB::MergeTreeBaseSelectProcessor::readFromPart() @ 0x1577512d in /usr/bin/clickhouse
7. DB::MergeTreeBaseSelectProcessor::generate() @ 0x157738a0 in /usr/bin/clickhouse
8. DB::ISource::tryGenerate() @ 0x1548cdf5 in /usr/bin/clickhouse
9. DB::ISource::work() @ 0x1548c9ba in /usr/bin/clickhouse
10. DB::SourceWithProgress::work() @ 0x156e4282 in /usr/bin/clickhouse
11. DB::ExecutionThreadContext::executeTask() @ 0x154ad143 in /usr/bin/clickhouse
12. DB::PipelineExecutor::executeStepImpl(unsigned long, std::__1::atomic<bool>*) @ 0x154a0b9e in /usr/bin/clickhouse
13. ? @ 0x154a2504 in /usr/bin/clickhouse
14. ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) @ 0xa584c97 in /usr/bin/clickhouse
15. ? @ 0xa58881d in /usr/bin/clickhouse
16. ? @ 0x7f421c9c6609 in ?
17. __clone @ 0x7f421c8eb163 in ?


0 rows in set. Elapsed: 0.018 sec.

Received exception from server (version 22.3.2):
Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Can't adjust last granule because it has 8161 rows, but try to subtract 65505 rows.: While executing MergeTreeThread. (LOGICAL_ERROR)
```

**Expected behavior**

No exception raised.
{
    if (!other.initialized)
        return;
    if (!initialized)
    {
        hyperrectangle = other.hyperrectangle;
        initialized = true;
    }
    else
    {
        for (size_t i = 0; i < hyperrectangle.size(); ++i)
        {
            hyperrectangle[i].left = std::min(hyperrectangle[i].left, other.hyperrectangle[i].left);
            hyperrectangle[i].right = std::max(hyperrectangle[i].right, other.hyperrectangle[i].right);
        }
    }
}
void IMergeTreeDataPart::MinMaxIndex::appendFiles(const MergeTreeData & data, Strings & files)
{
    auto metadata_snapshot = data.getInMemoryMetadataPtr();
    const auto & partition_key = metadata_snapshot->getPartitionKey();
    auto minmax_column_names = data.getMinMaxColumnsNames(partition_key);
    size_t minmax_idx_size = minmax_column_names.size();
    for (size_t i = 0; i < minmax_idx_size; ++i)
    {
        String file_name = "minmax_" + escapeForFileName(minmax_column_names[i]) + ".idx";
        files.push_back(file_name);
    }
}
static void incrementStateMetric(IMergeTreeDataPart::State state)