---
layout: docu
redirect_from:
- /docs/api/c/data_chunk
- /docs/api/c/data_chunk/
- /docs/clients/c/data_chunk
title: Data Chunks
---

<!-- markdownlint-disable MD001 -->

Data chunks represent a horizontal slice of a table. They hold a number of [vectors]({% link docs/stable/clients/c/vector.md %}), that can each hold up to the `VECTOR_SIZE` rows. The vector size can be obtained through the `duckdb_vector_size` function and is configurable, but is usually set to `2048`.

Data chunks and vectors are what DuckDB uses natively to store and represent data. For this reason, the data chunk interface is the most efficient way of interfacing with DuckDB. Be aware, however, that correctly interfacing with DuckDB using the data chunk API does require knowledge of DuckDB's internal vector format.

Data chunks can be used in two manners:

* **Reading Data**: Data chunks can be obtained from query results using the `duckdb_fetch_chunk` method, or as input to a user-defined function. In this case, the [vector methods]({% link docs/stable/clients/c/vector.md %}) can be used to read individual values.
* **Writing Data**: Data chunks can be created using `duckdb_create_data_chunk`. The data chunk can then be filled with values and used in `duckdb_append_data_chunk` to write data to the database.

The primary manner of interfacing with data chunks is by obtaining the internal vectors of the data chunk using the `duckdb_data_chunk_get_vector` method. Afterwards, the [vector methods]({% link docs/stable/clients/c/vector.md %}) can be used to read from or write to the individual vectors.

## API Reference Overview

<!-- This section is generated by scripts/generate_c_api_docs.py -->

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">duckdb_data_chunk</span> <a href="#duckdb_create_data_chunk"><span class="nf">duckdb_create_data_chunk</span></a>(<span class="kt">duckdb_logical_type</span> *<span class="nv">types</span>, <span class="kt">idx_t</span> <span class="nv">column_count</span>);
<span class="kt">void</span> <a href="#duckdb_destroy_data_chunk"><span class="nf">duckdb_destroy_data_chunk</span></a>(<span class="kt">duckdb_data_chunk</span> *<span class="nv">chunk</span>);
<span class="kt">void</span> <a href="#duckdb_data_chunk_reset"><span class="nf">duckdb_data_chunk_reset</span></a>(<span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>);
<span class="kt">idx_t</span> <a href="#duckdb_data_chunk_get_column_count"><span class="nf">duckdb_data_chunk_get_column_count</span></a>(<span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>);
<span class="kt">duckdb_vector</span> <a href="#duckdb_data_chunk_get_vector"><span class="nf">duckdb_data_chunk_get_vector</span></a>(<span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>, <span class="kt">idx_t</span> <span class="nv">col_idx</span>);
<span class="kt">idx_t</span> <a href="#duckdb_data_chunk_get_size"><span class="nf">duckdb_data_chunk_get_size</span></a>(<span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>);
<span class="kt">void</span> <a href="#duckdb_data_chunk_set_size"><span class="nf">duckdb_data_chunk_set_size</span></a>(<span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>, <span class="kt">idx_t</span> <span class="nv">size</span>);
</code></pre></div></div>

#### `duckdb_create_data_chunk`

Creates an empty data chunk with the specified column types.
The result must be destroyed with `duckdb_destroy_data_chunk`.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">duckdb_data_chunk</span> <span class="nv">duckdb_create_data_chunk</span>(<span class="nv">
</span>  <span class="kt">duckdb_logical_type</span> *<span class="nv">types</span>,<span class="nv">
</span>  <span class="kt">idx_t</span> <span class="nv">column_count
</span>);
</code></pre></div></div>

##### Parameters

* `types`: An array of column types. Column types can not contain ANY and INVALID types.
* `column_count`: The number of columns.

##### Return Value

The data chunk.

<br>

#### `duckdb_destroy_data_chunk`

Destroys the data chunk and de-allocates all memory allocated for that chunk.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nv">duckdb_destroy_data_chunk</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> *<span class="nv">chunk
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to destroy.

<br>

#### `duckdb_data_chunk_reset`

Resets a data chunk, clearing the validity masks and setting the cardinality of the data chunk to 0.
After calling this method, you must call `duckdb_vector_get_validity` and `duckdb_vector_get_data` to obtain current
data and validity pointers

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nv">duckdb_data_chunk_reset</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> <span class="nv">chunk
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to reset.

<br>

#### `duckdb_data_chunk_get_column_count`

Retrieves the number of columns in a data chunk.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">idx_t</span> <span class="nv">duckdb_data_chunk_get_column_count</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> <span class="nv">chunk
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to get the data from

##### Return Value

The number of columns in the data chunk

<br>

#### `duckdb_data_chunk_get_vector`

Retrieves the vector at the specified column index in the data chunk.

The pointer to the vector is valid for as long as the chunk is alive.
It does NOT need to be destroyed.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">duckdb_vector</span> <span class="nv">duckdb_data_chunk_get_vector</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>,<span class="nv">
</span>  <span class="kt">idx_t</span> <span class="nv">col_idx
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to get the data from

##### Return Value

The vector

<br>

#### `duckdb_data_chunk_get_size`

Retrieves the current number of tuples in a data chunk.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">idx_t</span> <span class="nv">duckdb_data_chunk_get_size</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> <span class="nv">chunk
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to get the data from

##### Return Value

The number of tuples in the data chunk

<br>

#### `duckdb_data_chunk_set_size`

Sets the current number of tuples in a data chunk.

##### Syntax

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nv">duckdb_data_chunk_set_size</span>(<span class="nv">
</span>  <span class="kt">duckdb_data_chunk</span> <span class="nv">chunk</span>,<span class="nv">
</span>  <span class="kt">idx_t</span> <span class="nv">size
</span>);
</code></pre></div></div>

##### Parameters

* `chunk`: The data chunk to set the size in
* `size`: The number of tuples in the data chunk

<br>