Skip to content

Escape [ in vocabulary search terms only on SQL Server dialects - #2603

Open
chenyangsu wants to merge 1 commit into
OHDSI:masterfrom
chenyangsu:fix/vocab-search-brackets
Open

chenyangsu wants to merge 1 commit into
OHDSI:masterfrom
chenyangsu:fix/vocab-search-brackets

Conversation

@chenyangsu

Copy link
Copy Markdown

prepareExecuteSearch replaces every [ in the search term with [[] before binding it into LIKE '%...%'. That is how T-SQL matches a literal bracket, but PostgreSQL's LIKE gives [ no special meaning, so there the pattern looks for the three characters [[]. RxNorm branded drug names carry the brand in brackets, and on PostgreSQL a search for tablet [lipitor] returns [] for the concept atorvastatin 10 MG Oral Tablet [Lipitor].

This change applies the escape only when the source dialect is sql server, pdw or synapse, the dialects that read [ as the start of a character class, and leaves the term as typed for the others. The lexical search uses the same term, so it is covered too. An ESCAPE clause in the search SQL would be another way to do this; I can switch if you prefer that.

Testing: the new VocabularySearchBracketTest runs the standard and lexical search on the embedded PostgreSQL (both return [] on master) and checks that a SQL Server source still binds %tablet [[]lipitor]%. ./mvnw test: 179 unit tests, 0 failures, 5 skipped; 35 integration tests, 0 failures, 3 skipped.

The search term had every [ replaced with [[], which is the T-SQL way to
match a literal bracket in LIKE. Other dialects read [[] literally, so on
PostgreSQL a search such as "tablet [lipitor]" matched no concept names.
@chrisknoll

Copy link
Copy Markdown
Collaborator

I believe this is an issue that should be handled in OHDSI/SqlRender?

@chenyangsu

Copy link
Copy Markdown
Author

Thanks for looking. As far as I can tell SqlRender never sees the search term. prepareExecuteSearch adds the [[] escape to search.query, and PreparedSqlRender.fixPreparedStatementSql replaces '%@query%' with a ? placeholder, so the term is bound as a JDBC parameter and SqlRender only translates the SQL around it. If you'd still rather handle it in SqlRender, I'm happy to open an issue there and close this PR.

@chrisknoll

Copy link
Copy Markdown
Collaborator

Hmmm not going to be handleable in sql render if it's a prepared statement issue: the ? isn't going to match on any rules. Hmmmmmmmmmmm (thinking) hmmmmmmmm

Ok, short term this might be the way we go, longer term, i think SqlRender needs a subset of functionality to manage like-search term transform, then we can mutate the value from sqlserver to whatever underlying dilects understand the escape.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants