-
Notifications
You must be signed in to change notification settings - Fork 186
New C++ API examples #8755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
myrrc
wants to merge
3
commits into
develop
Choose a base branch
from
myrrc/cxx2-examples
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
New C++ API examples #8755
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| coverage* |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| !goldenfiles/example.parquet |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| foreach(name reader writer dtype scan scan_to_arrow) | ||
| add_executable(${name} ${name}.cpp) | ||
| target_link_libraries(${name} PRIVATE vortex_cxx_shared nanoarrow_shared) | ||
| endforeach() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // SPDX-License-Identifier: CC-BY-4.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include <iostream> | ||
| #include <string_view> | ||
| #include <vortex/data_source.hpp> | ||
|
|
||
| using vortex::DataSource; | ||
| using vortex::DataType; | ||
| using vortex::DataTypeVariant; | ||
| using vortex::Session; | ||
|
|
||
| static void print_dtype(const DataType &dtype); | ||
|
|
||
| static std::string_view ptype_name(vortex::PType p) { | ||
| using enum vortex::PType; | ||
| switch (p) { | ||
| case U8: | ||
| return "uint8_t"; | ||
| case U16: | ||
| return "uint16_t"; | ||
| case U32: | ||
| return "uint32_t"; | ||
| case U64: | ||
| return "uint64_t"; | ||
| case I8: | ||
| return "int8_t"; | ||
| case I16: | ||
| return "int16_t"; | ||
| case I32: | ||
| return "int32_t"; | ||
| case I64: | ||
| return "int64_t"; | ||
| case F16: | ||
| return "float16"; | ||
| case F32: | ||
| return "float"; | ||
| case F64: | ||
| return "double"; | ||
| } | ||
| return "?"; | ||
| } | ||
|
|
||
| static void print_struct(const DataType &dtype) { | ||
| std::cout << "struct(\n"; | ||
| for (const auto &[name, dtype] : dtype.fields()) { | ||
| std::cout << " " << name << " = "; | ||
| print_dtype(dtype); | ||
| } | ||
| std::cout << ")"; | ||
| } | ||
|
|
||
| static void print_dtype(const DataType &dtype) { | ||
| using enum DataTypeVariant; | ||
| switch (dtype.variant()) { | ||
| case Null: | ||
| std::cout << "null"; | ||
| break; | ||
| case Bool: | ||
| std::cout << "bool"; | ||
| break; | ||
| case Utf8: | ||
| std::cout << "utf8"; | ||
| break; | ||
| case Binary: | ||
| std::cout << "binary"; | ||
| break; | ||
| case Extension: | ||
| std::cout << "extension"; | ||
| break; | ||
| case Primitive: | ||
| std::cout << "primitive(" << ptype_name(dtype.primitive_type()) << ")"; | ||
| break; | ||
| case Struct: | ||
| print_struct(dtype); | ||
| break; | ||
| case List: | ||
| std::cout << "list("; | ||
| print_dtype(dtype.list_element()); | ||
| std::cout << ")"; | ||
| break; | ||
| case FixedSizeList: | ||
| std::cout << "fixed_list("; | ||
| print_dtype(dtype.fixed_size_list_element()); | ||
| std::cout << ")"; | ||
| break; | ||
| case Decimal: | ||
| std::cout << "decimal(precision=" << static_cast<unsigned>(dtype.decimal_precision()) | ||
| << ", scale=" << static_cast<int>(dtype.decimal_scale()) << ")"; | ||
| break; | ||
| } | ||
| std::cout << (dtype.nullable() ? '?' : ' ') << '\n'; | ||
| } | ||
|
|
||
| int main(int argc, char **argv) { | ||
| if (argc != 2) { | ||
| std::cerr << "Usage: dtype <file glob>\n"; | ||
| return 1; | ||
| } | ||
|
|
||
| Session session; | ||
| DataSource ds = DataSource::open(session, {argv[1]}); | ||
| DataType dt = ds.dtype(); | ||
| std::cout << "dtype: "; | ||
| print_dtype(dt); | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include <vortex/data_source.hpp> | ||
| #include <vortex/writer.hpp> | ||
|
|
||
| using namespace std::string_view_literals; | ||
| using namespace vortex; | ||
| using namespace expr; | ||
| using namespace ops; // overloaded >= for Expressions | ||
| namespace fs = std::filesystem; | ||
|
|
||
| int main() { | ||
| const Session session; | ||
| const DataSource ds = DataSource::open(session, {"people*.vortex", "me.vortex"}); | ||
| Scan scan = ds.scan({.filter = col("height") >= lit<uint16_t>(50)}); | ||
|
|
||
| for (Partition &partition : scan.partitions()) { | ||
| for (Array &array : partition.batches()) { | ||
| const Array age = array.field("age"); | ||
| const PrimitiveView<uint8_t> age_view = age.values<uint8_t>(session); | ||
| const std::span<const uint8_t> age_values = age_view.values(); | ||
| for (uint8_t value : age_values) { | ||
| std::cout << int(value) << " "; | ||
| } | ||
| } | ||
| } | ||
| std::cout << "\n"; | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // SPDX-License-Identifier: CC-BY-4.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include <unistd.h> | ||
|
|
||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include <vortex/data_source.hpp> | ||
| #include <vortex/estimate.hpp> | ||
|
|
||
| using vortex::DataSource; | ||
| using vortex::Estimate; | ||
| using vortex::Scan; | ||
| using vortex::Session; | ||
|
|
||
| static void print_estimate(const char *what, const Estimate &est) { | ||
| using enum vortex::EstimateType; | ||
| switch (est.type()) { | ||
| case Unknown: | ||
| std::cout << what << ": unknown\n"; | ||
| break; | ||
| case Exact: | ||
| std::cout << what << ": " << est.value() << '\n'; | ||
| break; | ||
| case Inexact: | ||
| std::cout << what << ": at most " << est.value() << '\n'; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| struct ScanStats { | ||
| size_t partitions = 0; | ||
| size_t arrays = 0; | ||
| size_t rows = 0; | ||
| }; | ||
|
|
||
| static ScanStats worker(Scan &scan) { | ||
| ScanStats stats; | ||
| while (auto partition = scan.next_partition()) { | ||
| ++stats.partitions; | ||
| while (auto array = partition->next()) { | ||
| ++stats.arrays; | ||
| stats.rows += array->size(); | ||
| } | ||
| } | ||
| return stats; | ||
| } | ||
|
|
||
| int main(int argc, char **argv) { | ||
| size_t num_threads = 0; | ||
| int opt = 0; | ||
| while ((opt = getopt(argc, argv, "j:")) != -1) { | ||
| switch (opt) { | ||
| case 'j': | ||
| num_threads = static_cast<size_t>(std::atoi(optarg)); | ||
| break; | ||
| default: | ||
| std::cerr << "Multi-threaded file scan\nUsage: scan [-j " | ||
| "threads] <file glob>\n"; | ||
| return 1; | ||
| } | ||
| } | ||
| if (optind + 1 != argc) { | ||
| std::cerr << "Multi-threaded file scan\nUsage: scan [-j threads] <file " | ||
| "glob>\n"; | ||
| return 1; | ||
| } | ||
|
|
||
| const Session session; | ||
| std::cout << "Opening files: " << argv[optind] << '\n'; | ||
| const DataSource ds = DataSource::open(session, {argv[optind]}); | ||
|
|
||
| print_estimate("Data source row count", ds.row_count()); | ||
|
|
||
| Scan scan = ds.scan(); | ||
| print_estimate("Partition count", scan.partition_count()); | ||
|
|
||
| if (num_threads == 0) { | ||
| num_threads = scan.partition_count().value_or(1); | ||
| } | ||
|
|
||
| std::cout << "Starting scan, using " << num_threads << " threads\n"; | ||
| std::vector<std::thread> threads; | ||
| threads.reserve(num_threads); | ||
| std::vector<ScanStats> results(num_threads); | ||
|
|
||
| for (size_t i = 0; i < num_threads; ++i) { | ||
| threads.emplace_back([i, &scan, &results] { results[i] = worker(scan); }); | ||
| } | ||
| for (auto &t : threads) { | ||
| t.join(); | ||
| } | ||
|
|
||
| ScanStats total; | ||
| for (const auto &r : results) { | ||
| total.partitions += r.partitions; | ||
| total.arrays += r.arrays; | ||
| total.rows += r.rows; | ||
| } | ||
| std::cout << "Finished scan, processed " << total.partitions << " partitions, " << total.arrays | ||
| << " arrays, " << total.rows << " rows\n"; | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.