Fix unbin for corner cases

This commit is contained in:
vdimir 2021-07-05 14:56:39 +03:00
parent 231740f2d6
commit dd06866fa8
No known key found for this signature in database
GPG Key ID: F57B3E10A21DBB31
3 changed files with 21 additions and 5 deletions

View File

@ -1420,6 +1420,13 @@ struct UnbinImpl
static void decode(const char * pos, const char * end, char *& out)
{
if (pos == end)
{
*out = '\0';
++out;
return;
}
UInt8 left = 0;
/// end - pos is the length of input.
@ -1431,12 +1438,11 @@ struct UnbinImpl
{
left = left << 1;
if (*pos != '0')
{
left += 1;
}
++pos;
}
if (0 != left)
if (left != 0 || end - pos == 0)
{
*out = left;
++out;
@ -1451,9 +1457,7 @@ struct UnbinImpl
{
c = c << 1;
if (*pos != '0')
{
c += 1;
}
++pos;
}
*out = c;

View File

@ -17,6 +17,7 @@
0011000100110010001100110011001100110010001101000011001000110100
0011000100110010001100110011001100110010001101000011001000110100
1
0
10
测试
@ -25,3 +26,7 @@
0
1
1
1
1
1
1

View File

@ -18,6 +18,7 @@ select bin(toNullable(materialize('12332424')));
select bin(toLowCardinality(materialize('12332424')));
select unbin('');
select unbin('0') == '\0';
select unbin('00110000'); -- 0
select unbin('0011000100110000'); -- 10
select unbin('111001101011010110001011111010001010111110010101'); -- 测试
@ -27,3 +28,9 @@ select unbin(toLowCardinality(materialize('00110000')));
select unbin(bin('')) == '';
select bin(unbin('')) == '';
select bin(unbin('0')) == '00000000';
-- hex and bin consistent for corner cases
select hex('') == bin('');
select unhex('') == unbin('');
select unhex('0') == unbin('0');