Skip to content

Conversation

@WhatzGames
Copy link
Contributor

@WhatzGames WhatzGames commented Jan 16, 2024

closes #3059

I currently do not translate Regex.Count for Postgres versions < 15, but it should be possible to provide a subquery for older version if desired.

@WhatzGames WhatzGames marked this pull request as draft March 20, 2024 21:21
@WhatzGames WhatzGames marked this pull request as ready for review April 2, 2024 23:42
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good work here @WhatzGames. I'm submitting a review, but in the interest of time etc. have pushed a commit to do the suggested fixes/improvements. I'll go ahead and merge - but please take a look and tell me if you see anything problematic/improvable, we can always do more work here.

}

var (input, pattern) = (arguments[0], arguments[1]);
var typeMapping = ExpressionExtensions.InferTypeMapping(input, pattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the code was this way before, but it's incorrect - the type of the arguments/operands isn't related, and PG doesn't infer one from the other (for example, a varchar(8)-typed pattern doesn't mean that the input also has that type).

MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
=> TranslateIsMatch(instance, method, arguments, logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to have parameters for instance, logger if they're unused (this is all private code, which we can change in the future if we need to)

passingArguments,
nullable: true,
Enumerable.Repeat(true, passingArguments.Count),
typeof(int?),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be int, not int? - in SQL tree expressions, basically anything is nullable (due to SQL null propagation rules), so we have non-nullable types only.

"regexp_replace",
passingArguments,
nullable: true,
Enumerable.Repeat(true, passingArguments.Count),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use TrueArrays


return result;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: empty line

if (options.HasFlag(RegexOptions.Multiline))
{
result = "n";
}else if(!options.HasFlag(RegexOptions.Singleline))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: proper spacing


string? result;

if (options.HasFlag(RegexOptions.Multiline))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is a conceptual switch over options

/// </remarks>
public class NpgsqlRegexTranslator : IMethodCallTranslator
{
private static readonly MethodInfo IsMatch =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we're starting to move away from this way of matching things in method/member translators; this lookup of MethodInfos via reflection causes them to always get preserved when using trimming, regardless of whether the user actually needs them or not (and therefore bloats the final binary). Instead, it's better to check the declaring type, and then simply check the method name as a string.

I'll rewrite to do this.


AssertSql(
"""
SELECT regexp_replace(c."CompanyName", '^A', 'B', 'p')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this should be right-aligned like all the other SQL baselines (generally please try following existing style practices in an existing codebase).

@roji roji enabled auto-merge (squash) December 15, 2024 09:53
@roji roji merged commit ca0ba38 into npgsql:main Dec 15, 2024
16 checks passed
@WhatzGames
Copy link
Contributor Author

Thanks for the good work here @WhatzGames. I'm submitting a review, but in the interest of time etc. have pushed a commit to do the suggested fixes/improvements. I'll go ahead and merge - but please take a look and tell me if you see anything problematic/improvable, we can always do more work here.

Took the time, but as far as I can tell nothing seems off. Thanks for the review and final changes.

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.

Support for translating Regex.Replace and Regex.Count

2 participants