ARROW-7514: [C#] Make GetValueOffset Obsolete#6333
ARROW-7514: [C#] Make GetValueOffset Obsolete#6333HashidaTKS wants to merge 3 commits intoapache:masterfrom
Conversation
Make BinaryArray.GetValueOffset obsolete
|
@eerhardt do you have time to review? |
kou
left a comment
There was a problem hiding this comment.
Could you update test code to suppress obsolete warnings?
eerhardt
left a comment
There was a problem hiding this comment.
Could you update test code to suppress obsolete warnings?
👍
| public ReadOnlySpan<byte> Values => ValueBuffer.Span.CastTo<byte>(); | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [Obsolete] |
There was a problem hiding this comment.
It's probably best to add a message to the [Obsolete] attribute telling users what they should use instead.
Respond to feedback Add a message to Obsolete attributes Avoid Obsolete warnings
| public ReadOnlySpan<byte> Values => ValueBuffer.Span.CastTo<byte>(); | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [Obsolete("This method has been deprecated. Please use ValueOffsets instead.")] |
There was a problem hiding this comment.
How about ValueOffsets[index] instead of ValueOffsets?
There was a problem hiding this comment.
Sounds good.
I fixed it.
| if (index < 0 || index >= Length) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(index)); | ||
| } |
There was a problem hiding this comment.
The check is duplicated of the check in GetValueLength.
How about this?
var offset = ValueOffsets[index];
var length = GetValueLength(index);
return ValueBuffer.Span.Slice(offset, length);There was a problem hiding this comment.
Initially, I implemented as below to avoid duplication of checks.
However, I thought the intention was a little difficult to understand, so it was implemented like current.
var length = GetValueLength(index);
return ValueBuffer.Span.Slice(ValueOffsets[index], length);Also, if we don't care about the type of exception, we can simply remove the check.
In that case, this method throws IndexOutOfRangeException which ValueOffsets[index] throws.
There was a problem hiding this comment.
I understand.
How about adding a new helper private method to validate index:
private void ValidateIndex(int index)
{
if (index < 0 || index >= Length)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
}and use it in GetValueLength and GetBytes?
ValidateIndex(index);
var offsets = ValueOffsets;
var offset = offsets[index];
var length = offsets[index + 1] - offset;
return ValueBuffer.Span.Slice(offset, length);@eerhardt What do you think about this case?
There was a problem hiding this comment.
Also, if we don't care about the type of exception, we can simply remove the check.
In that case, this method throws IndexOutOfRangeException which ValueOffsets[index] throws.
Typically we do care about the type of exception. Bubbling up an IndexOutOfRangeException looks like a bug in our library - similar to if you let something NullReferenceException. See the Design Guidelines for more info about this.
Instead, it is better to throw an ArgumentOutOfRangeException.
I think a helper like ValidateIndex makes the most sense. Also note that as currently written you are validating the index twice - once in GetBytes and then again when GetBytes calls GetValueLength. Not a huge issue, just something I noticed.
Change a message of Obsolete attributes
|
I'm going to merge this to move this PR forward. If we want to tweak the |
* Add an [Obsolete] attribute to `BinaryArray.GetValueOffset` * `ListArray.GetValueOffset` already has the [Obsolete] attribute, so it is not changed * Avoid using `GetValueOffset` in the product source code As a precaution, I added tests for `ValueOffsets` and left tests for `GetValueOffset`. Closes #6333 from HashidaTKS/ARROW-7514_make_getvalueoffset_obsolete and squashes the following commits: 1dbaf39 <Takashi Hashida> ARROW-7514_make_getvalueoffset_obsolete 92b14c0 <Takashi Hashida> ARROW-7514_make_getvalueoffset_obsolete 07d106c <Takashi Hashida> ARROW-7514_make_getvalueoffset_obsolete Authored-by: Takashi Hashida <[email protected]> Signed-off-by: Eric Erhardt <[email protected]>
BinaryArray.GetValueOffsetListArray.GetValueOffsetalready has the [Obsolete] attribute, so it is not changedGetValueOffsetin the product source codeAs a precaution, I added tests for
ValueOffsetsand left tests forGetValueOffset.