Skip to content

Missing Parantheses around JSON patterns in generated SQL #3923

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions