Skip to content

Commit

Permalink
Query: Adds TRIM string system function support in LINQ (#3833)
Browse files Browse the repository at this point in the history
* add trim support

* Added some test coverage

* address reviews

---------

Co-authored-by: Minh Le <[email protected]>
  • Loading branch information
leminh98 and Minh Le authored May 2, 2023
1 parent 7465da2 commit 20121c8
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,45 @@ protected override SqlScalarExpression VisitImplicit(MethodCallExpression method
}
}

private class StringVisitTrim : SqlBuiltinFunctionVisitor
{
public StringVisitTrim()
: base("TRIM",
false,
null)
{
}

protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
bool validInNet = false;
bool validInNetCore = false;

if (methodCallExpression.Arguments.Count == 1 &&
methodCallExpression.Arguments[0].NodeType == ExpressionType.Constant &&
methodCallExpression.Arguments[0].Type == typeof(char[]))
{
char[] argumentsExpressions = (char[])((ConstantExpression)methodCallExpression.Arguments[0]).Value;
if (argumentsExpressions.Length == 0)
{
validInNet = true;
}
}
else if (methodCallExpression.Arguments.Count == 0)
{
validInNetCore = true;
}

if (validInNet || validInNetCore)
{
SqlScalarExpression str = ExpressionToSql.VisitScalarExpression(methodCallExpression.Object, context);
return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.Trim, str);
}

return null;
}
}

static StringBuiltinFunctions()
{
StringBuiltinFunctionDefinitions = new Dictionary<string, BuiltinFunctionVisitor>
Expand Down Expand Up @@ -415,6 +454,10 @@ static StringBuiltinFunctions()
"TrimEnd",
new StringVisitTrimEnd()
},
{
"Trim",
new StringVisitTrim()
},
{
"StartsWith",
new SqlStringWithComparisonVisitor(SqlFunctionCallScalarExpression.Names.Startswith)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ FROM root]]></SqlQuery>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LENGTH(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace char]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace(c, a))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "c", "a")
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace string]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace("str", "str2"))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "str", "str2")
FROM root]]></SqlQuery>
</Output>
</Result>
Expand All @@ -379,34 +401,45 @@ FROM root]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimStart())]]></Expression>
<Description><![CDATA[Trim]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Trim())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LTRIM(root["StringField"])
SELECT VALUE TRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace char]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace(c, a))]]></Expression>
<Description><![CDATA[Trim with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".Trim())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "c", "a")
SELECT VALUE "abc"
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Replace string]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Replace("str", "str2"))]]></Expression>
<Description><![CDATA[Trim with EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.Trim(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE REPLACE(root["StringField"], "str", "str2")
SELECT VALUE TRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Trim with Literal and EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".Trim(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "abc"
FROM root]]></SqlQuery>
</Output>
</Result>
Expand All @@ -418,6 +451,83 @@ FROM root]]></SqlQuery>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE RTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimEnd with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimEnd())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE " abc"
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimEnd with EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimEnd(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE RTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimEnd with Literal and EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimEnd(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE " abc"
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimStart())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart with Literal]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimStart())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "abc "
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart with EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => doc.StringField.TrimStart(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE LTRIM(root["StringField"])
FROM root]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[TrimStart with Literal and EmptyCharArray]]></Description>
<Expression><![CDATA[query.Select(doc => " abc ".TrimStart(new [] {}))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE "abc "
FROM root]]></SqlQuery>
</Output>
</Result>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,26 @@ public void TestStringFunctions()
new LinqTestInput("IndexOf string w/ startIndex", b => getQuery(b).Select(doc => doc.StringField.IndexOf("str", 0))),
// Count
new LinqTestInput("Count", b => getQuery(b).Select(doc => doc.StringField.Count())),
// ToLower
new LinqTestInput("ToLower", b => getQuery(b).Select(doc => doc.StringField.ToLower())),
// TrimStart
new LinqTestInput("TrimStart", b => getQuery(b).Select(doc => doc.StringField.TrimStart())),
// Replace
new LinqTestInput("Replace char", b => getQuery(b).Select(doc => doc.StringField.Replace('c', 'a'))),
new LinqTestInput("Replace string", b => getQuery(b).Select(doc => doc.StringField.Replace("str", "str2"))),
// ToLower
new LinqTestInput("ToLower", b => getQuery(b).Select(doc => doc.StringField.ToLower())),
// Trim
new LinqTestInput("Trim", b => getQuery(b).Select(doc => doc.StringField.Trim())),
new LinqTestInput("Trim with Literal", b => getQuery(b).Select(doc => " abc ".Trim())),
new LinqTestInput("Trim with EmptyCharArray", b => getQuery(b).Select(doc => doc.StringField.Trim(new char[]{ }))),
new LinqTestInput("Trim with Literal and EmptyCharArray", b => getQuery(b).Select(doc => " abc ".Trim(new char[]{ }))),
// TrimEnd
new LinqTestInput("TrimEnd", b => getQuery(b).Select(doc => doc.StringField.TrimEnd())),
new LinqTestInput("TrimEnd with Literal", b => getQuery(b).Select(doc => " abc ".TrimEnd())),
new LinqTestInput("TrimEnd with EmptyCharArray", b => getQuery(b).Select(doc => doc.StringField.TrimEnd(new char[]{ }))),
new LinqTestInput("TrimEnd with Literal and EmptyCharArray", b => getQuery(b).Select(doc => " abc ".TrimEnd(new char[]{ }))),
// TrimStart
new LinqTestInput("TrimStart", b => getQuery(b).Select(doc => doc.StringField.TrimStart())),
new LinqTestInput("TrimStart with Literal", b => getQuery(b).Select(doc => " abc ".TrimStart())),
new LinqTestInput("TrimStart with EmptyCharArray", b => getQuery(b).Select(doc => doc.StringField.TrimStart(new char[]{ }))),
new LinqTestInput("TrimStart with Literal and EmptyCharArray", b => getQuery(b).Select(doc => " abc ".TrimStart(new char[]{ }))),
//StartsWith
new LinqTestInput("StartsWith", b => getQuery(b).Select(doc => doc.StringField.StartsWith("str"))),
new LinqTestInput("String constant StartsWith", b => getQuery(b).Select(doc => "str".StartsWith(doc.StringField))),
Expand Down

0 comments on commit 20121c8

Please sign in to comment.