Skip to content

Commit e6fb730

Browse files
committed
Refactoring
1 parent ea49632 commit e6fb730

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

DuckDB.NET.Data/Internal/Reader/ListVectorDataReader.cs

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ internal override unsafe object GetValue(ulong offset, Type targetType)
3939
switch (DuckDBType)
4040
{
4141
case DuckDBType.List:
42-
{
43-
var listData = (DuckDBListEntry*)DataPointer + offset;
42+
{
43+
var listData = (DuckDBListEntry*)DataPointer + offset;
4444

45-
return GetList(targetType, listData->Offset, listData->Length);
46-
}
45+
return GetList(targetType, listData->Offset, listData->Length);
46+
}
4747
case DuckDBType.Array:
4848
return GetList(targetType, offset * arraySize, arraySize);
4949
default:
@@ -61,57 +61,50 @@ private unsafe object GetList(Type returnType, ulong listOffset, ulong length)
6161
?? throw new ArgumentException($"The type '{returnType.Name}' specified in parameter {nameof(returnType)} cannot be instantiated as an IList.");
6262

6363
//Special case for specific types to avoid boxing
64-
switch (list)
64+
return list switch
6565
{
66-
case List<int> theList:
67-
return BuildList<int>(theList);
68-
case List<int?> theList:
69-
return BuildList<int?>(theList);
70-
case List<float> theList:
71-
return BuildList<float>(theList);
72-
case List<float?> theList:
73-
return BuildList<float?>(theList);
74-
case List<double> theList:
75-
return BuildList<double>(theList);
76-
case List<double?> theList:
77-
return BuildList<double?>(theList);
78-
case List<decimal> theList:
79-
return BuildList<decimal>(theList);
80-
case List<decimal?> theList:
81-
return BuildList<decimal?>(theList);
82-
}
83-
84-
var targetType = nullableType ?? listType;
66+
List<int> theList => BuildList<int>(theList),
67+
List<int?> theList => BuildList<int?>(theList),
68+
List<float> theList => BuildList<float>(theList),
69+
List<float?> theList => BuildList<float?>(theList),
70+
List<double> theList => BuildList<double>(theList),
71+
List<double?> theList => BuildList<double?>(theList),
72+
List<decimal> theList => BuildList<decimal>(theList),
73+
List<decimal?> theList => BuildList<decimal?>(theList),
74+
_ => BuildListCommon(list, nullableType ?? listType)
75+
};
8576

86-
for (ulong i = 0; i < length; i++)
77+
List<T> BuildList<T>(List<T> result)
8778
{
88-
var childOffset = listOffset + i;
89-
if (listDataReader.IsValid(childOffset))
90-
{
91-
var item = listDataReader.GetValue(childOffset, targetType);
92-
list.Add(item);
93-
}
94-
else
79+
for (ulong i = 0; i < length; i++)
9580
{
96-
list.Add(allowNulls ? null : throw new InvalidCastException("The list contains null value"));
81+
var childOffset = listOffset + i;
82+
if (listDataReader.IsValid(childOffset))
83+
{
84+
var item = listDataReader.GetValue<T>(childOffset);
85+
result.Add(item);
86+
}
87+
else
88+
{
89+
result.Add(allowNulls ? default! : throw new InvalidCastException("The list contains null value"));
90+
}
9791
}
92+
return result;
9893
}
9994

100-
return list;
101-
102-
List<T> BuildList<T>(List<T> result)
95+
IList BuildListCommon(IList result, Type targetType)
10396
{
10497
for (ulong i = 0; i < length; i++)
10598
{
10699
var childOffset = listOffset + i;
107100
if (listDataReader.IsValid(childOffset))
108101
{
109-
var item = listDataReader.GetValue<T>(childOffset);
102+
var item = listDataReader.GetValue(childOffset, targetType);
110103
result.Add(item);
111104
}
112105
else
113106
{
114-
result.Add(allowNulls ? default! : throw new InvalidCastException("The list contains null value"));
107+
result.Add(allowNulls ? null : throw new InvalidCastException("The list contains null value"));
115108
}
116109
}
117110
return result;

0 commit comments

Comments
 (0)