From d938d3acfddcbaaa55660c98df3902bf4c0b94f0 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Mon, 5 Aug 2024 09:05:17 +0800 Subject: [PATCH] refactor: support reserved words in as clause in snowflake --- pegjs/snowflake.pegjs | 33 +++++++-------------------------- test/snowflake.spec.js | 7 +++++++ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/pegjs/snowflake.pegjs b/pegjs/snowflake.pegjs index 94fed031..09dcaf37 100644 --- a/pegjs/snowflake.pegjs +++ b/pegjs/snowflake.pegjs @@ -519,32 +519,13 @@ create_table_stmt tp:KW_TEMPORARY? __ KW_TABLE __ ife:if_not_exists_stmt? __ - t:table_ref_list __ + t:table_name __ c:create_table_definition? __ to:table_options? __ - ir: (KW_IGNORE / KW_REPLACE)? __ - as: KW_AS? __ - qe: union_stmt? { - /* - export type create_table_stmt_node = create_table_stmt_node_simple | create_table_stmt_node_like; - export interface create_table_stmt_node_base { - type: 'create'; - keyword: 'table'; - temporary?: 'temporary'; - replace?: string; - if_not_exists?: 'if not exists'; - table: table_ref_list; - } - export interface create_table_stmt_node_simple extends create_table_stmt_node_base{ - ignore_replace?: 'ignore' | 'replace'; - as?: 'as'; - query_expr?: union_stmt_node; - create_definitions?: create_table_definition; - table_options?: table_options; - } - => AstStatement - */ - if(t) t.forEach(tt => tableList.add(`create::${tt.db}::${tt.table}`)); + ir:(KW_IGNORE / KW_REPLACE)? __ + as:KW_AS? __ + qe:union_stmt? { + tableList.add(`create::${t.db}::${t.table}`) return { tableList: Array.from(tableList), columnList: columnListTableAlias(columnList), @@ -553,7 +534,7 @@ create_table_stmt keyword: 'table', temporary: tp && tp[0].toLowerCase(), if_not_exists:ife, - table: t, + table: [t], replace: or && 'or replace', ignore_replace: ir && ir[0].toLowerCase(), as: as && as[0].toLowerCase(), @@ -2296,7 +2277,7 @@ value_alias_clause = KW_AS? __ i:alias_ident { /*=>alias_ident*/ return i; } alias_clause - = KW_AS __ i:alias_ident { /*=>alias_ident*/ return i; } + = KW_AS __ i:ident_without_kw { /*=>alias_ident*/ return i; } / KW_AS? __ i:ident { /*=>ident*/ return i; } into_clause diff --git a/test/snowflake.spec.js b/test/snowflake.spec.js index 2659c697..05d4f5e4 100644 --- a/test/snowflake.spec.js +++ b/test/snowflake.spec.js @@ -401,6 +401,13 @@ describe('snowflake', () => { 'CREATE OR REPLACE VIEW "JACEK_DB"."PUBLIC"."VALIDATION_DATA_VIEW" ("ID", "DATA", "LOLZ") AS (SELECT *, "DATA" AS "LOLZ" FROM "VALIDATION_DATA")' ] }, + { + title: 'keyword in alias clause', + sql: [ + 'select user_id, Limits as Limit from tableName', + 'SELECT "user_id", "Limits" AS "Limit" FROM "tableName"', + ] + }, ] SQL_LIST.forEach(sqlInfo => { const { title, sql } = sqlInfo