• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Giorgi / DuckDB.NET / 45378422962

24 Nov 2026 10:49AM UTC coverage: 93.902% (+0.05%) from 93.854%
45378422962

push

github

Giorgi
Return the exception from the throw helpers

ListContainsNull, MapContainsNullKey, MapContainsNullValue,
PropertyNotNullable, NoRowRead, ColumnOutOfRange,
NullReceivedByNonNullableParam<T> and EnumValue.CannotConvert now build
the exception and return it, and the caller throws it, as CannotCast and
ValueIsNull already did. The throw at the call site tells the JIT that
the block does not return, whatever attributes the helper has.
CannotConvert no longer needs a fake ulong return type.

ThrowException<T> keeps throwing itself, because 27 default Append...
methods return what it returns. DataChunk/AGENTS.md says so, and that it
must never get NoInlining.

The JIT listings of the readers and the appender on .NET 8 show the same
hot paths as before; each call site adds mov rcx, rax and a call to the
throw helper in the cold block after ret (8 bytes per site).
Messages and exception types are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LUiK1opACtfiLjHgoobNfd

3120 of 3368 branches covered (92.64%)

Branch coverage included in aggregate %.

13 of 23 new or added lines in 9 files covered. (56.52%)

5 existing lines in 1 file now uncovered.

4810 of 5077 relevant lines covered (94.74%)

920339.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.78
/DuckDB.NET.Data/DataChunk/Reader/MapVectorDataReader.cs
1
using System.Runtime.CompilerServices;
2

3
namespace DuckDB.NET.Data.DataChunk.Reader;
4

5
// How the keys and values of a Dictionary<TKey, TValue> are read without a box (CreateBuilder and the builders)
6
// is in MapVectorDataReader.Builders.cs.
7
internal sealed partial class MapVectorDataReader : VectorDataReaderBase
8
{
9
    private readonly VectorDataReaderBase keyReader;
10
    private readonly VectorDataReaderBase valueReader;
11

12
    // What GetValueCore found out about the type it was asked for last. It is worked out once per type it is asked
13
    // for, as a column is read as the same type row after row.
14
    private Type? planType;
15
    private MapPlan plan;
16

17
    internal unsafe MapVectorDataReader(IntPtr vector, void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, DuckDBLogicalType logicalColumnType, string columnName)
18
        : base(dataPointer, validityMaskPointer, columnType, columnName)
1,716 ✔
19
    {
20
        using var keyTypeLogical = NativeMethods.LogicalType.DuckDBMapTypeKeyType(logicalColumnType);
1,716 ✔
21
        using var valueTypeLogical = NativeMethods.LogicalType.DuckDBMapTypeValueType(logicalColumnType);
1,716 ✔
22

23
        var childVector = NativeMethods.Vectors.DuckDBListVectorGetChild(vector);
1,716 ✔
24

25
        var keyVector = NativeMethods.Vectors.DuckDBStructVectorGetChild(childVector, 0);
1,716 ✔
26
        var valueVector = NativeMethods.Vectors.DuckDBStructVectorGetChild(childVector, 1);
1,716 ✔
27

28
        keyReader = VectorDataReaderFactory.CreateReader(keyVector, keyTypeLogical, columnName);
1,716 ✔
29
        valueReader = VectorDataReaderFactory.CreateReader(valueVector, valueTypeLogical, columnName);
1,716 ✔
30
    }
3,432 ✔
31

32
    protected override Type GetColumnType()
33
    {
34
        return typeof(Dictionary<,>).MakeGenericType(keyReader.ClrType, valueReader.ClrType);
42 ✔
35
    }
36

37
    protected override Type GetColumnProviderSpecificType()
38
    {
39
        return typeof(Dictionary<,>).MakeGenericType(keyReader.ProviderSpecificClrType, valueReader.ProviderSpecificClrType);
6 ✔
40
    }
41

42
    internal override unsafe object GetValueCore(ulong offset, Type targetType)
43
    {
44
        if (DuckDBType != DuckDBType.Map)
302,532 !
45
        {
46
            return base.GetValueCore(offset, targetType);
×
47
        }
48

49
        if (targetType != planType)
302,532 ✔
50
        {
51
            plan = CreatePlan(targetType);
221,517 ✔
52
            planType = targetType;
211,377 ✔
53
        }
54

55
        var listData = (DuckDBListEntry*)DataPointer + offset;
292,392 ✔
56

57
        if (plan.Builder != null)
292,392 ✔
58
        {
59
            return plan.Builder.Build(keyReader, valueReader, listData->Offset, listData->Length);
274,383 ✔
60
        }
61

62
        return GetBoxedDictionary(targetType, listData->Offset, listData->Length);
18,009 ✔
63
    }
64

65
    // Any type without a builder (see CreateBuilder): created as itself and filled through IDictionary, with every key
66
    // and value read as an object.
67
    private IDictionary GetBoxedDictionary(Type targetType, ulong listOffset, ulong length)
68
    {
69
        // CreatePlan refused a type that cannot be created or is not an IDictionary.
70
        var instance = (IDictionary)Activator.CreateInstance(targetType)!;
18,009 ✔
71

72
        for (ulong i = 0; i < length; i++)
108,060 ✔
73
        {
74
            var childOffset = i + listOffset;
36,021 ✔
75

76
            if (!keyReader.IsValid(childOffset))
36,021 !
77
            {
NEW
78
                throw MapContainsNullKey(ColumnName);
×
79
            }
80

81
            var key = keyReader.GetValue(childOffset, plan.KeyType);
36,021 ✔
82
            var value = valueReader.IsValid(childOffset) ? valueReader.GetValue(childOffset, plan.ValueType) : null;
36,021 ✔
83

84
            if (plan.AllowsNullValues || value != null)
36,021 !
85
            {
86
                instance.Add(key, value);
36,021 ✔
87
            }
88
            else
89
            {
NEW
90
                throw MapContainsNullValue(ColumnName);
×
91
            }
92
        }
93

94
        return instance;
18,009 ✔
95
    }
96

97
    // Used by the IDictionary path and by the builders. A child reader carries the name of the map column.
98
    [MethodImpl(MethodImplOptions.NoInlining)]
99
    private static InvalidCastException MapContainsNullValue(string columnName) => new($"The Map in column {columnName} contains null value but dictionary does not allow null values");
36,009 ✔
100

101
    // Used by the IDictionary path and by the builders. SQL cannot make a NULL map key, but a map can also arrive
102
    // from Parquet, Arrow or the C API.
103
    [MethodImpl(MethodImplOptions.NoInlining)]
NEW
104
    private static InvalidCastException MapContainsNullKey(string columnName) => new($"The Map in column {columnName} contains a null key");
×
105

106
    // Not inlined, so that GetValueCore does not set up a native-call frame on every row (Reader/DESIGN.md).
107
    [MethodImpl(MethodImplOptions.NoInlining)]
108
    private MapPlan CreatePlan(Type targetType)
109
    {
110
        // Any target is created as itself, so it has to be a class that can be created and filled.
111
        if (targetType.IsInterface || targetType.IsAbstract)
221,517 ✔
112
        {
113
            throw new InvalidCastException($"Cannot read Map column {ColumnName} as {targetType.Name}: it is an interface or an abstract class.");
9 ✔
114
        }
115

116
        if (!typeof(IDictionary).IsAssignableFrom(targetType))
221,508 ✔
117
        {
118
            throw new InvalidCastException($"Cannot read Map column {ColumnName} in a non-dictionary type");
9 ✔
119
        }
120

121
        // The key and value types are those of the Dictionary<TKey, TValue> the target is or derives from, so a class
122
        // that is not generic itself, or whose type arguments are not the key and value, is read by its base. Another
123
        // type (a SortedDictionary, a Hashtable) gives its own type arguments, or none.
124
        var dictionaryType = targetType.FindGenericBase(typeof(Dictionary<,>));
221,499 ✔
125
        var arguments = (dictionaryType ?? targetType).GetGenericArguments();
221,499 ✔
126

127
        // What the IDictionary path reads a key and a value as, and whether it adds a NULL value; a builder knows
128
        // its own types.
129
        var keyTargetType = keyReader.ClrType;
221,499 ✔
130
        var valueTargetType = valueReader.ClrType;
221,499 ✔
131
        var allowsNullValues = false;
221,499 ✔
132

133
        if (arguments.Length == 2)
221,499 ✔
134
        {
135
            if (arguments[0] == typeof(object) || arguments[1] == typeof(object))
221,496 ✔
136
            {
137
                throw new InvalidCastException($"Cannot read Map column {ColumnName} as a dictionary with object keys or values. Use a dictionary with concrete key and value types, or GetValue().");
10,122 ✔
138
            }
139

140
            arguments[0].AllowsNullValue(out _, out var underlyingKeyType);
211,374 ✔
141
            keyTargetType = underlyingKeyType ?? arguments[0];
211,374 ✔
142

143
            allowsNullValues = arguments[1].AllowsNullValue(out _, out var underlyingValueType);
211,374 ✔
144
            valueTargetType = underlyingValueType ?? arguments[1];
211,374 ✔
145
        }
146

147
        return new MapPlan(Builder: CreateBuilder(targetType, dictionaryType), KeyType: keyTargetType,
211,377 ✔
148
                           ValueType: valueTargetType, AllowsNullValues: allowsNullValues);
211,377 ✔
149
    }
150

151
    // Builder reads keys and values on the typed path, or is null. KeyType, ValueType and AllowsNullValues are for
152
    // the IDictionary path: the types a key and a value are read as, and whether a NULL value is added.
153
    private readonly record struct MapPlan(MapBuilder? Builder, Type KeyType, Type ValueType, bool AllowsNullValues);
656,832 ✔
154

155
    internal override void Reset(IntPtr vector)
156
    {
157
        base.Reset(vector);
381 ✔
158
        var childVector = NativeMethods.Vectors.DuckDBListVectorGetChild(vector);
381 ✔
159
        var keyVector = NativeMethods.Vectors.DuckDBStructVectorGetChild(childVector, 0);
381 ✔
160
        var valueVector = NativeMethods.Vectors.DuckDBStructVectorGetChild(childVector, 1);
381 ✔
161
        keyReader.Reset(keyVector);
381 ✔
162
        valueReader.Reset(valueVector);
381 ✔
163
    }
381 ✔
164

165
    public override void Dispose()
166
    {
167
        keyReader.Dispose();
1,524 ✔
168
        valueReader.Dispose();
1,524 ✔
169
        base.Dispose();
1,524 ✔
170
    }
1,524 ✔
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc