Skip to content

Commit 80db0da

Browse files
authored
nontransactional dml FAQ about alias (#22523) (#22536)
1 parent 0a10544 commit 80db0da

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

non-transactional-dml.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,27 @@ SELECT t2.id, t2.v, t3.id FROM t2 JOIN t3 ON t2.id = t3.id
366366
+----------------+---------------+
367367
```
368368
369+
### The `Unknown column '<alias>.<column>' in 'where clause'` error occurs when using table aliases in non-transactional DML statements
370+
371+
When you execute a non-transactional DML statement, TiDB internally constructs a query for dividing batches and then generates the actual split execution statements. You can use [`DRY RUN QUERY`](/non-transactional-dml.md#query-the-batch-dividing-statement) and [`DRY RUN`](/non-transactional-dml.md#query-the-statements-corresponding-to-the-first-and-the-last-batches) to view these two types of statements, respectively.
372+
373+
In the current version, the rewritten statements might not preserve the table aliases from the original DML statement. Therefore, if you use the `<alias>.<column>` format to reference columns in a `WHERE` clause or other expressions, an `Unknown column` error might occur.
374+
375+
For example, the following statement might return an error in some cases:
376+
377+
```sql
378+
BATCH ON t_old.id LIMIT 5000
379+
INSERT INTO t_new
380+
SELECT * FROM t_old AS t
381+
WHERE t.c1 IS NULL;
382+
```
383+
384+
To avoid this error, follow these recommendations:
385+
386+
- Avoid using table aliases in non-transactional DML statements. For example, rewrite `t.c1` as `c1` or `t_old.c1`.
387+
- When specifying the [shard column](#parameter-description), do not use table aliases. For example, rewrite `BATCH ON t.id` as `BATCH ON db.t_old.id` or `BATCH ON t_old.id`.
388+
- Before execution, use `DRY RUN QUERY` or `DRY RUN` to preview the rewritten statements and verify that they meet your expectations.
389+
369390
### The actual batch size is not the same as the specified batch size
370391

371392
During the execution of a non-transactional DML statement, the size of data to be processed in the last batch might be smaller than the specified batch size.

sql-statements/sql-statement-batch.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ BATCH ON id LIMIT 1 INSERT INTO t SELECT t2.id, t2.v, t3.v FROM t2 JOIN t3 ON t2
2727
Non-transactional DML, shard column must be fully specified
2828
```
2929

30+
> **Note:**
31+
>
32+
> The `BATCH` statement is internally rewritten and split into multiple DML statements during execution. In the current version, table aliases might not be preserved, which can result in errors such as `Unknown column '<alias>.<column>' in 'where clause'`. To avoid this issue, do not use table aliases. Before execution, use `DRY RUN QUERY` or `DRY RUN` to preview the split statements before execution. For more information, see [Non-Transactional DML Statements](/non-transactional-dml.md).
33+
3034
## Synopsis
3135

3236
```ebnf+diagram

0 commit comments

Comments
 (0)