diff --git a/pegjs/db2.pegjs b/pegjs/db2.pegjs index 396369c0..4155d4e1 100644 --- a/pegjs/db2.pegjs +++ b/pegjs/db2.pegjs @@ -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) } diff --git a/pegjs/flinksql.pegjs b/pegjs/flinksql.pegjs index 893fada3..63f0a6d8 100644 --- a/pegjs/flinksql.pegjs +++ b/pegjs/flinksql.pegjs @@ -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) } diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 6bb848d4..9f88fdac 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -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) } diff --git a/pegjs/noql.pegjs b/pegjs/noql.pegjs index 1564ac57..f9de2d4b 100644 --- a/pegjs/noql.pegjs +++ b/pegjs/noql.pegjs @@ -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) } diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 265575cd..48db21de 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -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) @@ -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) diff --git a/pegjs/redshift.pegjs b/pegjs/redshift.pegjs index 787a5de1..f057c789 100644 --- a/pegjs/redshift.pegjs +++ b/pegjs/redshift.pegjs @@ -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) } diff --git a/pegjs/snowflake.pegjs b/pegjs/snowflake.pegjs index 02f898cf..2c78905b 100644 --- a/pegjs/snowflake.pegjs +++ b/pegjs/snowflake.pegjs @@ -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) } diff --git a/pegjs/sqlite.pegjs b/pegjs/sqlite.pegjs index 7c4fed61..7be8ba67 100644 --- a/pegjs/sqlite.pegjs +++ b/pegjs/sqlite.pegjs @@ -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) } diff --git a/pegjs/trino.pegjs b/pegjs/trino.pegjs index 7b5b9532..6dbaaf4d 100644 --- a/pegjs/trino.pegjs +++ b/pegjs/trino.pegjs @@ -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) } diff --git a/test/postgres.spec.js b/test/postgres.spec.js index c00aad79..046998b0 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -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 => {