Skip to content

Commit

Permalink
Merge pull request #2071 from taozhi8833998/fix-jsonb-pg
Browse files Browse the repository at this point in the history
fix: jsonb bug in pg
  • Loading branch information
taozhi8833998 authored Aug 21, 2024
2 parents 571b652 + 013eb62 commit 52c681e
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 47 deletions.
7 changes: 2 additions & 5 deletions pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,11 +1826,8 @@ unary_operator
= '!' / '-' / '+' / '~'

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

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

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

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

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

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

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

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

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

string_constants_escape
= 'E'i"'" __ n:single_char* __ "'" {
Expand Down
6 changes: 3 additions & 3 deletions test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,10 +1107,10 @@ describe('mysql', () => {

it('should throw error when args is not right', () => {
let sql = `select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")));`
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "*", "+", ",", "-", "--", "->", "->>", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "?", "?&", "?|", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "*", "+", ",", "-", "--", "->", "->>", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "?", "?&", "?|", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "*", "+", ",", "-", "--", "->", "->>", "/", "/*", "<", "<<", "<=", "<>", "<@", "=", ">", ">=", ">>", "?", "?&", "?|", "@>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "*", "+", ",", "-", "--", "->", "->>", "/", "/*", "<", "<<", "<=", "<>", "<@", "=", ">", ">=", ">>", "?", "?&", "?|", "@>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
sql = 'SELECT AVG(Quantity,age) FROM table1;'
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "(", ")", "*", "+", "-", "--", "->", "->>", ".", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "?", "?&", "?|", "BETWEEN", "IN", "IS", "LIKE", "NOT", "REGEXP", "RLIKE", "XOR", "^", "div", "|", "||", [ \\t\\n\\r], [A-Za-z0-9_$\\x80-], or [A-Za-z0-9_:] but "," found.')
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "#-", "#>", "#>>", "%", "&", "&&", "(", ")", "*", "+", "-", "--", "->", "->>", ".", "/", "/*", "<", "<<", "<=", "<>", "<@", "=", ">", ">=", ">>", "?", "?&", "?|", "@>", "BETWEEN", "IN", "IS", "LIKE", "NOT", "REGEXP", "RLIKE", "XOR", "^", "div", "|", "||", [ \\t\\n\\r], [A-Za-z0-9_$\\x80-], or [A-Za-z0-9_:] but "," found.')
})

it('should join multiple table with comma', () => {
Expand Down
64 changes: 64 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1974,4 +1974,68 @@ describe('Postgres', () => {
expect(fun).to.throw(`Expected "--", "/*", "\\"", [ \\t\\n\\r], or [A-Za-z_一-龥] but "'" found.`)
})
})
describe('pg parse speed', function () {
this.timeout(30)
const sql = `SELECT
"pr"."destination_currency" AS "currency",
"pr"."idempotency_key" AS "unqiueRequestId",
"pr"."status" AS "paymentRequestStatus",
"pr"."payment_date" AS "paymentDate",
"pr"."provider_system_reference_number" AS "providerSystemReferenceNumber",
"pr"."destination_amount" AS "destinationAmount",
"pr"."error" AS "error",
"pr"."source_id" AS "sourceId",
"pr"."source_type" AS "sourceType",
"pr"."client_legal_entity_id" AS "clientLegalEntityId",
"pr"."beneficiary_legal_entity_id" AS "beneficiaryLegalEntityId",
"pr"."provider" AS "provider",
"txn"."created_at" AS "transactionCreatedAt",
"txn"."updated_at" AS "transactionUpdatedAt",
"txn"."provider_metadata" AS "providerMetadata",
"txn"."currency" AS "currency",
"txn"."amount" AS "amount",
"txn"."status" AS "status",
"txn"."type" AS "txnType",
"txn"."purpose_of_payment" AS "purposeOfPayment",
"txn"."client_reference" AS "clientReference",
"txn"."description" AS "transactionDescription",
pr.meta -> 'invoiceIds' AS "invoiceIds"
FROM
"public"."payment_request" "pr"
LEFT JOIN "public"."transaction" "txn" ON "txn"."payment_request_id" = "pr"."id"
WHERE
"pr"."source_id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $50)
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
OR pr.meta::jsonb -> 'invoiceIds' @> '["c937cd8c-65bc-4006-9413-dde7b43c61b6"]'
ORDER BY
"pr"."created_at" DESC`
const ast = parser.astify(sql, opt)
expect(ast).to.be.an('object')
})
})

0 comments on commit 52c681e

Please sign in to comment.