Trigram function(s) handle string-concatenating expressions incorrectly
See original GitHub issueAffected version Npgsql.EntityFrameworkCore.PostgreSQL.Trigrams version 3.1.4 (we’ve chosen not to migrate the project to .NET 5.0)
Problem The following code generates an SQL query that is incorrectly typed:
var entities = context.Entities.Where(entity =>
EF.Functions.TrigramsAreSimilar(
entity.Text1 + " " + entity.Text2,
"query"
)
);
Expected result A correct query selecting rows whose concatenations of textual fields are similar to the string “query”
Actual result A datatype mismatch, due to the similarity test taking precedence over string concatenation in the WHERE clause, and the whole clause being typed as text instead of boolean (see details for more info).
Solution Generate brackets around the string-concatenating part of the expression
Details
Query generated:
SELECT e."Id", e."Text1", e."Text2"
FROM "Entities" AS e
WHERE ((e."Text1" || ' ') || e."Text2" % 'query')
In this example, e.“Text2” % ‘query’ is evaluated first, and then the resulting boolean is converted into a string and appended to the syntactically-preceding string. Here’s a simplified SQL example demonstrating the problem - result on the bottom.
Exception data: Severity: ERROR SqlState: 42804 MessageText: argument of WHERE must be type boolean, not type text Position: 66 File: parse_coerce.c Line: 1148 Routine: coerce_to_boolean
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Seems to be fixed in 3.1.11
Thanks for filing - as you’ve noticed this bug has already been fixed in 5.0.2. I’ll backport the fix to the next patch release of 3.1.x.