Created
February 27, 2026 15:12
-
-
Save koron/28f6a42b972048230bdc9ba03dbda37e to your computer and use it in GitHub Desktop.
This is a C example of DuckDB that accesses parquet files on a server that does not return Content-Length to the HEAD method. By linking it to the pre-built library included in duckdb/duckdb-go-bindings and running it, we can confirm that the library itself is free of defects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <duckdb.h> | |
| int main(int argc, char **argv) | |
| { | |
| duckdb_database db; | |
| duckdb_connection con; | |
| if (duckdb_open(NULL, &db) == DuckDBError) { | |
| printf("duckdb_open failed\n"); | |
| return 1; | |
| } | |
| if (duckdb_connect(db, &con) == DuckDBError) { | |
| printf("duckdb_connect failed\n"); | |
| return 1; | |
| } | |
| // run queries... | |
| duckdb_state state; | |
| duckdb_result result; | |
| state = duckdb_query(con, "INSTALL httpfs", NULL); | |
| if (state == DuckDBError) { | |
| printf("failed: INSTALL httpfs\n"); | |
| return 1; | |
| } | |
| printf("httpfs installed\n"); | |
| state = duckdb_query(con, "LOAD httpfs", NULL); | |
| if (state == DuckDBError) { | |
| printf("failed: LOAD httpfs\n"); | |
| return 1; | |
| } | |
| printf("httpfs loaded\n"); | |
| #if 1 | |
| state = duckdb_query(con, "SELECT avg(c_acctbal) FROM 'https://shell.duckdb.org/data/tpch/0_01/parquet/customer.parquet'", &result); | |
| #else | |
| state = duckdb_query(con, "SELECT avg(c_acctbal) FROM 'customer.parquet'", &result); | |
| #endif | |
| if (state == DuckDBError) { | |
| printf("duckdb_query failed: %s\n", duckdb_result_error(&result)); | |
| } | |
| while (true) { | |
| duckdb_data_chunk chunk = duckdb_fetch_chunk(result); | |
| if (!chunk) { | |
| break; | |
| } | |
| printf("column#0: name=%s type=%d\n", | |
| duckdb_column_name(&result, 0), | |
| duckdb_column_type(&result, 0)); | |
| idx_t row_count = duckdb_data_chunk_get_size(chunk); | |
| duckdb_vector col0 = duckdb_data_chunk_get_vector(chunk, 0); | |
| double *col0_data = (double *)duckdb_vector_get_data(col0); | |
| uint64_t *col0_validity = duckdb_vector_get_validity(col0); | |
| printf("row_count=%d\n", row_count); | |
| for (idx_t row = 0; row < row_count; row++) { | |
| if (duckdb_validity_row_is_valid(col0_validity, row)) { | |
| printf("%e", col0_data[row]); | |
| } else { | |
| printf("NULL"); | |
| } | |
| printf("\n"); | |
| } | |
| duckdb_destroy_data_chunk(&chunk); | |
| } | |
| // cleanup | |
| duckdb_destroy_result(&result); | |
| duckdb_disconnect(&con); | |
| duckdb_close(&db); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DUCKDB_DIR=../duckdb-go-bindings/lib/windows-amd64 | |
| #DUCKDB_DIR=../duckdb-go-bindings/lib/linux-amd64 | |
| #DUCKDB_DIR=../duckdb-go-bindings/lib/darwin-arm64 | |
| CFLAGS=-I${DUCKDB_DIR} -DDUCKDB_STATIC_BUILD | |
| LDLIBS= \ | |
| -lduckdb_static \ | |
| -lautocomplete_extension \ | |
| -lcore_functions_extension \ | |
| -licu_extension \ | |
| -ljson_extension \ | |
| -lparquet_extension \ | |
| -ltpcds_extension \ | |
| -ltpch_extension \ | |
| -lduckdb_fastpforlib \ | |
| -lduckdb_fmt \ | |
| -lduckdb_fsst \ | |
| -lduckdb_hyperloglog \ | |
| -lduckdb_mbedtls \ | |
| -lduckdb_miniz \ | |
| -lduckdb_pg_query \ | |
| -lduckdb_re2 \ | |
| -lduckdb_skiplistlib \ | |
| -lduckdb_utf8proc \ | |
| -lduckdb_yyjson \ | |
| -lduckdb_zstd \ | |
| -lws2_32 \ | |
| -lwsock32 \ | |
| -lrstrtmgr \ | |
| -lstdc++ \ | |
| -lm \ | |
| --static \ | |
| -L${DUCKDB_DIR} | |
| main: main.c | |
| clean: | |
| rm -f *.o | |
| rm -f main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment