pingcap / pingcap/tidb

Recursive CTE throws Warning instead of Error for data truncation under strict mode (MySQL Compatibility)

Open
#64,711 2 comments 0 reactions 0 assignees View on GitHub
affects-7.5 affects-8.1 affects-8.5 severity/moderate sig/sql-infra type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```
-- 1. Set SQL mode to strict
SET SESSION sql_mode = 'STRICT_TRANS_TABLES';

-- 2. Execute a Recursive CTE
-- The anchor selects NULL (length 0).
-- The recursive part selects 'abc' (length 3).
WITH RECURSIVE cte_test AS (
-- Anchor: defines initial value as NULL
SELECT NULL AS test_col, 1 AS step

UNION ALL

-- Recursive: attempts to write 'abc'
SELECT 'abc', step + 1
FROM cte_test
WHERE step < 2
)
SELECT * FROM cte_test;

show warnings;
```

In STRICT_TRANS_TABLES mode, TiDB handles data truncation in Recursive CTEs differently from MySQL.

When data truncation occurs within a Recursive CTE, TiDB issues a Warning, whereas MySQL strictly throws an ERROR.

### 2. What did you expect to see? (Required)
```
mysql> SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> WITH RECURSIVE cte_test AS ( SELECT NULL AS test_col, 1 AS step UNION ALL SELECT 'abc', step + 1 FROM cte_test WHERE step < 2 ) SELECT * FROM cte_test;
ERROR 1406 (22001): Data too long for column 'test_col' at row 1
```

### 3. What did you see instead (Required)
```
mysql> SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
Query OK, 0 rows affected (0.01 sec)

mysql> WITH RECURSIVE cte_test AS (
-> -- Anchor: defines initial value as NULL
-> SELECT NULL AS test_col, 1 AS step
->
-> UNION ALL
->
-> -- Recursive: attempts to write 'abc'
-> SELECT 'abc', step + 1
-> FROM cte_test
-> WHERE step < 2
-> )
-> SELECT * FROM cte_test;
+----------+------+
| test_col | step |
+----------+------+
| NULL | 1 |
| NULL | 2 |
+----------+------+
2 rows in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1406 | Data Too Long, field len 0, data len 3 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

```

### 4. What is your TiDB version? (Required)
latest master

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.