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
{{ message }}
Repository navigation
EnumVectorDataWriter throws InvalidOperationException if given an enum with non-consecutive values. #330
usingDuckDB.NET.Data;enumMood:byte{Happy=1,Sad=2,// Skip 3Neutral=4}internalstaticclassProgram{privatestaticvoidMain(){varenumNames=Enum.GetNames<Mood>();varenumLabelsSql=string.Join(", ",enumNames.Select(n =>$"'{n}'"));varenumTypeSql=$"CREATE TYPE Mood AS ENUM ({enumLabelsSql});";usingvarconnection=newDuckDBConnection("Data Source=:memory:");connection.Open();usingvarcommand=connection.CreateCommand();command.CommandText=enumTypeSql;command.ExecuteNonQuery();command.CommandText="CREATE TABLE EnumTest (Value Mood);";command.ExecuteNonQuery();varvaluesToInsert=new[]{Mood.Happy,Mood.Sad,Mood.Neutral};using(varappender=connection.CreateAppender("EnumTest")){foreach(varenumValueinvaluesToInsert){varrow=appender.CreateRow();row.AppendValue(enumValue);row.EndRow();}}}}
I believe this is because DuckDB.NET casts the enum to an integer value and then writes that to the database. As long as the enum values are consecutive, DuckDB's internal enum map and C#'s enum map align, so this works out. When the C# enum has gaps in its values, this assumption is no longer true and the code crashes.
This also means that [Flags] enums can cause a bit of chaos as well. Since DuckDB enums aren't designed to be used as flags, DuckDB.NET should probably prevent [Flags] enums from being used entirely, because something like "Happy | Neutral" cannot convert to a DuckDB enum.
Activity