From 1438d8c940174b9a324bd1b2ff2b2419b61d2607 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Tue, 5 Nov 2024 08:59:12 +0800 Subject: [PATCH] feat: support lateral derived tables in mysql --- pegjs/mariadb.pegjs | 9 ++++++--- pegjs/mysql.pegjs | 8 +++++--- test/mysql-mariadb.spec.js | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) 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