You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "paulirwin (via GitHub)" <gi...@apache.org> on 2024/12/04 05:08:03 UTC
[PR] Add operator overrides for public IComparable types, #683 [lucenenet]
paulirwin opened a new pull request, #1056:
URL: https://github.com/apache/lucenenet/pull/1056
- [X] You've read the [Contributor Guide](https://github.com/apache/lucenenet/blob/main/CONTRIBUTING.md) and [Code of Conduct](https://www.apache.org/foundation/policies/conduct.html).
- [ ] You've included unit or integration tests for your change, where applicable.
- [ ] You've included inline docs for your change, where applicable.
- [X] There's an open issue for the PR that you are making. If you'd like to propose a change, please [open an issue](https://github.com/apache/lucenenet/issues/new/choose) to discuss the change or find an existing issue.
Add operator overrides for public IComparable types
Fixes #683
## Description
This is a draft PR, because I'm currently unsure about whether to override Equals and GetHashCode (and if so, how exactly to do that, i.e. what to compare) for QualityQuery. @NightOwl888 - please let me know your thoughts.
This is to resolve the Sonar warning about needing to override the comparison operators for any types that implement IComparable. We are only doing this here for the public types.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884101033
##########
src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs:
##########
@@ -470,6 +470,31 @@ public override bool Equals(object obj)
return true;
}
+ #region Operator overrides
+ #nullable enable
Review Comment:
Should I do that for the `#region` as well, then? It seems like it would look weird if the region is indented and the nullable directive is not. We haven't been consistent, and I'd say I prefer the look of both indented.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "eladmarg (via GitHub)" <gi...@apache.org>.
eladmarg commented on PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#issuecomment-2541718537
there is a big performance penalty for using this inside try/catch block.
its much better to use this with TryParse and avoid this block for so hot-path function.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#issuecomment-2541799469
> there is a big performance penalty for using this inside try/catch block. its much better to use this with TryParse and avoid this block for so hot-path function.
>
> ok, i see it fixed. great
Sorry, what are you referring to?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1882041412
##########
src/Lucene.Net.Benchmark/Quality/QualityQuery.cs:
##########
@@ -82,22 +82,59 @@ public virtual string GetValue(string name)
/// For a nicer sort of input queries before running them.
/// Try first as ints, fall back to string if not int.
/// </summary>
- /// <param name="other"></param>
- /// <returns></returns>
- public virtual int CompareTo(QualityQuery other)
+ /// <param name="other">The other <see cref="QualityQuery"/> to compare to.</param>
+ /// <returns>0 if equal, a negative value if smaller, a positive value if larger.</returns>
+ public virtual int CompareTo(QualityQuery? other)
{
- try
+ if (other is null)
{
- // compare as ints when ids ints
- int n = int.Parse(queryID, CultureInfo.InvariantCulture);
- int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture);
- return n - nOther;
+ return 1;
}
- catch (Exception e) when (e.IsNumberFormatException())
+
+ if (int.TryParse(queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int n)
+ && int.TryParse(other.queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int nOther))
{
- // fall back to string comparison
- return queryID.CompareToOrdinal(other.queryID);
+ return n - nOther;
}
+
+ // fall back to string comparison
+ return queryID.CompareToOrdinal(other.queryID);
+ }
+
+ // LUCENENET specific - provide Equals and GetHashCode due to providing operator overrides
+ protected bool Equals(QualityQuery? other) => queryID == other?.queryID;
Review Comment:
Being that `IEquatable<T>` is not being implemented (and we don't need to because this is a reference type), this extra overload doesn't seem necessary. Please move this into the `public override bool Equals(object? obj)` method and remove this one.
##########
src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs:
##########
@@ -28,8 +28,8 @@ namespace Lucene.Net.QueryParsers.Surround.Query
public abstract class SimpleTerm : SrndQuery, IDistanceSubQuery, IComparable<SimpleTerm>
Review Comment:
Being that `CompareTo()` is deperecated in this class, let's suppress the warnings for overriding `Equals(object)` and `GetHashCode()`.
##########
src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs:
##########
@@ -67,6 +67,29 @@ public int CompareTo(Completion o)
{
return this.Utf8.CompareTo(o.Utf8);
Review Comment:
Please override `Equals(object)` and `GetHashCode()` to suppress the warnings.
I think the most logical thing to do here is to cascade the call to the base class and continue using reference equality.
> We have introduced bugs in the past by changing from reference equality to equality based on fields (even though the `GetHashCode()` method was based on fields), so we would need to test it thoroughly if we change it.
##########
src/Lucene.Net.Suggest/Suggest/Lookup.cs:
##########
@@ -122,14 +122,37 @@ public int CompareTo(LookupResult o)
{
return CHARSEQUENCE_COMPARER.Compare(Key, o.Key);
Review Comment:
Please override `Equals(object)` and `GetHashCode()` to suppress the warnings.
I think the most logical thing to do here is to continue using reference equality by cascading the calls to the base class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884213581
##########
src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs:
##########
@@ -470,6 +470,31 @@ public override bool Equals(object obj)
return true;
}
+ #region Operator overrides
+ #nullable enable
Review Comment:
I think this looks rather silly, but updated 😄
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin merged PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1882955002
##########
src/Lucene.Net.Benchmark/Quality/QualityQuery.cs:
##########
@@ -82,22 +82,59 @@ public virtual string GetValue(string name)
/// For a nicer sort of input queries before running them.
/// Try first as ints, fall back to string if not int.
/// </summary>
- /// <param name="other"></param>
- /// <returns></returns>
- public virtual int CompareTo(QualityQuery other)
+ /// <param name="other">The other <see cref="QualityQuery"/> to compare to.</param>
+ /// <returns>0 if equal, a negative value if smaller, a positive value if larger.</returns>
+ public virtual int CompareTo(QualityQuery? other)
{
- try
+ if (other is null)
{
- // compare as ints when ids ints
- int n = int.Parse(queryID, CultureInfo.InvariantCulture);
- int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture);
- return n - nOther;
+ return 1;
}
- catch (Exception e) when (e.IsNumberFormatException())
+
+ if (int.TryParse(queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int n)
+ && int.TryParse(other.queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int nOther))
{
- // fall back to string comparison
- return queryID.CompareToOrdinal(other.queryID);
+ return n - nOther;
}
+
+ // fall back to string comparison
+ return queryID.CompareToOrdinal(other.queryID);
+ }
+
+ // LUCENENET specific - provide Equals and GetHashCode due to providing operator overrides
+ protected bool Equals(QualityQuery? other) => queryID == other?.queryID;
Review Comment:
That is how Rider generates it, so that subclasses (or any other private callers) don't have to do all the ReferenceEquals/GetType/etc. dance in the `object` version, but I'll remove it for now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1883183885
##########
src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs:
##########
@@ -67,6 +67,42 @@ public int CompareTo(Completion o)
{
return this.Utf8.CompareTo(o.Utf8);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
+
+ public override int GetHashCode()
+ {
+ unchecked
Review Comment:
Please cascade the call to the base class.
```c#
public override int GetHashCode() => base.GetHashCode();
```
##########
src/Lucene.Net.Suggest/Suggest/Lookup.cs:
##########
@@ -122,14 +122,53 @@ public int CompareTo(LookupResult o)
{
return CHARSEQUENCE_COMPARER.Compare(Key, o.Key);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
Review Comment:
Please cascade the call to the base class.
```c#
public override bool Equals(object obj) => base.Equals(obj);
```
##########
src/Lucene.Net/Util/Automaton/State.cs:
##########
@@ -360,17 +360,44 @@ public virtual int CompareTo(State s)
return s.id - id;
}
- // LUCENENET NOTE: DO NOT IMPLEMENT Equals()!!!
+ // LUCENENET NOTE: DO NOT IMPLEMENT Equals() with structural equality!!!
// Although it doesn't match GetHashCode(), checking for
// reference equality is by design.
// Implementing Equals() causes difficult to diagnose
// IndexOutOfRangeExceptions when using FuzzyTermsEnum.
// See GH-296.
+ // Overriding here to prevent CS0660 warning due to defining the == operator.
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
Review Comment:
Please cascade the call to the base class.
```c#
public override bool Equals(object obj) => base.Equals(obj);
```
##########
src/Lucene.Net.Suggest/Suggest/Lookup.cs:
##########
@@ -122,14 +122,53 @@ public int CompareTo(LookupResult o)
{
return CHARSEQUENCE_COMPARER.Compare(Key, o.Key);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
+
+ public override int GetHashCode()
Review Comment:
Please cascade the call to the base class.
```c#
public override int GetHashCode() => base.GetHashCode();
```
##########
src/Lucene.Net.QueryParser/Surround/Query/SimpleTerm.cs:
##########
@@ -2,6 +2,7 @@
using Lucene.Net.Index;
using System;
using System.Text;
+#pragma warning disable CS0660, CS0661 - CompareTo is deprecated, so skipping implementing equality members (lucenenet#683)
Review Comment:
Please fix the inline comment by preceeding with `//`.
##########
src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs:
##########
@@ -67,6 +67,42 @@ public int CompareTo(Completion o)
{
return this.Utf8.CompareTo(o.Utf8);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
Review Comment:
Please cascade the call to the base class.
```c#
public override bool Equals(object obj) => base.Equals(obj);
```
##########
src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs:
##########
@@ -470,6 +470,31 @@ public override bool Equals(object obj)
return true;
}
+ #region Operator overrides
+ #nullable enable
Review Comment:
By default, Visual Studio left aligns `#nullable enable` and `#nullable restore`, so please do that in each case in this PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#issuecomment-2518035078
> This is a draft PR, because I'm currently unsure about whether to override Equals and GetHashCode (and if so, how exactly to do that, i.e. what to compare) for QualityQuery. @NightOwl888 - please let me know your thoughts.
>
Well, being that they didn't override the default behavior (which is reference equality), a direct port would be to override each and then cascade the call to the base class.
--------------------
But given the fact that the comparer uses `queryID` and it says "ID of this quality query" for a description, it would probably be best to use that to compare for equality/hash code. It would keep the equality and compare checks exactly in sync.
BTW - I noticed that the `QualityQuery.CompareTo()` implementation has an exception handler that we can remove because we can use `int.TryParse()` and fallback to string comparison if parsing either side returns `false`.
```c#
public virtual int CompareTo(QualityQuery other)
{
try
{
// compare as ints when ids ints
int n = int.Parse(queryID, CultureInfo.InvariantCulture);
int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture);
return n - nOther;
}
catch (Exception e) when (e.IsNumberFormatException())
{
// fall back to string comparison
return queryID.CompareToOrdinal(other.queryID);
}
}
```
The constructor could also be improved by using guard clauses on `queryID` and `nameValPairs` since there will be NREs if either is passed `null`. If we do so, we don't need to change `CompareTo()` to account for the fact that `queryID` may be `null` (which it currently doesn't handle).
`#nullable enable` would catch this sort of thing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884098808
##########
src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs:
##########
@@ -67,6 +67,42 @@ public int CompareTo(Completion o)
{
return this.Utf8.CompareTo(o.Utf8);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
+
+ public override int GetHashCode()
+ {
+ unchecked
Review Comment:
Rider was giving a code analysis warning of having an override that just calls to the base class. If you prefer it that way, I'll suppress that warning for now. I figured it would be better to be explicit about the expectation of the behavior of Equals.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884216430
##########
src/Lucene.Net.Suggest/Suggest/Lookup.cs:
##########
@@ -124,20 +124,13 @@ public int CompareTo(LookupResult o)
}
// LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
- public override bool Equals(object obj) => ReferenceEquals(this, obj);
+ // ReSharper disable once BaseObjectEqualsIsObjectEquals
+ // ReSharper disable once RedundantOverriddenMember
+ public override bool Equals(object obj) => base.Equals(obj);
- public override int GetHashCode()
- {
- unchecked
Review Comment:
Created #1065
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#issuecomment-2517854641
This change caught a NRE bug in MutableValue 😄 Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884154424
##########
src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs:
##########
@@ -470,6 +470,31 @@ public override bool Equals(object obj)
return true;
}
+ #region Operator overrides
+ #nullable enable
Review Comment:
Actually, it is sensible to have `#region` indented because that is the way Visual Studio formats it.
`#nullable` acts like `#pragma` in that they don't need to be paired and don't need to respect other code elements such as curly brackets. These are both compiler directives. `#region` is something that only an IDE understands and must be paired with `#endregion`.
Hopefully, we will eventually move the `#nullable` statements up to the top of the file or to an MSBuild file. It is helpful to have them left aligned to make them stand apart because eventually they may be removed, but we don't want to remove the `#region` when that happens.
##########
src/Lucene.Net.Suggest/Suggest/Lookup.cs:
##########
@@ -124,20 +124,13 @@ public int CompareTo(LookupResult o)
}
// LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
- public override bool Equals(object obj) => ReferenceEquals(this, obj);
+ // ReSharper disable once BaseObjectEqualsIsObjectEquals
+ // ReSharper disable once RedundantOverriddenMember
+ public override bool Equals(object obj) => base.Equals(obj);
- public override int GetHashCode()
- {
- unchecked
Review Comment:
Let's open a new issue to review and use `unchecked` on each `GetHashCode()` method where it would make a difference.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add operator overrides for public IComparable types, #683 [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1056:
URL: https://github.com/apache/lucenenet/pull/1056#discussion_r1884098808
##########
src/Lucene.Net.Suggest/Suggest/Fst/FSTCompletion.cs:
##########
@@ -67,6 +67,42 @@ public int CompareTo(Completion o)
{
return this.Utf8.CompareTo(o.Utf8);
}
+
+ // LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode
+ public override bool Equals(object obj) => ReferenceEquals(this, obj);
+
+ public override int GetHashCode()
+ {
+ unchecked
Review Comment:
Rider was giving a code analysis warning of having an override that just calls to the base class. If you prefer it that way, I'll suppress that warning for now. I figured it would be better to be explicit about the expectation of the behavior of Equals and GetHashCode.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org