Fix signedness conversion monotonicity

This commit is contained in:
Amos Bird 2020-09-14 11:34:14 +08:00
parent c2f762e20a
commit 654245af3c
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
3 changed files with 17 additions and 17 deletions

View File

@ -1570,25 +1570,15 @@ struct ToNumberMonotonicity
if (left.isNull() || right.isNull())
return {};
if (from_is_unsigned == to_is_unsigned)
{
/// all bits other than that fits, must be same.
if (divideByRangeOfType(left.get<UInt64>()) == divideByRangeOfType(right.get<UInt64>()))
return {true};
/// Function cannot be monotonic when left and right are not on the same ranges.
if (divideByRangeOfType(left.get<UInt64>()) != divideByRangeOfType(right.get<UInt64>()))
return {};
}
if (to_is_unsigned)
return {true};
else
{
/// When signedness is changed, it's also required for arguments to be from the same half.
/// And they must be in the same half after converting to the result type.
if (left_in_first_half == right_in_first_half
&& (T(left.get<Int64>()) >= 0) == (T(right.get<Int64>()) >= 0)
&& divideByRangeOfType(left.get<UInt64>()) == divideByRangeOfType(right.get<UInt64>()))
return {true};
return {};
}
// If To is signed, it's possible that the signedness is different after conversion. So we check it explicitly.
return {(T(left.get<UInt64>()) >= 0) == (T(right.get<UInt64>()) >= 0)};
}
__builtin_unreachable();

View File

@ -0,0 +1,9 @@
drop table if exists test1;
create table test1 (i Int64) engine MergeTree order by i;
insert into test1 values (53), (1777), (53284);
select count() from test1 where toInt16(i) = 1777;
drop table if exists test1;