**Describe what's wrong**
Logical error during query

**Does it reproduce on recent release?**

Yes

**Enable crash reporting**

**How to reproduce**

22.3.13
22.10

```
SELECT if(materialize(0), extract(materialize(CAST('aaaaaa', 'LowCardinality(String)')), '\\w'), extract(materialize(CAST('bbbbb', 'LowCardinality(String)')), '\\w*')) AS res
FROM numbers(2)

Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Bad cast from type DB::ColumnLowCardinality to DB::ColumnString. (LOGICAL_ERROR)

SELECT if(materialize(0), extract(materialize(CAST('aaaaaa', 'LowCardinality(String)')), '\\w'), extract(materialize(CAST('bbbbb', 'LowCardinality(String)')), '\\w*')) AS res
FROM numbers(2)
SETTINGS short_circuit_function_evaluation = 'disable'

Query id: 12b09223-0b1c-4336-9d6c-7312f754fc28

┌─res───┐
│ bbbbb │
│ bbbbb │
└───────┘
```

**Expected behavior**

Query should work

**Additional context**

It's also possible that we also have crash somewhere nearby
Stacktrace & LOGICAL_ERROR looks similar to this https://github.com/ClickHouse/ClickHouse/issues/30980

```
2022.10.31 16:03:49.028819 [ 25199 ] {} <Fatal> BaseDaemon: Address: 0x1 Access: read. Address not mapped to object.
2022.10.31 16:03:49.031334 [ 25199 ] {} <Fatal> BaseDaemon: Stack trace: 0xb3b04a0 0x167b8f63 0x167b8a60 0x167b7e4a 0x167ab2a3 0x1679ecfe 0x1679e04b 0x1679d918 0x1679c3c4 0x15bba2b3 0x164cbf48 0x164d06f2 0x167576fb 0x19a16f8f 0x19a193e1 0x19bd63a9 0x19bd3700 0x7fb7d9ab3ea5 0x7fb7d97dc9fd
2022.10.31 16:03:49.037644 [ 25199 ] {} <Fatal> BaseDaemon: 2. void DB::writeAnyEscapedString<(char)39, false>(char const*, char const*, DB::WriteBuffer&) @ 0xb3b04a0 in /usr/bin/clickhouse
2022.10.31 16:03:49.047634 [ 25199 ] {} <Fatal> BaseDaemon: 3. DB::IRowOutputFormat::write(std::__1::vector<COW<DB::IColumn>::immutable_ptr<DB::IColumn>, std::__1::allocator<COW<DB::IColumn>::immutable_ptr<DB::IColumn> > > const&, unsigned long) @ 0x167b8f63 in /usr/bin/clickhouse
2022.10.31 16:03:49.047665 [ 25199 ] {} <Fatal> BaseDaemon: 4. DB::IRowOutputFormat::consume(DB::Chunk) @ 0x167b8a60 in /usr/bin/clickhouse
2022.10.31 16:03:49.049235 [ 25199 ] {} <Fatal> BaseDaemon: 5. DB::IOutputFormat::work() @ 0x167b7e4a in /usr/bin/clickhouse
2022.10.31 16:03:49.050920 [ 25199 ] {} <Fatal> BaseDaemon: 6. DB::ExecutionThreadContext::executeTask() @ 0x167ab2a3 in /usr/bin/clickhouse
2022.10.31 16:03:49.050942 [ 25199 ] {} <Fatal> BaseDaemon: 7. DB::PipelineExecutor::executeStepImpl(unsigned long, std::__1::atomic<bool>*) @ 0x1679ecfe in /usr/bin/clickhouse
2022.10.31 16:03:49.050960 [ 25199 ] {} <Fatal> BaseDaemon: 8. DB::PipelineExecutor::executeImpl(unsigned long) @ 0x1679e04b in /usr/bin/clickhouse
2022.10.31 16:03:49.053886 [ 25199 ] {} <Fatal> BaseDaemon: 9. DB::PipelineExecutor::execute(unsigned long) @ 0x1679d918 in /usr/bin/clickhouse
2022.10.31 16:03:49.056971 [ 25199 ] {} <Fatal> BaseDaemon: 10. DB::CompletedPipelineExecutor::execute() @ 0x1679c3c4 in /usr/bin/clickhouse
2022.10.31 16:03:49.061374 [ 25199 ] {} <Fatal> BaseDaemon: 11. DB::executeQuery(DB::ReadBuffer&, DB::WriteBuffer&, bool, std::__1::shared_ptr<DB::Context>, std::__1::function<void (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::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>, std::__1::optional<DB::FormatSettings> const&) @ 0x15bba2b3 in /usr/bin/clickhouse
2022.10.31 16:03:49.061495 [ 25199 ] {} <Fatal> BaseDaemon: 12. DB::HTTPHandler::processQuery(DB::HTTPServerRequest&, DB::HTMLForm&, DB::HTTPServerResponse&, DB::HTTPHandler::Output&, std::__1::optional<DB::CurrentThread::QueryScope>&) @ 0x164cbf48 in /usr/bin/clickhouse
2022.10.31 16:03:49.061600 [ 25199 ] {} <Fatal> BaseDaemon: 13. DB::HTTPHandler::handleRequest(DB::HTTPServerRequest&, DB::HTTPServerResponse&) @ 0x164d06f2 in /usr/bin/clickhouse
2022.10.31 16:03:49.061703 [ 25199 ] {} <Fatal> BaseDaemon: 14. DB::HTTPServerConnection::run() @ 0x167576fb in /usr/bin/clickhouse
2022.10.31 16:03:49.061733 [ 25199 ] {} <Fatal> BaseDaemon: 15. Poco::Net::TCPServerConnection::start() @ 0x19a16f8f in /usr/bin/clickhouse
2022.10.31 16:03:49.061750 [ 25199 ] {} <Fatal> BaseDaemon: 16. Poco::Net::TCPServerDispatcher::run() @ 0x19a193e1 in /usr/bin/clickhouse
2022.10.31 16:03:49.061773 [ 25199 ] {} <Fatal> BaseDaemon: 17. Poco::PooledThread::run() @ 0x19bd63a9 in /usr/bin/clickhouse
2022.10.31 16:03:49.061790 [ 25199 ] {} <Fatal> BaseDaemon: 18. Poco::ThreadImpl::runnableEntry(void*) @ 0x19bd3700 in /usr/bin/clickhouse
2022.10.31 16:03:49.061826 [ 25199 ] {} <Fatal> BaseDaemon: 19. start_thread @ 0x7ea5 in /usr/lib64/libpthread-2.17.so
2022.10.31 16:03:49.061857 [ 25199 ] {} <Fatal> BaseDaemon: 20. __clone @ 0xfe9fd in /usr/lib64/libc-2.17.so
2022.10.31 16:03:52.823399 [ 25199 ] {} <Fatal> BaseDaemon: Calculated checksum of the binary: 503E001D1437A91819E6548330C8B27F. There is no information about the reference checksum.

```
{
    const ColumnString & column_string = typeid_cast<const ColumnString &>(column);
    const ColumnString::Chars & data = column_string.getChars();
    const ColumnString::Offsets & offsets = column_string.getOffsets();
    size_t size = column.size();
    if (!size)
        return;
    size_t end = limit && offset + limit < size
        ? offset + limit
        : size;
    if (offset == 0)
    {
        UInt64 str_size = offsets[0] - 1;
        writeVarUInt(str_size, ostr);
        ostr.write(reinterpret_cast<const char *>(data.data()), str_size);
        ++offset;
    }
    for (size_t i = offset; i < end; ++i)
    {
        UInt64 str_size = offsets[i] - offsets[i - 1] - 1;
        writeVarUInt(str_size, ostr);
        ostr.write(reinterpret_cast<const char *>(&data[offsets[i - 1]]), str_size);
    }
}
template <int UNROLL_TIMES>
static NO_INLINE void deserializeBinarySSE2(ColumnString::Chars & data, ColumnString::Offsets & offsets, ReadBuffer & istr, size_t limit)