Skip to content

Commit 8e70414

Browse files
committed
better
1 parent badd59d commit 8e70414

10 files changed

Lines changed: 393 additions & 274 deletions

File tree

vortex-ffi/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ Static library path ${LIBRARY_PATH}
7979
Headers path ${LIBRARY_HEADERS}")
8080

8181
if (NOT EXISTS "${LIBRARY_PATH_SHARED}")
82-
message(FATAL_ERROR "Shared library not found")
82+
message(FATAL_ERROR "Shared library not found, run `cargo build --release -p vortex-ffi`")
8383
endif()
8484
if (NOT EXISTS "${LIBRARY_PATH}")
85-
message(FATAL_ERROR "Static library not found")
85+
message(FATAL_ERROR "Static library not found, run `cargo build --release -p vortex-ffi`")
8686
endif()
8787

8888
add_library(vortex_ffi STATIC IMPORTED)

vortex-ffi/README.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
1-
# Foreign Function Interface
2-
3-
Vortex is a file format that can be used by any execution engine. Nearly every
4-
programming language supports the C ABI (Application Binary Interface), so by
5-
providing an FFI interface to work with Vortex objects we can make it easy to
6-
support a variety of languages.
7-
8-
Check out the [`examples`](./examples/) directory to see an example of how to
9-
use scanning API from C to read vortex files.
10-
Full FFI API is documented in `docs/api/c` and in `cinclude/vortex.h` header.
11-
1+
# Vortex C interface
122
## Usage from a CMake project
133

14-
1. Build .so/.dylib/.dll
15-
16-
```sh
17-
cargo build --release -p vortex-ffi
184
```
5+
# in vortex folder
6+
cargo build --release -p vortex-ffi
197
20-
2. Add shared or static library
21-
22-
```cmake
238
# in your CMakeLists.txt
249
include_directory(vortex/vortex-ffi)
2510
target_link_libraries(my_target, vortex_ffi_shared)
2611
# or target_link_libraries(my_target, vortex_ffi)
2712
```
2813

29-
## Updating Headers
30-
31-
To rebuild the header file:
14+
## Running C examples:
3215

3316
```sh
34-
cargo +nightly build -p vortex-ffi
17+
cmake -Bbuild -DBUILD_EXAMPLES=1
18+
cmake --build build
19+
./build/examples/dtype
20+
./build/examples/scan
21+
./build/examples/scan_to_arrow
3522
```
3623

37-
Header generation uses cbindgen's macro expansion feature which requires nightly.
38-
Stable builds use header file at `cinclude/vortex.h`.
24+
## Updating Headers
25+
26+
If you're developing FFI and want to rebuild `cinclude/vortex.h`, run
27+
`cargo +nightly build -p vortex-ffi`.
3928

4029
## Testing C part
4130

42-
Build the test library
31+
Build the test library:
4332

4433
```sh
45-
cmake -Bbuild
46-
cmake --build build -j $(nproc)
34+
cmake -Bbuild -DBUILD_TESTS=1
35+
cmake --build build
4736
```
4837

49-
Run the tests
38+
Run the tests:
5039

5140
```sh
5241
ctest --test-dir build -j $(nproc)
5342
```
5443

55-
You would need C++ compiler toolchain to run the tests since they use Catch2.
44+
You will need C++ compiler toolchain to run the tests since they use Catch2.
5645

5746
## Testing Rust part with sanitizers
5847

@@ -92,7 +81,7 @@ with sanitizers.
9281
- `allow-abi-mismatch` is safe because in our dependency graph only crates like
9382
`compiler_builtins` unset sanitization, and they do it on purpose.
9483
- Make sure to use `cargo test` and not `cargo nextest` as nextest reports less
95-
leaks.
84+
leaks.
9685
- If you want stack trace symbolization, install `llvm-symbolizer`.
9786

9887
## Testing Rust and C with sanitizers
@@ -102,10 +91,10 @@ leaks.
10291
```sh
10392
RUSTFLAGS="-Zsanitizer=address -Zexternal-clangrt" \
10493
cargo +nightly build -Zbuild-std --target=<target triple> \
105-
--no-default-features -p vortex-ffi
94+
--no-default-features -p vortex-ffi
10695
```
10796

108-
2. Build tests with target triple
97+
2. Build tests with target triple:
10998

11099
```sh
111100
cmake -Bbuild -DWITH_ASAN=1 -DTARGET_TRIPLE=<target triple>
@@ -116,11 +105,3 @@ cmake -Bbuild -DWITH_ASAN=1 -DTARGET_TRIPLE=<target triple>
116105
```sh
117106
./build/test/vortex_ffi_test 2>& 1 | rustfilt -i-
118107
```
119-
120-
## Running C example
121-
122-
```sh
123-
cmake -Bbuild -DBUILD_EXAMPLES=1
124-
cmake --build build
125-
./build/examples/vortex_scan
126-
```

vortex-ffi/cinclude/vortex.h

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,19 @@ typedef enum {
175175
} vx_validity_type;
176176

177177
typedef enum {
178-
VX_CARD_UNKNOWN = 0,
179-
VX_CARD_ESTIMATE = 1,
180-
VX_CARD_MAXIMUM = 2,
181-
} vx_cardinality;
178+
/**
179+
* No estimate is available.
180+
*/
181+
VX_ESTIMATE_UNKNOWN = 0,
182+
/**
183+
* The value in vx_estimate.estimate is exact.
184+
*/
185+
VX_ESTIMATE_EXACT = 1,
186+
/**
187+
* The value in vx_estimate.estimate is an upper bound.
188+
*/
189+
VX_ESTIMATE_INEXACT = 2,
190+
} vx_estimate_type;
182191

183192
/**
184193
* Equalities, inequalities, and boolean operations over possibly null values.
@@ -282,21 +291,6 @@ typedef enum {
282291
VX_SELECTION_EXCLUDE_RANGE = 2,
283292
} vx_scan_selection_include;
284293

285-
typedef enum {
286-
/**
287-
* No estimate is available.
288-
*/
289-
VX_ESTIMATE_UNKNOWN = 0,
290-
/**
291-
* The value in vx_estimate.estimate is exact.
292-
*/
293-
VX_ESTIMATE_EXACT = 1,
294-
/**
295-
* The value in vx_estimate.estimate is an upper bound.
296-
*/
297-
VX_ESTIMATE_INEXACT = 2,
298-
} vx_estimate_type;
299-
300294
/**
301295
* Physical type enum, represents the in-memory physical layout but might represent a different logical type.
302296
*/
@@ -490,6 +484,10 @@ typedef struct vx_file vx_file;
490484
*/
491485
typedef struct vx_partition vx_partition;
492486

487+
/**
488+
* A scan is a single traversal of a data source with projections and
489+
* filters. A scan can be consumed only once.
490+
*/
493491
typedef struct vx_scan vx_scan;
494492

495493
/**
@@ -537,13 +535,17 @@ typedef struct {
537535
const char *paths;
538536
} vx_data_source_options;
539537

538+
/**
539+
* Used for estimating number of partitions in a data source or number of rows
540+
* in a partition.
541+
*/
540542
typedef struct {
541-
vx_cardinality cardinality;
543+
vx_estimate_type type;
542544
/**
543-
* Set only when "cardinality" is not VX_CARD_UNKNOWN
545+
* Set only when "type" is not VX_ESTIMATE_UNKNOWN.
544546
*/
545-
uint64_t rows;
546-
} vx_data_source_row_count;
547+
uint64_t estimate;
548+
} vx_estimate;
547549

548550
/**
549551
* Options supplied for opening a file.
@@ -662,18 +664,6 @@ typedef struct {
662664
bool ordered;
663665
} vx_scan_options;
664666

665-
/**
666-
* Used for estimating number of partitions in a data source or number of rows
667-
* in a partition.
668-
*/
669-
typedef struct {
670-
vx_estimate_type type;
671-
/**
672-
* Set only when "type" is not VX_ESTIMATE_UNKNOWN.
673-
*/
674-
uint64_t estimate;
675-
} vx_estimate;
676-
677667
#ifdef __cplusplus
678668
extern "C" {
679669
#endif // __cplusplus
@@ -921,7 +911,7 @@ const vx_dtype *vx_data_source_dtype(const vx_data_source *ds);
921911
/**
922912
* Write data source's row count estimate into "row_count".
923913
*/
924-
void vx_data_source_get_row_count(const vx_data_source *ds, vx_data_source_row_count *row_count);
914+
void vx_data_source_get_row_count(const vx_data_source *ds, vx_estimate *row_count);
925915

926916
/**
927917
* Clone a borrowed [`vx_dtype`], returning an owned [`vx_dtype`].
@@ -1319,6 +1309,17 @@ vx_partition *vx_scan_next_partition(vx_scan *scan, vx_error **err);
13191309
*/
13201310
int vx_partition_row_count(const vx_partition *partition, vx_estimate *count, vx_error **err);
13211311

1312+
/**
1313+
* Scan partition to ArrowArrayStream.
1314+
* Consumes partition fully: subsequent calls to vx_partition_scan_arrow or
1315+
* vx_partition_next are undefined behaviour.
1316+
* This call blocks current thread until underlying stream is fully consumed.
1317+
*
1318+
* Caller must not free partition after calling this function.
1319+
*
1320+
* On success, sets "stream" and returns 0.
1321+
* On error, sets "err" and returns 1, freeing the partition.
1322+
*/
13221323
int vx_partition_scan_arrow(const vx_session *session,
13231324
vx_partition *partition,
13241325
FFI_ArrowArrayStream *stream,

vortex-ffi/examples/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# SPDX-License-Identifier: CC-BY-4.0
22
# SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
# allow linking with vortex_ffi_shared although it's not in current folder
35
cmake_policy(SET CMP0079 NEW)
4-
add_executable(vortex_scan scan.c)
5-
target_link_libraries(vortex_scan PRIVATE vortex_ffi_shared)
6+
7+
add_executable(scan scan.c)
8+
target_link_libraries(scan PRIVATE vortex_ffi_shared)
9+
10+
add_executable(scan_to_arrow scan_to_arrow.c)
11+
target_link_libraries(scan_to_arrow PRIVATE nanoarrow_shared vortex_ffi_shared)
12+
13+
add_executable(dtype dtype.c)
14+
target_link_libraries(dtype PRIVATE vortex_ffi_shared)

0 commit comments

Comments
 (0)