pingcap / pingcap/tidb

`ERROR 3593` when a window function is used as the left operand of an IN subquery within the SELECT list

Open
#67,152 3 comments 0 reactions 0 assignees View on GitHub
contribution severity/moderate sig/planner 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)
```SQL
mysql> CREATE TABLE t (a INT, b INT);
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t VALUES (1, 10), (2, 20);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (1, 2) FROM t;
+-------------------------------------------+
| LAST_VALUE(a) OVER (ORDER BY a) IN (1, 2) |
+-------------------------------------------+
| 1 |
| 1 |
+-------------------------------------------+
2 rows in set (0.00 sec)

mysql> EXPLAIN SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (1, 2) FROM t;
+------------------------------+---------+-----------+---------------+----------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------+---------+-----------+---------------+----------------------------------------------------------------------------------------------------------+
| Projection_7 | 2.00 | root | | in(Column#6, 1, 2)->Column#8 |
| └─Window_10 | 2.00 | root | | last_value(test.t.a)->Column#6 over(order by test.t.a range between unbounded preceding and current row) |
| └─Sort_13 | 2.00 | root | | test.t.a |
| └─TableReader_12 | 2.00 | root | | data:TableFullScan_11 |
| └─TableFullScan_11 | 2.00 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+------------------------------+---------+-----------+---------------+----------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

mysql> SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
ERROR 3593 (HY000): You cannot use the window function 'last_value' in this context.'
mysql> EXPLAIN SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
ERROR 3593 (HY000): You cannot use the window function 'last_value' in this context.'
```

### 2. What did you expect to see? (Required)
```SQL
mysql> SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
+------------------------------------------------------+
| LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) |
+------------------------------------------------------+
| 0 |
| 0 |
+------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.45 |
+-----------+
1 row in set (0.03 sec)
```
```SQL
mysql> SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
+------------------------------------------------------+
| LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) |
+------------------------------------------------------+
| 0 |
| 0 |
+------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.4.4 |
+-----------+
1 row in set (0.00 sec)
```
```SQL
root@e7ea8fd09659:/# psql test root
psql (16.13 (Debian 16.13-1.pgdg13+1))
Type "help" for help.

test=# CREATE TABLE t (a INT, b INT);
CREATE TABLE
test=# INSERT INTO t VALUES (1, 10), (2, 20);
INSERT 0 2
test=# SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
?column?
----------
f
f
(2 rows)

test=# SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (1, 2) FROM t;
?column?
----------
t
t
(2 rows)
```

### 3. What did you see instead (Required)
```SQL
mysql> SELECT LAST_VALUE(a) OVER (ORDER BY a) IN (SELECT b FROM t) FROM t;
ERROR 3593 (HY000): You cannot use the window function 'last_value' in this context.'
```

### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1407-g3a9aad6095
Edition: Community
Git Commit Hash: 3a9aad6095fc096858c9525d0751fab5645f820a
Git Branch: master
UTC Build Time: 2026-03-19 09:24:25
GoVersion: go1.25.8
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

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.