snowflakedb / snowflake-connector-net

Snowflake Connector for .NET
Apache License 2.0
179 stars 138 forks source link

SNOW-1618183: Dataset load with VARIANT in resultset lead to ConstraintException #1006

Open MasterKuat opened 3 months ago

MasterKuat commented 3 months ago
  1. What version of .NET driver are you using? 4.1.0

  2. What operating system and processor architecture are you using? Windows 10, x64

  3. What version of .NET framework are you using? .net framework 4.8

  4. What did you do? Mainly :

           var reader = cmd.ExecuteReader();
            var dt = new DataTable();
            dt.Load(reader);

cmd is a SnowflakeDbCommand with a simple query : SELECT MyVariantcolumn FROM T

dt.Load(reader) raise an exception : Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

  1. What did you expect to see?

No exception

  1. Can you set logging to DEBUG and collect the logs?

This came from ColumnSize

For variant rowType.length is 0, should be -1 for unknown. Replace L124 by : row[SchemaTableColumn.ColumnSize] = rowType.length == 0 ? -1: (int)rowType.length;

sfc-gh-dszmolka commented 3 months ago

hi - thanks for reporting this issue and all the details (and the suggested solution too!) managed to reproduce it as you described; we'll take a look and fix it.

i also observed that using reader.Read(), something like:

...
                    cmd.CommandText = "SELECT variantcol from test_db.dotnet.gh1006;";
                    IDataReader reader = cmd.ExecuteReader();             
                        while (reader.Read())
                        {
                            Console.WriteLine(reader.GetString(0));
                        }
...

did not crash the application and could return the resultset; so perhaps could be used as a workaround until the issue is fixed, of course only if that makes sense in your case.

Anyways; thank you again and I'll keep this issue posted with the progress as there's any.

mrmoses commented 1 month ago

Same issue seems to be happening with ARRAY data types in the result.

One workaround is to specify the columns on the DataTable (generated from the results) before loading the data into it

IDataReader reader = cmd.ExecuteReader();

var table = new DataTable();

for (int i = 0; i < reader.FieldCount; i++)
{
    table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
}

table.Load(reader);

This loaded the ARRAY columns as (serialized) strings.