-
Notifications
You must be signed in to change notification settings - Fork 710
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.
+69
−1
Open
Changes from all commits
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
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 | ||
|
|
||
| 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 its execution is not yet supported and will return 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)`. | ||
|
|
||
| ## 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. | ||
|
|
||
| ## 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -68,7 +68,7 @@ You can try out TiDB features on [TiDB Playground](https://play.tidbcloud.com/?u | |||||
| + "Session Tracker: Add GTIDs context to the OK packet" | ||||||
| + Descending Index [#2519](https://github.com/pingcap/tidb/issues/2519) | ||||||
| + `SKIP LOCKED` syntax [#18207](https://github.com/pingcap/tidb/issues/18207) | ||||||
| + Lateral derived tables [#40328](https://github.com/pingcap/tidb/issues/40328) | ||||||
| + Lateral derived tables (syntax is recognized but execution is not yet supported) [#40328](https://github.com/pingcap/tidb/issues/40328). For syntax details, see [LATERAL Derived Tables](/lateral-derived-tables.md). | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| + JOIN ON subquery [#11414](https://github.com/pingcap/tidb/issues/11414) | ||||||
|
|
||||||
| ## Differences from MySQL | ||||||
|
|
||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@terry1purcell, after planner: Add optimizer support to LATERAL join | tidb-test=pr/2712 by terry1purcell · Pull Request #67131 · pingcap/tidb is merged, may I know if the execution of some examples mentioned in this doc (for example, the comma join example) is already supported?