[MDEV-23481] BIGINT UNSIGNED value is out of range in '`test`.`edge`.`x1` - 1' Created: 2020-08-14  Updated: 2021-06-09  Resolved: 2021-06-09

Status: Closed
Project: MariaDB Server
Component/s: Data types
Affects Version/s: 10.5.4
Fix Version/s: 10.5.11

Type: Bug Priority: Critical
Reporter: Georg Richter Assignee: Oleksandr Byelkin
Resolution: Duplicate Votes: 0
Labels: None

Issue Links:
Duplicate
duplicates MDEV-14910 Unexpected "BIGINT UNSIGNED value is ... Open

 Description   

When executing the following example server sends metadata + error packet instead of sending metadata and data:

static int test_ps(MYSQL *mysql)
{
  MYSQL_STMT *stmt;
  int rc, i;
  MYSQL_BIND bind[5];
  unsigned char vals[5]= {1,1,1,1,1};
 
  rc= mysql_query(mysql, "CREATE TEMPORARY TABLE edge ( id INTEGER NOT NULL AUTO_INCREMENT, x1 INTEGER, y1 INTEGER, x2 INTEGER, y2 INTEGER, PRIMARY KEY (id))ENGINE=MyISAM");
  check_mysql_rc(rc, mysql);
 
  rc= mysql_query(mysql, "INSERT INTO edge (x1, y1, x2, y2) VALUES (0, 0, 3, 5)");
  check_mysql_rc(rc, mysql);
 
  rc= mysql_query(mysql, "INSERT INTO edge (x1, y1, x2, y2) VALUES (0, 1, 3, 5)");
  check_mysql_rc(rc, mysql);
 
  stmt= mysql_stmt_init(mysql);
  check_stmt_rc(rc, stmt);
 
  rc= mysql_stmt_prepare(stmt, SL("SELECT edge.id AS edge_id, edge.x1 AS edge_x1, edge.y1 AS edge_y1, edge.x2 AS edge_x2, edge.y2 AS edge_y2 FROM edge WHERE (edge.x1 - ?) * (edge.x1 - ?) + (edge.y1 - ?) * (edge.y1 - ?) <= ?"));
  check_stmt_rc(rc, stmt);
 
  memset(bind, 0, sizeof(MYSQL_BIND) * 5);
  for (i=0; i < 5; i++)
  {
    bind[i].buffer_type= MYSQL_TYPE_TINY;
    bind[i].buffer= &vals[i];
    bind[i].is_unsigned= 1;
  }
 
  rc= mysql_stmt_bind_param(stmt, bind);
  check_stmt_rc(rc, stmt);
 
  rc= mysql_stmt_execute(stmt);
  check_stmt_rc(rc, stmt);
 
  /* Error will be read here */
  rc= mysql_stmt_store_result(stmt);
  check_stmt_rc(rc, stmt);
 
  mysql_stmt_close(stmt);
  return OK;
}

Output:

Error: BIGINT UNSIGNED value is out of range in '`test`.`edge`.`x1` - 1'

Running the same statement in text protocol returns correct result:

mysql> SELECT edge.id AS edge_id, edge.x1 AS edge_x1, edge.y1 AS edge_y1, edge.x2 AS edge_x2, edge.y2 AS edge_y2 FROM edge WHERE (edge.x1 - 1) * (edge.x1 - 1) + (edge.y1 - 1) * (edge.y1 - 1) <= 1;
+---------+---------+---------+---------+---------+
| edge_id | edge_x1 | edge_y1 | edge_x2 | edge_y2 |
+---------+---------+---------+---------+---------+
|       2 |       0 |       1 |       3 |       5 |
+---------+---------+---------+---------+---------+
1 row in set (0,00 sec)



 Comments   
Comment by Georg Richter [ 2020-08-14 ]

Note: When setting bind[i].is_unsigned=0 everything works as expected.

Comment by Oleksandr Byelkin [ 2021-06-09 ]

As we figured it out, it is normal when after metadata goes error packet

Comment by Oleksandr Byelkin [ 2021-06-09 ]

ah, problem is error at all

Comment by Oleksandr Byelkin [ 2021-06-09 ]

[MySQL-10.5.11-MariaDB-debug-log][1690] BIGINT UNSIGNED value is out of range i
n '`client_test_db`.`edge`.`x1` - 1'

is the error.

Comment by Oleksandr Byelkin [ 2021-06-09 ]

test suite for client test, without unneeded thing, with cleanup:

static void test_mdev_23481()
{
  MYSQL_STMT *stmt;
  int rc;
  MYSQL_BIND bind[1];
  unsigned char vals[1]= {1};
 
  rc= mysql_query(mysql, "CREATE TABLE t1 ( id INTEGER NOT NULL AUTO_INCREMENT, x1 INTEGER, PRIMARY KEY (id))ENGINE=MyISAM");
  myquery(rc);
 
  rc= mysql_query(mysql, "INSERT INTO t1 (x1) VALUES (0)");
  myquery(rc);
 
  stmt= mysql_stmt_init(mysql);
  DIE_UNLESS(stmt != 0);
 
  rc= mysql_stmt_prepare(stmt, STRING_WITH_LEN("SELECT id FROM t1 WHERE (x1 - ?)"));
  check_execute(stmt, rc);
 
  memset(bind, 0, sizeof(MYSQL_BIND));
  bind[0].buffer_type= MYSQL_TYPE_TINY;
  bind[0].buffer= &vals[0];
  bind[0].is_unsigned= 1;
 
  rc= mysql_stmt_bind_param(stmt, bind);
  check_execute(stmt, rc);
 
  rc= mysql_stmt_execute(stmt);
  check_execute(stmt, rc);
 
  /* Error will be read here */
  rc= mysql_stmt_store_result(stmt);
  check_execute(stmt, rc);
 
  mysql_stmt_close(stmt);
  rc= mysql_query(mysql, "DROP TABLE t1");
  myquery(rc);
}

Comment by Oleksandr Byelkin [ 2021-06-09 ]

It has nothing to do with PS, here is text protocol example of the same bahaviour:

CREATE TEMPORARY TABLE t1 ( id INTEGER NOT NULL AUTO_INCREMENT, x1
INTEGER, y1 INTEGER UNSIGNED, PRIMARY KEY (id))ENGINE=MyISAM;
 
INSERT INTO t1 (x1, y1) VALUES (0, 1);
 
SELECT id, x1, y1 FROM t1 WHERE (x1 - y1);
 
DROP TABLE t1;

result:

CREATE TEMPORARY TABLE t1 ( id INTEGER NOT NULL AUTO_INCREMENT, x1
INTEGER, y1 INTEGER UNSIGNED, PRIMARY KEY (id))ENGINE=MyISAM;
INSERT INTO t1 (x1, y1) VALUES (0, 1);
SELECT id, x1, y1 FROM t1 WHERE (x1 - y1);
main.test                                [ fail ]
        Test ended at 2021-06-09 13:39:00
 
CURRENT_TEST: main.test
mysqltest: At line 6: query 'SELECT id, x1, y1 FROM t1 WHERE (x1 - y1)' failed: 1690: BIGINT UNSIGNED value is out of range in '`test`.`t1`.`x1` - `test`.`t1`.`y1`'

Generated at Thu Feb 08 09:22:44 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.