h1
stringclasses
12 values
h2
stringclasses
93 values
h3
stringclasses
960 values
h5
stringlengths
0
79
content
stringlengths
24
533k
tokens
int64
7
158k
Data Import
Appender
Appender Support in Other Clients
The Appender is also available in the following client APIs: * [C](#docs:api:c:appender) * [Go](#docs:api:go::appender) * [Julia](#docs:api:julia::appender-api) * [JDBC (Java)](#docs:api:java::appender) * [Rust](#docs:api:rust::appender)
88
Data Import
INSERT Statements
`INSERT` statements are the standard way of loading data into a relational database. When using `INSERT` statements, the values are supplied row-by-row. While simple, there is significant overhead involved in parsing and processing individual `INSERT` statements. This makes lots of individual row-by-row insertions very inefficient for bulk insertion. > **Best practice. ** As a rule-of-thumb, avoid using lots of individual row-by-row `INSERT` statements when inserting more than a few rows (i.e., avoid using `INSERT` statements as part of a loop). When bulk inserting data, try to maximize the amount of data that is inserted per statement. If you must use `INSERT` statements to load data in a loop, avoid executing the statements in auto-commit mode. After every commit, the database is required to sync the changes made to disk to ensure no data is lost. In auto-commit mode every single statement will be wrapped in a separate transaction, meaning `fsync` will be called for every statement. This is typically unnecessary when bulk loading and will significantly slow down your program. > **Tip. ** If you absolutely must use `INSERT` statements in a loop to load data, wrap them in calls to `BEGIN TRANSACTION` and `COMMIT`.
258
Data Import
INSERT Statements
Syntax
An example of using `INSERT INTO` to load data in a table is as follows: ```sql CREATE TABLE people (id INTEGER, name VARCHAR); INSERT INTO people VALUES (1, 'Mark'), (2, 'Hannes'); ``` For a more detailed description together with syntax diagram can be found, see the [page on the `INSERT` statement](#docs:sql:statements:insert).
84
Client APIs
Client APIs Overview
DuckDB is an in-process database system and offers client APIs for several languages. These clients support the same DuckDB file format and SQL syntax. We strived to make their APIs follow their host language's conventions. Client APIs: * Standalone [Command Line Interface (CLI)](#docs:api:cli:overview) client * [ADBC API](#docs:api:adbc) * [C](#docs:api:c:overview) * [C#](https://github.com/Giorgi/DuckDB.NET) by [Giorgi](https://github.com/Giorgi) * [C++](#docs:api:cpp) * [Common Lisp](https://github.com/ak-coram/cl-duckdb) by [ak-coram](https://github.com/ak-coram) * [Crystal](https://github.com/amauryt/crystal-duckdb) by [amauryt](https://github.com/amauryt) * [Dart](#docs:api:dart) by [TigerEye](https://www.tigereye.com/) * [Elixir](https://github.com/AlexR2D2/duckdbex) by [AlexR2D2](https://github.com/AlexR2D2/duckdbex) * [Go](#docs:api:go) by [marcboeker](https://github.com/marcboeker) * [Java](#docs:api:java) * [Julia](#docs:api:julia) * [Node.js](#docs:api:nodejs:overview) * [ODBC API](#docs:api:odbc:overview) * [Python](#docs:api:python:overview) * [R](#docs:api:r) * [Ruby](https://github.com/suketa/ruby-duckdb) by [suketa](https://github.com/suketa) * [Rust](#docs:api:rust) * [Swift](#docs:api:swift) * [WebAssembly (Wasm)](#docs:api:wasm:overview) * [Zig](https://github.com/karlseguin/zuckdb.zig) by [karlseguin](https://github.com/karlseguin)
490
Client APIs
C
Overview
DuckDB implements a custom C API modelled somewhat following the SQLite C API. The API is contained in the `duckdb.h` header. Continue to [Startup & Shutdown](#docs:api:c:connect) to get started, or check out the [Full API overview](#docs:api:c:api). We also provide a SQLite API wrapper which means that if your applications is programmed against the SQLite C API, you can re-link to DuckDB and it should continue working. See the [`sqlite_api_wrapper`](https://github.com/duckdb/duckdb/tree/main/tools/sqlite3_api_wrapper) folder in our source repository for more information.
136
Client APIs
C
Installation
The DuckDB C API can be installed as part of the `libduckdb` packages. Please see the [installation page](https://duckdb.org/docs/installation/) for details.
38
Client APIs
C
Startup & Shutdown
To use DuckDB, you must first initialize a `duckdb_database` handle using `duckdb_open()`. `duckdb_open()` takes as parameter the database file to read and write from. The special value `NULL` (` nullptr`) can be used to create an **in-memory database**. Note that for an in-memory database no data is persisted to disk (i.e., all data is lost when you exit the process). With the `duckdb_database` handle, you can create one or many `duckdb_connection` using `duckdb_connect()`. While individual connections are thread-safe, they will be locked during querying. It is therefore recommended that each thread uses its own connection to allow for the best parallel performance. All `duckdb_connection`s have to explicitly be disconnected with `duckdb_disconnect()` and the `duckdb_database` has to be explicitly closed with `duckdb_close()` to avoid memory and file handle leaking.
193
Client APIs
C
Example
```c duckdb_database db; duckdb_connection con; if (duckdb_open(NULL, &db) == DuckDBError) { // handle error } if (duckdb_connect(db, &con) == DuckDBError) { // handle error } // run queries... // cleanup duckdb_disconnect(&con); duckdb_close(&db); ```
75
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c duckdb_state duckdb_open(const char *path, duckdb_database *out_database); duckdb_state duckdb_open_ext(const char *path, duckdb_database *out_database, duckdb_config config, char **out_error); void duckdb_close(duckdb_database *database); duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out_connection); void duckdb_interrupt(duckdb_connection connection); duckdb_query_progress_type duckdb_query_progress(duckdb_connection connection); void duckdb_disconnect(duckdb_connection *connection); const char *duckdb_library_version(); ```
140
Client APIs
C
API Reference Overview
`duckdb_open`
Creates a new database or opens an existing database file stored at the given path. If no path is given a new in-memory database is created instead. The instantiated database should be closed with 'duckdb_close'. ###### Syntax {#docs:api:c:connect::syntax} ```c duckdb_state duckdb_open( const char *path, duckdb_database *out_database ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `path`: Path to the database file on disk, or `nullptr` or `:memory:` to open an in-memory database. * `out_database`: The result database object. ###### Return Value {#docs:api:c:connect::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
172
Client APIs
C
API Reference Overview
`duckdb_open_ext`
Extended version of duckdb_open. Creates a new database or opens an existing database file stored at the given path. The instantiated database should be closed with 'duckdb_close'. ###### Syntax {#docs:api:c:connect::syntax} ```c duckdb_state duckdb_open_ext( const char *path, duckdb_database *out_database, duckdb_config config, char **out_error ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `path`: Path to the database file on disk, or `nullptr` or `:memory:` to open an in-memory database. * `out_database`: The result database object. * `config`: (Optional) configuration used to start up the database system. * `out_error`: If set and the function returns DuckDBError, this will contain the reason why the start-up failed. Note that the error must be freed using `duckdb_free`. ###### Return Value {#docs:api:c:connect::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
231
Client APIs
C
API Reference Overview
`duckdb_close`
Closes the specified database and de-allocates all memory allocated for that database. This should be called after you are done with any database allocated through `duckdb_open` or `duckdb_open_ext`. Note that failing to call `duckdb_close` (in case of e.g., a program crash) will not cause data corruption. Still, it is recommended to always correctly close a database object after you are done with it. ###### Syntax {#docs:api:c:connect::syntax} ```c void duckdb_close( duckdb_database *database ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `database`: The database object to shut down. <br>
148
Client APIs
C
API Reference Overview
`duckdb_connect`
Opens a connection to a database. Connections are required to query the database, and store transactional state associated with the connection. The instantiated connection should be closed using 'duckdb_disconnect'. ###### Syntax {#docs:api:c:connect::syntax} ```c duckdb_state duckdb_connect( duckdb_database database, duckdb_connection *out_connection ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `database`: The database file to connect to. * `out_connection`: The result connection object. ###### Return Value {#docs:api:c:connect::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
151
Client APIs
C
API Reference Overview
`duckdb_interrupt`
Interrupt running query ###### Syntax {#docs:api:c:connect::syntax} ```c void duckdb_interrupt( duckdb_connection connection ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `connection`: The connection to interrupt <br>
59
Client APIs
C
API Reference Overview
`duckdb_query_progress`
Get progress of the running query ###### Syntax {#docs:api:c:connect::syntax} ```c duckdb_query_progress_type duckdb_query_progress( duckdb_connection connection ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `connection`: The working connection ###### Return Value {#docs:api:c:connect::return-value} -1 if no progress or a percentage of the progress <br>
94
Client APIs
C
API Reference Overview
`duckdb_disconnect`
Closes the specified connection and de-allocates all memory allocated for that connection. ###### Syntax {#docs:api:c:connect::syntax} ```c void duckdb_disconnect( duckdb_connection *connection ); ``` ###### Parameters {#docs:api:c:connect::parameters} * `connection`: The connection to close. <br>
75
Client APIs
C
API Reference Overview
`duckdb_library_version`
Returns the version of the linked DuckDB, with a version postfix for dev versions Usually used for developing C extensions that must return this for a compatibility check. ###### Syntax {#docs:api:c:connect::syntax} ```c const char *duckdb_library_version( ); ``` <br>
63
Client APIs
C
Configuration
Configuration options can be provided to change different settings of the database system. Note that many of these settings can be changed later on using [`PRAGMA` statements](#..:..:configuration:pragmas) as well. The configuration object should be created, filled with values and passed to `duckdb_open_ext`.
67
Client APIs
C
Example
```c duckdb_database db; duckdb_config config; // create the configuration object if (duckdb_create_config(&config) == DuckDBError) { // handle error } // set some configuration options duckdb_set_config(config, "access_mode", "READ_WRITE"); // or READ_ONLY duckdb_set_config(config, "threads", "8"); duckdb_set_config(config, "max_memory", "8GB"); duckdb_set_config(config, "default_order", "DESC"); // open the database using the configuration if (duckdb_open_ext(NULL, &db, config, NULL) == DuckDBError) { // handle error } // cleanup the configuration object duckdb_destroy_config(&config); // run queries... // cleanup duckdb_close(&db); ```
164
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c duckdb_state duckdb_create_config(duckdb_config *out_config); size_t duckdb_config_count(); duckdb_state duckdb_get_config_flag(size_t index, const char **out_name, const char **out_description); duckdb_state duckdb_set_config(duckdb_config config, const char *name, const char *option); void duckdb_destroy_config(duckdb_config *config); ```
100
Client APIs
C
API Reference Overview
`duckdb_create_config`
Initializes an empty configuration object that can be used to provide start-up options for the DuckDB instance through `duckdb_open_ext`. The duckdb_config must be destroyed using 'duckdb_destroy_config' This will always succeed unless there is a malloc failure. Note that `duckdb_destroy_config` should always be called on the resulting config, even if the function returns `DuckDBError`. ###### Syntax {#docs:api:c:config::syntax} ```c duckdb_state duckdb_create_config( duckdb_config *out_config ); ``` ###### Parameters {#docs:api:c:config::parameters} * `out_config`: The result configuration object. ###### Return Value {#docs:api:c:config::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
182
Client APIs
C
API Reference Overview
`duckdb_config_count`
This returns the total amount of configuration options available for usage with `duckdb_get_config_flag`. This should not be called in a loop as it internally loops over all the options. ###### Return Value {#docs:api:c:config::return-value} The amount of config options available. ###### Syntax {#docs:api:c:config::syntax} ```c size_t duckdb_config_count( ); ``` <br>
91
Client APIs
C
API Reference Overview
`duckdb_get_config_flag`
Obtains a human-readable name and description of a specific configuration option. This can be used to e.g. display configuration options. This will succeed unless `index` is out of range (i.e., `>= duckdb_config_count`). The result name or description MUST NOT be freed. ###### Syntax {#docs:api:c:config::syntax} ```c duckdb_state duckdb_get_config_flag( size_t index, const char **out_name, const char **out_description ); ``` ###### Parameters {#docs:api:c:config::parameters} * `index`: The index of the configuration option (between 0 and `duckdb_config_count`) * `out_name`: A name of the configuration flag. * `out_description`: A description of the configuration flag. ###### Return Value {#docs:api:c:config::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
203
Client APIs
C
API Reference Overview
`duckdb_set_config`
Sets the specified option for the specified configuration. The configuration option is indicated by name. To obtain a list of config options, see `duckdb_get_config_flag`. In the source code, configuration options are defined in `config.cpp`. This can fail if either the name is invalid, or if the value provided for the option is invalid. ###### Syntax {#docs:api:c:config::syntax} ```c duckdb_state duckdb_set_config( duckdb_config config, const char *name, const char *option ); ``` ###### Parameters {#docs:api:c:config::parameters} * `config`: The configuration object to set the option on. * `name`: The name of the configuration flag to set. * `option`: The value to set the configuration flag to. ###### Return Value {#docs:api:c:config::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
205
Client APIs
C
API Reference Overview
`duckdb_destroy_config`
Destroys the specified configuration object and de-allocates all memory allocated for the object. ###### Syntax {#docs:api:c:config::syntax} ```c void duckdb_destroy_config( duckdb_config *config ); ``` ###### Parameters {#docs:api:c:config::parameters} * `config`: The configuration object to destroy. <br>
78
Client APIs
C
Query
The `duckdb_query` method allows SQL queries to be run in DuckDB from C. This method takes two parameters, a (null-terminated) SQL query string and a `duckdb_result` result pointer. The result pointer may be `NULL` if the application is not interested in the result set or if the query produces no result. After the result is consumed, the `duckdb_destroy_result` method should be used to clean up the result. Elements can be extracted from the `duckdb_result` object using a variety of methods. The `duckdb_column_count` can be used to extract the number of columns. `duckdb_column_name` and `duckdb_column_type` can be used to extract the names and types of individual columns.
155
Client APIs
C
Example
```c duckdb_state state; duckdb_result result; // create a table state = duckdb_query(con, "CREATE TABLE integers (i INTEGER, j INTEGER);", NULL); if (state == DuckDBError) { // handle error } // insert three rows into the table state = duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL); if (state == DuckDBError) { // handle error } // query rows again state = duckdb_query(con, "SELECT * FROM integers", &result); if (state == DuckDBError) { // handle error } // handle the result // ... // destroy the result after we are done with it duckdb_destroy_result(&result); ```
168
Client APIs
C
Value Extraction
Values can be extracted using either the `duckdb_fetch_chunk` function, or using the `duckdb_value` convenience functions. The `duckdb_fetch_chunk` function directly hands you data chunks in DuckDB's native array format and can therefore be very fast. The `duckdb_value` functions perform bounds- and type-checking, and will automatically cast values to the desired type. This makes them more convenient and easier to use, at the expense of being slower. See the [Types](#docs:api:c:types) page for more information. > For optimal performance, use `duckdb_fetch_chunk` to extract data from the query result. > The `duckdb_value` functions perform internal type-checking, bounds-checking and casting which makes them slower. #### `duckdb_fetch_chunk` {#docs:api:c:query::duckdb_fetch_chunk} Below is an end-to-end example that prints the above result to CSV format using the `duckdb_fetch_chunk` function. Note that the function is NOT generic: we do need to know exactly what the types of the result columns are. ```c duckdb_database db; duckdb_connection con; duckdb_open(nullptr, &db); duckdb_connect(db, &con); duckdb_result res; duckdb_query(con, "CREATE TABLE integers (i INTEGER, j INTEGER);", NULL); duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL); duckdb_query(con, "SELECT * FROM integers;", &res); // iterate until result is exhausted while (true) { duckdb_data_chunk result = duckdb_fetch_chunk(res); if (!result) { // result is exhausted break; } // get the number of rows from the data chunk idx_t row_count = duckdb_data_chunk_get_size(result); // get the first column duckdb_vector col1 = duckdb_data_chunk_get_vector(result, 0); int32_t *col1_data = (int32_t *) duckdb_vector_get_data(col1); uint64_t *col1_validity = duckdb_vector_get_validity(col1); // get the second column duckdb_vector col2 = duckdb_data_chunk_get_vector(result, 1); int32_t *col2_data = (int32_t *) duckdb_vector_get_data(col2); uint64_t *col2_validity = duckdb_vector_get_validity(col2); // iterate over the rows for (idx_t row = 0; row < row_count; row++) { if (duckdb_validity_row_is_valid(col1_validity, row)) { printf("%d", col1_data[row]); } else { printf("NULL"); } printf(","); if (duckdb_validity_row_is_valid(col2_validity, row)) { printf("%d", col2_data[row]); } else { printf("NULL"); } printf("\n"); } duckdb_destroy_data_chunk(&result); } // clean-up duckdb_destroy_result(&res); duckdb_disconnect(&con); duckdb_close(&db); ``` This prints the following result: ```csv 3,4 5,6 7,NULL ``` #### `duckdb_value` {#docs:api:c:query::duckdb_value} > **Deprecated. ** The `duckdb_value` functions are deprecated and are scheduled for removal in a future release. Below is an example that prints the above result to CSV format using the `duckdb_value_varchar` function. Note that the function is generic: we do not need to know about the types of the individual result columns. ```c // print the above result to CSV format using `duckdb_value_varchar` idx_t row_count = duckdb_row_count(&result); idx_t column_count = duckdb_column_count(&result); for (idx_t row = 0; row < row_count; row++) { for (idx_t col = 0; col < column_count; col++) { if (col > 0) printf(","); auto str_val = duckdb_value_varchar(&result, col, row); printf("%s", str_val); duckdb_free(str_val); } printf("\n"); } ```
882
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c duckdb_state duckdb_query(duckdb_connection connection, const char *query, duckdb_result *out_result); void duckdb_destroy_result(duckdb_result *result); const char *duckdb_column_name(duckdb_result *result, idx_t col); duckdb_type duckdb_column_type(duckdb_result *result, idx_t col); duckdb_statement_type duckdb_result_statement_type(duckdb_result result); duckdb_logical_type duckdb_column_logical_type(duckdb_result *result, idx_t col); idx_t duckdb_column_count(duckdb_result *result); idx_t duckdb_row_count(duckdb_result *result); idx_t duckdb_rows_changed(duckdb_result *result); void *duckdb_column_data(duckdb_result *result, idx_t col); bool *duckdb_nullmask_data(duckdb_result *result, idx_t col); const char *duckdb_result_error(duckdb_result *result); duckdb_error_type duckdb_result_error_type(duckdb_result *result); ```
230
Client APIs
C
API Reference Overview
`duckdb_query`
Executes a SQL query within a connection and stores the full (materialized) result in the out_result pointer. If the query fails to execute, DuckDBError is returned and the error message can be retrieved by calling `duckdb_result_error`. Note that after running `duckdb_query`, `duckdb_destroy_result` must be called on the result object even if the query fails, otherwise the error stored within the result will not be freed correctly. ###### Syntax {#docs:api:c:query::syntax} ```c duckdb_state duckdb_query( duckdb_connection connection, const char *query, duckdb_result *out_result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `connection`: The connection to perform the query in. * `query`: The SQL query to run. * `out_result`: The query result. ###### Return Value {#docs:api:c:query::return-value} `DuckDBSuccess` on success or `DuckDBError` on failure. <br>
222
Client APIs
C
API Reference Overview
`duckdb_destroy_result`
Closes the result and de-allocates all memory allocated for that connection. ###### Syntax {#docs:api:c:query::syntax} ```c void duckdb_destroy_result( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result to destroy. <br>
75
Client APIs
C
API Reference Overview
`duckdb_column_name`
Returns the column name of the specified column. The result should not need to be freed; the column names will automatically be destroyed when the result is destroyed. Returns `NULL` if the column is out of range. ###### Syntax {#docs:api:c:query::syntax} ```c const char *duckdb_column_name( duckdb_result *result, idx_t col ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the column name from. * `col`: The column index. ###### Return Value {#docs:api:c:query::return-value} The column name of the specified column. <br>
149
Client APIs
C
API Reference Overview
`duckdb_column_type`
Returns the column type of the specified column. Returns `DUCKDB_TYPE_INVALID` if the column is out of range. ###### Syntax {#docs:api:c:query::syntax} ```c duckdb_type duckdb_column_type( duckdb_result *result, idx_t col ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the column type from. * `col`: The column index. ###### Return Value {#docs:api:c:query::return-value} The column type of the specified column. <br>
129
Client APIs
C
API Reference Overview
`duckdb_result_statement_type`
Returns the statement type of the statement that was executed ###### Syntax {#docs:api:c:query::syntax} ```c duckdb_statement_type duckdb_result_statement_type( duckdb_result result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the statement type from. ###### Return Value {#docs:api:c:query::return-value} duckdb_statement_type value or DUCKDB_STATEMENT_TYPE_INVALID <br>
107
Client APIs
C
API Reference Overview
`duckdb_column_logical_type`
Returns the logical column type of the specified column. The return type of this call should be destroyed with `duckdb_destroy_logical_type`. Returns `NULL` if the column is out of range. ###### Syntax {#docs:api:c:query::syntax} ```c duckdb_logical_type duckdb_column_logical_type( duckdb_result *result, idx_t col ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the column type from. * `col`: The column index. ###### Return Value {#docs:api:c:query::return-value} The logical column type of the specified column. <br>
147
Client APIs
C
API Reference Overview
`duckdb_column_count`
Returns the number of columns present in a the result object. ###### Syntax {#docs:api:c:query::syntax} ```c idx_t duckdb_column_count( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object. ###### Return Value {#docs:api:c:query::return-value} The number of columns present in the result object. <br>
98
Client APIs
C
API Reference Overview
`duckdb_row_count`
> **Warning. ** Deprecation notice. This method is scheduled for removal in a future release. Returns the number of rows present in the result object. ###### Syntax {#docs:api:c:query::syntax} ```c idx_t duckdb_row_count( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object. ###### Return Value {#docs:api:c:query::return-value} The number of rows present in the result object. <br>
120
Client APIs
C
API Reference Overview
`duckdb_rows_changed`
Returns the number of rows changed by the query stored in the result. This is relevant only for INSERT/UPDATE/DELETE queries. For other queries the rows_changed will be 0. ###### Syntax {#docs:api:c:query::syntax} ```c idx_t duckdb_rows_changed( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object. ###### Return Value {#docs:api:c:query::return-value} The number of rows changed. <br>
120
Client APIs
C
API Reference Overview
`duckdb_column_data`
> **Deprecated. ** This method has been deprecated. Prefer using `duckdb_result_get_chunk` instead. Returns the data of a specific column of a result in columnar format. The function returns a dense array which contains the result data. The exact type stored in the array depends on the corresponding duckdb_type (as provided by `duckdb_column_type`). For the exact type by which the data should be accessed, see the comments in [the types section](#types) or the `DUCKDB_TYPE` enum. For example, for a column of type `DUCKDB_TYPE_INTEGER`, rows can be accessed in the following manner: ```c int32_t *data = (int32_t *) duckdb_column_data(&result, 0); printf("Data for row %d: %d\n", row, data[row]); ``` ###### Syntax {#docs:api:c:query::syntax} ```c void *duckdb_column_data( duckdb_result *result, idx_t col ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the column data from. * `col`: The column index. ###### Return Value {#docs:api:c:query::return-value} The column data of the specified column. <br>
284
Client APIs
C
API Reference Overview
`duckdb_nullmask_data`
> **Deprecated. ** This method has been deprecated. Prefer using `duckdb_result_get_chunk` instead. Returns the nullmask of a specific column of a result in columnar format. The nullmask indicates for every row whether or not the corresponding row is `NULL`. If a row is `NULL`, the values present in the array provided by `duckdb_column_data` are undefined. ```c int32_t *data = (int32_t *) duckdb_column_data(&result, 0); bool *nullmask = duckdb_nullmask_data(&result, 0); if (nullmask[row]) { printf("Data for row %d: NULL\n", row); } else { printf("Data for row %d: %d\n", row, data[row]); } ``` ###### Syntax {#docs:api:c:query::syntax} ```c bool *duckdb_nullmask_data( duckdb_result *result, idx_t col ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the nullmask from. * `col`: The column index. ###### Return Value {#docs:api:c:query::return-value} The nullmask of the specified column. <br>
269
Client APIs
C
API Reference Overview
`duckdb_result_error`
Returns the error message contained within the result. The error is only set if `duckdb_query` returns `DuckDBError`. The result of this function must not be freed. It will be cleaned up when `duckdb_destroy_result` is called. ###### Syntax {#docs:api:c:query::syntax} ```c const char *duckdb_result_error( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the error from. ###### Return Value {#docs:api:c:query::return-value} The error of the result. <br>
141
Client APIs
C
API Reference Overview
`duckdb_result_error_type`
Returns the result error type contained within the result. The error is only set if `duckdb_query` returns `DuckDBError`. ###### Syntax {#docs:api:c:query::syntax} ```c duckdb_error_type duckdb_result_error_type( duckdb_result *result ); ``` ###### Parameters {#docs:api:c:query::parameters} * `result`: The result object to fetch the error from. ###### Return Value {#docs:api:c:query::return-value} The error type of the result. <br>
120
Client APIs
C
Data Chunks
Data chunks represent a horizontal slice of a table. They hold a number of [vectors](#docs:api:c:vector), 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](#docs:api:c:vector) 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](#docs:api:c:vector) can be used to read from or write to the individual vectors.
306
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c duckdb_data_chunk duckdb_create_data_chunk(duckdb_logical_type *types, idx_t column_count); void duckdb_destroy_data_chunk(duckdb_data_chunk *chunk); void duckdb_data_chunk_reset(duckdb_data_chunk chunk); idx_t duckdb_data_chunk_get_column_count(duckdb_data_chunk chunk); duckdb_vector duckdb_data_chunk_get_vector(duckdb_data_chunk chunk, idx_t col_idx); idx_t duckdb_data_chunk_get_size(duckdb_data_chunk chunk); void duckdb_data_chunk_set_size(duckdb_data_chunk chunk, idx_t size); ```
138
Client APIs
C
API Reference Overview
`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 {#docs:api:c:data_chunk::syntax} ```c duckdb_data_chunk duckdb_create_data_chunk( duckdb_logical_type *types, idx_t column_count ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `types`: An array of column types. Column types can not contain ANY and INVALID types. * `column_count`: The number of columns. ###### Return Value {#docs:api:c:data_chunk::return-value} The data chunk. <br>
135
Client APIs
C
API Reference Overview
`duckdb_destroy_data_chunk`
Destroys the data chunk and de-allocates all memory allocated for that chunk. ###### Syntax {#docs:api:c:data_chunk::syntax} ```c void duckdb_destroy_data_chunk( duckdb_data_chunk *chunk ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to destroy. <br>
79
Client APIs
C
API Reference Overview
`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 {#docs:api:c:data_chunk::syntax} ```c void duckdb_data_chunk_reset( duckdb_data_chunk chunk ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to reset. <br>
116
Client APIs
C
API Reference Overview
`duckdb_data_chunk_get_column_count`
Retrieves the number of columns in a data chunk. ###### Syntax {#docs:api:c:data_chunk::syntax} ```c idx_t duckdb_data_chunk_get_column_count( duckdb_data_chunk chunk ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to get the data from ###### Return Value {#docs:api:c:data_chunk::return-value} The number of columns in the data chunk <br>
103
Client APIs
C
API Reference Overview
`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 {#docs:api:c:data_chunk::syntax} ```c duckdb_vector duckdb_data_chunk_get_vector( duckdb_data_chunk chunk, idx_t col_idx ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to get the data from ###### Return Value {#docs:api:c:data_chunk::return-value} The vector <br>
130
Client APIs
C
API Reference Overview
`duckdb_data_chunk_get_size`
Retrieves the current number of tuples in a data chunk. ###### Syntax {#docs:api:c:data_chunk::syntax} ```c idx_t duckdb_data_chunk_get_size( duckdb_data_chunk chunk ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to get the data from ###### Return Value {#docs:api:c:data_chunk::return-value} The number of tuples in the data chunk <br>
103
Client APIs
C
API Reference Overview
`duckdb_data_chunk_set_size`
Sets the current number of tuples in a data chunk. ###### Syntax {#docs:api:c:data_chunk::syntax} ```c void duckdb_data_chunk_set_size( duckdb_data_chunk chunk, idx_t size ); ``` ###### Parameters {#docs:api:c:data_chunk::parameters} * `chunk`: The data chunk to set the size in * `size`: The number of tuples in the data chunk <br>
92
Client APIs
C
Vectors
Vectors represent a horizontal slice of a column. They hold a number of values of a specific type, similar to an array. Vectors are the core data representation used in DuckDB. Vectors are typically stored within [data chunks](#docs:api:c:data_chunk). The vector and data chunk interfaces are the most efficient way of interacting with DuckDB, allowing for the highest performance. However, the interfaces are also difficult to use and care must be taken when using them.
97
Client APIs
C
Vector Format
Vectors are arrays of a specific data type. The logical type of a vector can be obtained using `duckdb_vector_get_column_type`. The type id of the logical type can then be obtained using `duckdb_get_type_id`. Vectors themselves do not have sizes. Instead, the parent data chunk has a size (that can be obtained through `duckdb_data_chunk_get_size`). All vectors that belong to a data chunk have the same size. #### Primitive Types {#docs:api:c:vector::primitive-types} For primitive types, the underlying array can be obtained using the `duckdb_vector_get_data` method. The array can then be accessed using the correct native type. Below is a table that contains a mapping of the `duckdb_type` to the native type of the array. <div class="narrow_table monospace_table"></div> | duckdb_type | NativeType | |--------------------------|------------------| | DUCKDB_TYPE_BOOLEAN | bool | | DUCKDB_TYPE_TINYINT | int8_t | | DUCKDB_TYPE_SMALLINT | int16_t | | DUCKDB_TYPE_INTEGER | int32_t | | DUCKDB_TYPE_BIGINT | int64_t | | DUCKDB_TYPE_UTINYINT | uint8_t | | DUCKDB_TYPE_USMALLINT | uint16_t | | DUCKDB_TYPE_UINTEGER | uint32_t | | DUCKDB_TYPE_UBIGINT | uint64_t | | DUCKDB_TYPE_FLOAT | float | | DUCKDB_TYPE_DOUBLE | double | | DUCKDB_TYPE_TIMESTAMP | duckdb_timestamp | | DUCKDB_TYPE_DATE | duckdb_date | | DUCKDB_TYPE_TIME | duckdb_time | | DUCKDB_TYPE_INTERVAL | duckdb_interval | | DUCKDB_TYPE_HUGEINT | duckdb_hugeint | | DUCKDB_TYPE_UHUGEINT | duckdb_uhugeint | | DUCKDB_TYPE_VARCHAR | duckdb_string_t | | DUCKDB_TYPE_BLOB | duckdb_string_t | | DUCKDB_TYPE_TIMESTAMP_S | duckdb_timestamp | | DUCKDB_TYPE_TIMESTAMP_MS | duckdb_timestamp | | DUCKDB_TYPE_TIMESTAMP_NS | duckdb_timestamp | | DUCKDB_TYPE_UUID | duckdb_hugeint | | DUCKDB_TYPE_TIME_TZ | duckdb_time_tz | | DUCKDB_TYPE_TIMESTAMP_TZ | duckdb_timestamp | #### Null Values {#docs:api:c:vector::null-values} Any value in a vector can be `NULL`. When a value is `NULL`, the values contained within the primary array at that index is undefined (and can be uninitialized). The validity mask is a bitmask consisting of `uint64_t` elements. For every `64` values in the vector, one `uint64_t` element exists (rounded up). The validity mask has its bit set to 1 if the value is valid, or set to 0 if the value is invalid (i.e .`NULL`). The bits of the bitmask can be read directly, or the slower helper method `duckdb_validity_row_is_valid` can be used to check whether or not a value is `NULL`. The `duckdb_vector_get_validity` returns a pointer to the validity mask. Note that if all values in a vector are valid, this function **might** return `nullptr` in which case the validity mask does not need to be checked. #### Strings {#docs:api:c:vector::strings} String values are stored as a `duckdb_string_t`. This is a special struct that stores the string inline (if it is short, i.e., `<= 12 bytes`) or a pointer to the string data if it is longer than `12` bytes. ```c typedef struct { union { struct { uint32_t length; char prefix[4]; char *ptr; } pointer; struct { uint32_t length; char inlined[12]; } inlined; } value; } duckdb_string_t; ``` The length can either be accessed directly, or the `duckdb_string_is_inlined` can be used to check if a string is inlined. #### Decimals {#docs:api:c:vector::decimals} Decimals are stored as integer values internally. The exact native type depends on the `width` of the decimal type, as shown in the following table: <div class="narrow_table monospace_table"></div> | Width | NativeType | |-------|----------------| | <= 4 | int16_t | | <= 9 | int32_t | | <= 18 | int64_t | | <= 38 | duckdb_hugeint | The `duckdb_decimal_internal_type` can be used to obtain the internal type of the decimal. Decimals are stored as integer values multiplied by `10^scale`. The scale of a decimal can be obtained using `duckdb_decimal_scale`. For example, a decimal value of `10.5` with type `DECIMAL(8, 3)` is stored internally as an `int32_t` value of `10500`. In order to obtain the correct decimal value, the value should be divided by the appropriate power-of-ten. #### Enums {#docs:api:c:vector::enums} Enums are stored as unsigned integer values internally. The exact native type depends on the size of the enum dictionary, as shown in the following table: <div class="narrow_table monospace_table"></div> | Dictionary Size | NativeType | |-----------------|------------| | <= 255 | uint8_t | | <= 65535 | uint16_t | | <= 4294967295 | uint32_t | The `duckdb_enum_internal_type` can be used to obtain the internal type of the enum. In order to obtain the actual string value of the enum, the `duckdb_enum_dictionary_value` function must be used to obtain the enum value that corresponds to the given dictionary entry. Note that the enum dictionary is the same for the entire column - and so only needs to be constructed once. #### Structs {#docs:api:c:vector::structs} Structs are nested types that contain any number of child types. Think of them like a `struct` in C. The way to access struct data using vectors is to access the child vectors recursively using the `duckdb_struct_vector_get_child` method. The struct vector itself does not have any data (i.e., you should not use `duckdb_vector_get_data` method on the struct). **However**, the struct vector itself **does** have a validity mask. The reason for this is that the child elements of a struct can be `NULL`, but the struct **itself** can also be `NULL`. #### Lists {#docs:api:c:vector::lists} Lists are nested types that contain a single child type, repeated `x` times per row. Think of them like a variable-length array in C. The way to access list data using vectors is to access the child vector using the `duckdb_list_vector_get_child` method. The `duckdb_vector_get_data` must be used to get the offsets and lengths of the lists stored as `duckdb_list_entry`, that can then be applied to the child vector. ```c typedef struct { uint64_t offset; uint64_t length; } duckdb_list_entry; ``` Note that both list entries itself **and** any children stored in the lists can also be `NULL`. This must be checked using the validity mask again. #### Arrays {#docs:api:c:vector::arrays} Arrays are nested types that contain a single child type, repeated exactly `array_size` times per row. Think of them like a fixed-size array in C. Arrays work exactly the same as lists, **except** the length and offset of each entry is fixed. The fixed array size can be obtained by using `duckdb_array_type_array_size`. The data for entry `n` then resides at `offset = n * array_size`, and always has `length = array_size`. Note that much like lists, arrays can still be `NULL`, which must be checked using the validity mask.
1,774
Client APIs
C
Examples
Below are several full end-to-end examples of how to interact with vectors. #### Example: Reading an int64 Vector with `NULL` Values {#docs:api:c:vector::example-reading-an-int64-vector-with-null-values} ```c duckdb_database db; duckdb_connection con; duckdb_open(nullptr, &db); duckdb_connect(db, &con); duckdb_result res; duckdb_query(con, "SELECT CASE WHEN i%2=0 THEN NULL ELSE i END res_col FROM range(10) t(i)", &res); // iterate until result is exhausted while (true) { duckdb_data_chunk result = duckdb_fetch_chunk(res); if (!result) { // result is exhausted break; } // get the number of rows from the data chunk idx_t row_count = duckdb_data_chunk_get_size(result); // get the first column duckdb_vector res_col = duckdb_data_chunk_get_vector(result, 0); // get the native array and the validity mask of the vector int64_t *vector_data = (int64_t *) duckdb_vector_get_data(res_col); uint64_t *vector_validity = duckdb_vector_get_validity(res_col); // iterate over the rows for (idx_t row = 0; row < row_count; row++) { if (duckdb_validity_row_is_valid(vector_validity, row)) { printf("%lld\n", vector_data[row]); } else { printf("NULL\n"); } } duckdb_destroy_data_chunk(&result); } // clean-up duckdb_destroy_result(&res); duckdb_disconnect(&con); duckdb_close(&db); ``` #### Example: Reading a String Vector {#docs:api:c:vector::example-reading-a-string-vector} ```c duckdb_database db; duckdb_connection con; duckdb_open(nullptr, &db); duckdb_connect(db, &con); duckdb_result res; duckdb_query(con, "SELECT CASE WHEN i%2=0 THEN CONCAT('short_', i) ELSE CONCAT('longstringprefix', i) END FROM range(10) t(i)", &res); // iterate until result is exhausted while (true) { duckdb_data_chunk result = duckdb_fetch_chunk(res); if (!result) { // result is exhausted break; } // get the number of rows from the data chunk idx_t row_count = duckdb_data_chunk_get_size(result); // get the first column duckdb_vector res_col = duckdb_data_chunk_get_vector(result, 0); // get the native array and the validity mask of the vector duckdb_string_t *vector_data = (duckdb_string_t *) duckdb_vector_get_data(res_col); uint64_t *vector_validity = duckdb_vector_get_validity(res_col); // iterate over the rows for (idx_t row = 0; row < row_count; row++) { if (duckdb_validity_row_is_valid(vector_validity, row)) { duckdb_string_t str = vector_data[row]; if (duckdb_string_is_inlined(str)) { // use inlined string printf("%.*s\n", str.value.inlined.length, str.value.inlined.inlined); } else { // follow string pointer printf("%.*s\n", str.value.pointer.length, str.value.pointer.ptr); } } else { printf("NULL\n"); } } duckdb_destroy_data_chunk(&result); } // clean-up duckdb_destroy_result(&res); duckdb_disconnect(&con); duckdb_close(&db); ``` #### Example: Reading a Struct Vector {#docs:api:c:vector::example-reading-a-struct-vector} ```c duckdb_database db; duckdb_connection con; duckdb_open(nullptr, &db); duckdb_connect(db, &con); duckdb_result res; duckdb_query(con, "SELECT CASE WHEN i%5=0 THEN NULL ELSE {'col1': i, 'col2': CASE WHEN i%2=0 THEN NULL ELSE 100 + i * 42 END} END FROM range(10) t(i)", &res); // iterate until result is exhausted while (true) { duckdb_data_chunk result = duckdb_fetch_chunk(res); if (!result) { // result is exhausted break; } // get the number of rows from the data chunk idx_t row_count = duckdb_data_chunk_get_size(result); // get the struct column duckdb_vector struct_col = duckdb_data_chunk_get_vector(result, 0); uint64_t *struct_validity = duckdb_vector_get_validity(struct_col); // get the child columns of the struct duckdb_vector col1_vector = duckdb_struct_vector_get_child(struct_col, 0); int64_t *col1_data = (int64_t *) duckdb_vector_get_data(col1_vector); uint64_t *col1_validity = duckdb_vector_get_validity(col1_vector); duckdb_vector col2_vector = duckdb_struct_vector_get_child(struct_col, 1); int64_t *col2_data = (int64_t *) duckdb_vector_get_data(col2_vector); uint64_t *col2_validity = duckdb_vector_get_validity(col2_vector); // iterate over the rows for (idx_t row = 0; row < row_count; row++) { if (!duckdb_validity_row_is_valid(struct_validity, row)) { // entire struct is NULL printf("NULL\n"); continue; } // read col1 printf("{'col1': "); if (!duckdb_validity_row_is_valid(col1_validity, row)) { // col1 is NULL printf("NULL"); } else { printf("%lld", col1_data[row]); } printf(", 'col2': "); if (!duckdb_validity_row_is_valid(col2_validity, row)) { // col2 is NULL printf("NULL"); } else { printf("%lld", col2_data[row]); } printf("}\n"); } duckdb_destroy_data_chunk(&result); } // clean-up duckdb_destroy_result(&res); duckdb_disconnect(&con); duckdb_close(&db); ``` #### Example: Reading a List Vector {#docs:api:c:vector::example-reading-a-list-vector} ```c duckdb_database db; duckdb_connection con; duckdb_open(nullptr, &db); duckdb_connect(db, &con); duckdb_result res; duckdb_query(con, "SELECT CASE WHEN i % 5 = 0 THEN NULL WHEN i % 2 = 0 THEN [i, i + 1] ELSE [i * 42, NULL, i * 84] END FROM range(10) t(i)", &res); // iterate until result is exhausted while (true) { duckdb_data_chunk result = duckdb_fetch_chunk(res); if (!result) { // result is exhausted break; } // get the number of rows from the data chunk idx_t row_count = duckdb_data_chunk_get_size(result); // get the list column duckdb_vector list_col = duckdb_data_chunk_get_vector(result, 0); duckdb_list_entry *list_data = (duckdb_list_entry *) duckdb_vector_get_data(list_col); uint64_t *list_validity = duckdb_vector_get_validity(list_col); // get the child column of the list duckdb_vector list_child = duckdb_list_vector_get_child(list_col); int64_t *child_data = (int64_t *) duckdb_vector_get_data(list_child); uint64_t *child_validity = duckdb_vector_get_validity(list_child); // iterate over the rows for (idx_t row = 0; row < row_count; row++) { if (!duckdb_validity_row_is_valid(list_validity, row)) { // entire list is NULL printf("NULL\n"); continue; } // read the list offsets for this row duckdb_list_entry list = list_data[row]; printf("["); for (idx_t child_idx = list.offset; child_idx < list.offset + list.length; child_idx++) { if (child_idx > list.offset) { printf(", "); } if (!duckdb_validity_row_is_valid(child_validity, child_idx)) { // col1 is NULL printf("NULL"); } else { printf("%lld", child_data[child_idx]); } } printf("]\n"); } duckdb_destroy_data_chunk(&result); } // clean-up duckdb_destroy_result(&res); duckdb_disconnect(&con); duckdb_close(&db); ```
1,765
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c duckdb_logical_type duckdb_vector_get_column_type(duckdb_vector vector); void *duckdb_vector_get_data(duckdb_vector vector); uint64_t *duckdb_vector_get_validity(duckdb_vector vector); void duckdb_vector_ensure_validity_writable(duckdb_vector vector); void duckdb_vector_assign_string_element(duckdb_vector vector, idx_t index, const char *str); void duckdb_vector_assign_string_element_len(duckdb_vector vector, idx_t index, const char *str, idx_t str_len); duckdb_vector duckdb_list_vector_get_child(duckdb_vector vector); idx_t duckdb_list_vector_get_size(duckdb_vector vector); duckdb_state duckdb_list_vector_set_size(duckdb_vector vector, idx_t size); duckdb_state duckdb_list_vector_reserve(duckdb_vector vector, idx_t required_capacity); duckdb_vector duckdb_struct_vector_get_child(duckdb_vector vector, idx_t index); duckdb_vector duckdb_array_vector_get_child(duckdb_vector vector); ``` #### Validity Mask Functions {#docs:api:c:vector::validity-mask-functions} ```c bool duckdb_validity_row_is_valid(uint64_t *validity, idx_t row); void duckdb_validity_set_row_validity(uint64_t *validity, idx_t row, bool valid); void duckdb_validity_set_row_invalid(uint64_t *validity, idx_t row); void duckdb_validity_set_row_valid(uint64_t *validity, idx_t row); ```
336
Client APIs
C
API Reference Overview
`duckdb_vector_get_column_type`
Retrieves the column type of the specified vector. The result must be destroyed with `duckdb_destroy_logical_type`. ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_logical_type duckdb_vector_get_column_type( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector get the data from ###### Return Value {#docs:api:c:vector::return-value} The type of the vector <br>
111
Client APIs
C
API Reference Overview
`duckdb_vector_get_data`
Retrieves the data pointer of the vector. The data pointer can be used to read or write values from the vector. How to read or write values depends on the type of the vector. ###### Syntax {#docs:api:c:vector::syntax} ```c void *duckdb_vector_get_data( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector to get the data from ###### Return Value {#docs:api:c:vector::return-value} The data pointer <br>
122
Client APIs
C
API Reference Overview
`duckdb_vector_get_validity`
Retrieves the validity mask pointer of the specified vector. If all values are valid, this function MIGHT return NULL! The validity mask is a bitset that signifies null-ness within the data chunk. It is a series of uint64_t values, where each uint64_t value contains validity for 64 tuples. The bit is set to 1 if the value is valid (i.e., not NULL) or 0 if the value is invalid (i.e., NULL). Validity of a specific value can be obtained like this: idx_t entry_idx = row_idx / 64; idx_t idx_in_entry = row_idx % 64; bool is_valid = validity_mask[entry_idx] & (1 << idx_in_entry); Alternatively, the (slower) duckdb_validity_row_is_valid function can be used. ###### Syntax {#docs:api:c:vector::syntax} ```c uint64_t *duckdb_vector_get_validity( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector to get the data from ###### Return Value {#docs:api:c:vector::return-value} The pointer to the validity mask, or NULL if no validity mask is present <br>
271
Client APIs
C
API Reference Overview
`duckdb_vector_ensure_validity_writable`
Ensures the validity mask is writable by allocating it. After this function is called, `duckdb_vector_get_validity` will ALWAYS return non-NULL. This allows null values to be written to the vector, regardless of whether a validity mask was present before. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_vector_ensure_validity_writable( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector to alter <br>
116
Client APIs
C
API Reference Overview
`duckdb_vector_assign_string_element`
Assigns a string element in the vector at the specified location. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_vector_assign_string_element( duckdb_vector vector, idx_t index, const char *str ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector to alter * `index`: The row position in the vector to assign the string to * `str`: The null-terminated string <br>
107
Client APIs
C
API Reference Overview
`duckdb_vector_assign_string_element_len`
Assigns a string element in the vector at the specified location. You may also use this function to assign BLOBs. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_vector_assign_string_element_len( duckdb_vector vector, idx_t index, const char *str, idx_t str_len ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector to alter * `index`: The row position in the vector to assign the string to * `str`: The string * `str_len`: The length of the string (in bytes) <br>
137
Client APIs
C
API Reference Overview
`duckdb_list_vector_get_child`
Retrieves the child vector of a list vector. The resulting vector is valid as long as the parent vector is valid. ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_vector duckdb_list_vector_get_child( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector ###### Return Value {#docs:api:c:vector::return-value} The child vector <br>
105
Client APIs
C
API Reference Overview
`duckdb_list_vector_get_size`
Returns the size of the child vector of the list. ###### Syntax {#docs:api:c:vector::syntax} ```c idx_t duckdb_list_vector_get_size( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector ###### Return Value {#docs:api:c:vector::return-value} The size of the child list <br>
92
Client APIs
C
API Reference Overview
`duckdb_list_vector_set_size`
Sets the total size of the underlying child-vector of a list vector. ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_state duckdb_list_vector_set_size( duckdb_vector vector, idx_t size ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The list vector. * `size`: The size of the child list. ###### Return Value {#docs:api:c:vector::return-value} The duckdb state. Returns DuckDBError if the vector is nullptr. <br>
122
Client APIs
C
API Reference Overview
`duckdb_list_vector_reserve`
Sets the total capacity of the underlying child-vector of a list. After calling this method, you must call `duckdb_vector_get_validity` and `duckdb_vector_get_data` to obtain current data and validity pointers ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_state duckdb_list_vector_reserve( duckdb_vector vector, idx_t required_capacity ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The list vector. * `required_capacity`: the total capacity to reserve. ###### Return Value {#docs:api:c:vector::return-value} The duckdb state. Returns DuckDBError if the vector is nullptr. <br>
154
Client APIs
C
API Reference Overview
`duckdb_struct_vector_get_child`
Retrieves the child vector of a struct vector. The resulting vector is valid as long as the parent vector is valid. ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_vector duckdb_struct_vector_get_child( duckdb_vector vector, idx_t index ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector * `index`: The child index ###### Return Value {#docs:api:c:vector::return-value} The child vector <br>
117
Client APIs
C
API Reference Overview
`duckdb_array_vector_get_child`
Retrieves the child vector of a array vector. The resulting vector is valid as long as the parent vector is valid. The resulting vector has the size of the parent vector multiplied by the array size. ###### Syntax {#docs:api:c:vector::syntax} ```c duckdb_vector duckdb_array_vector_get_child( duckdb_vector vector ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `vector`: The vector ###### Return Value {#docs:api:c:vector::return-value} The child vector <br>
121
Client APIs
C
API Reference Overview
`duckdb_validity_row_is_valid`
Returns whether or not a row is valid (i.e., not NULL) in the given validity mask. ###### Syntax {#docs:api:c:vector::syntax} ```c bool duckdb_validity_row_is_valid( uint64_t *validity, idx_t row ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `validity`: The validity mask, as obtained through `duckdb_vector_get_validity` * `row`: The row index ###### Return Value {#docs:api:c:vector::return-value} true if the row is valid, false otherwise <br>
132
Client APIs
C
API Reference Overview
`duckdb_validity_set_row_validity`
In a validity mask, sets a specific row to either valid or invalid. Note that `duckdb_vector_ensure_validity_writable` should be called before calling `duckdb_vector_get_validity`, to ensure that there is a validity mask to write to. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_validity_set_row_validity( uint64_t *validity, idx_t row, bool valid ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `validity`: The validity mask, as obtained through `duckdb_vector_get_validity`. * `row`: The row index * `valid`: Whether or not to set the row to valid, or invalid <br>
160
Client APIs
C
API Reference Overview
`duckdb_validity_set_row_invalid`
In a validity mask, sets a specific row to invalid. Equivalent to `duckdb_validity_set_row_validity` with valid set to false. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_validity_set_row_invalid( uint64_t *validity, idx_t row ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `validity`: The validity mask * `row`: The row index <br>
105
Client APIs
C
API Reference Overview
`duckdb_validity_set_row_valid`
In a validity mask, sets a specific row to valid. Equivalent to `duckdb_validity_set_row_validity` with valid set to true. ###### Syntax {#docs:api:c:vector::syntax} ```c void duckdb_validity_set_row_valid( uint64_t *validity, idx_t row ); ``` ###### Parameters {#docs:api:c:vector::parameters} * `validity`: The validity mask * `row`: The row index <br>
105
Client APIs
C
Values
The value class represents a single value of any type.
11
Client APIs
C
API Reference Overview
<!-- This section is generated by scripts/generate_c_api_docs.py --> ```c void duckdb_destroy_value(duckdb_value *value); duckdb_value duckdb_create_varchar(const char *text); duckdb_value duckdb_create_varchar_length(const char *text, idx_t length); duckdb_value duckdb_create_bool(bool input); duckdb_value duckdb_create_int8(int8_t input); duckdb_value duckdb_create_uint8(uint8_t input); duckdb_value duckdb_create_int16(int16_t input); duckdb_value duckdb_create_uint16(uint16_t input); duckdb_value duckdb_create_int32(int32_t input); duckdb_value duckdb_create_uint32(uint32_t input); duckdb_value duckdb_create_uint64(uint64_t input); duckdb_value duckdb_create_int64(int64_t val); duckdb_value duckdb_create_hugeint(duckdb_hugeint input); duckdb_value duckdb_create_uhugeint(duckdb_uhugeint input); duckdb_value duckdb_create_float(float input); duckdb_value duckdb_create_double(double input); duckdb_value duckdb_create_date(duckdb_date input); duckdb_value duckdb_create_time(duckdb_time input); duckdb_value duckdb_create_time_tz_value(duckdb_time_tz value); duckdb_value duckdb_create_timestamp(duckdb_timestamp input); duckdb_value duckdb_create_interval(duckdb_interval input); duckdb_value duckdb_create_blob(const uint8_t *data, idx_t length); bool duckdb_get_bool(duckdb_value val); int8_t duckdb_get_int8(duckdb_value val); uint8_t duckdb_get_uint8(duckdb_value val); int16_t duckdb_get_int16(duckdb_value val); uint16_t duckdb_get_uint16(duckdb_value val); int32_t duckdb_get_int32(duckdb_value val); uint32_t duckdb_get_uint32(duckdb_value val); int64_t duckdb_get_int64(duckdb_value val); uint64_t duckdb_get_uint64(duckdb_value val); duckdb_hugeint duckdb_get_hugeint(duckdb_value val); duckdb_uhugeint duckdb_get_uhugeint(duckdb_value val); float duckdb_get_float(duckdb_value val); double duckdb_get_double(duckdb_value val); duckdb_date duckdb_get_date(duckdb_value val); duckdb_time duckdb_get_time(duckdb_value val); duckdb_time_tz duckdb_get_time_tz(duckdb_value val); duckdb_timestamp duckdb_get_timestamp(duckdb_value val); duckdb_interval duckdb_get_interval(duckdb_value val); duckdb_logical_type duckdb_get_value_type(duckdb_value val); duckdb_blob duckdb_get_blob(duckdb_value val); char *duckdb_get_varchar(duckdb_value value); duckdb_value duckdb_create_struct_value(duckdb_logical_type type, duckdb_value *values); duckdb_value duckdb_create_list_value(duckdb_logical_type type, duckdb_value *values, idx_t value_count); duckdb_value duckdb_create_array_value(duckdb_logical_type type, duckdb_value *values, idx_t value_count); idx_t duckdb_get_map_size(duckdb_value value); duckdb_value duckdb_get_map_key(duckdb_value value, idx_t index); duckdb_value duckdb_get_map_value(duckdb_value value, idx_t index); ```
729
Client APIs
C
API Reference Overview
`duckdb_destroy_value`
Destroys the value and de-allocates all memory allocated for that type. ###### Syntax {#docs:api:c:value::syntax} ```c void duckdb_destroy_value( duckdb_value *value ); ``` ###### Parameters {#docs:api:c:value::parameters} * `value`: The value to destroy. <br>
73
Client APIs
C
API Reference Overview
`duckdb_create_varchar`
Creates a value from a null-terminated string ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_varchar( const char *text ); ``` ###### Parameters {#docs:api:c:value::parameters} * `text`: The null-terminated string ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
98
Client APIs
C
API Reference Overview
`duckdb_create_varchar_length`
Creates a value from a string ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_varchar_length( const char *text, idx_t length ); ``` ###### Parameters {#docs:api:c:value::parameters} * `text`: The text * `length`: The length of the text ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
107
Client APIs
C
API Reference Overview
`duckdb_create_bool`
Creates a value from a boolean ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_bool( bool input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The boolean value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
90
Client APIs
C
API Reference Overview
`duckdb_create_int8`
Creates a value from a int8_t (a tinyint) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_int8( int8_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The tinyint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
101
Client APIs
C
API Reference Overview
`duckdb_create_uint8`
Creates a value from a uint8_t (a utinyint) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_uint8( uint8_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The utinyint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
103
Client APIs
C
API Reference Overview
`duckdb_create_int16`
Creates a value from a int16_t (a smallint) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_int16( int16_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The smallint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
101
Client APIs
C
API Reference Overview
`duckdb_create_uint16`
Creates a value from a uint16_t (a usmallint) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_uint16( uint16_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The usmallint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
103
Client APIs
C
API Reference Overview
`duckdb_create_int32`
Creates a value from a int32_t (an integer) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_int32( int32_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The integer value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
99
Client APIs
C
API Reference Overview
`duckdb_create_uint32`
Creates a value from a uint32_t (a uinteger) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_uint32( uint32_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The uinteger value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
101
Client APIs
C
API Reference Overview
`duckdb_create_uint64`
Creates a value from a uint64_t (a ubigint) ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_uint64( uint64_t input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The ubigint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
103
Client APIs
C
API Reference Overview
`duckdb_create_int64`
Creates a value from an int64 ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_int64( int64_t val ); ``` <br>
73
Client APIs
C
API Reference Overview
`duckdb_create_hugeint`
Creates a value from a hugeint ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_hugeint( duckdb_hugeint input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The hugeint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
98
Client APIs
C
API Reference Overview
`duckdb_create_uhugeint`
Creates a value from a uhugeint ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_uhugeint( duckdb_uhugeint input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The uhugeint value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
100
Client APIs
C
API Reference Overview
`duckdb_create_float`
Creates a value from a float ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_float( float input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The float value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
90
Client APIs
C
API Reference Overview
`duckdb_create_double`
Creates a value from a double ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_double( double input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The double value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
90
Client APIs
C
API Reference Overview
`duckdb_create_date`
Creates a value from a date ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_date( duckdb_date input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The date value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
92
Client APIs
C
API Reference Overview
`duckdb_create_time`
Creates a value from a time ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_time( duckdb_time input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The time value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
92
Client APIs
C
API Reference Overview
`duckdb_create_time_tz_value`
Creates a value from a time_tz. Not to be confused with `duckdb_create_time_tz`, which creates a duckdb_time_tz_t. ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_time_tz_value( duckdb_time_tz value ); ``` ###### Parameters {#docs:api:c:value::parameters} * `value`: The time_tz value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
125
Client APIs
C
API Reference Overview
`duckdb_create_timestamp`
Creates a value from a timestamp ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_timestamp( duckdb_timestamp input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The timestamp value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
92
Client APIs
C
API Reference Overview
`duckdb_create_interval`
Creates a value from an interval ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_interval( duckdb_interval input ); ``` ###### Parameters {#docs:api:c:value::parameters} * `input`: The interval value ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
92
Client APIs
C
API Reference Overview
`duckdb_create_blob`
Creates a value from a blob ###### Syntax {#docs:api:c:value::syntax} ```c duckdb_value duckdb_create_blob( const uint8_t *data, idx_t length ); ``` ###### Parameters {#docs:api:c:value::parameters} * `data`: The blob data * `length`: The length of the blob data ###### Return Value {#docs:api:c:value::return-value} The value. This must be destroyed with `duckdb_destroy_value`. <br>
109
Client APIs
C
API Reference Overview
`duckdb_get_bool`
Returns the boolean value of the given value. ###### Syntax {#docs:api:c:value::syntax} ```c bool duckdb_get_bool( duckdb_value val ); ``` ###### Parameters {#docs:api:c:value::parameters} * `val`: A duckdb_value containing a boolean ###### Return Value {#docs:api:c:value::return-value} A boolean, or false if the value cannot be converted <br>
94
Client APIs
C
API Reference Overview
`duckdb_get_int8`
Returns the int8_t value of the given value. ###### Syntax {#docs:api:c:value::syntax} ```c int8_t duckdb_get_int8( duckdb_value val ); ``` ###### Parameters {#docs:api:c:value::parameters} * `val`: A duckdb_value containing a tinyint ###### Return Value {#docs:api:c:value::return-value} A int8_t, or MinValue<int8> if the value cannot be converted <br>
106
Client APIs
C
API Reference Overview
`duckdb_get_uint8`
Returns the uint8_t value of the given value. ###### Syntax {#docs:api:c:value::syntax} ```c uint8_t duckdb_get_uint8( duckdb_value val ); ``` ###### Parameters {#docs:api:c:value::parameters} * `val`: A duckdb_value containing a utinyint ###### Return Value {#docs:api:c:value::return-value} A uint8_t, or MinValue<uint8> if the value cannot be converted <br>
107
Client APIs
C
API Reference Overview
`duckdb_get_int16`
Returns the int16_t value of the given value. ###### Syntax {#docs:api:c:value::syntax} ```c int16_t duckdb_get_int16( duckdb_value val ); ``` ###### Parameters {#docs:api:c:value::parameters} * `val`: A duckdb_value containing a smallint ###### Return Value {#docs:api:c:value::return-value} A int16_t, or MinValue<int16> if the value cannot be converted <br>
106
Client APIs
C
API Reference Overview
`duckdb_get_uint16`
Returns the uint16_t value of the given value. ###### Syntax {#docs:api:c:value::syntax} ```c uint16_t duckdb_get_uint16( duckdb_value val ); ``` ###### Parameters {#docs:api:c:value::parameters} * `val`: A duckdb_value containing a usmallint ###### Return Value {#docs:api:c:value::return-value} A uint16_t, or MinValue<uint16> if the value cannot be converted <br>
107