Skip to content

Arrays with lower bounds != 1 #442

Closed

Description

@Emill

Arrays in PostgreSQL are by default 1-indexed, while .NET's arrays are by default 0-indexed.
It is however possible to have other lower bounds than 1 in a PostgreSQL array.

It seems natural to translate 1-indexed PostgreSQL arrays to 0-indexed .NET arrays.
But what should we do with PostgreSQL arrays that have other lower bounds?

The method Array.CreateInstance: http://msdn.microsoft.com/en-us/library/x836773a(v=vs.110).aspx can create arrays with arbitrary dimensions and arbitrary lower bounds. However, when creating a 1-dimensional array with non-zero lower bound, the returned type is (for int) a "System.Int32[*]" and not a System.Int32[], which can't be declared in the C# language. So it seems a bit useless to try to support non-standard-lower-bound arrays. However, multidimensional arrays in C# have support for non-zero lower bounds, such as int[,].

My proposal is to silently ignore the lower bounds, leaving the lower bounds at 0 when converting to a .NET type. A problem might be that the lower bounds is not preserved if they are inserted again in the database. Or we could have an "internal" array/provider specific that holds the full array and its dimensions + lower bounds, but that seems a bit useless for common cases.

What do you think?

Activity

  1. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    My proposal is to silently ignore the lower bounds, leaving the lower bounds at 0 when converting to a .NET type. A problem might be that the lower bounds is not preserved if they are inserted again in the database. Or we could have an "internal" array/provider specific that holds the full array and its dimensions + lower bounds, but that seems a bit useless for common cases.

    Agreed. +1

    I think those non standard array bounds aren't so common and we should support them if there is enough requests. If so, we can create a provider specific type to add this support.

  2. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    I think that when #441 is implemented, we will start to see a lot of contributions of many data types support which may include support for those lower than 0 array bounds.

  3. Emill commented on Dec 27, 2014

    @Emill
    ContributorAuthor

    Do you have any suggestions regarding nullable value-types?
    For example, a postgresql int-array can contain nulls. An int[] can't, but an int?[] can...

  4. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    I remember we tried to handle this situation but we couldn't get to a solid solution, because handing nullable types inside the arrays were very tricky.
    Also, we stopped when we faced the problem with backward compatibility regarding the datatype. As you said, int?[] would do the work, but users wouldn't expect this type. With Npgsql 3.0 where we can break those backward compatibilities, I think int?[] would make the trick.

  5. glenebob commented on Dec 27, 2014

    @glenebob
    Contributor

    Whenever I've thought about it, nullable types was always my answer. Or
    maybe something like NpgsqlArrayElement. Same idea, really,

    -Glen

    On Sat, Dec 27, 2014 at 12:20 PM, Emill [email protected] wrote:

    Do you have any suggestions regarding nullable value-types?
    For example, a postgresql int-array can contain nulls. An int[] can't, but
    an int?[] can...

    —
    Reply to this email directly or view it on GitHub
    #442 (comment).

  6. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    @glenebob , you tried to play with it, right? I remember you were working on something about the nullable types.

  7. glenebob commented on Dec 27, 2014

    @glenebob
    Contributor

    I ran into the problem when I wrote the binary array codec. It was obvious
    how to fix it, but of course the compatibility disaster that would have
    ensued caused me to leave it. For v3, maybe you should change it to
    nullable, and add a connect string flag to turn it off so people can
    continue to use it without big changes to their code if they like.
    Obviously if people are using arrays, they're already doing whatever it
    takes to prevent nulls.

    -Glen

    On Sat, Dec 27, 2014 at 1:08 PM, Francisco Figueiredo Jr. <
    [email protected]> wrote:

    @glenebob https://github.com/glenebob , you tried to play with it,
    right? I remember you were working on something about the nullable types.

    —
    Reply to this email directly or view it on GitHub
    #442 (comment).

  8. franciscojunior commented on Dec 27, 2014

    @franciscojunior
    Member

    I ran into the problem when I wrote the binary array codec. It was obvious how to fix it, but of course the compatibility disaster that would have ensued caused me to leave it. For v3, maybe you should change it to nullable, and add a connect string flag to turn it off so people can continue to use it without big changes to their code if they like. Obviously if people are using arrays, they're already doing whatever it takes to prevent nulls.

    Yes, the compatibility was the biggest problem. I think with v3 we wouldn't have this problem.

    I liked your idea of the flag to turn it off if the user want it. +1

  9. roji commented on Dec 28, 2014

    @roji
    Member

    Here are some thoughts on all this...

    I think that if a user actually creates a Postgres array with lower bounds, they have some use case which actually needs it, and it also happens to be a feature that's supported in .NET. So we should probably make the effort to support it.

    One thing that helps here, is that the user can indicate what it is they want by invoking GetFieldValue(). I mean that if a user invokes GetFieldValue<int[]>(), they are saying they want a one-dimensional int array with a 0 lower bound - and that's what they should receive (even if the Postgres array is a one-dimensional int array with a non-zero lower bound). But if they simply do GetValue() on a non-zero lower bound array, they should probably receive a System.Int32[*] because that's what most faithfully maps to the database type... This way they have the option to get what they want.

    In a way, this is also a possible solution for the nullable problem. If the user invokes GetFieldValue<int?[]>(), they should get an array of nullables and everything works great. If they invoke GetFieldValue<int[]>() and there is a null somewhere in there, an exception should probably be raised (the only alternative is to have zeros in there, but that seems like a pretty dangerous thing to me).

    The remaining question would then be what the default should be, i.e. when they call GetValue() - I'm not sure. Unfortunately there's no "array of non-nullable types" in Postgres, so we can't know in advance whether we need to return int[] or int?[] - we have to pick one of these as the default, arbitrarily. If we go down this route I'm leaning towards int?[] as the default: it's the option that will never generate an exception, and it represents more faithfully the Postgres type (which is really an array of nullables). If the user knows there aren't any nulls in there and wants an int[], they can use GetFieldValue<int[]>().

    What do you guys think?

  10. Emill commented on Dec 28, 2014

    @Emill
    ContributorAuthor

    I think that if a user actually creates a Postgres array with lower bounds, they have some use case which actually needs it, and it also happens to be a feature that's supported in .NET. So we should probably make the effort to support it.

    Well, in .NET it is, but not in C# (at least for the one-dimensional [*]-type. You have to use the Array type and run .GetValue and .SetValue which I hardly think is easier to use for the user compared to just subtract their lower bound to the index and use a normal array).

    But if they simply do GetValue() on a non-zero lower bound array, they should probably receive a System.Int32[*] because that's what most faithfully maps to the database type... This way they have the option to get what they want.

    The most common array type in PostgreSQL is probably one-dimensional with 1 as lower bound. That way the "common" function GetValue (that is used to retrieve what we think the user wants) should in my opinion return a normal 0-indexed array, and not the strange type [*].

    .NET has non-zero lower bound arrays because of an ancient battle: http://www.panopticoncentral.net/2004/03/17/non-zero-lower-bounded-arrays-the-other-side-of-the-coin/ that with C# seems to have come to an end and some people want to disallow non-default lower bounds in PostgreSQL: http://postgresql.nabble.com/Disallow-arrays-with-non-standard-lower-bounds-td5786191.html.

    Also remember that the current array-parser in Npgsql breaks with inputs from non-default lower bounds, and nobody seems to have complained so far...

  11. roji commented on Dec 28, 2014

    @roji
    Member

    Hmm, it's interesting to see the PostgreSQL team regretting the whole thing.

    OK, I'm not sure. I guess I'm OK with just doing 0-lower-bound arrays until someone complains. It's really not worth discussing (much less coding) until we have a real reason to, as you say it's not like it was actually supported before (with text encoding).

    What about the nullable discussion? Does returning int?[] by default and int[] if requested make sense?

  12. Emill commented on Dec 28, 2014

    @Emill
    ContributorAuthor

    Returning int?[] seems good to me. And if int[] is requested, we can
    convert it to a such, as long as there aren't any null values inside.

    2014-12-28 15:41 GMT+01:00 Shay Rojansky [email protected]:

    Hmm, it's interesting to see the PostgreSQL team regretting the whole
    thing.

    OK, I'm not sure. I guess I'm OK with just doing 0-lower-bound arrays
    until someone complains. It's really not worth discussing (much less
    coding) until we have a real reason to, as you say it's not like it was
    actually supported before (with text encoding).

    What about the nullable discussion? Does returning int?[] by default and
    int[] if requested make sense?

    —
    Reply to this email directly or view it on GitHub
    #442 (comment).

  13. added this to the milestone on Dec 29, 2014
  14. self-assigned this
    on Dec 29, 2014
  15. added a commit that references this issue on Dec 30, 2014
    da05571
  16. roji commented on Dec 30, 2014

    @roji
    Member

    Fixed this, we now disregard lower bounds and always return standard .NET arrays with bounds == 0, both binary and text.

  17. franciscojunior commented on Dec 31, 2014

    @franciscojunior
    Member

    Fixed this, we now disregard lower bounds and always return standard .NET arrays with bounds == 0, both binary and text.

    Great!

    OK, I'm not sure. I guess I'm OK with just doing 0-lower-bound arrays until someone complains. It's really not worth discussing (much less coding) until we have a real reason to, as you say it's not like it was actually supported before (with text encoding).

    Agreed! +1

    What about the nullable discussion? Does returning int?[] by default and int[] if requested make sense?
    Yes.
    I agree with @Emill.

  18. added a commit that references this issue on Jan 1, 2015
    93cc23b
  19. modified the milestones: , 3.0-beta1 on Mar 30, 2015
  20. modified the milestones: 3.0-beta1, on Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions