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

Giorgi / DuckDB.NET / 9095397334

15 May 2024 12:10PM UTC coverage: 89.5% (-0.3%) from 89.79%
9095397334

push

github

Giorgi
Merge branch 'Appender-Data-Chunk' (early part) into develop

663 of 770 branches covered (86.1%)

Branch coverage included in aggregate %.

262 of 293 new or added lines in 22 files covered. (89.42%)

9 existing lines in 2 files now uncovered.

1485 of 1630 relevant lines covered (91.1%)

11521.31 hits per line

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

93.75
/DuckDB.NET.Data/DuckDBAppender.cs
1
using DuckDB.NET.Data.Internal.Writer;
2
using DuckDB.NET.Native;
3
using System;
4
using System.Diagnostics;
5
using System.Diagnostics.CodeAnalysis;
6

7
namespace DuckDB.NET.Data;
8

9
public class DuckDBAppender : IDisposable
10
{
11
    private static readonly ulong DuckDBVectorSize = NativeMethods.Helpers.DuckDBVectorSize();
3✔
12

13
    private bool closed;
14
    private readonly Native.DuckDBAppender nativeAppender;
15
    private readonly string qualifiedTableName;
16

17
    private ulong rowCount;
18

19
    private readonly DuckDBLogicalType[] logicalTypes;
20
    private readonly DuckDBDataChunk dataChunk;
21
    private readonly VectorDataWriterBase[] vectorWriters;
22

23
    internal unsafe DuckDBAppender(Native.DuckDBAppender appender, string qualifiedTableName)
45✔
24
    {
25
        nativeAppender = appender;
45✔
26
        this.qualifiedTableName = qualifiedTableName;
45✔
27

28
        var columnCount = NativeMethods.Appender.DuckDBAppenderColumnCount(nativeAppender);
45✔
29

30
        vectorWriters = new VectorDataWriterBase[columnCount];
45✔
31
        logicalTypes = new DuckDBLogicalType[columnCount];
45✔
32
        var logicalTypeHandles = new IntPtr[columnCount];
45✔
33

34
        for (ulong index = 0; index < columnCount; index++)
474✔
35
        {
36
            logicalTypes[index] = NativeMethods.Appender.DuckDBAppenderColumnType(nativeAppender, index);
192✔
37
            logicalTypeHandles[index] = logicalTypes[index].DangerousGetHandle();
192✔
38
        }
39

40
        dataChunk = NativeMethods.DataChunks.DuckDBCreateDataChunk(logicalTypeHandles, columnCount);
45✔
41
    }
45✔
42

43
    public DuckDBAppenderRow CreateRow()
44
    {
45
        if (closed)
30,360✔
46
        {
47
            throw new InvalidOperationException("Appender is already closed");
3✔
48
        }
49

50
        if (rowCount % DuckDBVectorSize == 0)
30,357✔
51
        {
52
            AppendDataChunk();
54✔
53

54
            InitVectorWriters();
54✔
55
            rowCount = 0;
54✔
56
        }
57

58
        rowCount++;
30,357✔
59
        return new DuckDBAppenderRow(qualifiedTableName, vectorWriters, rowCount - 1);
30,357✔
60
    }
61

62
    public void Close()
63
    {
64
        closed = true;
45✔
65

66
        try
67
        {
68
            AppendDataChunk();
45✔
69

70
            foreach (var logicalType in logicalTypes)
474✔
71
            {
72
                logicalType.Dispose();
192✔
73
            }
74

75
            var state = NativeMethods.Appender.DuckDBAppenderClose(nativeAppender);
45✔
76
            if (!state.IsSuccess())
45!
77
            {
UNCOV
78
                ThrowLastError(nativeAppender);
×
79
            }
80

81
            dataChunk.Dispose();
45✔
82
        }
45✔
83
        finally
84
        {
85
            nativeAppender.Close();
45✔
86
        }
45✔
87
    }
45✔
88

89
    public void Dispose()
90
    {
91
        if (!closed)
45✔
92
        {
93
            Close();
39✔
94
        }
95
    }
45✔
96

97
    private unsafe void InitVectorWriters()
98
    {
99
        for (long index = 0; index < vectorWriters.LongLength; index++)
522✔
100
        {
101
            var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(dataChunk, index);
207✔
102

103
            vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]);
207✔
104
        }
105
    }
54✔
106

107
    private void AppendDataChunk()
108
    {
109
        NativeMethods.DataChunks.DuckDBDataChunkSetSize(dataChunk, rowCount);
99✔
110
        var state = NativeMethods.Appender.DuckDBAppendDataChunk(nativeAppender, dataChunk);
99✔
111

112
        if (!state.IsSuccess())
99!
113
        {
NEW
114
            ThrowLastError(nativeAppender);
×
115
        }
116

117
        NativeMethods.DataChunks.DuckDBDataChunkReset(dataChunk);
99✔
118
    }
99✔
119

120
    [DoesNotReturn]
121
    [StackTraceHidden]
122
    internal static void ThrowLastError(Native.DuckDBAppender appender)
123
    {
124
        var errorMessage = NativeMethods.Appender.DuckDBAppenderError(appender).ToManagedString(false);
3✔
125

126
        throw new DuckDBException(errorMessage);
3✔
127
    }
128
}
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

© 2025 Coveralls, Inc