Skip to content

Commit

Permalink
Merge pull request #1765 from taozhi8833998/fix-double-quoted-pg
Browse files Browse the repository at this point in the history
fix: double quoted column cast in pg
  • Loading branch information
taozhi8833998 authored Jan 25, 2024
2 parents b8e918a + 9bc55dc commit 0941417
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2837,12 +2837,13 @@ column_list_item
// => { expr: expr; as: null; }
return { expr: c, as: null }
}
/ e:expr_item __ s:KW_DOUBLE_COLON __ t:cast_data_type __ a:((DOUBLE_ARROW / SINGLE_ARROW) __ (literal_string / literal_numeric))* __ tail:(__ (additive_operator / multiplicative_operator) __ expr_item)* __ alias:alias_clause? {
/ e:(double_quoted_ident / expr_item) __ s:KW_DOUBLE_COLON __ t:cast_data_type __ a:((DOUBLE_ARROW / SINGLE_ARROW) __ (literal_string / literal_numeric))* __ tail:(__ (additive_operator / multiplicative_operator) __ expr_item)* __ alias:alias_clause? {
if (typeof e === 'string') columnList.add(`select::null::${e}`)
// => { type: 'cast'; expr: expr; symbol: '::'; target: cast_data_type; as?: null; arrows?: ('->>' | '->')[]; property?: (literal_string | literal_numeric)[]; }
return {
as: alias,
type: 'cast',
expr: e,
expr: typeof e === 'string' ? { type: 'double_quote_string', value: e } : e,
symbol: '::',
target: t,
tail: tail && tail[0] && { operator: tail[0][1], expr: tail[0][3] },
Expand Down Expand Up @@ -2883,7 +2884,7 @@ column_list_item
as: null
};
}
/ c:double_quoted_ident __ d:DOT? !{ if(d) return true } __ alias: alias_clause? {
/ c:double_quoted_ident __ d:(DOT / KW_DOUBLE_COLON)? !{ if(d) return true } __ alias: alias_clause? {
// => { type: 'expr'; expr: expr; as?: alias_clause; }
columnList.add(`select::null::${c}`)
return { type: 'expr', expr: { type: 'column_ref', table: null, column: c }, as: alias };
Expand Down
9 changes: 8 additions & 1 deletion test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,14 @@ describe('Postgres', () => {
SET search_path TO ht_hyt;
COMMIT;`,
]
}
},
{
title: 'double quoted column cast',
sql: [
'SELECT "created_date"::date FROM src_hosts',
'SELECT "created_date"::DATE FROM "src_hosts"'
]
},
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit 0941417

Please sign in to comment.