diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 02ceb613..92435f48 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -53,6 +53,7 @@ 'KEY': true, + 'LATERAL': true, 'LEFT': true, 'LIKE': true, 'LIMIT': true, @@ -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 diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 311b2e48..ac6374a5 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -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 diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index 01c18e03..8d7496ba 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -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