question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigram function(s) handle string-concatenating expressions incorrectly

See original GitHub issue

Affected 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lukaepticommented, Mar 3, 2021

Seems to be fixed in 3.1.11

1reaction
rojicommented, Jan 21, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing parentheses in query translation (NpgsqlTsVector ...
Functions.ToTsVector(m.SomeProp) .Matches(EF.Functions . ... Trigram function(s) handle string-concatenating expressions incorrectly #1659.
Read more >
Trigram search gets much slower as search string gets longer
My ultimate problem is that the search string comes from a web interface which may send quite long strings and thus be quite...
Read more >
Trigram Wildcard String Search in SQL Server
Wildcard searching using trigrams · Persist three-character substrings (trigrams) of the target data. · Split the search term(s) into trigrams.
Read more >
Performance Optimisation for Wildcards Search in Postgres ...
A trigram is a sequence of three consecutive characters in a string. For example, the trigrams of Hello are Hel , ell ,...
Read more >
What Are N-grams and How to Implement Them in Python?
In this beginner-level tutorial, we will learn what n-grams are and explore them on text data in Python. The objective of the blog...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found