• 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

73.27
/DuckDB.NET.Data/DataChunk/Reader/StructVectorDataReader.Setters.cs
1
using System.Collections.Concurrent;
2
using System.Diagnostics.CodeAnalysis;
3
using System.Linq.Expressions;
4
using System.Reflection;
5
using System.Runtime.CompilerServices;
6

7
namespace DuckDB.NET.Data.DataChunk.Reader;
8

9
// How a field is read into a property of the target type. Nothing here depends on the column: the setters of a type
10
// are made once per process and shared by every reader.
11
internal sealed partial class StructVectorDataReader
12
{
13
    // A typed setter per settable property where code can be made at run time, a boxed one where it cannot.
14
    private static readonly ConcurrentDictionary<Type, PropertySetter[]> SetterCache = new();
3 ✔
15

16
    // The public properties that have a set method, whatever its access.
17
    private static List<PropertyInfo> SettableProperties(Type type)
18
    {
19
        var properties = new List<PropertyInfo>();
225 ✔
20

21
        foreach (var property in type.GetProperties())
1,206 ✔
22
        {
23
            if (property.SetMethod != null)
378 ✔
24
            {
25
                properties.Add(property);
372 ✔
26
            }
27
        }
28

29
        return properties;
225 ✔
30
    }
31

32
    // A setter per property, generic over the type that declares the property and the type of the property, so that a
33
    // field is read with ReadValidValue (the conversion of GetFieldValue<T> on a column) and given to the set method
34
    // without a box. The set method is called through a delegate made from it; nothing is compiled.
35
    // Null where no code can be made at run time: the types are the caller's, so none can be named in advance.
36
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(ClassPropertySetter<,>))]
37
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(NullableClassPropertySetter<,>))]
38
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(StructPropertySetter<,>))]
39
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(NullableStructPropertySetter<,>))]
40
    [UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "As IL2076, reported by the .NET 8 analyzer.")]
41
    [UnconditionalSuppressMessage("Trimming", "IL2076", Justification = "The new() constraint that 'where T : struct' implies is met by every value type, and the setters' constructors are kept by the DynamicDependency attributes. The MakeGenericType calls run only where dynamic code is supported.")]
42
    [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.")]
43
    private static PropertySetter[]? CreateTypedSetters(Type type)
44
    {
45
        if (!RuntimeFeature.IsDynamicCodeSupported)
225 !
46
        {
47
            return null;
×
48
        }
49

50
        // By name, as the boxed setters are: two properties of the same name fail in the same way.
51
        var setters = new Dictionary<string, PropertySetter>();
225 ✔
52

53
        foreach (var property in SettableProperties(type))
1,194 ✔
54
        {
55
            var setMethod = property.SetMethod!;
372 ✔
56
            var declaringType = setMethod.DeclaringType!;
372 ✔
57
            var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
372 ✔
58

59
            var setterType = (declaringType.IsValueType, underlyingType != null) switch
372 ✔
60
            {
372 ✔
61
                (false, false) => typeof(ClassPropertySetter<,>).MakeGenericType(declaringType, property.PropertyType),
258 ✔
62
                (false, true) => typeof(NullableClassPropertySetter<,>).MakeGenericType(declaringType, underlyingType!),
87 ✔
63
                (true, false) => typeof(StructPropertySetter<,>).MakeGenericType(declaringType, property.PropertyType),
24 ✔
64
                (true, true) => typeof(NullableStructPropertySetter<,>).MakeGenericType(declaringType, underlyingType!)
3 ✔
65
            };
372 ✔
66

67
            // CreateDelegate in the setter's constructor throws for a static or indexed set method, as Expression.Call
68
            // does on the boxed path.
69
            var setter = Activator.CreateInstance(setterType, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DoNotWrapExceptions, null, [property.Name, setMethod], null);
372 ✔
70
            setters.Add(property.Name, (PropertySetter)setter!);
372 ✔
71
        }
72

73
        return [.. setters.Values];
225 ✔
74
    }
75

76
    // Where no code can be made at run time: a boxed setter per property.
77
    private static PropertySetter[] CreateBoxedSetters(Type type)
78
    {
79
        // By name, as the typed setters are: two properties of the same name fail in the same way.
80
        var setters = new Dictionary<string, PropertySetter>();
×
81

82
        foreach (var property in SettableProperties(type))
×
83
        {
84
            var nullable = property.PropertyType.AllowsNullValue(out _, out var underlyingType);
×
85

86
            var instanceParam = Expression.Parameter(typeof(object));
×
87
            var argumentParam = Expression.Parameter(typeof(object));
×
88

89
            // For a C# struct, a compiled delegate would unbox it and set a copy; this path runs only without dynamic
90
            // code, where Compile interprets the expression and sets the struct inside the box.
91
            var setValue = Expression.Lambda<Action<object, object>>(
×
92
                Expression.Call(Expression.Convert(instanceParam, type), property.SetMethod!, Expression.Convert(argumentParam, property.PropertyType)),
×
93
                instanceParam, argumentParam
×
94
            ).Compile();
×
95

96
            setters.Add(property.Name, new BoxedPropertySetter(property.Name, underlyingType ?? property.PropertyType, nullable, setValue));
×
97
        }
98

99
        return [.. setters.Values];
×
100
    }
101

102
    // Reads one field of a row and sets one property of the target with it. A NULL field leaves a property that can
103
    // hold null as it is, and is refused for one that cannot.
104
    //
105
    // The typed setters are named after the type that declares the property (Class or Struct) and whether the property
106
    // is of type TValue? (Nullable). Their constructors are called through Activator, in CreateTypedSetters.
107
    private abstract class PropertySetter(string name)
372 ✔
108
    {
109
        public string Name { get; } = name;
63,150 ✔
110

111
        public abstract void Set(object target, VectorDataReaderBase fieldReader, ulong offset);
112

113
        [MethodImpl(MethodImplOptions.NoInlining)]
114
        protected InvalidCastException PropertyNotNullable() => new($"Property '{Name}' is not nullable but struct contains null value");
9 ✔
115
    }
116

117
    // The set method of a C# struct's property, taking the struct by reference: it is given the one inside the box.
118
    private delegate void RefSetMethod<TTarget, in TProperty>(ref TTarget target, TProperty value);
119

120
    private sealed class ClassPropertySetter<TTarget, TProperty>(string name, MethodInfo setMethod)
121
        : PropertySetter(name) where TTarget : class
258 ✔
122
    {
123
        private readonly Action<TTarget, TProperty> setValue = setMethod.CreateDelegate<Action<TTarget, TProperty>>();
258 ✔
124

125
        public override void Set(object target, VectorDataReaderBase fieldReader, ulong offset)
126
        {
127
            if (fieldReader.IsValid(offset))
195,120 ✔
128
            {
129
                setValue(Unsafe.As<TTarget>(target), fieldReader.ReadValidValue<TProperty>(offset));
191,547 ✔
130
            }
131
            else if (typeof(TProperty).IsValueType)
3,573 ✔
132
            {
133
                throw PropertyNotNullable();
6 ✔
134
            }
135
        }
3,567 ✔
136
    }
137

138
    private sealed class NullableClassPropertySetter<TTarget, TValue>(string name, MethodInfo setMethod)
139
        : PropertySetter(name) where TTarget : class where TValue : struct
87 ✔
140
    {
141
        private readonly Action<TTarget, TValue?> setValue = setMethod.CreateDelegate<Action<TTarget, TValue?>>();
87 ✔
142

143
        public override void Set(object target, VectorDataReaderBase fieldReader, ulong offset)
144
        {
145
            if (fieldReader.IsValid(offset))
37,068 ✔
146
            {
147
                setValue(Unsafe.As<TTarget>(target), fieldReader.ReadValidValue<TValue>(offset));
27,753 ✔
148
            }
149
        }
32,415 ✔
150
    }
151

152
    private sealed class StructPropertySetter<TTarget, TProperty>(string name, MethodInfo setMethod)
153
        : PropertySetter(name) where TTarget : struct
24 ✔
154
    {
155
        private readonly RefSetMethod<TTarget, TProperty> setValue = setMethod.CreateDelegate<RefSetMethod<TTarget, TProperty>>();
24 ✔
156

157
        public override void Set(object target, VectorDataReaderBase fieldReader, ulong offset)
158
        {
159
            if (fieldReader.IsValid(offset))
127,557 ✔
160
            {
161
                setValue(ref Unsafe.Unbox<TTarget>(target), fieldReader.ReadValidValue<TProperty>(offset));
118,554 ✔
162
            }
163
            else if (typeof(TProperty).IsValueType)
9,003 ✔
164
            {
165
                throw PropertyNotNullable();
3 ✔
166
            }
167
        }
9,000 ✔
168
    }
169

170
    private sealed class NullableStructPropertySetter<TTarget, TValue>(string name, MethodInfo setMethod)
171
        : PropertySetter(name) where TTarget : struct where TValue : struct
3 ✔
172
    {
173
        private readonly RefSetMethod<TTarget, TValue?> setValue = setMethod.CreateDelegate<RefSetMethod<TTarget, TValue?>>();
3 ✔
174

175
        public override void Set(object target, VectorDataReaderBase fieldReader, ulong offset)
176
        {
177
            if (fieldReader.IsValid(offset))
42,300 ✔
178
            {
179
                setValue(ref Unsafe.Unbox<TTarget>(target), fieldReader.ReadValidValue<TValue>(offset));
25,200 ✔
180
            }
181
        }
42,300 ✔
182
    }
183

184
    // Where no code can be made at run time: the field is read as an object of readType (the property type, or the
185
    // type under a nullable one) and set through a boxed delegate.
UNCOV
186
    private sealed class BoxedPropertySetter(string name, Type readType, bool nullable, Action<object, object> setValue) : PropertySetter(name)
×
187
    {
188
        public override void Set(object target, VectorDataReaderBase fieldReader, ulong offset)
189
        {
UNCOV
190
            if (fieldReader.IsValid(offset))
×
191
            {
UNCOV
192
                setValue(target, fieldReader.GetValue(offset, readType));
×
193
            }
UNCOV
194
            else if (!nullable)
×
195
            {
NEW
196
                throw PropertyNotNullable();
×
197
            }
UNCOV
198
        }
×
199
    }
200
}
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