Skip to content

Commit 3f0d07e

Browse files
author
Alexander Barkov
committed
MDEV-9372 select 100 between 1 and 9223372036854775808 returns false
Integer comparison of INT expressions with different signess in BETWEEN is not safe. Switching to DECIMAL comparison in case if INT arguments have different signess.
1 parent 0991e19 commit 3f0d07e

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

mysql-test/r/bigint.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,9 @@ a
502502
SELECT * FROM t1 WHERE a IN (0.8,0.9);
503503
a
504504
DROP TABLE t1;
505+
#
506+
# MDEV-9372 select 100 between 1 and 9223372036854775808 returns false
507+
#
508+
SELECT 100 BETWEEN 1 AND 9223372036854775808;
509+
100 BETWEEN 1 AND 9223372036854775808
510+
1

mysql-test/t/bigint.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,8 @@ SELECT * FROM t1 WHERE a=0.9;
409409
SELECT * FROM t1 WHERE a IN (0.8,0.9);
410410

411411
DROP TABLE t1;
412+
413+
--echo #
414+
--echo # MDEV-9372 select 100 between 1 and 9223372036854775808 returns false
415+
--echo #
416+
SELECT 100 BETWEEN 1 AND 9223372036854775808;

sql/item_cmpfunc.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ static int cmp_row_type(Item* item1, Item* item2)
160160

161161
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
162162
{
163-
uint i;
163+
uint unsigned_count= items[0]->unsigned_flag;
164164
type[0]= items[0]->cmp_type();
165-
for (i= 1 ; i < nitems ; i++)
165+
for (uint i= 1 ; i < nitems ; i++)
166166
{
167+
unsigned_count+= items[i]->unsigned_flag;
167168
type[0]= item_cmp_type(type[0], items[i]->cmp_type());
168169
/*
169170
When aggregating types of two row expressions we have to check
@@ -175,6 +176,12 @@ static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
175176
if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
176177
return 1; // error found: invalid usage of rows
177178
}
179+
/**
180+
If all arguments are of INT type but have different unsigned_flag values,
181+
switch to DECIMAL_RESULT.
182+
*/
183+
if (type[0] == INT_RESULT && unsigned_count != nitems && unsigned_count != 0)
184+
type[0]= DECIMAL_RESULT;
178185
return 0;
179186
}
180187

0 commit comments

Comments
 (0)