Skip to content

Commit

Permalink
Merge pull request #2056 from taozhi8833998/refactor-no-space-line-co…
Browse files Browse the repository at this point in the history
…mment

refactor: support no space before line comment
  • Loading branch information
taozhi8833998 authored Aug 7, 2024
2 parents 4b5fe45 + 05a4252 commit f7d994a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -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
} {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')})`
Expand Down
2 changes: 1 addition & 1 deletion pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')})`
Expand Down
4 changes: 2 additions & 2 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
5 changes: 5 additions & 0 deletions test/redshift.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
})

})

0 comments on commit f7d994a

Please sign in to comment.