Skip to content

Fix {Starts,Ends}With and support citext properly - #1147

Merged
roji merged 2 commits into
devfrom
StringStuff
Dec 2, 2019
Merged

roji merged 2 commits into
devfrom
StringStuff

Conversation

@roji

@roji roji commented Dec 1, 2019 •

Copy link
Copy Markdown
Member

Fixes #1146
Fixes #388
Fixes #996

@roji
roji force-pushed the StringStuff branch 2 times, most recently from e0dd19e to 02318f6 Compare December 1, 2019 19:53
* Fix broken {Starts,Ends}With
* Support citext properly
* Contains succeeds for empty pattern

Fixes #1146
Fixes #388
Fixes #996
typeof(int)),
_sqlExpressionFactory.Constant(0));

if (pattern is SqlConstantExpression constantPattern)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Check for a constant pattern earlier and do not allocate an STRPOS expression.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unless I'm missing something that would require checking for constant twice, or duplicating the code for constructing the STRPOS expression; and the only advantage would be to save a single expression construction during compilation when the pattern is an empty string (an edge case if there ever was one...). So I think it's better to keep the code simple as it is...

: strposCheck;
}

return _sqlExpressionFactory.OrElse(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And check the pattern for null here:

var pattern = arguments[0];
var constantPattern = pattern as SqlConstantExpression;
if (constantPattern != null && (string)constantPattern.Value == string.Empty)
    return _sqlExpressionFactory.Constant(true);

// ...

return constantPattern is null
    ? _sqlExpressionFactory.OrElse(
        _sqlExpressionFactory.Equal(
            pattern,
            _sqlExpressionFactory.Constant(string.Empty, stringTypeMapping)),
        strposCheck)
    : strposCheck;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, this checks constantPattern for null twice... I think it's cleaner/more understandable as-is, where you have one block handling the constant case and another the non-constance.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Without double check this introduces a few more useless allocations, while a null check is very cheap action and it won't hurt performance. If you think that constant patterns isn't a common case, feel free to leave it as is.

: _sqlExpressionFactory.Convert(
pattern,
typeof(string),
pattern.TypeMapping));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These two parameters could be generated via two nested methods, and you won't need to duplicate the code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it really worth it? I like nested methods for more complicated cases, but here it seems more readable to just keep the code inline, so that everything is clear when reading and you don't have to jump to another function...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's worth at least for conversion to citext. It minimizes a chance to make a bug or a fix in the code and not to affect another place.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This isn't just about conversion to citext, it could be another text type as well (e.g. json). Can you post a sample of exactly what you mean?

IMHO we shouldn't always, everywhere extract duplicated code into methods - methods make it more difficult to follow what's happening (you have to jump around). It's always a case-by-case decision based on how much is being duplicated, etc. etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

// Could be used for RIGHT and LEFT
return _sqlExpressionFactory.Equal(
    GetInstanceMapping(rightPart),
    GetPatternMapping());

SqlExpression GetInstanceMapping(SqlExpression value)
    => instance.TypeMapping == _textTypeMapping
        ? value
        : _sqlExpressionFactory.Convert(value, typeof(string), _textTypeMapping);

// The same method for pattern or make a common method until the bug isn't fixed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, I ended up refactoring in a slightly different way but I think you'll like it - can you take another look?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now it looks much better. Let's merge (:

@chrisckc

chrisckc commented Dec 2, 2019

Copy link
Copy Markdown

@roji Thanks, looks good, tested StringStuff branch, StartsWith and EndsWith both now work and also work using the reverse matching.

@roji
roji merged commit 8364015 into dev Dec 2, 2019
@delete-merged-branch
delete-merged-branch Bot deleted the StringStuff branch December 2, 2019 15:50
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.

StartsWith and EndsWith are broken String.Contain without empty pattern should always succeed Properly support citext in all string translations

3 participants