pingcap / pingcap/tidb

CHECK constraint containing an aggregate/window function leaks an internal error

Open
#70,816 2 comments 0 reactions 0 assignees View on GitHub
component/ddl contribution severity/minor 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)
```
SET GLOBAL tidb_enable_check_constraint=ON;
CREATE TABLE t(a INT CHECK (max(a) > 0));
```

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

The CHECK validation path lacks an aggregate/window whitelist, so the expression reaches the planner expression rewriter, whose internal assertion text (Go type names) leaks to the client.

### 3. What did you see instead (Required)
```
mysql> CREATE TABLE t(a INT CHECK (max(a) > 0));
ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.AggregateFuncExpr'
mysql> -- ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.AggregateFuncExpr'
mysql>
mysql> CREATE TABLE t(a INT CHECK (max_count(a) > 0)); -- 同
ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.AggregateFuncExpr'
mysql> CREATE TABLE t(a INT CHECK (count(a) > 0)); -- 同
ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.AggregateFuncExpr'
mysql> CREATE TABLE t(a INT CHECK (sum(a) > 0)); -- 同(所有聚合 函数)
ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.AggregateFuncExpr'
mysql> CREATE TABLE t(a INT CHECK (RANK() OVER (ORDER BY a) > 0));
ERROR 1105 (HY000): planCtx is required when rewriting node: '*ast.WindowFuncExpr'
mysql> -- ERROR 1105: planCtx is required when rewriting node: '*ast.WindowFuncExpr'
mysql>
mysql> -- 对照:子查询 CHECK 正确报用户级错误
mysql> CREATE TABLE t(a INT CHECK (a IN (SELECT 1)));
ERROR 3815 (HY000): An expression of a check constraint 't_chk_1' contains disallowed function.
mysql> -- ERROR 3815 (HY000): An expression of a check constraint contains disallowed function.
```
### 4. What is your TiDB version? (Required)

```
| Release Version: v9.0.0-beta.2.pre-2174-g65ac2fad58-dirty
Edition: Community
Git Commit Hash: 65ac2fad582510b92d3c4b79999e48217c989737
Git Branch: master
UTC Build Time: 2026-08-30 15:18:00
GoVersion: go1.25.12
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the SQL examples first, then trace TiDB's CHECK validation path into the planner expression rewriter, focusing on AggregateFuncExpr and WindowFuncExpr handling. Done means aggregate and window functions in CHECK constraints return the user-level disallowed-function error instead of exposing internal Go type names.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.