-
Notifications
You must be signed in to change notification settings - Fork 713
docs: add LATERAL derived tables documentation #23123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qw4990
wants to merge
4
commits into
pingcap:master
Choose a base branch
from
qw4990:lateral-join
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| title: LATERAL Derived Tables | ||
| summary: Learn the syntax and current limitations of LATERAL derived tables in TiDB. | ||
| --- | ||
|
|
||
| # LATERAL Derived Tables | ||
|
qiancai marked this conversation as resolved.
|
||
|
|
||
| A **lateral derived table** is a subquery in the `FROM` clause that can reference columns from tables that appear earlier in the same `FROM` clause. This makes it more powerful than a standard derived table, whose subquery cannot reference outer columns in the same `FROM` clause. | ||
|
|
||
| TiDB recognizes the `LATERAL` syntax for derived tables, following the MySQL 8.0 syntax ([WL#8652](https://dev.mysql.com/worklog/task/?id=8652)). | ||
|
|
||
| > **Note:** | ||
| > | ||
| > Currently, TiDB supports parsing the `LATERAL` derived table syntax, but does not support executing it. If you attempt to execute such a query, TiDB returns an error. You can track the progress for full execution support in issue [#40328](https://github.com/pingcap/tidb/issues/40328). | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```sql | ||
| SELECT ... FROM table_ref, LATERAL (subquery) [AS] alias [(col_list)] ... | ||
| SELECT ... FROM table_ref [LEFT] JOIN LATERAL (subquery) [AS] alias [(col_list)] ON ... | ||
| ``` | ||
|
|
||
| - The `LATERAL` keyword must precede the derived table subquery. | ||
| - A table alias is required after the closing parenthesis of the subquery. | ||
| - The `AS` keyword before the alias is optional. | ||
| - An optional derived column list can follow the alias: `LATERAL (...) AS dt(col1, col2)`. | ||
|
qiancai marked this conversation as resolved.
|
||
|
|
||
| ## Examples | ||
|
|
||
| ### Comma join | ||
|
|
||
| ```sql | ||
| SELECT * FROM t1, LATERAL (SELECT * FROM t2 WHERE t2.id = t1.id) AS dt; | ||
| ``` | ||
|
|
||
| In this example, the subquery references `t1.id`, a column from the preceding table `t1`. A regular derived table (without `LATERAL`) cannot do this. | ||
|
|
||
| ### LEFT JOIN with a derived column list | ||
|
|
||
| ```sql | ||
| SELECT t1.id, dt.val | ||
| FROM t1 | ||
| LEFT JOIN LATERAL (SELECT t2.val FROM t2 WHERE t2.id = t1.id LIMIT 1) AS dt(val) | ||
| ON TRUE; | ||
| ``` | ||
|
|
||
| The derived column list `(val)` renames the columns that the subquery returns. | ||
|
|
||
| ## Comparison with standard derived tables | ||
|
|
||
| | Feature | Standard derived table | LATERAL derived table | | ||
| |---|---|---| | ||
| | Can reference outer `FROM` columns | No | Yes | | ||
| | Alias required | Yes | Yes | | ||
| | Derived column list | Supported | Supported | | ||
|
|
||
| ## MySQL compatibility | ||
|
|
||
| TiDB's LATERAL derived table syntax is compatible with MySQL 8.0 at the syntax level. | ||
|
qiancai marked this conversation as resolved.
|
||
|
|
||
| ## See also | ||
|
|
||
| - [Subquery Related Optimizations](/subquery-optimization.md) | ||
| - [Decorrelation of Correlated Subquery](/correlated-subquery-optimization.md) | ||
| - [Explain Statements That Use Subqueries](/explain-subqueries.md) | ||
| - [MySQL Compatibility](/mysql-compatibility.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.