You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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.
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.
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.
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...
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.
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).
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:
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
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[]>().
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 [*].
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?
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).
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.
Activity
franciscojunior commentedon Dec 27, 2014
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.
franciscojunior commentedon Dec 27, 2014
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.
Emill commentedon Dec 27, 2014
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...
franciscojunior commentedon Dec 27, 2014
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.
glenebob commentedon Dec 27, 2014
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:
franciscojunior commentedon Dec 27, 2014
@glenebob , you tried to play with it, right? I remember you were working on something about the nullable types.
glenebob commentedon Dec 27, 2014
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:
franciscojunior commentedon Dec 27, 2014
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
roji commentedon Dec 28, 2014
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?
Emill commentedon Dec 28, 2014
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).
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...
roji commentedon Dec 28, 2014
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?
Emill commentedon Dec 28, 2014
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]:
Work on arrays with lower bounds != 1
roji commentedon Dec 30, 2014
Fixed this, we now disregard lower bounds and always return standard .NET arrays with bounds == 0, both binary and text.
franciscojunior commentedon Dec 31, 2014
Great!
Agreed! +1
Work on arrays with lower bounds != 1