Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup to Cosmos regex translation #28261

Merged
1 commit merged into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/EFCore.Cosmos/Query/Internal/CosmosRegexTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class CosmosRegexTranslator : IMethodCallTranslator
private static readonly MethodInfo IsMatchWithRegexOptions =
typeof(Regex).GetRuntimeMethod(nameof(Regex.IsMatch), new[] { typeof(string), typeof(string), typeof(RegexOptions) })!;

private const RegexOptions SupportedOptions = RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;

private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
Expand Down Expand Up @@ -53,48 +51,49 @@ public CosmosRegexTranslator(ISqlExpressionFactory sqlExpressionFactory)

var (input, pattern) = (arguments[0], arguments[1]);
var typeMapping = ExpressionExtensions.InferTypeMapping(input, pattern);
(input, pattern) = (
_sqlExpressionFactory.ApplyTypeMapping(input, typeMapping),
_sqlExpressionFactory.ApplyTypeMapping(pattern, typeMapping));

if (method == IsMatch)
if (method == IsMatch || arguments[2] is SqlConstantExpression { Value: RegexOptions.None })
{
return _sqlExpressionFactory.Function(
"RegexMatch",
new[] {
_sqlExpressionFactory.ApplyTypeMapping(input, typeMapping),
_sqlExpressionFactory.ApplyTypeMapping(pattern, typeMapping)
},
typeof(bool));
return _sqlExpressionFactory.Function("RegexMatch", new[] { input, pattern }, typeof(bool));
}
else if (arguments[2] is SqlConstantExpression { Value: RegexOptions regexOptions })

if (arguments[2] is SqlConstantExpression { Value: RegexOptions regexOptions })
{
string modifier = "";
var modifier = "";

if (regexOptions.HasFlag(RegexOptions.Multiline))
{
regexOptions &= ~RegexOptions.Multiline;
modifier += "m";
}

if (regexOptions.HasFlag(RegexOptions.Singleline))
{
regexOptions &= ~RegexOptions.Singleline;
modifier += "s";
}

if (regexOptions.HasFlag(RegexOptions.IgnoreCase))
{
regexOptions &= ~RegexOptions.IgnoreCase;
modifier += "i";
}

if (regexOptions.HasFlag(RegexOptions.IgnorePatternWhitespace))
{
regexOptions &= ~RegexOptions.IgnorePatternWhitespace;
modifier += "x";
}

return (regexOptions & ~SupportedOptions) == 0
return regexOptions == 0
? _sqlExpressionFactory.Function(
"RegexMatch",
new[]
{
_sqlExpressionFactory.ApplyTypeMapping(input, typeMapping),
_sqlExpressionFactory.ApplyTypeMapping(pattern, typeMapping),
_sqlExpressionFactory.Constant(modifier)
},
new[] { input, pattern, _sqlExpressionFactory.Constant(modifier) },
typeof(bool))
: null;
: null; // TODO: Report unsupported RegexOption, #26410
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ await AssertQuery(
AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND RegexMatch(c[""CustomerID""], ""^T"", """"))");
WHERE ((c[""Discriminator""] = ""Customer"") AND RegexMatch(c[""CustomerID""], ""^T""))");
}

[ConditionalTheory]
Expand Down Expand Up @@ -1313,15 +1313,19 @@ FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND RegexMatch(c[""CustomerID""], ""^T"", ""ix""))");
}

[Fact]
public virtual void Regex_IsMatch_MethodCall_With_Unsupported_Option()
=> Assert.Throws<InvalidOperationException>(() =>
Fixture.CreateContext().Customers.Where(o => Regex.IsMatch(o.CustomerID, "^T", RegexOptions.RightToLeft)).ToList());
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Regex_IsMatch_MethodCall_With_Unsupported_Option(bool async)
=> AssertTranslationFailed(() => AssertQuery(
async,
ss => ss.Set<Customer>().Where(o => Regex.IsMatch(o.CustomerID, "^T", RegexOptions.RightToLeft))));

[Fact]
public virtual void Regex_IsMatch_MethodCall_With_Any_Unsupported_Option()
=> Assert.Throws<InvalidOperationException>(() =>
Fixture.CreateContext().Customers.Where(o => Regex.IsMatch(o.CustomerID, "^T", RegexOptions.IgnoreCase | RegexOptions.RightToLeft)).ToList());
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Regex_IsMatch_MethodCall_With_Any_Unsupported_Option(bool async)
=> AssertTranslationFailed(() => AssertQuery(
async,
ss => ss.Set<Customer>().Where(o => Regex.IsMatch(o.CustomerID, "^T", RegexOptions.IgnoreCase | RegexOptions.RightToLeft))));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
Expand Down