Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions non-transactional-dml.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,27 @@ SELECT t2.id, t2.v, t3. FROM t2 JOIN t3 ON t2.id=t3.id
+----------------+---------------+
```

### The `Unknown column '<alias>.<column>' in 'where clause'` error occurs when using table aliases in non-transactional DML statements

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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This sentence can be rephrased for better clarity and conciseness, and to remove the vague phrase 'In the current version'.

Suggested change
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.
Because the rewritten statements might not preserve the table aliases from the original DML statement, you might get an `Unknown column` error if you use the `<alias>.<column>` format to reference columns in a `WHERE` clause or other expressions.
References
  1. The style guide recommends writing in the second person ('you'), avoiding unnecessary words, and being direct. This suggestion rephrases the sentence to be more active and concise, removing the vague phrase 'In the current version'. (link)


For example, the following statement might return an error in some cases:

```sql
BATCH ON t_old.id LIMIT 5000
INSERT INTO t_new
SELECT * FROM t_old AS t
WHERE t.c1 IS NULL;
```

To avoid this error, follow these recommendations:

- Avoid using table aliases in non-transactional DML statements. For example, rewrite `t.c1` as `c1` or `t_old.c1`.
- 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`.
- Before execution, use `DRY RUN QUERY` or `DRY RUN` to preview the rewritten statements and verify that they meet your expectations.

### The actual batch size is not the same as the specified batch size

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.
Expand Down
4 changes: 4 additions & 0 deletions sql-statements/sql-statement-batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Non-transactional DML, shard column must be fully specified
```

> **Note:**
>
> 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This note can be improved for clarity and conciseness. The phrase 'In the current version' is vague, and 'before execution' is repeated. This suggestion rephrases the note to be more direct and easier to read.

Suggested change
> 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).
> The `BATCH` statement is internally rewritten and split into multiple DML statements during execution. These rewritten statements might not preserve table aliases, which can result in errors such as `Unknown column '<alias>.<column>' in 'where clause'`. To avoid this issue, do not use table aliases. You can 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).
References
  1. The style guide recommends avoiding unnecessary words and repetition. This suggestion removes the repeated phrase 'before execution' and the vague phrase 'In the current version' to improve readability and conciseness. (link)


## Synopsis

```ebnf+diagram
Expand Down
Loading