pingcap / pingcap/tidb

SHOW CREATE VIEW still emits non-replayable schema-qualified CTE aliases

Open
#69,547 3 comments 2 reactions 1 assignee Claimed by @locker95 View on GitHub
affects-8.5 component/ddl contribution may-affects-7.5 may-affects-8.1 severity/major type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

### 1. Minimal reproduce step

On TiDB v8.5.x, create a view that references a CTE through an alias, then inspect the generated DDL from `SHOW CREATE VIEW`.

```sql
DROP DATABASE IF EXISTS db_a_cte_replay;
DROP DATABASE IF EXISTS db_b_cte_replay;
CREATE DATABASE db_a_cte_replay;
CREATE DATABASE db_b_cte_replay;

USE db_a_cte_replay;

CREATE TABLE tmp_table1 (
id DECIMAL(18,0) NOT NULL,
row_1 VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO tmp_table1 VALUES (1, 'x');

CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW view_test_v1 AS (
WITH rs1 AS (
SELECT otn.*
FROM tmp_table1 otn
)
SELECT ojt.* FROM rs1 ojt
);

USE db_b_cte_replay;
SELECT COUNT(*) FROM db_a_cte_replay.view_test_v1;
SHOW CREATE VIEW db_a_cte_replay.view_test_v1;
```

The query succeeds, but `SHOW CREATE VIEW` emits schema-qualified references to the CTE alias, for example:

```sql
CREATE ALGORITHM=UNDEFINED ... VIEW `view_test_v1` (`id`, `row_1`) AS (
WITH `rs1` AS (
SELECT `db_a_cte_replay`.`otn`.`id` AS `id`,
`db_a_cte_replay`.`otn`.`row_1` AS `row_1`
FROM `db_a_cte_replay`.`tmp_table1` AS `otn`
)
SELECT `db_a_cte_replay`.`ojt`.`id` AS `id`,
`db_a_cte_replay`.`ojt`.`row_1` AS `row_1`
FROM `rs1` AS `ojt`
)
```

If the emitted DDL is replayed, it fails because `ojt` is a CTE alias, not a real table under `db_a_cte_replay`:

```sql
CREATE OR REPLACE VIEW db_b_cte_replay.replayed_view AS
WITH rs1 AS (
SELECT otn.*
FROM db_a_cte_replay.tmp_table1 otn
)
SELECT db_a_cte_replay.ojt.id, db_a_cte_replay.ojt.row_1
FROM rs1 ojt;
```

Actual error:

```text
ERROR 1054 (42S22): Unknown column 'db_a_cte_replay.ojt.id' in 'field list'
```

The replay succeeds if the CTE alias references are not schema-qualified:

```sql
CREATE OR REPLACE VIEW db_b_cte_replay.replayed_view AS
WITH rs1 AS (
SELECT otn.*
FROM db_a_cte_replay.tmp_table1 otn
)
SELECT ojt.id, ojt.row_1
FROM rs1 ojt;
```

### 2. What did you expect to see?

`SHOW CREATE VIEW` should emit replayable DDL. CTE or derived-table aliases should not be schema-qualified as `db_name.alias.column`.

Expected output should be closer to:

```sql
SELECT `ojt`.`id` AS `id`, `ojt`.`row_1` AS `row_1`
FROM `rs1` AS `ojt`
```

not:

```sql
SELECT `db_a_cte_replay`.`ojt`.`id` AS `id`,
`db_a_cte_replay`.`ojt`.`row_1` AS `row_1`
FROM `rs1` AS `ojt`
```

### 3. What did you see instead?

The original view can be queried successfully, but `SHOW CREATE VIEW` still emits DDL that cannot be replayed during migration/export/import workflows.

This is especially visible when migrating views by exporting `SHOW CREATE VIEW` output and importing it into another TiDB cluster. The import fails with errors like:

```text
ERROR 1054 (42S22): Unknown column 'qa_quality_map.ojt.oncall_created_time' in 'field list'
ERROR 1054 (42S22): Unknown column 'qa_quality_map.d.n' in 'field list'
ERROR 1054 (42S22): Unknown column 'qa_quality_map.t1.fy' in 'field list'
```

### 4. Version tested

Reproduced on:

```text
Release Version: v8.5.6
Edition: Community
Git Commit Hash: ae18096e023780bb56bfce33698abec0d4640d0a
UTC Build Time: 2026-04-14 07:11:54
Store: tikv
```

Also reproduced on TiDB Cloud Serverless:

```text
8.0.11-TiDB-v8.5.3-serverless
```

### 5. Related issue / PRs

This looks related to:

- #54582
- #57253
- #57329

Those changes appear to fix the query/planner path for an already-created view. However, the DDL emitted by `SHOW CREATE VIEW` still contains schema-qualified CTE aliases and is not directly replayable. It may need a separate fix or an additional regression test that executes the `SHOW CREATE VIEW` output again.

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.