From 05a42528a5e8d1c4b7b913eb9838457748b97dd8 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Wed, 7 Aug 2024 09:29:09 +0800 Subject: [PATCH] refactor: support no space before line comment --- pegjs/bigquery.pegjs | 6 +++--- pegjs/postgresql.pegjs | 2 +- pegjs/redshift.pegjs | 2 +- pegjs/snowflake.pegjs | 4 ++-- test/postgres.spec.js | 7 +++++++ test/redshift.spec.js | 5 +++++ 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pegjs/bigquery.pegjs b/pegjs/bigquery.pegjs index 00b43206..3fb0077c 100644 --- a/pegjs/bigquery.pegjs +++ b/pegjs/bigquery.pegjs @@ -1800,7 +1800,7 @@ column_list_item alias_clause = KW_AS __ i:alias_ident { return i; } - / KW_AS? __ i:ident { return i; } + / KW_AS? __ i:column { return i; } from_unnest_item = 'UNNEST'i __ LPAREN __ a:expr? __ RPAREN __ alias:alias_clause? __ wf:with_offset? { @@ -2485,7 +2485,7 @@ ident } alias_ident - = name:ident_name !{ + = name:column_name !{ if (reservedMap[name.toUpperCase()] === true) throw new Error("Error: "+ JSON.stringify(name)+" is a reserved word, can not as alias clause"); return false } { @@ -2548,7 +2548,7 @@ ident_start = [A-Za-z_] ident_part = [A-Za-z0-9_-] // to support column name like `cf1:name` in hbase -column_part = [A-Za-z0-9_:-] +column_part = [A-Za-z0-9_:] param = s:(':'/'@') n:ident_name { diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 05a956cb..769287e7 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -4405,7 +4405,7 @@ ident_list return createList(head, tail) } alias_ident - = name:ident_name !{ return reservedMap[name.toUpperCase()] === true } c:(__ LPAREN __ column_list __ RPAREN)? { + = name:column_name !{ return reservedMap[name.toUpperCase()] === true } c:(__ LPAREN __ column_list __ RPAREN)? { // => string if (!c) return name; return `${name}(${c[3].map(v => v.value).join(', ')})` diff --git a/pegjs/redshift.pegjs b/pegjs/redshift.pegjs index d5a3cddc..787a5de1 100644 --- a/pegjs/redshift.pegjs +++ b/pegjs/redshift.pegjs @@ -4074,7 +4074,7 @@ ident_list return createList(head, tail) } alias_ident - = name:ident_name !{ return reservedMap[name.toUpperCase()] === true } c:(__ LPAREN __ column_list __ RPAREN)? { + = name:column_name !{ return reservedMap[name.toUpperCase()] === true } c:(__ LPAREN __ column_list __ RPAREN)? { // => string if (!c) return name; return `${name}(${c[3].map(v => v.value).join(', ')})` diff --git a/pegjs/snowflake.pegjs b/pegjs/snowflake.pegjs index 09dcaf37..02f898cf 100644 --- a/pegjs/snowflake.pegjs +++ b/pegjs/snowflake.pegjs @@ -2277,8 +2277,8 @@ value_alias_clause = KW_AS? __ i:alias_ident { /*=>alias_ident*/ return i; } alias_clause - = KW_AS __ i:ident_without_kw { /*=>alias_ident*/ return i; } - / KW_AS? __ i:ident { /*=>ident*/ return i; } + = KW_AS __ i:column_without_kw { /*=>alias_ident*/ return i; } + / KW_AS? __ i:column { /*=>ident*/ return i; } into_clause = KW_INTO __ v:var_decl_list { diff --git a/test/postgres.spec.js b/test/postgres.spec.js index dbdad739..c00aad79 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -1602,6 +1602,13 @@ describe('Postgres', () => { 'CREATE TABLE "User" ("gender" "Gender" NOT NULL)', ] }, + { + title: 'no space before line comment', + sql: [ + 'select * from model_a a-- comment', + 'SELECT * FROM "model_a" AS "a"' + ] + }, ] function neatlyNestTestedSQL(sqlList){ sqlList.forEach(sqlInfo => { diff --git a/test/redshift.spec.js b/test/redshift.spec.js index fe36082b..38e98a70 100644 --- a/test/redshift.spec.js +++ b/test/redshift.spec.js @@ -37,4 +37,9 @@ describe('redshift', () => { expect(getParsedSql(sql)).to.be.equal('SELECT * FROM "montara_raw"."raw_listings" WHERE price = -minimum_nights') }) + it('should support no space before line comment', () => { + const sql = 'select * from model_a a-- comment' + expect(getParsedSql(sql)).to.be.equal('SELECT * FROM "model_a" AS "a"') + }) + }) \ No newline at end of file