Skip to content

Commit

Permalink
Merge pull request #2059 from taozhi8833998/fix-cast-jsonb
Browse files Browse the repository at this point in the history
fix: mixed cast and jsonb expr
  • Loading branch information
taozhi8833998 authored Aug 10, 2024
2 parents 32c6168 + ca995fc commit 1eb158e
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,11 +1826,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2629,11 +2629,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3068,11 +3068,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3934,11 +3934,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
6 changes: 3 additions & 3 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4294,11 +4294,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary __ tail: (__ ('@>' / '<@') __ column_list_item)+ {
= head:primary __ tail: (__ ('@>' / '<@') __ primary)+ {
// => binary_expr
return createBinaryExprChain(head, tail)
}
/ head:primary __ tail: (__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
/ head:primary __ tail: (__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
// => primary | binary_expr
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
Expand Down Expand Up @@ -4908,7 +4908,7 @@ cast_expr
/ e:(column_ref_quoted / literal / aggr_func / window_func / func_call / case_expr / interval_expr / column_ref_array_index / param) __ c:cast_double_colon? {
/* => ({
type: 'cast';
expr: literal | aggr_func | func_call | case_expr | interval_expr | column_ref | param
expr: literal | jsonb_expr | aggr_func | func_call | case_expr | interval_expr | column_ref | param
| expr;
keyword: 'cast';
} & cast_double_colon)
Expand Down
4 changes: 2 additions & 2 deletions pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3974,11 +3974,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3400,11 +3400,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,11 +2158,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
/ head:primary tail:(__ ('@>' / '<@') __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
Expand Down
4 changes: 2 additions & 2 deletions pegjs/trino.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3383,11 +3383,11 @@ unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary __ tail: (__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
= head:primary __ tail: (__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ primary)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary __ tail: (__ ('@>' / '<@') __ column_list_item)+ {
/ head:primary __ tail: (__ ('@>' / '<@') __ primary)+ {
return createBinaryExprChain(head, tail)
}

Expand Down
15 changes: 15 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,21 @@ describe('Postgres', () => {
'SELECT * FROM "model_a" AS "a"'
]
},
{
title: 'mixed jsonb and cast',
sql: [
"SELECT person.name FROM person WHERE person.email_addresses::json -> 'items'::text ILIKE '%sam%'",
`SELECT "person".name FROM "person" WHERE "person".email_addresses::JSON -> 'items'::TEXT ILIKE '%sam%'`
]
},
{
title: 'multiple jsonb operator expr',
sql: [
`select '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}', '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb, '{"a":1, "b":2}'::jsonb ? 'b', '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']
, '["a", "b"]'::jsonb ?& array['a', 'b'], '["a", "b"]'::jsonb || '["c", "d"]'::jsonb, '{"a": "b"}'::jsonb - 'a', '["a", "b"]'::jsonb - 1, '["a", {"b":1}]'::jsonb #- '{1,b}'`,
`SELECT '{"a": {"b":{"c": "foo"}}}'::JSON #> '{a,b}', '{"a":1, "b":2}'::JSONB @> '{"b":2}'::JSONB, '{"a":1, "b":2}'::JSONB ? 'b', '{"a":1, "b":2, "c":3}'::JSONB ?| ARRAY['b','c'], '["a", "b"]'::JSONB ?& ARRAY['a','b'], '["a", "b"]'::JSONB || '["c", "d"]'::JSONB, '{"a": "b"}'::JSONB - 'a', '["a", "b"]'::JSONB - 1, '["a", {"b":1}]'::JSONB #- '{1,b}'`
]
},
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit 1eb158e

Please sign in to comment.