diff --git a/ffi/examples/read-table/CMakeLists.txt b/ffi/examples/read-table/CMakeLists.txt index 4c98ef3a..6533bf73 100644 --- a/ffi/examples/read-table/CMakeLists.txt +++ b/ffi/examples/read-table/CMakeLists.txt @@ -18,11 +18,11 @@ add_test(NAME read_and_print_basic_partitioned COMMAND ${TestRunner} ${DatPath}/ if(WIN32) set(CMAKE_C_FLAGS_DEBUG "/MT") - target_link_libraries(read_table PUBLIC ws2_32 userenv bcrypt ntdll) + target_link_libraries(read_table PUBLIC ws2_32 userenv bcrypt ncrypt crypt32 secur32 ntdll RuntimeObject) endif(WIN32) if(MSVC) - target_compile_options(read_table PRIVATE /W4 /WX) + target_compile_options(read_table PRIVATE /W3 /WX) else() # no-strict-prototypes because arrow headers have fn defs without prototypes target_compile_options(read_table PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-strict-prototypes -g -fsanitize=address) diff --git a/ffi/examples/read-table/README.md b/ffi/examples/read-table/README.md index ebf90403..abe7b86b 100644 --- a/ffi/examples/read-table/README.md +++ b/ffi/examples/read-table/README.md @@ -1,4 +1,44 @@ read table ========== -Simple example to show how to read and dump the data of a table using kernel's cffi, and arrow-glib +Simple example to show how to read and dump the data of a table using kernel's cffi, and arrow-glib. + +# Building + +This example is built with [cmake]. Instructions below assume you start in the directory containing this README. + +## Linux / MacOS + +Most likely something like this should work: +``` +$ mkdir build +$ cd build +$ cmake .. +$ make +$ ./read_table [path/to/table] +``` + +## Windows + +For windows, assuming you already have a working cmake + c toolchain: +``` +PS mkdir build +PS cd build +PS cmake -G "Visual Studio 17 2022" .. +PS cmake --build . +PS .\Debug\read_table.exe [path\to\table] +``` + +If running on windows you should also run `chcp.exe 65001` to set the codepage to utf-8, or things +won't print out correctly. + +## Arrow GLib +This example uses the `arrow-glib (c)` component from arrow to print out data. This requires +_installing_ that component which can be non-trivial. Please see +[here](https://arrow.apache.org/install/) to find installation instructions for your system. + +If you don't want to have to install this, you can run `ccmake ..` (`cmake-gui.exe ..` on windows) +from the `build` directory, and turn `OFF`/uncheckmark `PRINT_DATA`. Then "configure" and +"generate" and follow the above instructions again. + +[cmake]: https://cmake.org/ diff --git a/ffi/examples/read-table/arrow.c b/ffi/examples/read-table/arrow.c index 63966f45..0aeedc13 100644 --- a/ffi/examples/read-table/arrow.c +++ b/ffi/examples/read-table/arrow.c @@ -61,7 +61,7 @@ static GArrowRecordBatch* add_partition_columns( gint64 cols = garrow_record_batch_get_n_columns(record_batch); GArrowRecordBatch* cur_record_batch = record_batch; GError* error = NULL; - for (int i = 0; i < partition_cols->len; i++) { + for (uintptr_t i = 0; i < partition_cols->len; i++) { char* col = partition_cols->cols[i]; guint pos = cols + i; KernelStringSlice key = { col, strlen(col) }; diff --git a/ffi/examples/read-table/read_table.c b/ffi/examples/read-table/read_table.c index e73d7c85..b40f7a0d 100644 --- a/ffi/examples/read-table/read_table.c +++ b/ffi/examples/read-table/read_table.c @@ -51,7 +51,7 @@ void print_selection_vector(const char* indent, const KernelBoolSlice* selection void print_partition_info(struct EngineContext* context, const CStringMap* partition_values) { #ifdef VERBOSE - for (int i = 0; i < context->partition_cols->len; i++) { + for (uintptr_t i = 0; i < context->partition_cols->len; i++) { char* col = context->partition_cols->cols[i]; KernelStringSlice key = { col, strlen(col) }; char* partition_val = get_from_map(partition_values, key, allocate_string); @@ -79,6 +79,18 @@ EngineError* allocate_error(KernelError etype, const KernelStringSlice msg) return (EngineError*)error; } +#ifdef WIN32 // windows doesn't have strndup +char *strndup(const char *s, size_t n) { + size_t len = strnlen(s, n); + char *p = malloc(len + 1); + if (p) { + memcpy(p, s, len); + p[len] = '\0'; + } + return p; +} +#endif + // utility to turn a slice into a char* void* allocate_string(const KernelStringSlice slice) { @@ -165,7 +177,7 @@ void visit_partition(void* context, const KernelStringSlice partition) PartitionList* get_partition_list(SharedGlobalScanState* state) { print_diag("Building list of partition columns\n"); - int count = get_partition_column_count(state); + uintptr_t count = get_partition_column_count(state); PartitionList* list = malloc(sizeof(PartitionList)); // We set the `len` to 0 here and use it to track how many items we've added to the list list->len = 0; @@ -184,7 +196,7 @@ PartitionList* get_partition_list(SharedGlobalScanState* state) } if (list->len > 0) { print_diag("Partition columns are:\n"); - for (int i = 0; i < list->len; i++) { + for (uintptr_t i = 0; i < list->len; i++) { print_diag(" - %s\n", list->cols[i]); } } else { @@ -195,7 +207,7 @@ PartitionList* get_partition_list(SharedGlobalScanState* state) } void free_partition_list(PartitionList* list) { - for (int i = 0; i < list->len; i++) { + for (uintptr_t i = 0; i < list->len; i++) { free(list->cols[i]); } free(list->cols); diff --git a/ffi/examples/read-table/read_table.h b/ffi/examples/read-table/read_table.h index 8e40a988..d3fe5ff9 100644 --- a/ffi/examples/read-table/read_table.h +++ b/ffi/examples/read-table/read_table.h @@ -8,7 +8,7 @@ // A list of partition column names typedef struct PartitionList { - int len; + uintptr_t len; char** cols; } PartitionList;