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

Giorgi / DuckDB.NET / 10762260192

08 Sep 2024 06:40PM UTC coverage: 90.033% (-0.2%) from 90.226%
10762260192

push

github

Giorgi
Merge branch 'nightly-varint' into nightly-build

886 of 1013 branches covered (87.46%)

Branch coverage included in aggregate %.

45 of 48 new or added lines in 2 files covered. (93.75%)

32 existing lines in 5 files now uncovered.

1833 of 2007 relevant lines covered (91.33%)

876732.29 hits per line

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

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

8
namespace DuckDB.NET.Data;
9

10
public class DuckDBAppender : IDisposable
11
{
12
    private bool closed;
13
    private readonly Native.DuckDBAppender nativeAppender;
14
    private readonly string qualifiedTableName;
15

16
    private ulong rowCount;
17

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

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

27
        var columnCount = NativeMethods.Appender.DuckDBAppenderColumnCount(nativeAppender);
150✔
28

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

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

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

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

49
        if (rowCount % DuckDBGlobalData.VectorSize == 0)
252,363✔
50
        {
51
            AppendDataChunk();
171✔
52

53
            InitVectorWriters();
171✔
54

55
            rowCount = 0;
171✔
56
        }
57

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

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

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

70
            foreach (var logicalType in logicalTypes)
1,290✔
71
            {
72
                logicalType.Dispose();
498✔
73
            }
74

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

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

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

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

103
            if (vectorWriters[index] == null)
546✔
104
            {
105
                vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]);
498✔
106
            }
107
            else
108
            {
109
                vectorWriters[index].InitializerWriter();
48✔
110
            }
111
        }
112
    }
171✔
113

114
    private void AppendDataChunk()
115
    {
116
        NativeMethods.DataChunks.DuckDBDataChunkSetSize(dataChunk, rowCount);
318✔
117
        var state = NativeMethods.Appender.DuckDBAppendDataChunk(nativeAppender, dataChunk);
318✔
118

119
        if (!state.IsSuccess())
318!
120
        {
UNCOV
121
            ThrowLastError(nativeAppender);
×
122
        }
123

124
        NativeMethods.DataChunks.DuckDBDataChunkReset(dataChunk);
318✔
125
    }
318✔
126

127
    [DoesNotReturn]
128
    [StackTraceHidden]
129
    internal static void ThrowLastError(Native.DuckDBAppender appender)
130
    {
131
        var errorMessage = NativeMethods.Appender.DuckDBAppenderError(appender).ToManagedString(false);
3✔
132

133
        throw new DuckDBException(errorMessage);
3✔
134
    }
135
}
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