Skip to content

Commit

Permalink
fix: unary operator in redshift
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed May 5, 2024
1 parent 25989ba commit a6f931d
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 24 deletions.
14 changes: 12 additions & 2 deletions pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,8 +1754,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -1782,6 +1782,16 @@ primary
}
/ var_decl

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:ident __ DOT __ col:column {
columnList.add(`select::${tbl}::${col}`);
Expand Down
14 changes: 12 additions & 2 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,8 +2263,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -2288,6 +2288,16 @@ primary
return list;
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

interval_expr
= KW_INTERVAL __
e:expr __
Expand Down
14 changes: 12 additions & 2 deletions pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,8 +1770,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -1793,6 +1793,16 @@ primary
}
/ var_decl

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:(ident __ DOT __)? col:column __ a:((DOUBLE_ARROW / SINGLE_ARROW) __ (literal_string / literal_numeric))+ __ ca:collate_expr? {
const tableName = tbl && tbl[0] || null
Expand Down
14 changes: 12 additions & 2 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,8 +2577,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -2608,6 +2608,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

map_expr_item
= k:literal_string __ COMMA __ v:ident_without_kw_type {
columnList.add(`select::null::${v.value}`);
Expand Down
14 changes: 12 additions & 2 deletions pegjs/hive.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1758,8 +1758,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -1786,6 +1786,16 @@ primary
}
/ var_decl

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:ident __ DOT __ col:column {
columnList.add(`select::${tbl}::${col}`);
Expand Down
14 changes: 12 additions & 2 deletions pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3905,8 +3905,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -3936,6 +3936,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
// => { type: 'origin'; value: string; }
Expand Down
14 changes: 12 additions & 2 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4087,8 +4087,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -4118,6 +4118,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
// => { type: 'origin'; value: string; }
Expand Down
14 changes: 12 additions & 2 deletions pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3941,8 +3941,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -3972,6 +3972,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
// => { type: 'origin'; value: string; }
Expand Down
14 changes: 12 additions & 2 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3296,8 +3296,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -3327,6 +3327,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
// => { type: 'origin'; value: string; }
Expand Down
14 changes: 12 additions & 2 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,8 +1934,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand Down Expand Up @@ -1963,6 +1963,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:(ident __ DOT __)? col:column __ a:((DOUBLE_ARROW / SINGLE_ARROW) __ (literal_string / literal_numeric))+ __ ca:collate_expr? {
const tableName = tbl && tbl[0] || null
Expand Down
14 changes: 12 additions & 2 deletions pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2331,8 +2331,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -2354,6 +2354,16 @@ primary
}
/ var_decl

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

column_ref
= db:(ident __ DOT)? __ schema:(ident __ DOT)? __ tbl:(ident __ DOT)? __ col:column {
const obj = { table: null, db: null, schema: null }
Expand Down
14 changes: 12 additions & 2 deletions pegjs/trino.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3339,8 +3339,8 @@ additive_operator
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ primary)* {
= head:unary_expr_or_primary
tail:(__ (multiplicative_operator / LOGIC_OPERATOR) __ unary_expr_or_primary)* {
// => binary_expr
return createBinaryExprChain(head, tail)
}
Expand Down Expand Up @@ -3376,6 +3376,16 @@ primary
}
}

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
}

unary_operator
= '!' / '-' / '+' / '~'

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
// => { type: 'origin'; value: string; }
Expand Down
5 changes: 5 additions & 0 deletions test/redshift.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ describe('redshift', () => {
expect(getParsedSql(sql)).to.be.equal(`WITH "pv" AS (SELECT action_date, visitor_id_v, visit_country_name, referer_channel_group, email, sgid, "mp"."brand/non-brand" AS "is_brand" FROM "dwh_fact_pageviews" AS "pv" LEFT JOIN "ppc_keywords_mapping" AS "mp" USING ("campaign_keyword")) SELECT is_brand AS "b/nb" FROM "pv" WHERE "mp"."brand/non-brand" = 'brand'`)
})

it('should support unary operator', () => {
const sql = 'SELECT * FROM montara_raw.raw_listings WHERE price = -minimum_nights'
expect(getParsedSql(sql)).to.be.equal('SELECT * FROM "montara_raw"."raw_listings" WHERE price = -minimum_nights')
})

})

0 comments on commit a6f931d

Please sign in to comment.