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

Giorgi / DuckDB.NET / 10762504699

08 Sep 2024 07:21PM UTC coverage: 89.795% (-0.2%) from 90.033%
10762504699

push

github

Giorgi
Merge branch 'develop' into nightly-builds

920 of 1056 branches covered (87.12%)

Branch coverage included in aggregate %.

58 of 68 new or added lines in 8 files covered. (85.29%)

1 existing line in 1 file now uncovered.

1887 of 2070 relevant lines covered (91.16%)

896537.08 hits per line

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

76.12
/DuckDB.NET.Data/Internal/Writer/EnumVectorDataWriter.cs
1
using DuckDB.NET.Native;
2
using System;
3
using System.Collections.Generic;
4

5
namespace DuckDB.NET.Data.Internal.Writer;
6

7
internal sealed unsafe class EnumVectorDataWriter(IntPtr vector, void* vectorData, DuckDBLogicalType logicalType, DuckDBType columnType) : VectorDataWriterBase(vector, vectorData, columnType)
45✔
8
{
9
    private readonly DuckDBType enumType = NativeMethods.LogicalType.DuckDBEnumInternalType(logicalType);
45✔
10

11
    private readonly uint enumDictionarySize = NativeMethods.LogicalType.DuckDBEnumDictionarySize(logicalType);
45✔
12

13
    private readonly Dictionary<string, uint> enumValues = [];
45✔
14

15
    internal override bool AppendString(string value, ulong rowIndex)
16
    {
17
        if (enumValues.Count == 0)
563,165✔
18
        {
19
            for (uint index = 0; index < enumDictionarySize; index++)
606,690✔
20
            {
21
                var enumValueName = NativeMethods.LogicalType.DuckDBEnumDictionaryValue(logicalType, index).ToManagedString();
303,327✔
22
                enumValues.Add(enumValueName, index);
303,327✔
23
            }
24
        }
25

26
        if (enumValues.TryGetValue(value, out var enumValue))
563,165✔
27
        {
28
            // The following casts to byte and ushort are safe because we ensure in the constructor that the value enumDictionarySize is not too high.
29
            return enumType switch
563,162!
30
            {
563,162✔
31
                DuckDBType.UnsignedTinyInt => AppendValueInternal((byte)enumValue, rowIndex),
563,156✔
32
                DuckDBType.UnsignedSmallInt => AppendValueInternal((ushort)enumValue, rowIndex),
3✔
33
                DuckDBType.UnsignedInteger => AppendValueInternal(enumValue, rowIndex),
3✔
NEW
34
                _ => throw new InvalidOperationException($"Failed to write Enum column because the internal enum type must be utinyint, usmallint, or uinteger."),
×
35
            };
563,162✔
36
        }
37

38
        throw new InvalidOperationException($"Failed to write Enum column because the value \"{value}\" is not valid.");
3✔
39
    }
40

41
    internal override bool AppendEnum<TEnum>(TEnum value, ulong rowIndex)
42
    {
43
        var enumValue = ConvertEnumValueToUInt64(value);
569,357✔
44
        if (enumValue < enumDictionarySize)
569,357✔
45
        {
46
            // The following casts to byte, ushort and uint are safe because we ensure in the constructor that the value enumDictionarySize is not too high.
47
            return enumType switch
569,354!
48
            {
569,354✔
49
                DuckDBType.UnsignedTinyInt => AppendValueInternal((byte)enumValue, rowIndex),
569,348✔
50
                DuckDBType.UnsignedSmallInt => AppendValueInternal((ushort)enumValue, rowIndex),
3✔
51
                DuckDBType.UnsignedInteger => AppendValueInternal((uint)enumValue, rowIndex),
3✔
NEW
52
                _ => throw new InvalidOperationException($"Failed to write Enum column because the internal enum type must be utinyint, usmallint, or uinteger."),
×
53
            };
569,354✔
54
        }
55

56
        throw new InvalidOperationException($"Failed to write Enum column because the value is outside the range (0-{enumDictionarySize - 1}).");
3✔
57
    }
58

59
    private static ulong ConvertEnumValueToUInt64<TEnum>(TEnum value) where TEnum : Enum
60
    {
61
        return value.GetTypeCode() switch
569,357!
62
        {
569,357✔
NEW
63
            TypeCode.SByte => (ulong)Convert.ToSByte(value),
×
NEW
64
            TypeCode.Byte => Convert.ToByte(value),
×
65
            TypeCode.Int16 => (ulong)Convert.ToInt16(value),
3✔
NEW
66
            TypeCode.UInt16 => Convert.ToUInt16(value),
×
67
            TypeCode.Int32 => (ulong)Convert.ToInt32(value),
569,351✔
NEW
68
            TypeCode.UInt32 => Convert.ToUInt32(value),
×
NEW
69
            TypeCode.Int64 => (ulong)Convert.ToInt64(value),
×
70
            TypeCode.UInt64 => Convert.ToUInt64(value),
3✔
NEW
71
            _ => throw new InvalidOperationException($"Failed to convert the enum value {value} to ulong."),
×
72
        };
569,357✔
73
    }
74

75
    public override void Dispose()
76
    {
77
        logicalType.Dispose();
45✔
78
        base.Dispose();
45✔
79
    }
45✔
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc