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

Giorgi / DuckDB.NET / 44526529088

19 Nov 2026 12:40PM UTC coverage: 93.724% (-0.03%) from 93.75%
44526529088

push

github

Giorgi
Add the parameter changes of 2.0 to the release notes

The breaking changes of the parameter conversion (an assigned DbType kept
and deciding the value, the Object default, TimeSpan for DbType.Time,
integer parameters, function arguments typed as integers, text without the
culture) and the exact-type parameters as a new feature. Taken from the
release notes draft; the measurements wait for the final release.

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

3091 of 3342 branches covered (92.49%)

Branch coverage included in aggregate %.

4675 of 4944 relevant lines covered (94.56%)

978051.08 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.Diagnostics.CodeAnalysis;
2
using System.Runtime.CompilerServices;
3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

77
            if (!keyReader.IsValid(childOffset))
36,021 !
78
            {
79
                ThrowNullKey(ColumnName);
×
80
            }
81

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

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

95
        return instance;
18,009 ✔
96
    }
97

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

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

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

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

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

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

136
        if (arguments.Length == 2)
221,499 ✔
137
        {
138
            if (arguments[0] == typeof(object) || arguments[1] == typeof(object))
221,496 ✔
139
            {
140
                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 ✔
141
            }
142

143
            arguments[0].AllowsNullValue(out _, out var underlyingKeyType);
211,374 ✔
144
            keyTargetType = underlyingKeyType ?? arguments[0];
211,374 ✔
145

146
            allowsNullValues = arguments[1].AllowsNullValue(out _, out var underlyingValueType);
211,374 ✔
147
            valueTargetType = underlyingValueType ?? arguments[1];
211,374 ✔
148
        }
149

150
        return new MapPlan(Builder: CreateBuilder(targetType, dictionaryType), KeyType: keyTargetType,
211,377 ✔
151
                           ValueType: valueTargetType, AllowsNullValues: allowsNullValues);
211,377 ✔
152
    }
153

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

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

168
    public override void Dispose()
169
    {
170
        keyReader.Dispose();
1,524 ✔
171
        valueReader.Dispose();
1,524 ✔
172
        base.Dispose();
1,524 ✔
173
    }
1,524 ✔
174
}
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