Skip to content

Commit

Permalink
feat: support lateral derived tables in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Nov 5, 2024
1 parent 51ab218 commit 1438d8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
9 changes: 6 additions & 3 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

'KEY': true,

'LATERAL': true,
'LEFT': true,
'LIKE': true,
'LIMIT': true,
Expand Down Expand Up @@ -2223,13 +2224,15 @@ table_base
as: alias
};
}
/ LPAREN __ stmt:(set_op_stmt / value_clause) __ RPAREN __ alias:alias_clause? {
/ l:('LATERAL'i)? __ LPAREN __ stmt:(set_op_stmt / value_clause) __ RPAREN __ alias:alias_clause? {
if (Array.isArray(stmt)) stmt = { type: 'values', values: stmt, prefix: 'row' }
stmt.parentheses = true;
return {
const result = {
expr: stmt,
as: alias
};
}
if (l) result.prefix = l;
return result
}

join_op
Expand Down
8 changes: 5 additions & 3 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2479,13 +2479,15 @@ table_base
as: alias
};
}
/ LPAREN __ stmt:(set_op_stmt / value_clause) __ RPAREN __ alias:alias_clause? {
/ l:('LATERAL'i)? __ LPAREN __ stmt:(set_op_stmt / value_clause) __ RPAREN __ alias:alias_clause? {
if (Array.isArray(stmt)) stmt = { type: 'values', values: stmt, prefix: 'row' }
stmt.parentheses = true;
return {
const result = {
expr: stmt,
as: alias
};
}
if (l) result.prefix = l;
return result
}

join_op
Expand Down
7 changes: 7 additions & 0 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,13 @@ describe('mysql', () => {
'CREATE TABLE `d` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `d_name` VARCHAR(15) NOT NULL, `d_id` INT(11) GENERATED ALWAYS AS (CAST(TRIM(`d_name`) AS SIGNED)) VIRTUAL NOT NULL)'
]
},
{
title: 'lateral derived tables',
sql: [
'SELECT * FROM table1, LATERAL (SELECT * FROM table2 WHERE table2.id = table1.id) AS subquery',
'SELECT * FROM `table1`, LATERAL (SELECT * FROM `table2` WHERE `table2`.`id` = `table1`.`id`) AS `subquery`'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 1438d8c

Please sign in to comment.