From 6bb00bc52e00346cabde7947ec64d5b0a2b25bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 19 Apr 2024 08:25:41 +0200 Subject: [PATCH 1/8] ebnf select --- sql-statements/sql-statement-select.md | 80 ++++++++++---------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 8077af19dc5ae..f547b40b3d27e 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -10,82 +10,64 @@ The `SELECT` statement is used to read data from TiDB. ## Synopsis -**SelectStmt:** - -![SelectStmt](/media/sqlgram/SelectStmt.png) - -**FromDual:** - -![FromDual](/media/sqlgram/FromDual.png) - -**SelectStmtOpts:** - -![SelectStmtOpts](/media/sqlgram/SelectStmtOpts.png) +```ebnf+diagram +SelectStmt ::= + ( SelectStmtBasic | SelectStmtFromDualTable | SelectStmtFromTable ) + OrderByOptional SelectStmtLimit SelectLockOpt SelectStmtIntoOption -**SelectStmtFieldList:** +FromDual ::= + "FROM" "DUAL" -![SelectStmtFieldList](/media/sqlgram/SelectStmtFieldList.png) +SelectStmtOpts ::= + TableOptimizerHints DefaultFalseDistictOpt PriorityOpt SelectStmtSQLSmallResult + SelectStmtSQLBigResult SelectStmtSQLBufferResult SelectStmtSQLCache SelectStmtCalcFoundRows + SelectStmtStraightJoin -**TableRefsClause:** +SelectStmtFieldList ::= + Field ("," Field)* -```ebnf+diagram TableRefsClause ::= TableRef AsOfClause? ( ',' TableRef AsOfClause? )* AsOfClause ::= 'AS' 'OF' 'TIMESTAMP' Expression -``` - -**WhereClauseOptional:** - -![WhereClauseOptional](/media/sqlgram/WhereClauseOptional.png) - -**SelectStmtGroup:** -![SelectStmtGroup](/media/sqlgram/SelectStmtGroup.png) +WhereClauseOptional ::= + WhereClause? -**HavingClause:** +SelectStmtGroup ::= + GroupByClause? -![HavingClause](/media/sqlgram/HavingClause.png) +HavingClause ::= + ( "HAVING" Expression)? -**OrderByOptional:** -![OrderByOptional](/media/sqlgram/OrderByOptional.png) +OrderByOptional ::= + OrderBy? -**SelectStmtLimit:** +SelectStmtLimit ::= + ("LIMIT" LimitOption ( ("," | "OFFSET") LimitOption )? +| "FETCH" FirstOrNext FetchFirstOpt RowOrRows "ONLY" ) -![SelectStmtLimit](/media/sqlgram/SelectStmtLimit.png) +FirstOrNext ::= + ("FIRST" | "NEXT") -**FirstOrNext:** +FetchFirstOpt ::= + LimitOption? -![FirstOrNext](/media/sqlgram/FirstOrNext.png) +RowOrRows ::= + ("ROW" | "ROWS") -**FetchFirstOpt:** - -![FetchFirstOpt](/media/sqlgram/FetchFirstOpt.png) - -**RowOrRows:** - -![RowOrRows](/media/sqlgram/RowOrRows.png) - -**SelectLockOpt:** - -```ebnf+diagram SelectLockOpt ::= ( ( 'FOR' 'UPDATE' ( 'OF' TableList )? 'NOWAIT'? ) | ( 'LOCK' 'IN' 'SHARE' 'MODE' ) )? TableList ::= TableName ( ',' TableName )* -``` - -**WindowClauseOptional** -![WindowClauseOptional](/media/sqlgram/WindowClauseOptional.png) +WindowClauseOptional ::= + "WINDOW" WindowDefinition ("," WindowDefinition)* -**TableSampleOpt** - -```ebnf+diagram TableSampleOpt ::= 'TABLESAMPLE' 'REGIONS()' ``` From 0182ca69005b003cfac08c0d5b6b1eec7c136569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 19 Apr 2024 08:39:56 +0200 Subject: [PATCH 2/8] Updates --- sql-statements/sql-statement-select.md | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index f547b40b3d27e..3c43ea0c4c552 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -13,7 +13,7 @@ The `SELECT` statement is used to read data from TiDB. ```ebnf+diagram SelectStmt ::= ( SelectStmtBasic | SelectStmtFromDualTable | SelectStmtFromTable ) - OrderByOptional SelectStmtLimit SelectLockOpt SelectStmtIntoOption + OrderBy? SelectStmtLimit? SelectLockOpt? SelectStmtIntoOption FromDual ::= "FROM" "DUAL" @@ -41,26 +41,13 @@ SelectStmtGroup ::= HavingClause ::= ( "HAVING" Expression)? - -OrderByOptional ::= - OrderBy? - SelectStmtLimit ::= ("LIMIT" LimitOption ( ("," | "OFFSET") LimitOption )? -| "FETCH" FirstOrNext FetchFirstOpt RowOrRows "ONLY" ) - -FirstOrNext ::= - ("FIRST" | "NEXT") - -FetchFirstOpt ::= - LimitOption? - -RowOrRows ::= - ("ROW" | "ROWS") +| "FETCH" ("FIRST" | "NEXT") LimitOption ("ROW" | "ROWS") "ONLY" ) SelectLockOpt ::= - ( ( 'FOR' 'UPDATE' ( 'OF' TableList )? 'NOWAIT'? ) -| ( 'LOCK' 'IN' 'SHARE' 'MODE' ) )? + ( 'FOR' 'UPDATE' ( 'OF' TableList )? 'NOWAIT'? +| 'LOCK' 'IN' 'SHARE' 'MODE' ) TableList ::= TableName ( ',' TableName )* From 6b66751ab585c6b7f4b8f93230ca7c1d723fa1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 19 Apr 2024 08:52:37 +0200 Subject: [PATCH 3/8] Updates --- sql-statements/sql-statement-select.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 3c43ea0c4c552..0178bc81e5e4c 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -15,17 +15,20 @@ SelectStmt ::= ( SelectStmtBasic | SelectStmtFromDualTable | SelectStmtFromTable ) OrderBy? SelectStmtLimit? SelectLockOpt? SelectStmtIntoOption -FromDual ::= - "FROM" "DUAL" +SelectStmtBasic ::= + "SELECT" SelectStmtOpts Field ("," Field)* ( "HAVING" Expression)? + +SelectStmtFromDualTable ::= + "SELECT" SelectStmtOpts Field ("," Field)* "FROM" "DUAL" WhereClauseOptional + +SelectStmtFromTable ::= + "SELECT" SelectStmtOpts Field ("," Field)* "FROM" TableRefsClause WhereClauseOptional SelectStmtGroup HavingClause WindowClauseOptional SelectStmtOpts ::= TableOptimizerHints DefaultFalseDistictOpt PriorityOpt SelectStmtSQLSmallResult SelectStmtSQLBigResult SelectStmtSQLBufferResult SelectStmtSQLCache SelectStmtCalcFoundRows SelectStmtStraightJoin -SelectStmtFieldList ::= - Field ("," Field)* - TableRefsClause ::= TableRef AsOfClause? ( ',' TableRef AsOfClause? )* @@ -38,9 +41,6 @@ WhereClauseOptional ::= SelectStmtGroup ::= GroupByClause? -HavingClause ::= - ( "HAVING" Expression)? - SelectStmtLimit ::= ("LIMIT" LimitOption ( ("," | "OFFSET") LimitOption )? | "FETCH" ("FIRST" | "NEXT") LimitOption ("ROW" | "ROWS") "ONLY" ) From 647761ef719a9c29747a9289d81f82b1460bbc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 19 Apr 2024 09:29:21 +0200 Subject: [PATCH 4/8] Update sql-statements/sql-statement-select.md Co-authored-by: kennytm --- sql-statements/sql-statement-select.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 0178bc81e5e4c..725ac0a6680e1 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -56,7 +56,7 @@ WindowClauseOptional ::= "WINDOW" WindowDefinition ("," WindowDefinition)* TableSampleOpt ::= - 'TABLESAMPLE' 'REGIONS()' + 'TABLESAMPLE' 'REGIONS' '(' ')' ``` ## Description of the syntax elements From 4db84fff400215ac0e3473b3daef290d1f08bc74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 19 Apr 2024 12:44:39 +0200 Subject: [PATCH 5/8] Update sql-statements/sql-statement-select.md Co-authored-by: kennytm --- sql-statements/sql-statement-select.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 725ac0a6680e1..2dbd3bf5608da 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -43,7 +43,7 @@ SelectStmtGroup ::= SelectStmtLimit ::= ("LIMIT" LimitOption ( ("," | "OFFSET") LimitOption )? -| "FETCH" ("FIRST" | "NEXT") LimitOption ("ROW" | "ROWS") "ONLY" ) +| "FETCH" ("FIRST" | "NEXT") LimitOption? ("ROW" | "ROWS") "ONLY" ) SelectLockOpt ::= ( 'FOR' 'UPDATE' ( 'OF' TableList )? 'NOWAIT'? From d00f107a07b3db24e3d58aaf59e2e48d901cd88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 30 Apr 2024 09:57:30 +0200 Subject: [PATCH 6/8] Update based on review --- sql-statements/sql-statement-select.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 2dbd3bf5608da..9447cfa5a0f1e 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -19,10 +19,10 @@ SelectStmtBasic ::= "SELECT" SelectStmtOpts Field ("," Field)* ( "HAVING" Expression)? SelectStmtFromDualTable ::= - "SELECT" SelectStmtOpts Field ("," Field)* "FROM" "DUAL" WhereClauseOptional + "SELECT" SelectStmtOpts Field ("," Field)* "FROM" "DUAL" WhereClause? SelectStmtFromTable ::= - "SELECT" SelectStmtOpts Field ("," Field)* "FROM" TableRefsClause WhereClauseOptional SelectStmtGroup HavingClause WindowClauseOptional + "SELECT" SelectStmtOpts Field ("," Field)* "FROM" TableRefsClause WhereClauseOptional GroupByClause? HavingClause WindowClause? SelectStmtOpts ::= TableOptimizerHints DefaultFalseDistictOpt PriorityOpt SelectStmtSQLSmallResult @@ -35,12 +35,6 @@ TableRefsClause ::= AsOfClause ::= 'AS' 'OF' 'TIMESTAMP' Expression -WhereClauseOptional ::= - WhereClause? - -SelectStmtGroup ::= - GroupByClause? - SelectStmtLimit ::= ("LIMIT" LimitOption ( ("," | "OFFSET") LimitOption )? | "FETCH" ("FIRST" | "NEXT") LimitOption? ("ROW" | "ROWS") "ONLY" ) From 2f6e007b5bf1e95286847368e48288847bb2d103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 30 Apr 2024 10:00:29 +0200 Subject: [PATCH 7/8] Another update --- sql-statements/sql-statement-select.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index 9447cfa5a0f1e..c9a88f4448a80 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -22,7 +22,8 @@ SelectStmtFromDualTable ::= "SELECT" SelectStmtOpts Field ("," Field)* "FROM" "DUAL" WhereClause? SelectStmtFromTable ::= - "SELECT" SelectStmtOpts Field ("," Field)* "FROM" TableRefsClause WhereClauseOptional GroupByClause? HavingClause WindowClause? + "SELECT" SelectStmtOpts Field ("," Field)* "FROM" TableRefsClause + WhereClause? GroupByClause? ( "HAVING" Expression)? WindowClause? SelectStmtOpts ::= TableOptimizerHints DefaultFalseDistictOpt PriorityOpt SelectStmtSQLSmallResult From 4b39e243c903747d71b4a65b4708e28763cc85de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 30 Apr 2024 10:01:48 +0200 Subject: [PATCH 8/8] Update WindowClause --- sql-statements/sql-statement-select.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-statements/sql-statement-select.md b/sql-statements/sql-statement-select.md index c9a88f4448a80..1e101c27e9a3a 100644 --- a/sql-statements/sql-statement-select.md +++ b/sql-statements/sql-statement-select.md @@ -47,7 +47,7 @@ SelectLockOpt ::= TableList ::= TableName ( ',' TableName )* -WindowClauseOptional ::= +WindowClause ::= "WINDOW" WindowDefinition ("," WindowDefinition)* TableSampleOpt ::=