From 9bc55dca74c3b27ef7f751cf82b9cf572962f4a5 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Wed, 24 Jan 2024 09:32:20 +0800 Subject: [PATCH] fix: double quoted column cast in pg --- pegjs/postgresql.pegjs | 7 ++++--- test/postgres.spec.js | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 244e0baf..7b02fc8c 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -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] }, @@ -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 }; diff --git a/test/postgres.spec.js b/test/postgres.spec.js index 3b566138..31f4c90a 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -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 => {