Looks like the clang analyzer was right after all...

```
SELECT '-1E9-1E9-1E9-1E9' AS x, toDecimal32(x, 0)




clickhouse-server: /home/akuzm/ch4/ch/src/IO/readDecimalText.h:165: void DB::readDecimalText(DB::ReadBuffer &, T &, uint32_t, uint32_t &, bool) [T = DB::Decimal<int>]: Assertion `divisor > 0' failed.
2020.07.31 17:12:07.801360 [ 1343156 ] {} <Trace> BaseDaemon: Received signal 6
2020.07.31 17:12:07.801685 [ 1343327 ] {} <Fatal> BaseDaemon: ########################################
2020.07.31 17:12:07.802294 [ 1343327 ] {} <Fatal> BaseDaemon: (version 20.7.1.1, build id: 21B0DCC47427FD8E) (from thread 1343197) (query_id: 3a7fd03b-e763-4108-a351-9c0f385be26f) Received signal Aborted (6)
2020.07.31 17:12:07.802424 [ 1343327 ] {} <Fatal> BaseDaemon: 
2020.07.31 17:12:07.802597 [ 1343327 ] {} <Fatal> BaseDaemon: Stack trace: 0x7feacc9b818b 0x7feacc997859 0x7feacc997729 0x7feacc9a8f36 0x7feac0c3ea0d 0x7feac0c3eb1e 0x7feac69c1215 0x7feac69bc4fb 0x7feac69ba4a7 0x7feac69ba0d7 0x7feac69b9608 0x7feac67c4581 0x7feac67c3a8f 0x7feac6fe6053 0x7feac6fe5c2e 0x7feac6fe5fd0 0x7feac6fe715d 0x7feabfaa9b86 0x7feabfaad407 0x7feabfaacd33 0x7feabf6d30c2 0x7feabf6de966 0x7feabf6d64da 0x7feabf6d34c7 0x7feabfacca2d 0x7feabfada0e6 0x7feabfacca3f 0x7feabfac3bcd
2020.07.31 17:12:07.802898 [ 1343327 ] {} <Fatal> BaseDaemon: 4. /build/glibc-YYA7BZ/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51: raise @ 0x4618b in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.31 17:12:07.803048 [ 1343327 ] {} <Fatal> BaseDaemon: 5. /build/glibc-YYA7BZ/glibc-2.31/stdlib/abort.c:81: abort @ 0x25859 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.31 17:12:07.803266 [ 1343327 ] {} <Fatal> BaseDaemon: 6. /build/glibc-YYA7BZ/glibc-2.31/intl/loadmsgcat.c:509: _nl_load_domain.cold @ 0x25729 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.31 17:12:07.803529 [ 1343327 ] {} <Fatal> BaseDaemon: 7. ? @ 0x36f36 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.31 17:12:07.804359 [ 1343327 ] {} <Fatal> BaseDaemon: 8. /home/akuzm/ch4/ch/src/IO/readDecimalText.h:166: void DB::readDecimalText<DB::Decimal<int> >(DB::ReadBuffer&, DB::Decimal<int>&, unsigned int, unsigned int&, bool) @ 0x4a7a0d in /home/akuzm/ch4/build-clang10/src/libclickhouse_datatypesd.so
2020.07.31 17:12:07.805018 [ 1343327 ] {} <Fatal> BaseDaemon: 9. /home/akuzm/ch4/ch/src/DataTypes/DataTypesDecimal.cpp:82: DB::DataTypeDecimal<DB::Decimal<int> >::readText(DB::Decimal<int>&, DB::ReadBuffer&, unsigned int, unsigned int, bool) @ 0x4a7b1e in /home/akuzm/ch4/build-clang10/src/libclickhouse_datatypesd.so
2020.07.31 17:12:07.815157 [ 1343327 ] {} <Fatal> BaseDaemon: 10. /home/akuzm/ch4/ch/src/Functions/FunctionsConversion.h:693: void DB::ConvertThroughParsing<DB::DataTypeString, DB::DataTypeDecimal<DB::Decimal<int> >, DB::NameToDecimal32, (DB::ConvertFromStringExceptionMode)0, (DB::ConvertFromStringParsingMode)0>::execute<unsigned int>(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, unsigned int) @ 0x2a81215 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.824591 [ 1343327 ] {} <Fatal> BaseDaemon: 11. /home/akuzm/ch4/ch/src/Functions/FunctionsConversion.h:1085: bool DB::FunctionConvert<DB::DataTypeDecimal<DB::Decimal<int> >, DB::NameToDecimal32, DB::UnknownMonotonicity>::executeInternal(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) const::'lambda'(auto const&)::operator()<DB::TypePair<DB::DataTypeString, DB::DataTypeDecimal<DB::Decimal<int> > > >(auto const&) const @ 0x2a7c4fb in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.833869 [ 1343327 ] {} <Fatal> BaseDaemon: 12. /home/akuzm/ch4/ch/src/Core/callOnTypeIndex.h:185: bool DB::callOnIndexAndDataType<DB::DataTypeDecimal<DB::Decimal<int> >, DB::FunctionConvert<DB::DataTypeDecimal<DB::Decimal<int> >, DB::NameToDecimal32, DB::UnknownMonotonicity>::executeInternal(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) const::'lambda'(DB::DataTypeDecimal<DB::Decimal<int> > const&)&>(DB::TypeIndex, DB::NameToDecimal32&&) @ 0x2a7a4a7 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.843408 [ 1343327 ] {} <Fatal> BaseDaemon: 13. /home/akuzm/ch4/ch/src/Functions/FunctionsConversion.h:1088: DB::FunctionConvert<DB::DataTypeDecimal<DB::Decimal<int> >, DB::NameToDecimal32, DB::UnknownMonotonicity>::executeInternal(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) const @ 0x2a7a0d7 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.852731 [ 1343327 ] {} <Fatal> BaseDaemon: 14. /home/akuzm/ch4/ch/src/Functions/FunctionsConversion.h:1002: DB::FunctionConvert<DB::DataTypeDecimal<DB::Decimal<int> >, DB::NameToDecimal32, DB::UnknownMonotonicity>::executeImpl(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) const @ 0x2a79608 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.853198 [ 1343327 ] {} <Fatal> BaseDaemon: 15. /home/akuzm/ch4/ch/src/Functions/IFunctionImpl.h:202: DB::IFunction::executeImplDryRun(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) const @ 0x2884581 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.853566 [ 1343327 ] {} <Fatal> BaseDaemon: 16. /home/akuzm/ch4/ch/src/Functions/IFunctionAdaptors.h:156: DB::DefaultExecutable::executeDryRun(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long) @ 0x2883a8f in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.854916 [ 1343327 ] {} <Fatal> BaseDaemon: 17. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:323: DB::ExecutableFunctionAdaptor::executeWithoutLowCardinalityColumns(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30a6053 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.856287 [ 1343327 ] {} <Fatal> BaseDaemon: 18. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:266: DB::ExecutableFunctionAdaptor::defaultImplementationForConstantArguments(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30a5c2e in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.857614 [ 1343327 ] {} <Fatal> BaseDaemon: 19. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:316: DB::ExecutableFunctionAdaptor::executeWithoutLowCardinalityColumns(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30a5fd0 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.859012 [ 1343327 ] {} <Fatal> BaseDaemon: 20. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:486: DB::ExecutableFunctionAdaptor::execute(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30a715d in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.31 17:12:07.860048 [ 1343327 ] {} <Fatal> BaseDaemon: 21. /home/akuzm/ch4/ch/src/Interpreters/ExpressionActions.cpp:206: DB::ExpressionAction::prepare(DB::Block&, DB::Settings const&, std::__1::unordered_set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::equal_to<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> > > >&) @ 0x11acb86 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.861112 [ 1343327 ] {} <Fatal> BaseDaemon: 22. /home/akuzm/ch4/ch/src/Interpreters/ExpressionActions.cpp:625: DB::ExpressionActions::addImpl(DB::ExpressionAction, 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> > > >&) @ 0x11b0407 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.862125 [ 1343327 ] {} <Fatal> BaseDaemon: 23. /home/akuzm/ch4/ch/src/Interpreters/ExpressionActions.cpp:586: DB::ExpressionActions::add(DB::ExpressionAction 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> > > >&) @ 0x11afd33 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.862440 [ 1343327 ] {} <Fatal> BaseDaemon: 24. /home/akuzm/ch4/ch/src/Interpreters/ActionsVisitor.cpp:430: DB::ScopeStack::addAction(DB::ExpressionAction const&) @ 0xdd60c2 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.862888 [ 1343327 ] {} <Fatal> BaseDaemon: 25. /home/akuzm/ch4/ch/src/Interpreters/ActionsVisitor.h:117: DB::ActionsMatcher::Data::addAction(DB::ExpressionAction const&) @ 0xde1966 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.863187 [ 1343327 ] {} <Fatal> BaseDaemon: 26. /home/akuzm/ch4/ch/src/Interpreters/ActionsVisitor.cpp:770: DB::ActionsMatcher::visit(DB::ASTFunction const&, std::__1::shared_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0xdd94da in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.863469 [ 1343327 ] {} <Fatal> BaseDaemon: 27. /home/akuzm/ch4/ch/src/Interpreters/ActionsVisitor.cpp:487: DB::ActionsMatcher::visit(std::__1::shared_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0xdd64c7 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.864668 [ 1343327 ] {} <Fatal> BaseDaemon: 28. /home/akuzm/ch4/ch/src/Interpreters/InDepthNodeVisitor.h:32: DB::InDepthNodeVisitor<DB::ActionsMatcher, true, std::__1::shared_ptr<DB::IAST> const>::visit(std::__1::shared_ptr<DB::IAST> const&) @ 0x11cfa2d in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.868059 [ 1343327 ] {} <Fatal> BaseDaemon: 29. /home/akuzm/ch4/ch/src/Interpreters/InDepthNodeVisitor.h:45: DB::InDepthNodeVisitor<DB::ActionsMatcher, true, std::__1::shared_ptr<DB::IAST> const>::visitChildren(std::__1::shared_ptr<DB::IAST> const&) @ 0x11dd0e6 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.869405 [ 1343327 ] {} <Fatal> BaseDaemon: 30. /home/akuzm/ch4/ch/src/Interpreters/InDepthNodeVisitor.h:35: DB::InDepthNodeVisitor<DB::ActionsMatcher, true, std::__1::shared_ptr<DB::IAST> const>::visit(std::__1::shared_ptr<DB::IAST> const&) @ 0x11cfa3f in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.31 17:12:07.870459 [ 1343327 ] {} <Fatal> BaseDaemon: 31. /home/akuzm/ch4/ch/src/Interpreters/ExpressionAnalyzer.cpp:396: DB::ExpressionAnalyzer::getRootActions(std::__1::shared_ptr<DB::IAST> const&, bool, std::__1::shared_ptr<DB::ExpressionActions>&, bool) @ 0x11c6bcd in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
```
if (adaptive_parts >= parts.size() / 2)
        index_granularity_bytes = data_settings->index_granularity_bytes;
    const size_t max_marks_to_use_cache = roundRowsOrBytesToMarks(
        settings.merge_tree_max_rows_to_use_cache,
        settings.merge_tree_max_bytes_to_use_cache,
        data_settings->index_granularity,
        index_granularity_bytes);
    if (sum_marks > max_marks_to_use_cache)
        use_uncompressed_cache = false;
    Pipes pipes;
    for (const auto & part : parts)
    {
        auto source_processor = std::make_shared<MergeTreeSelectProcessor>(
            data, metadata_snapshot, part.data_part, max_block_size, settings.preferred_block_size_bytes,
            settings.preferred_max_column_in_block_size_bytes, column_names, part.ranges, use_uncompressed_cache,
            query_info.prewhere_info, true, reader_settings,
            virt_columns, part.part_index_in_query);
        Pipe pipe(std::move(source_processor));
        
        if (!out_projection)
            out_projection = createProjection(pipe, data);
        pipe.addSimpleTransform(std::make_shared<ExpressionTransform>(pipe.getHeader(), metadata_snapshot->getSortingKey().expression));
        pipes.emplace_back(std::move(pipe));
    }
    Names sort_columns = metadata_snapshot->getSortingKeyColumns();
    SortDescription sort_description;
    size_t sort_columns_size = sort_columns.size();
    sort_description.reserve(sort_columns_size);
    Names partition_key_columns = metadata_snapshot->getPartitionKey().column_names;
    Block header = pipes.at(0).getHeader();