[PostgreSQL] Comparing jsonb columns to string values in WHERE statements
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- fsharp, postgresql
- Domain
- databases
Research direction
Start by tracing the filterBuilder function and how PostgreSQL types treated as strings are handled. Compare the two proposed jsonb-to-text and text-to-jsonb equality approaches, including invalid and semantically equivalent JSON cases. Done means the chosen behavior prevents the reported jsonb = text error without hiding invalid comparisons.
Written by the indexing model from the issue text.
Description
... results in an error "operator does not exist: jsonb = text".
This is because the filterBuilder function does not add a type cast for PostgreSQL types which are treated as strings .NET-side, such as jsonb.
Workaround: define a jsonb -> text -> bool equality function in your database schema, then associate it to the = operator (or invoke it explicitly).
However, there are two ways to define this equality:
-
Cast the jsonb column to text. This will never fail, however it may return
falseif the resulting text differs from the parameter in semantically-insignificant ways, such as whitespace, or the ordering of JSON properties -
Cast the text value to jsonb. This will perform a proper semantic comparison, but will throw an exception if the text isn't valid JSON - and I can imagine instances where you may want to check an unknown (user-provided?) text against a stored JSON value. Note that an empty string is not valid json.
If I implement this in a PR (which may be tricky, as filterBuilder doesn't seem to have access to the column type), which approach do you think should be taken? I strongly believe that the latter is the better one (because any failures will be immediately obvious rather than stealthy, and because both F# and PostgreSQL have a culture of correctness over permissiveness), but I'm throwing it out there in case people have different opinions.
Below an example of the workaround above, for anybody who may run into this issue:
create or replace function jsonb_compare(j jsonb, t text)
returns bool
as $$ select (j=t::jsonb)
$$ language sql;
create or replace function jsonb_compare_2(t text, j jsonb)
returns bool
as $$ select (j=t::jsonb)
$$ language sql;
create operator = (
leftarg = jsonb,
rightarg = text,
procedure = jsonb_compare,
commutator = =
);
create operator = (
leftarg = text,
rightarg = jsonb,
procedure = jsonb_compare_2,
commutator = =
);
- Dominant language
- F#
- Stars
- 627
- Forks
- 147
- Avg merge
- 2h 2m
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fsprojects/SQLProvider
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 55/100
fsprojects/SQLProvider#872 · 2 comments ·
-
Repo Assist? Openenhancement
Difficulty 5/5 Over a week Newbie friendliness 10/100
fsprojects/SQLProvider#870 · 1 comment ·
-
postgresql
Difficulty 4/5 3-5 days Newbie friendliness 58/100
fsprojects/SQLProvider#869 · 2 comments ·
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 42/100
fsprojects/SQLProvider#868 · 2 comments ·
-
sql server
Difficulty 4/5 3-5 days Newbie friendliness 45/100
fsprojects/SQLProvider#851 · 1 comment ·
All issues in fsprojects/SQLProvider
Similar issues
-
[6.x]: "Cannot use object of type stdClass as array" loading Users index (regression of #19182) Open
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
community-scripts/ProxmoxVE#17396 · 1 comment ·
-
bug possible-duplicate
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
AOSSIE-Org/PictoPy#1553 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
BasedHardware/omi#15274 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100