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

Giorgi / DuckDB.NET / 9507021691

13 Jun 2024 09:28PM UTC coverage: 90.539% (+0.01%) from 90.528%
9507021691

push

github

Giorgi
Enable appending blob in .Net Standard target

805 of 915 branches covered (87.98%)

Branch coverage included in aggregate %.

1683 of 1833 relevant lines covered (91.82%)

540285.14 hits per line

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

86.67
/DuckDB.NET.Data/Extensions/GuidConverter.cs
1
using System;
2
using DuckDB.NET.Data.Internal.Reader;
3
using DuckDB.NET.Native;
4

5
namespace DuckDB.NET.Data.Extensions;
6

7
internal static class GuidConverter
8
{
9
    private const string GuidFormat = "D";
10
    private static readonly char[] HexDigits = "0123456789abcdef".ToCharArray();
3✔
11

12
    //Ported from duckdb source code UUID::ToString
13
    //https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L56
14
    public static Guid ConvertToGuid(this DuckDBHugeInt input)
15
    {
16
        Span<char> buffer = stackalloc char[36];
746,141✔
17
        var num = input.Upper ^ long.MinValue;
746,141✔
18
        var position = 0;
746,141✔
19
        
20
        ByteToHex(buffer, ref position, (ulong)((num >> 56) & 0xFF));
746,141✔
21
        ByteToHex(buffer, ref position, (ulong)((num >> 48) & 0xFF));
746,141✔
22
        ByteToHex(buffer, ref position, (ulong)((num >> 40) & 0xFF));
746,141✔
23
        ByteToHex(buffer, ref position, (ulong)((num >> 32) & 0xFF));
746,141✔
24
        
25
        buffer[position++] = '-';
746,141✔
26
        
27
        ByteToHex(buffer, ref position, (ulong)((num >> 24) & 0xFF));
746,141✔
28
        ByteToHex(buffer, ref position, (ulong)((num >> 16) & 0xFF));
746,141✔
29
        
30
        buffer[position++] = '-';
746,141✔
31
        
32
        ByteToHex(buffer, ref position, (ulong)((num >> 8) & 0xFF));
746,141✔
33
        ByteToHex(buffer, ref position, (ulong)(num & 0xFF));
746,141✔
34
        
35
        buffer[position++] = '-';
746,141✔
36
        
37
        ByteToHex(buffer, ref position, (input.Lower >> 56) & 0xFF);
746,141✔
38
        ByteToHex(buffer, ref position, (input.Lower >> 48) & 0xFF);
746,141✔
39
        
40
        buffer[position++] = '-';
746,141✔
41
        
42
        ByteToHex(buffer, ref position, (input.Lower >> 40) & 0xFF);
746,141✔
43
        ByteToHex(buffer, ref position, (input.Lower >> 32) & 0xFF);
746,141✔
44
        ByteToHex(buffer, ref position, (input.Lower >> 24) & 0xFF);
746,141✔
45
        ByteToHex(buffer, ref position, (input.Lower >> 16) & 0xFF);
746,141✔
46
        ByteToHex(buffer, ref position, (input.Lower >> 8) & 0xFF);
746,141✔
47
        ByteToHex(buffer, ref position, input.Lower & 0xFF);
746,141✔
48

49
#if NET6_0_OR_GREATER
50
        return Guid.ParseExact(buffer, GuidFormat);
746,141✔
51
#else
52
        return Guid.ParseExact(new string(buffer.ToArray()), GuidFormat);
53
#endif
54

55
        static void ByteToHex(Span<char> buffer, ref int position, ulong value)
56
        {
57
            buffer[position++] = HexDigits[(value >> 4) & 0xF];
11,938,256✔
58
            buffer[position++] = HexDigits[value & 0xF];
11,938,256✔
59
        }
11,938,256✔
60
    }
61

62
    //https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L6
63
    public static DuckDBHugeInt ToHugeInt(this Guid guid)
64
    {
65
        char HexToChar(char ch)
66
        {
67
            return ch switch
23,875,264!
68
            {
23,875,264✔
69
                >= '0' and <= '9' => (char)(ch - '0'),
14,922,330✔
70
                >= 'a' and <= 'f' => (char)(10 + ch - 'a'),
8,952,934✔
71
                >= 'A' and <= 'F' => (char)(10 + ch - 'A'),
×
72
                _ => (char)0
×
73
            };
23,875,264✔
74
        }
75

76
        ulong lower = 0;
746,102✔
77
        long upper = 0;
746,102✔
78

79
        var str = guid.ToString("N");
746,102✔
80

81
        for (var index = 0; index < str.Length; index++)
49,242,732✔
82
        {
83
            if (index >= 16)
23,875,264✔
84
            {
85
                lower = (lower << 4) | HexToChar(str[index]);
11,937,632✔
86
            }
87
            else
88
            {
89
                upper = (upper << 4) | HexToChar(str[index]);
11,937,632✔
90
            }
91
        }
92

93
        // Flip the first bit to make `order by uuid` same as `order by uuid::varchar`
94
        upper ^= ((long)1 << 63);
746,102✔
95
        return new DuckDBHugeInt(lower, upper);
746,102✔
96
    }
97
}
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