pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
36.54k stars 5.75k forks source link

Using JSON_TYPE on non-JSON values should return an error #54029

Open dveeden opened 3 weeks ago

dveeden commented 3 weeks ago

Bug Report

1. Minimal reproduce step (Required)

CREATE TABLE t1(id INT PRIMARY KEY, d1 DATE, d2 DATETIME, t1 TIME, t2 TIMESTAMP, b1 BIT, b2 BINARY);
INSERT INTO t1 VALUES (1, '2024-06-14', '2024-06-14 09:37:00', '09:37:00', '2024-06-14 09:37:00', b'0', 0x41);
SELECT JSON_TYPE(d1),JSON_TYPE(d2),JSON_TYPE(t1),JSON_TYPE(t2),JSON_TYPE(b1),JSON_TYPE(b2) FROM t1;

2. What did you expect to see? (Required)

mysql-8.4.0> CREATE TABLE t1(id INT PRIMARY KEY, d1 DATE, d2 DATETIME, t1 TIME, t2 TIMESTAMP, b1 BIT, b2 BINARY);
Query OK, 0 rows affected (0.02 sec)

mysql-8.4.0> INSERT INTO t1 VALUES (1, '2024-06-14', '2024-06-14 09:37:00', '09:37:00', '2024-06-14 09:37:00', b'0', 0x41);
Query OK, 1 row affected (0.01 sec)

mysql-8.4.0> SELECT JSON_TYPE(d1),JSON_TYPE(d2),JSON_TYPE(t1),JSON_TYPE(t2),JSON_TYPE(b1),JSON_TYPE(b2) FROM t1;
ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required.

3. What did you see instead (Required)

mysql-8.0.11-TiDB-v8.1.0> CREATE TABLE t1(id INT PRIMARY KEY, d1 DATE, d2 DATETIME, t1 TIME, t2 TIMESTAMP, b1 BIT, b2 BINARY);
Query OK, 0 rows affected (0.11 sec)

mysql-8.0.11-TiDB-v8.1.0> INSERT INTO t1 VALUES (1, '2024-06-14', '2024-06-14 09:37:00', '09:37:00', '2024-06-14 09:37:00', b'0', 0x41);
Query OK, 1 row affected (0.01 sec)

mysql-8.0.11-TiDB-v8.1.0> SELECT JSON_TYPE(d1),JSON_TYPE(d2),JSON_TYPE(t1),JSON_TYPE(t2),JSON_TYPE(b1),JSON_TYPE(b2) FROM t1;
+---------------+---------------+---------------+---------------+------------------+---------------+
| JSON_TYPE(d1) | JSON_TYPE(d2) | JSON_TYPE(t1) | JSON_TYPE(t2) | JSON_TYPE(b1)    | JSON_TYPE(b2) |
+---------------+---------------+---------------+---------------+------------------+---------------+
| DATE          | DATETIME      | TIME          | DATETIME      | UNSIGNED INTEGER | BLOB          |
+---------------+---------------+---------------+---------------+------------------+---------------+
1 row in set (0.00 sec)

4. What is your TiDB version? (Required)

Release Version: v8.1.0
Edition: Community
Git Commit Hash: 945d07c5d5c7a1ae212f6013adfb187f2de24b23
Git Branch: HEAD
UTC Build Time: 2024-05-21 03:51:57
GoVersion: go1.21.10
Race Enabled: false
Check Table Before Drop: false
Store: tikv
YangKeao commented 2 weeks ago

This issue actually has two bugs:

  1. TiDB didn't verify type of args for json_type.
  2. TiDB didn't verify charset of args for many json related functions ('binary' is not allowed).