Bug #114650 The prepare statement execution result is wrong, but there is data output
Submitted: 16 Apr 2024 7:17 Modified: 16 Apr 2024 10:38
Reporter: MIAO CHEN Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Prepared statements Severity:S3 (Non-critical)
Version:8.0.34 OS:CentOS
Assigned to: CPU Architecture:x86

[16 Apr 2024 7:17] MIAO CHEN
Description:
Statement that executes time conversion to integer
1.Execute time to integer sql statement, prompt error,It's right

mysql> SELECT * from stmt_test where int_col < 24:59:59.000000;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':59:59.000000' at line 1

2.When executing the prepare time to integer statement for the first time, a statement error message appears, which is also correct.

mysql> prepare stmt2 from "SELECT * from stmt_test where int_col < ?";SET @a = 24:59:59.000000;EXECUTE stmt2 USING @a;deallocate prepare stmt2;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':59:59.000000' at line 1
Empty set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

3.However, in the same session,after executing other correct prepare statements, executing the second statement prompts an error, but there is output

prepare stmt from "SELECT * from stmt_test where int_col <?";SET @a ='10.2';EXECUTE stmt USING @a;deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

Query OK, 0 rows affected (0.00 sec)

+--------+---------+
| id_col | int_col |
+--------+---------+
|      1 |       2 |
|      2 |       4 |
|      3 |       5 |
|      4 |       7 |
+--------+---------+
4 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> prepare stmt2 from "SELECT * from stmt_test where int_col < ?";SET @a = 24:59:59.000000;EXECUTE stmt2 USING @a;deallocate prepare stmt2;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':59:59.000000' at line 1
+--------+---------+
| id_col | int_col |
+--------+---------+
|      1 |       2 |
|      2 |       4 |
|      3 |       5 |
|      4 |       7 |
+--------+---------+
4 rows in set (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

How to repeat:
create database test;
use test;
CREATE TABLE `stmt_test` (
  `id_col` int unsigned NOT NULL,
  `int_col` int DEFAULT NULL
  ) ENGINE=InnoDB ;
INSERT INTO stmt_test (id_col, int_col)VALUES(1, 2),(2, 4),(3, 5),(4, 7),(5, 11);

SELECT * from stmt_test where int_col < 24:59:59.000000;
prepare stmt2 from "SELECT * from stmt_test where int_col < ?";SET @a = 24:59:59.000000;EXECUTE stmt2 USING @a;deallocate prepare stmt2;
prepare stmt from "SELECT * from stmt_test where int_col <?";SET @a ='10.2';EXECUTE stmt USING @a;deallocate prepare stmt;
prepare stmt2 from "SELECT * from stmt_test where int_col < ?";SET @a = 24:59:59.000000;EXECUTE stmt2 USING @a;deallocate prepare stmt2;

Suggested fix:
When executing this statement, an error should be reported and there should be no output.
prepare stmt2 from "SELECT * from stmt_test where int_col < ?";SET @a = 24:59:59.000000;EXECUTE stmt2 USING @a;deallocate prepare stmt2;
[16 Apr 2024 10:38] MySQL Verification Team
Hi Mr. Chen,

Thank you for your bug report.

However, we had not problem of repeating this bug with our 8.0.36 binary. We also did not have any problems with repeating with our  8.0.37 binary, which will soon be out.

Here is the output:

-----------------------------------------

id_col	int_col
1	2
2	4
3	5
4	7
5	11
id_col	int_col
1	2
2	4
3	5
4	7
5	11
id_col	int_col
1	2
2	4
3	5
4	7
id_col	int_col
1	2
2	4
3	5
4	7
5	11

-----------------------------------------

You have introduced many syntax mistakes in your test case. Please, read our Reference Manual about how date/time/datetime types have to be quoted.

Not a bug.