• 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

94.74
/DuckDB.NET.Data/DataChunk/Reader/MapVectorDataReader.Builders.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. Nothing here depends on the column:
7
// a builder is given the key and value readers, and the map reader keeps the builder of the last target type in its
8
// plan.
9
internal sealed partial class MapVectorDataReader
10
{
11
    // The builder for a Dictionary<TKey, TValue> or a class derived from one (dictionaryType, its base), or null when
12
    // the type is read through IDictionary. CreatePlan has already refused object keys and values; a nullable key type
13
    // is left to IDictionary, which reads it as the type under it.
14
    //
15
    // The key and value types are read from dictionaryType again rather than passed from CreatePlan: passed as a Type
16
    // parameter, MakeGenericType of NullableValueMapBuilder<,> raises the trim warnings IL2070 and IL2071 (the new()
17
    // that 'where TValue : struct' implies), which the analyzer does not raise for the result of GetGenericArguments.
18
    // The list reader takes its element type as a parameter and suppresses IL2071 instead.
19
    //
20
    // A builder is generic over the key and value types, and is found in the same three ways as the nullable reader
21
    // in VectorDataReaderBase, for applications that are trimmed or compiled ahead of time:
22
    // - the common pairs are named below, so an application compiled ahead of time has the code for them;
23
    // - any other pair is made with MakeGenericType where code can be made at run time;
24
    // - elsewhere the dictionary is filled through IDictionary.
25
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MapBuilder<,>))]
26
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(NullableValueMapBuilder<,>))]
27
    [UnconditionalSuppressMessage("AOT", "IL3050", Justification = "The MakeGenericType calls run only where RuntimeFeature.IsDynamicCodeSupported is true, tested in this method; the .NET 8 analyzer does not recognise that guard.")]
28
    private static MapBuilder? CreateBuilder(Type targetType, Type? dictionaryType)
29
    {
30
        if (dictionaryType == null)
211,377 ✔
31
        {
32
            return null;
6 ✔
33
        }
34

35
        var arguments = dictionaryType.GetGenericArguments();
211,371 ✔
36
        var (keyType, valueType) = (arguments[0], arguments[1]);
211,371 ✔
37

38
        if (Nullable.GetUnderlyingType(keyType) != null)
211,371 ✔
39
        {
40
            return null;
18,003 ✔
41
        }
42

43
        var result = KnownBuilder(dictionaryType);
193,368 ✔
44

45
        if (result == null && RuntimeFeature.IsDynamicCodeSupported)
193,368 !
46
        {
47
            var underlyingValueType = Nullable.GetUnderlyingType(valueType);
34,707 ✔
48

49
            var builderType = underlyingValueType == null
34,707 ✔
50
                ? typeof(MapBuilder<,>).MakeGenericType(keyType, valueType)
34,707 ✔
51
                : typeof(NullableValueMapBuilder<,>).MakeGenericType(keyType, underlyingValueType);
34,707 ✔
52

53
            result = (MapBuilder)Activator.CreateInstance(builderType)!;
34,707 ✔
54
        }
55

56
        if (result != null && targetType != dictionaryType)
193,368 !
57
        {
58
            result.DerivedType = targetType;
45,000 ✔
59
        }
60

61
        return result;
193,368 ✔
62
    }
63

64
    // Keys of VARCHAR, INTEGER, BIGINT and UUID columns with values of the commonest types. Every pair named here
65
    // (each key with each value) is compiled into an application compiled ahead of time, used or not, so the lists
66
    // are kept short.
67
    private static MapBuilder? KnownBuilder(Type dictionaryType)
68
    {
69
        return KnownForKey<string>(dictionaryType) ?? KnownForKey<int>(dictionaryType) ?? KnownForKey<long>(dictionaryType)
193,368 ✔
70
               ?? KnownForKey<Guid>(dictionaryType);
193,368 ✔
71
    }
72

73
    private static MapBuilder? KnownForKey<TKey>(Type dictionaryType) where TKey : notnull
74
    {
75
        return Known<TKey, string>(dictionaryType) ?? Known<TKey, int>(dictionaryType) ?? Known<TKey, long>(dictionaryType)
411,129 ✔
76
               ?? Known<TKey, double>(dictionaryType) ?? Known<TKey, bool>(dictionaryType) ?? Known<TKey, DateTime>(dictionaryType)
411,129 ✔
77
               ?? Known<TKey, Guid>(dictionaryType) ?? KnownNullable<TKey, int>(dictionaryType)
411,129 ✔
78
               ?? KnownNullable<TKey, long>(dictionaryType) ?? KnownNullable<TKey, double>(dictionaryType)
411,129 ✔
79
               ?? KnownNullable<TKey, bool>(dictionaryType) ?? KnownNullable<TKey, DateTime>(dictionaryType)
411,129 ✔
80
               ?? KnownNullable<TKey, Guid>(dictionaryType);
411,129 ✔
81
    }
82

83
    private static MapBuilder? Known<TKey, TValue>(Type dictionaryType) where TKey : notnull
84
    {
85
        return dictionaryType == typeof(Dictionary<TKey, TValue>) ? new MapBuilder<TKey, TValue>() : null;
2,670,081 ✔
86
    }
87

88
    private static MapBuilder? KnownNullable<TKey, TValue>(Type dictionaryType) where TKey : notnull where TValue : struct
89
    {
90
        return dictionaryType == typeof(Dictionary<TKey, TValue?>) ? new NullableValueMapBuilder<TKey, TValue>() : null;
1,761,156 ✔
91
    }
92

93
    private abstract class MapBuilder
94
    {
95
        // A class derived from Dictionary<TKey, TValue>, which is created as itself; null for the dictionary type.
96
        public Type? DerivedType { get; set; }
364,383 ✔
97

98
        public abstract object Build(VectorDataReaderBase keyReader, VectorDataReaderBase valueReader, ulong listOffset, ulong length);
99

100
        // A Dictionary<TKey, TValue> has room for every entry when it is created; a derived class grows as entries
101
        // are added.
102
        [UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "DerivedType is the type the caller asked for, which the caller's code refers to, as the IDictionary path creates it with Activator as well.")]
103
        protected Dictionary<TKey, TValue> Create<TKey, TValue>(ulong length) where TKey : notnull
104
        {
105
            if (DerivedType == null)
274,383 ✔
106
            {
107
                return new Dictionary<TKey, TValue>((int)length);
229,383 ✔
108
            }
109

110
            return (Dictionary<TKey, TValue>)Activator.CreateInstance(DerivedType)!;
45,000 ✔
111
        }
112
    }
113

114
    // Keys and values are read with ReadValidValue, the conversion of GetFieldValue<T> on a column, and added
115
    // without a box. A NULL value is added as null for a reference type and refused for a value type.
116
    private sealed class MapBuilder<TKey, TValue> : MapBuilder where TKey : notnull
117
    {
118
        public override object Build(VectorDataReaderBase keyReader, VectorDataReaderBase valueReader, ulong listOffset, ulong length)
119
        {
120
            var result = Create<TKey, TValue>(length);
183,339 ✔
121

122
            for (ulong i = 0; i < length; i++)
748,740 ✔
123
            {
124
                var childOffset = listOffset + i;
237,411 ✔
125

126
                if (!keyReader.IsValid(childOffset))
237,411 !
127
                {
NEW
128
                    throw MapContainsNullKey(keyReader.ColumnName);
×
129
                }
130

131
                var key = keyReader.ReadValidValue<TKey>(childOffset);
237,411 ✔
132

133
                if (valueReader.IsValid(childOffset))
232,233 ✔
134
                {
135
                    result.Add(key, valueReader.ReadValidValue<TValue>(childOffset));
178,194 ✔
136
                }
137
                else if (!typeof(TValue).IsValueType)
54,039 ✔
138
                {
139
                    result.Add(key, default!);
18,030 ✔
140
                }
141
                else
142
                {
143
                    throw MapContainsNullValue(valueReader.ColumnName);
36,009 ✔
144
                }
145
            }
146

147
            return result;
136,959 ✔
148
        }
149
    }
150

151
    // A dictionary of TValue?: a value is read as TValue, and NULL is added as null.
152
    private sealed class NullableValueMapBuilder<TKey, TValue> : MapBuilder where TKey : notnull where TValue : struct
153
    {
154
        public override object Build(VectorDataReaderBase keyReader, VectorDataReaderBase valueReader, ulong listOffset, ulong length)
155
        {
156
            var result = Create<TKey, TValue?>(length);
91,044 ✔
157

158
            for (ulong i = 0; i < length; i++)
516,900 ✔
159
            {
160
                var childOffset = listOffset + i;
172,059 ✔
161

162
                if (!keyReader.IsValid(childOffset))
172,059 !
163
                {
NEW
164
                    throw MapContainsNullKey(keyReader.ColumnName);
×
165
                }
166

167
                var key = keyReader.ReadValidValue<TKey>(childOffset);
172,059 ✔
168

169
                result.Add(key, valueReader.IsValid(childOffset) ? valueReader.ReadValidValue<TValue>(childOffset) : null);
172,059 ✔
170
            }
171

172
            return result;
86,391 ✔
173
        }
174
    }
175
}
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