Skip to content

Added support for writing nullable values - #1993

Closed
YohDeadfall wants to merge 1 commit into
npgsql:devfrom
YohDeadfall:NullableScalars
Closed

YohDeadfall wants to merge 1 commit into
npgsql:devfrom
YohDeadfall:NullableScalars

Conversation

@YohDeadfall

Copy link
Copy Markdown
Contributor

Fixes #1965

@YohDeadfall YohDeadfall added this to the 4.0.1 milestone Jun 12, 2018
@YohDeadfall
YohDeadfall requested a review from roji June 12, 2018 00:49
@YohDeadfall

Copy link
Copy Markdown
Contributor Author

@roji rerun Travis-CI, please.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @YohDeadfall, it's nice to see this.

I wrote some comments below, but here's a general comment - I think I already wrote this when you were implementing read support. Your current implementation checks up-front whether a type is nullable (NullableHandler<T>.Exists), and then dispatches either to NullableHandler<TAny>.WriteAsync or to the handler's async (same for validation). I'd prefer it if we moved nullable handling to after we tried to write in the normal way. If you look at the current WriteWithLength<TAny>(), we could change the code to the following:

if (this is INpgsqlTypeHandler<TAny> typedHandler)
{
    buf.WriteInt32(typedHandler.ValidateAndGetLength(value, ref lengthCache, parameter));
    return typedHandler.Write(value, buf, lengthCache, parameter, async);                
}
else if (NullableHandler<T>.Exists)
{
    // Write it as nullable
} else
    throw new InvalidCastException(...);

The advantage of this approach is the it eliminates the upfront conditional check on each and write (actually twice - once for validate, once for write). It also makes the code clearer by removing the need for the extra *Entry methods which basically dispatch between nullable/non-nullable.

The only disadvantage I can see is that WriteWithLength<TAny>() is overridden in some very special cases, and so the logic would have to be duplicated there. But that seems pretty minor to me.

What do you think?


namespace Npgsql.TypeHandling
{
internal delegate T ReadDelegate<T>(NpgsqlReadBuffer buffer, int columnLength, FieldDescription fieldDescription);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: remove internal here and below, it's the default for types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I copied it as is and forgot about the modifier. Will do.

internal delegate int ValidateDelegate<T>(NpgsqlTypeHandler handler, T value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter);
internal delegate Task WriteAsyncDelegate<T>(NpgsqlTypeHandler handler, T value, NpgsqlWriteBuffer buffer, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async);

internal static class NullableHandler<T>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: please add the following line at the start of the file to silence the Resharper warning:

// ReSharper disable StaticMemberInGenericType

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do.


internal static class NullableHandler<T>
{
public static readonly Type UnderlyingType;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about making this into an UnwrapNullableType extension method on type, like EF Core does (only faster)? At the moment every access to NullableHandler.UnderlyingType seems to have ?? type right after it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.

/// <returns>The fully-read value.</returns>
internal abstract TAny Read<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null);

internal TAny ReadEntry<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new Entry suffix isn't very clear naming-wise - what it really seems to mean is "read this thing which may be a nullable". Maybe just ReadNullable or ReadWrapper?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I know, but the best thing came to my mind was an entry point. Will do.

internal abstract Task WriteWithLengthInternal<TAny>([CanBeNull] TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async);
protected internal abstract Task WriteWithLength<TAny>([CanBeNull] TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async);

internal Task WriteWithLengthEntry<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's nice that you lifted this from NpgsqlTypeHandler<T> to NpgsqlTypeHandler.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Will think about it and test, but two weeks later. I'm on vacation far away from the PC (:

@roji

roji commented Jun 14, 2018

Copy link
Copy Markdown
Member

No problem, this PR can definitely wait. The idea is simply to make this 100% pay-per-play.

@wiktor-golonka

wiktor-golonka commented Jun 14, 2018 •

Copy link
Copy Markdown

;(
If that's fine with both of you I may pick this up and work this out during this or next week. Actually it's pretty critical for me so I will be happy to involve.

@roji

roji commented Jun 14, 2018

Copy link
Copy Markdown
Member

@wiktor-golonka even if this PR were merged today, it would only be released for the next minor version (4.1) which is months away. In the meantime you're welcome to use the nugets from the unstable feed if you're OK with running on prerelease. Even if this PR is modified in the way I suggest, it's a purely internal implementation detail which shouldn't affect the API in any way.

Regardless, it may be better to wait for @YohDeadfall on this one, it modifies some fairly core parts of Npgsql and if you're not familiar with the Npgsql codebase it's not the ideal place to start.

@wiktor-golonka

wiktor-golonka commented Jun 14, 2018 •

Copy link
Copy Markdown

@roji: I really, really hope we can get it working for 4.0.1. I am happy to spend more time on this if needed to better understand the core code.

@roji

roji commented Jun 14, 2018

Copy link
Copy Markdown
Member

@wiktor-golonka it's not really a matter of getting it working for 4.0.1 - this change is a non-trivial feature, and patch versions such as 4.0.1 in general are really only about bug fixes, not adding new features. This is how we generally version Npgsql, following the spirit of semver (even if we're not strict at all). If we start adding features in 4.0.1, well, the version doesn't really mean anything anymore...

Of course this doesn't stop you from running with your patched version of Npgsql until 4.1 is released.

Finally, it's a bit strange for me that this feature would be so critical - it just allows you to write nullables. You can just as easily do a small and simple wrapper today that accepts the nullable and either sets the parameter value to DBNull.Value, or to the non-null value. So this really shouldn't be that critical.

@roji

roji commented Jun 14, 2018

Copy link
Copy Markdown
Member

To be clear, what I'm trying to say is that strictly speaking this is really a convenience feature - there is nothing added by this feature that cannot be done with a little more code outside of Npgsql.

@wiktor-golonka

wiktor-golonka commented Jun 15, 2018 •

Copy link
Copy Markdown

@roji: Right, I agree, it can be worked around. I said critical in the meaning of reliability as my only concern is if that's enough from the core code point of view.
But I guess I will just do more tests.
Thanks!

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

The advantage of this approach is the it eliminates the upfront conditional check on each and write (actually twice - once for validate, once for write). It also makes the code clearer by removing the need for the extra *Entry methods which basically dispatch between nullable/non-nullable.

There are no checks at all. The JIT elides them because it treats static readonly fields as constants. But I'm not happy with entry methods too and very long dispatches even without *Entrys. Currently value handling takes a lot of time, much more than just reading or writing it from/to buffer. So I will work on it.

@roji
roji force-pushed the dev branch 2 times, most recently from fe46200 to 1f156c8 Compare July 20, 2018 12:57
@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Closing it due to a lot of merge conflicts. Later I will open a new PR to continue the work.

@YohDeadfall YohDeadfall closed this Aug 9, 2018
@YohDeadfall
YohDeadfall deleted the NullableScalars branch September 17, 2018 20:41
@YohDeadfall YohDeadfall removed this from the Backlog milestone Oct 13, 2018
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.

3 participants