```
CREATE TABLE default.pk ( `d` Date DEFAULT '2000-01-01', `x` DateTime, `y` UInt64, `z` UInt64 ) ENGINE = MergeTree(d, (toStartOfMinute(x), y, z), 1);

SELECT toUInt32(x), y, z FROM pk WHERE (x >= toDateTime(120)) AND (x <= toDateTime(NULL));



clickhouse-server: /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:295: bool DB::ExecutableFunctionAdaptor::defaultImplementationForNulls(DB::Block &, const DB::ColumnNumbers &, size_t, size_t, bool): Assertion `result_type->isNullable()' failed.
2020.07.13 16:31:20.073915 [ 49673 ] {} <Trace> BaseDaemon: Received signal 6
2020.07.13 16:31:20.074204 [ 49749 ] {} <Fatal> BaseDaemon: ########################################
2020.07.13 16:31:20.074579 [ 49749 ] {} <Fatal> BaseDaemon: (version 20.6.1.1, build id: F176DE2A5C050ABF) (from thread 49729) (query_id: e8bccb87-47ba-419b-b9f7-4b8ef1fbb049) Received signal Aborted (6)
2020.07.13 16:31:20.074705 [ 49749 ] {} <Fatal> BaseDaemon: 
2020.07.13 16:31:20.074879 [ 49749 ] {} <Fatal> BaseDaemon: Stack trace: 0x7ff5832f918b 0x7ff5832d8859 0x7ff5832d8729 0x7ff5832e9f36 0x7ff57d9791f2 0x7ff57d979034 0x7ff57d978c5e 0x7ff57d979000 0x7ff57d97a18d 0x7ff57d21b1b9 0x7ff573fb1929 0x7ff573fb1492 0x7ff573fb0869 0x7ff573faff06 0x7ff573fafe5a 0x7ff573fafc4c 0x7ff574123cfa 0x7ff5741237f0 0x7ff574f6d039 0x7ff571e3fdd2 0x7ff576bef674 0x7ff576be4263 0x7ff576bde35d 0x7ff576bdd9d6 0x7ff576c4ad68 0x7ff576c4af96 0x7ff576f70c0a 0x7ff576f6fcca
2020.07.13 16:31:20.075196 [ 49749 ] {} <Fatal> BaseDaemon: 4. /build/glibc-YYA7BZ/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51: __GI_raise @ 0x4618b in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.13 16:31:20.075365 [ 49749 ] {} <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.13 16:31:20.075591 [ 49749 ] {} <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.13 16:31:20.075873 [ 49749 ] {} <Fatal> BaseDaemon: 7. ? @ 0x36f36 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.31.so
2020.07.13 16:31:20.077364 [ 49749 ] {} <Fatal> BaseDaemon: 8. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:311: DB::ExecutableFunctionAdaptor::defaultImplementationForNulls(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30c11f2 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.078739 [ 49749 ] {} <Fatal> BaseDaemon: 9. /home/akuzm/ch4/ch/src/Functions/IFunction.cpp:319: DB::ExecutableFunctionAdaptor::executeWithoutLowCardinalityColumns(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x30c1034 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.080107 [ 49749 ] {} <Fatal> BaseDaemon: 10. /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) @ 0x30c0c5e in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.081437 [ 49749 ] {} <Fatal> BaseDaemon: 11. /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) @ 0x30c1000 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.083187 [ 49749 ] {} <Fatal> BaseDaemon: 12. /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) @ 0x30c218d in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.085302 [ 49749 ] {} <Fatal> BaseDaemon: 13. /home/akuzm/ch4/ch/src/Functions/IFunction.h:78: DB::IFunctionBase::execute(DB::Block&, std::__1::vector<unsigned long, std::__1::allocator<unsigned long> > const&, unsigned long, unsigned long, bool) @ 0x29631b9 in /home/akuzm/ch4/build-clang10/src/Functions/libclickhouse_functionsd.so
2020.07.13 16:31:20.085867 [ 49749 ] {} <Fatal> BaseDaemon: 14. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:462: DB::applyFunctionForField(std::__1::shared_ptr<DB::IFunctionBase> const&, std::__1::shared_ptr<DB::IDataType const> const&, DB::Field const&) @ 0x60a929 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.086256 [ 49749 ] {} <Fatal> BaseDaemon: 15. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:557: DB::KeyCondition::canConstantBeWrappedByMonotonicFunctions(std::__1::shared_ptr<DB::IAST> const&, unsigned long&, std::__1::shared_ptr<DB::IDataType const>&, DB::Field&, std::__1::shared_ptr<DB::IDataType const>&) @ 0x60a492 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.086624 [ 49749 ] {} <Fatal> BaseDaemon: 16. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:800: DB::KeyCondition::tryParseAtomFromAST(std::__1::shared_ptr<DB::IAST> const&, DB::Context const&, DB::Block&, DB::KeyCondition::RPNElement&) @ 0x609869 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.086986 [ 49749 ] {} <Fatal> BaseDaemon: 17. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:511: DB::KeyCondition::traverseAST(std::__1::shared_ptr<DB::IAST> const&, DB::Context const&, DB::Block&) @ 0x608f06 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.087394 [ 49749 ] {} <Fatal> BaseDaemon: 18. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:498: DB::KeyCondition::traverseAST(std::__1::shared_ptr<DB::IAST> const&, DB::Context const&, DB::Block&) @ 0x608e5a in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.087822 [ 49749 ] {} <Fatal> BaseDaemon: 19. /home/akuzm/ch4/ch/src/Storages/MergeTree/KeyCondition.cpp:401: DB::KeyCondition::KeyCondition(DB::SelectQueryInfo const&, DB::Context 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::ExpressionActions> const&) @ 0x608c4c in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.088860 [ 49749 ] {} <Fatal> BaseDaemon: 20. /home/akuzm/ch4/ch/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp:233: DB::MergeTreeDataSelectExecutor::readFromParts(std::__1::vector<std::__1::shared_ptr<DB::IMergeTreeDataPart const>, std::__1::allocator<std::__1::shared_ptr<DB::IMergeTreeDataPart 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&, DB::SelectQueryInfo const&, DB::Context const&, unsigned long, unsigned int, std::__1::unordered_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, long, 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::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, long> > > const*) const @ 0x77ccfa in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.090030 [ 49749 ] {} <Fatal> BaseDaemon: 21. /home/akuzm/ch4/ch/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp:160: DB::MergeTreeDataSelectExecutor::read(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&, DB::SelectQueryInfo const&, DB::Context const&, unsigned long, unsigned int, std::__1::unordered_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, long, 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::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, long> > > const*) const @ 0x77c7f0 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storages_mergetreed.so
2020.07.13 16:31:20.091588 [ 49749 ] {} <Fatal> BaseDaemon: 22. /home/akuzm/ch4/ch/src/Storages/StorageMergeTree.cpp:184: DB::StorageMergeTree::read(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&, DB::SelectQueryInfo const&, DB::Context const&, DB::QueryProcessingStage::Enum, unsigned long, unsigned int) @ 0xa69039 in /home/akuzm/ch4/build-clang10/src/libclickhouse_storagesd.so
2020.07.13 16:31:20.092414 [ 49749 ] {} <Fatal> BaseDaemon: 23. /home/akuzm/ch4/ch/src/Processors/QueryPlan/ReadFromStorageStep.cpp:40: DB::ReadFromStorageStep::ReadFromStorageStep(std::__1::shared_ptr<DB::RWLockImpl::LockHolderImpl>, std::__1::shared_ptr<DB::StorageInMemoryMetadata const>&, DB::SelectQueryOptions, std::__1::shared_ptr<DB::IStorage>, 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&, DB::SelectQueryInfo const&, std::__1::shared_ptr<DB::Context>, DB::QueryProcessingStage::Enum, unsigned long, unsigned long) @ 0x21bdd2 in /home/akuzm/ch4/build-clang10/src/libclickhouse_processors_querypland.so
2020.07.13 16:31:20.095310 [ 49749 ] {} <Fatal> BaseDaemon: 24. /home/akuzm/ch4/ch/contrib/libcxx/include/memory:3028: std::__1::__unique_if<DB::ReadFromStorageStep>::__unique_single std::__1::make_unique<DB::ReadFromStorageStep, std::__1::shared_ptr<DB::RWLockImpl::LockHolderImpl>&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const>&, DB::SelectQueryOptions&, std::__1::shared_ptr<DB::IStorage>&, 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> > > >&, DB::SelectQueryInfo&, std::__1::shared_ptr<DB::Context>&, DB::QueryProcessingStage::Enum&, unsigned long&, unsigned long&>(std::__1::shared_ptr<DB::RWLockImpl::LockHolderImpl>&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const>&, DB::SelectQueryOptions&, std::__1::shared_ptr<DB::IStorage>&, 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> > > >&, DB::SelectQueryInfo&, std::__1::shared_ptr<DB::Context>&, DB::QueryProcessingStage::Enum&, unsigned long&, unsigned long&) @ 0x15f7674 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.097771 [ 49749 ] {} <Fatal> BaseDaemon: 25. /home/akuzm/ch4/ch/src/Interpreters/InterpreterSelectQuery.cpp:1359: DB::InterpreterSelectQuery::executeFetchColumns(DB::QueryProcessingStage::Enum, DB::QueryPlan&, std::__1::shared_ptr<DB::PrewhereInfo> 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&) @ 0x15ec263 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.100101 [ 49749 ] {} <Fatal> BaseDaemon: 26. /home/akuzm/ch4/ch/src/Interpreters/InterpreterSelectQuery.cpp:793: DB::InterpreterSelectQuery::executeImpl(DB::QueryPlan&, std::__1::shared_ptr<DB::IBlockInputStream> const&, std::__1::optional<DB::Pipe>) @ 0x15e635d in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.102171 [ 49749 ] {} <Fatal> BaseDaemon: 27. /home/akuzm/ch4/ch/src/Interpreters/InterpreterSelectQuery.cpp:472: DB::InterpreterSelectQuery::buildQueryPlan(DB::QueryPlan&) @ 0x15e59d6 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.104538 [ 49749 ] {} <Fatal> BaseDaemon: 28. /home/akuzm/ch4/ch/src/Interpreters/InterpreterSelectWithUnionQuery.cpp:183: DB::InterpreterSelectWithUnionQuery::buildQueryPlan(DB::QueryPlan&) @ 0x1652d68 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.106912 [ 49749 ] {} <Fatal> BaseDaemon: 29. /home/akuzm/ch4/ch/src/Interpreters/InterpreterSelectWithUnionQuery.cpp:198: DB::InterpreterSelectWithUnionQuery::execute() @ 0x1652f96 in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.110977 [ 49749 ] {} <Fatal> BaseDaemon: 30. /home/akuzm/ch4/ch/src/Interpreters/executeQuery.cpp:385: DB::executeQueryImpl(char const*, char const*, DB::Context&, bool, DB::QueryProcessingStage::Enum, bool, DB::ReadBuffer*) @ 0x1978c0a in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
2020.07.13 16:31:20.115112 [ 49749 ] {} <Fatal> BaseDaemon: 31. /home/akuzm/ch4/ch/src/Interpreters/executeQuery.cpp:653: DB::executeQuery(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, DB::Context&, bool, DB::QueryProcessingStage::Enum, bool) @ 0x1977cca in /home/akuzm/ch4/build-clang10/src/libclickhouse_interpretersd.so
```
#include <Storages/MergeTree/KeyCondition.h>
#include <Storages/MergeTree/BoolMask.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/TreeRewriter.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/ExpressionActions.h>
#include <Interpreters/misc.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <Common/FieldVisitorsAccurateComparison.h>
#include <Common/typeid_cast.h>
#include <Interpreters/convertFieldToType.h>
#include <Interpreters/Set.h>
#include <Parsers/queryToString.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTIdentifier.h>
#include <cassert>
namespace DB
{
namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
    extern const int BAD_TYPE_OF_FIELD;
}
String Range::toString() const