Skip to content

Missing Parantheses around JSON patterns in generated SQL #3923

Closed

Description

@fritz3n

As far as i can see, JSON patterns are never surrounded by parenthesis, even if needed.

In our particular use case, we load the pattern from a json columns property. This currently results in a generated query like the following:

SELECT 'a' ~ ('(?p)' || '{"a":1}'::json ->> 'a')

Because of operator precedence, this is parsed as ('(?p)' || '{"a":1}'::json) ->> 'a', e.g. ('(?p){"a":1}') ->> 'a' which results in a 22P02: invalid input syntax for type json.

This can be fixed by always including parenthesis around sql patterns. Particularly changing the code block at

Sql.Append("' || ");
Visit(expression.Pattern);
Sql.Append(")");
to:

Sql.Append("' || (");
Visit(expression.Pattern);
Sql.Append("))");

A more correct approach may utilize RequiresParentheses.

Can supply minimal example and tests if needed.

Activity

  1. roji commented on Oct 4, 2026

    @roji
    Member

    Thanks, makes sense. I opened #3927 (replacing #3924) which fixes this via RequiresParentheses as you suggested. Will backport the fix.

  2. added this to the 10.0.4 milestone on Oct 4, 2026
  3. added theissue type on Oct 4, 2026
  4. added 3 commits that reference this issue on Oct 4, 2026
    26aadb3
    9a84250
    1d873bd
  5. roji commented on Oct 4, 2026

    @roji
    Member

    Backported to 10.0.4 via 26aadb3
    Backported to 9.0.5 via 9a84250
    Backported to 8.0.12 via 1d873bd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions