Skip to content

Support for nullable arrays #443

Description

@roji

As discussed in #442. This includes both scalars and arrays.

For value-type scalars, it should be possible to call GetFieldValue<int?>() and receive the proper result.

PostgreSQL arrays are always nullable. By default return an int?[], but support GetFieldValue<int[]>() as well. In the latter case throw an exception if a null value is encountered.

Final behavior is spec'ed in: #443 (comment)

This issue only tracks support for nullable arrays, see #1728.

Activity

  1. self-assigned this
    on Dec 29, 2014
  2. added this to the milestone on Dec 29, 2014
  3. modified the milestones: , on Mar 26, 2015
  4. modified the milestones: 3.2, on Apr 20, 2016
  5. modified the milestone: 3.2 on Sep 24, 2016
  6. MihaMarkic commented on Jan 10, 2017

    @MihaMarkic

    I'm really surprised nobody needed this. Specially the arrays where you can't workaround.

  7. roji commented on Jan 10, 2017

    @roji
    MemberAuthor

    You're right that arrays are a good use case - with regular values you can always work around this by checking with IsDBNull.

    The beta for Npgsql 3.2 is about to be released, and unfortunately it's too late to include nullable support. I'll definitely try to work on this for 3.3.

    Note that the current read behavior is to populate the array with the default value when a null is read. In other words, an int array with nulls will get zeros. If your data happens not to have zeros in it, you can use that to detect nulls - otherwise you're out of luck...

  8. added this to the milestone on Jan 10, 2017
  9. MihaMarkic commented on Jan 10, 2017

    @MihaMarkic

    Yep. 0 for int and DateTime.MinValue for timestamps. While are we at DateTime arrays, I think that time zone (timestamp with timezone) isn't working properly with arrays. From a brief test it just stores the DateTime as it was UTC.

  10. roji commented on Jan 11, 2017

    @roji
    MemberAuthor

    @MihaMarkic regarding timestamptz, you should probably read http://www.npgsql.org/doc/types/datetime.html, especially the warning. PostgreSQL timestamptz doesn't store the time zone (very frequent source of confusion), it simply assumes that the timestamp is in UTC.

  11. MihaMarkic commented on Jan 11, 2017

    @MihaMarkic

    @roji I get that but array timestamptz acts differently. It doesn't convert local DateTime to UTC before sending the value, I think. I didn't experiment a lot though.

  12. 90 remaining items

  13. Brar commented on Nov 11, 2020

    @Brar
    Member

    I like the idea of this change but I think the implementation of NpgsqlDataReader.GetValues should be updated so that it uses GetFieldValue()

    Can you elaborate?
    What would T be in that case?
    Or do you mean adding a non-standard GetFieldValues<T>()?

  14. calexander3 commented on Nov 11, 2020

    @calexander3
    Contributor

    Can you elaborate?
    What would T be in that case?
    Or do you mean adding a non-standard GetFieldValues()?

    Yeah I guess it would have to be something like

            public override int GetValues(object[] values)
            {
                if (values == null)
                    throw new ArgumentNullException(nameof(values));
                CheckResultSet();
    
                var count = Math.Min(FieldCount, values.Length);
                for (var i = 0; i < count; i++)
                    var type = GetFieldType(i)
                    values[i] = GetFieldValue(i, type);
                return count;
            }
    

    I think @YohDeadfall 's suggestion was better though.

  15. Brar commented on Nov 11, 2020

    @Brar
    Member

    The most preferrable way in my opinion is to return an array of the right type when GetValue is called.

    You mean to determine the right type at runtime and return int[] or int?[] depending on the question whether the array actually does contain null values or not?
    That was the very initial implementation and we discarded it because of the somewhat unpredictable/surprising runtime behavior.
    I'm on my phone but the discussion must be somewhere upthread.
    It really feels like we're circling here because different people/use cases seem to have different requirements/expectations.

  16. YohDeadfall commented on Nov 11, 2020

    @YohDeadfall
    Contributor

    Unpredictable and breaks the current behavior, but the user haven't specified the required type. Therefore, it's fine to determine the right one. Anyway, we won't be able to finish it for 5.0. To late for such changes.

  17. Brar commented on Nov 11, 2020

    @Brar
    Member

    Anyway, we won't be able to finish it for 5.0. To late for such changes.

    Since all those variants have already been implemented and I already know how where to do that I wouldn't necessarily say so.
    The major obstacle seems do be to agree on what's the correct implementation here.

  18. YohDeadfall commented on Nov 11, 2020

    @YohDeadfall
    Contributor

    Previously we broke a lot of code having good intentions and reverted changes back. Right now while the release is around the corner changing something isn't a good idea, but what we can do is to keep GetValue behavior untouched. Can make a fix in a few hours.

  19. roji commented on Nov 11, 2020

    @roji
    MemberAuthor

    Here's my view...

    First of all, assuming we leave things as they are, it's pretty trivial to work around this by writing a MyGetValues extension method which checks the type and calls GetFieldValue<T> as appropriate.

    Don't like connection string parameters especially for type handling

    I tend to agree with @YohDeadfall here; it's true that we recently added CancellationTimeout, but that's a very different case. I do think we could consider a connection string parameter, but I also think we should wait for more user feedback before doing so - especially as an easy workaround is available (via the extension method).

    The most preferrable way in my opinion is to return an array of the right type when GetValue is called.

    OTOH there are currently no cases where Npgsql returns different CLR types based on column contents - and I think it's a very good idea to leave it like this. Think about how this API will actually get used by users: it may return only int[] because there happen to not be nulls in the database on tests data, and suddenly things blow up because the API starts returning int?[] out of the blue. With GetValue, the user doesn't specify a requested type, but that doesn't mean that the type should be variable/random.

    Finally, GetValues specifically is a bad API, since it needlessly allocates an array internally for each and every row. It's better to manage that array yourself (recycling the same instance) and to read using GetFieldValue instead of GetValue whenever possible.

    To summarize, this indeed seems to be a case where it's hard to satisfy everyone. We can always introduce a connection string parameter a bit later, but I wouldn't jump and do it too quickly - this may be just a matter of guiding users to use GetFieldValue<T>, and they may be happy with that.

  20. calexander3 commented on Nov 11, 2020

    @calexander3
    Contributor

    First of all, assuming we leave things as they are, it's pretty trivial to work around this by writing a MyGetValues extension method which checks the type and calls GetFieldValue as appropriate.

    Our product can connect to anyone's database so any type is possible. If I understand this correctly, I would need a case statement for every possible postgres type? Also to give more context on why we are using IDataReader methods, we are using the IDataReader as an abstraction so we can use the same code on multiple database types.

  21. YohDeadfall commented on Nov 11, 2020

    @YohDeadfall
    Contributor

    Interfaces in ADO.NET is a dead abstraction (the first version), while abstract classes is the second generation and they are more preferrable. All providers inherit from classes.

  22. Brar commented on Nov 11, 2020

    @Brar
    Member

    I'd say the scenario I predicted in #443 (comment) is manifesting here.
    There are actual users out there that use the abstractions and as long as we implement them (why do we implement the interfaces and derive from the base classes after all?) we shouldn't tell them not to do so.

    I understand that what I described in #443 (comment) would work for @calexander3 and I understand why he doesn't like it.

    We jumped in one direction pretty quickly when we got a user request and now we don't want to jump too quickly as that's causing problems.

    Personally I'm not very happy with our decisions here but I'm also tired and will see what a good sleep will bring 😴

  23. roji commented on Nov 11, 2020

    @roji
    MemberAuthor

    @Brar I agree with everything you're saying, but what are our options here? To recapitulate:

    • Make int?[] the default again. This would work for any database value, but would break all existing code, and is probably not what most users want (as in Support for nullable arrays #443 (comment)).
    • Return int[] or int?[] based on the database value - I wrote above why I don't think this is a good idea.
    • Add a connection string parameter to make Npgsql always return int?[]. I think that doesn't address @calexander3 needs very well; I'm guessing that connection strings are controlled by their users, and not by their own application (that's how it usually is), so they'd need some sort of guidance for every user to add this flag.
    • Something programmatic which does the same. That's better because the product can do it without the user's intervention, but they'd have to use an Npgsql-specific API (the connection string option doesn't have this problem)
    • Bring back null => 0 behavior. I don't think anybody wants that.

    I'm not happy with the situation, but I'm not sure our decisions were bad or that we jumped too quickly...

    FWIW there are other cases of database values which Npgsql doesn't support out of the box via GetValue - date/time/decimal values which are out of range are an example (and GetFieldValue with a provider-specific type can workaround it, as is being proposed here). But I agree that nulls in the array is a more severe case of an out-of-range date/time value...

    I admit that I'm also not sure what's the best way forward here. If the connection string (to make us return int?[] by default) really solves things for everyone, I guess I'll be fine with adding it - I'd just rather wait and hear from a few more users on their problem and expectations.

  24. calexander3 commented on Nov 23, 2020

    @calexander3
    Contributor

    Hey All,
    Any more thoughts on this issue? I'm personally in favor of have a Npgsql-specific API to configure the behavior.

  25. ABildstein commented on Dec 2, 2020

    @ABildstein

    I'm hitting a problem with the GetValue behavior with Dapper. It seems that Dapper is using GetValue internally and I don't think I can override that behavior. Adding some option to Npgsql to always return int?[] would fix this and with a custom Dapper type map everyone could implement their wanted behavior for non-nullable int[] properties (which may involve copying the array, but at least it'd work).

  26. roji commented on Dec 2, 2020

    @roji
    MemberAuthor

    @JBildstein @calexander3 it does seem that users are actually hitting this, and that we need to provide some solution.

    I've opened #3386 to track this there - anyone who's interested, please go upvote that.

  27. marked Is bool?[] not supported? #3203 as a duplicate of this issue on Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions