Skip to content

InvalidCastException in GetFieldValue with empty multi-dimensional array #1271

Closed

Description

@lsedlacek

Running the code below expectedly prints 'foobar':

var conn = new NpgsqlConnection(DatabaseConfig.ConnectionString);
conn.Open();

new NpgsqlCommand(@"CREATE TABLE test (arr TEXT[]);", conn)
    .ExecuteNonQuery();

var arr = new[,] { { "foo", "bar" } };

var insCmd = new NpgsqlCommand(@"INSERT INTO test(arr) VALUES(:arr);", conn);
insCmd.Parameters.AddWithValue("arr", arr);
insCmd.ExecuteNonQuery();

var reader = new NpgsqlCommand(@"SELECT arr FROM test;", conn).ExecuteReader();
reader.Read();

Console.Write(reader.GetFieldValue<string[,]>(0)[0, 0]);
Console.Write(reader.GetFieldValue<string[,]>(0)[0, 1]);

Running the same code with arr = new string[0,0] throws the following expection:

System.InvalidCastException : Unable to cast object of type 'System.String[]' to type 'System.String[,]'.
   at Npgsql.NpgsqlDataReader.GetFieldValueInternal[T](Int32 ordinal)
   at Tests.Npgsql.ReadSingle_WithEmptyMultidimensionalArray_ReturnsEmptyArray() in C:\dev\tmp\Tests\Npgsql.cs:line 100

Activity

  1. roji commented on Sep 8, 2016

    @roji
    Member

    Thanks for reporting, this seems to be a PostgreSQL issue - writing a multidimensional array with length zero for a dimension makes PostgreSQL drop that dimension. This seems like a bug to me, have reported this to PostgreSQL.

    Am going to close this as this isn't related to Npgsql.

  2. lsedlacek commented on Sep 8, 2016

    @lsedlacek
    Author

    With the PostgreSQL discussion in mind I suggest re-opening and adding special handling if the returned array is empty.

    Theoretically speaking, an empty array has any dimensionality you choose, which is why I think the design was chosen.

  3. roji commented on Sep 8, 2016

    @roji
    Member

    I understand the motivation for this. I'm not sure about the theoretical reasoning justifying the PostgreSQL behavior - most (all?) languages I know allow you to define and clear distinguish between multidimensional arrays that happen to be empty.

    Regarding adding special handling... In general I see Npgsql as a thin layer for PostgreSQL access - I don't think it's a good idea for the driver to start "fixing" PostgreSQL quirks; the orientation is to simply expose PostgreSQL as-is to the user. Even though I agree that PostgreSQL's behavior here is pretty problematic, it is what it is and I'd rather have Npgsql show the same behavior as psql, drivers for other languages, etc.

    Aside from that, providing special handling in this case is really non-trivial. The current Npgsql type handler design simply doesn't allow for passing the requested array dimensionality (i.e. from GetFieldValue's type parameter) all the way down to the ArrayHandler, which is where the array is actually constructed. Doing this would mean quite a lot of work with some design changes, which seems really unwarranted for this edge case which is the expected PostgreSQL behavior.

    Please feel free to continue the discussion though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions