-
Notifications
You must be signed in to change notification settings - Fork 709
docs: add LATERAL derived tables documentation #22583
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
terry1purcell
wants to merge
9
commits into
pingcap:master
Choose a base branch
from
terry1purcell:feat/lateral-derived-tables-docs
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.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4d1e17d
docs: add LATERAL derived tables documentation
terry1purcell 7e078e0
Merge branch 'master' into feat/lateral-derived-tables-docs
terry1purcell 9cfbdfa
docs: add LATERAL as reserved keyword
terry1purcell f600b30
Update lateral-derived-tables.md
terry1purcell 29ada87
Update lateral-derived-tables.md
terry1purcell 9a2db58
Update lateral-derived-tables.md
terry1purcell 4932c5c
Update lateral-derived-tables.md
terry1purcell 1f79850
Update lateral-derived-tables.md
terry1purcell 68dcfad
Update lateral-derived-tables.md
terry1purcell 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| title: LATERAL Derived Tables | ||
| summary: Learn how to use LATERAL derived tables in TiDB. | ||
| --- | ||
|
|
||
| # LATERAL Derived Tables | ||
|
|
||
| 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 MySQL 8.0 compatibility ([WL#8652](https://dev.mysql.com/worklog/task/?id=8652)). | ||
|
terry1purcell marked this conversation as resolved.
Outdated
|
||
|
|
||
| > **Note:** | ||
| > | ||
| > Currently, TiDB supports parsing LATERAL derived table syntax, but execution is not yet supported and will return an error. Full execution support is tracked in [#40328](https://github.com/pingcap/tidb/issues/40328). | ||
|
terry1purcell marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 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)`. | ||
|
|
||
| ## 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 produced by the subquery. | ||
|
terry1purcell marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 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. Once full execution support is available, queries that use `LATERAL` in MySQL should also work in TiDB. | ||
|
terry1purcell marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 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.