Repository navigation
Read and Write arrays of nullable value types - #1996
Conversation
83b6224 to
8eb5eeb
Compare
|
Hey @Brar, thanks for the PR. I'll give this a proper review, but right off the bat:
If I understand this correctly, this seems problematic to me. I don't think that the type of the value returned should depend on its contents in this way: it shifts a lot of responsibility on the user and will make it very easy to write buggy code. In effect, it means that whenever a row is read, it's necessary to to check what type was returned. IIRC it would also be the only case where CLR types read change across rows. Let's revisit our conversation on this. Here's how I see things
What do you think? @YohDeadfall you may be interested as well. |
I certainly considered this when thinking about the new interface. My arguments are the following: Basically an API is about promises guaranteed by the framework.
This also solves an important problem in our previous discussion: Finally: |
|
I agree with the above, but returning a different type for different rows of the same database type seems to go too far.
It's true that we have this unverified intuition. However, I simply don't see the downside of returning Also, I very much agree with what you wrote above, that with To summarize, I can see many more downsides to switching returned data types for different rows of the same resultset, than I can see for simply always returning |
I can agree with what you wrote @roji . I'll modify the code accordingly. |
a4989c3 to
3bec9ad
Compare
|
@roji the latest commit changes the implementation according to your comments above (I've squashed the others but left this one to make the changes transparent). |
|
Wow! |
|
Thanks @Brar, drop me a line when you want this reviewed. |
|
... but the fixes are quite easy. @roji ready to review if you want. |
|
One ugly part that we didn't talk about yet is the fact that |
You're absolutely right. I think it makes it even more important to return a single, clear type and not alternate between
Good idea. I opened https://github.com/dotnet/corefx/issues/30426 for this, but we can also implement it as an Npgsql-specific feature in the meantime. Feel free to submit a PR on this if you want. |
|
@roji after working around all the issues I mentioned at https://github.com/dotnet/corefx/issues/30426 the code would look like this: public bool TryExecuteScalar<T>(out T result)
{
var (hasResult, res) = TryExecuteScalar<T>(false, CancellationToken.None).GetAwaiter().GetResult();
result = res;
return hasResult;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ItemCanBeNull]
async ValueTask<(bool hasResult, T result)> TryExecuteScalar<T>(bool async, CancellationToken cancellationToken)
{
var behavior = CommandBehavior.SingleRow;
if (!Parameters.HasOutputParameters)
behavior |= CommandBehavior.SequentialAccess;
using (var reader = await ExecuteDbDataReader(behavior, async, cancellationToken))
{
return reader.Read() && reader.FieldCount != 0 ? (true, reader.GetFieldValue<T>(0)) : (false, default);
}
}To be honest this doesn't look like an API to me. |
|
I'll try to get around to this as soon as I can, although I have some very intensive non-Npgsql days coming up. I'll definitely get there eventually! |
d579cdc to
05aefc1
Compare
05aefc1 to
a7d33c0
Compare
|
Rebased to avoid code drift. |
fe46200 to
1f156c8
Compare
a7d33c0 to
af4f219
Compare
af4f219 to
adea324
Compare
6045d1d to
b6899c3
Compare
|
After a lot of discussions, reviews and rewriting I now consider this ready for a serious final review. As @roji already knows most of this, I explicitly request a review from @austindrenski and @YohDeadfall in order to get another couple of eyes to look at it and probably give some completely new feedback. Below you can find another two benchmark runs. Click to see the benchmark results
Interpretation hints: We have four methods
When BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
AMD Ryzen 5 2600X, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.0.100
[Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
|
8474354 to
26d05d8
Compare
|
Thank you for the review @YohDeadfall. |
38b06e5 to
43919c0
Compare
This closes npgsql#443 and prepares the ground for npgsql/efcore.pg#1025
* Use compiled expression trees to avoid boxing * Also move code to the non-generic base class 'ArrayHandler'
43919c0 to
847ab11
Compare
847ab11 to
ea0a5a2
Compare
|
Thank you @YohDeadfall @austindrenski and @roji, @YohDeadfall and I have completed the review process. Now it's on you. |
|
Great work @Brar, and thanks @YohDeadfall for the deep review and helpful comments. Very happy this is finally merged :) |
|
Hi npgsql team! Would you be able to kick off a build of the 5.0.0 unstable branch that includes this merge now that it's merged? Would love to try it out. Thanks! |
|
Hmm, looks like our appveyor build has some issue and doesn't upload unstable packages anymore... Rather than spending time to debug this, hopefully we'll merge #2717 soon (switch to Github Actions). @christophertrml for now you can clone and build Npgsql (shouldn't be hard). |
I couldn't resist investigating this... See #2722 |
After deciding that my extended testing project should not hold this one up forever, I'm finally submitting this one that fixes #443
I've explicitly broken backwards compatibility for
NpgsqlDataReader.GetFieldValue<T>()where reading an array containing null values resulted in an array where the null values were replaced with the value types default value.Another thing to note is that
NpgsqlDataReader.GetValue()now returns an array of the value type or an array of its nullable counterpart on a row to row base, depending on the question whether the actual array returned by the backend contains null values or not.