Repository navigation
Translate string.Trim with characters to TRIM on SQL Server - #39603
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
- Translate string.Trim(char) and string.Trim(char[]) to TRIM(characters FROM string) when targeting SQL Server 2017 (compatibility level 140) or later, Azure SQL or Azure Synapse. Older targets keep failing translation as before, and Trim() without characters still translates to LTRIM(RTRIM(...)). - Generate the TRIM(characters FROM string) syntax in SqlServerQuerySqlGenerator for a built-in TRIM function with two arguments, as is already done for the special syntax of other functions. - Share the character handling of TrimStart/TrimEnd (constant char and char[], char parameters sent as a one-character string), and rename ProcessTrimStartEnd to ProcessTrim accordingly. - Add Trim tests with a char parameter, a non-ASCII char parameter and a char array parameter, matching the existing TrimStart/TrimEnd tests, and update the SQL Server baselines. Fixes dotnet#22927
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22927
On SQL Server,
string.Trim()without arguments translates toLTRIM(RTRIM(...)), andTrimStart/TrimEndwith characters translate to the SQL Server 2022LTRIM/RTRIMoverloads.Trimwith characters wasn't translated at all, so a query likeWhere(b => b.Name.Trim('-') == "foo")failed with a translation error.Fix
Trim(char)andTrim(char[])now translate toTRIMwith itscharacters FROMsyntax:TRIMis available from SQL Server 2017, so the translation applies when the compatibility level is 140 or higher (and on Azure SQL with 140 or higher, and on Azure Synapse), the same waystring.Join→CONCAT_WSis gated. With older compatibility levels the query fails translation as before.TRIMfunction with the string and the characters as its arguments, andSqlServerQuerySqlGeneratorwrites it asTRIM(characters FROM string). This follows the existing special cases there (ISNULL,JSON_OBJECT,modify), so no new expression type is needed, and nullability, parameter extraction and precompiled queries work as for any other function.TrimStart/TrimEnd, so I shared that code (ProcessTrimStartEndis renamed toProcessTrim): a constantcharorchar[]is inlined as a string literal, and acharparameter is sent as a one-character string. A non-constantchar[]still isn't translated.Trim()without characters is unchanged (LTRIM(RTRIM(...))).Tests
StringTranslationsSqlServerTest:Trim_with_char_argument_in_predicateandTrim_with_char_array_argument_in_predicatepreviously asserted a translation failure that referenced this issue. They now assert theTRIMSQL whenIsFunctions2017Supported, and a translation failure otherwise, like theTrimStart/TrimEndtests do for 2022.StringTranslationsTestBase: newTrim_with_char_parameter_in_predicate,Trim_with_non_ASCII_char_parameter_in_predicateandTrim_with_char_array_parameter_in_predicate, matching the existingTrimStart/TrimEndtests, with overrides for SQL Server, SQLite and Cosmos.I ran
StringTranslationsSqlServerTest(122 passed) against Azure SQL Edge (SQL Server 2019 engine, below compatibility level 160), so the newTRIMassertions ran against a real server, with the data assertions checking the trimmed results, while theTrimStart/TrimEndcharacter tests took their translation-failure branch. I also ranStringTranslationsSqliteTest(125 passed) andStringTranslationsInMemoryTest(115 passed), and built the Cosmos test project. I only ran these test classes, not the full suite.