Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions udf-runner-cpp/v2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

package(default_visibility = ["//visibility:public"])

alias(
name = "arrow_core",
actual = "@v2_arrow//:arrow_core",
)

filegroup(
name = "arrow_c_data_demo_exports",
srcs = ["arrow_c_data_demo.exports.map"],
)

cc_binary(
name = "libarrow_c_data_demo.so",
srcs = ["arrow_c_data_demo.cc"],
additional_linker_inputs = [":arrow_c_data_demo_exports"],
copts = [
"-std=c++20",
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
],
linkopts = [
"-Wl,--exclude-libs,ALL",
"-Wl,--exclude-libs,libarrow_core.a",
"-Wl,--version-script=$(location :arrow_c_data_demo_exports)",
],
linkshared = 1,
deps = [":arrow_core"],
)

cc_test(
name = "arrow_core_test",
srcs = ["arrow_core_test.cc"],
copts = ["-std=c++20"],
deps = [":arrow_core"],
)

cc_test(
name = "arrow_c_data_demo_test",
srcs = ["arrow_c_data_demo_test.cc"],
copts = ["-std=c++20"],
data = [":libarrow_c_data_demo.so"],
args = ["$(location :libarrow_c_data_demo.so)"],
linkopts = ["-ldl"],
target_compatible_with = ["@platforms//os:linux"],
deps = [":arrow_core"],
)

cc_library(
name = "json_schema",
hdrs = ["include/exasol/udf/v2/json_schema.hpp"],
Expand Down
28 changes: 28 additions & 0 deletions udf-runner-cpp/v2/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ http_archive = use_repo_rule(
"http_archive",
)

http_archive(
name = "v2_xsimd",
urls = ["https://github.com/xtensor-stack/xsimd/archive/14.2.0.tar.gz"],
sha256 = "21e841ab684b05331e81e7f782431753a029ef7b7d9d6d3ddab837e7782a40ee",
strip_prefix = "xsimd-14.2.0",
build_file_content = """
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "xsimd",
hdrs = glob(["include/**/*.hpp"]),
includes = ["include"],
)
""",
)

http_archive(
name = "v2_arrow",
urls = ["https://github.com/apache/arrow/archive/refs/tags/apache-arrow-25.0.0.tar.gz"],
sha256 = "7ecdd404862c3d312601457861a11c30bb8f520328693e1323e3db1db985dbe5",
strip_prefix = "arrow-apache-arrow-25.0.0",
build_file = "//third_party/arrow:arrow.BUILD",
patches = ["//third_party/arrow:config_headers.patch"],
patch_args = ["-p1"],
)

http_archive(
name = "v2_nlohmann_json",
urls = ["https://github.com/nlohmann/json/archive/refs/tags/v3.12.0.tar.gz"],
Expand Down
124 changes: 124 additions & 0 deletions udf-runner-cpp/v2/arrow_c_data_demo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <arrow/api.h>
#include <arrow/c/bridge.h>

#include <cstring>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#if defined(_WIN32)
#define UDF_RUNNER_CPP_V2_EXPORT __declspec(dllexport)
#else
#define UDF_RUNNER_CPP_V2_EXPORT __attribute__((visibility("default")))
#endif

namespace {

thread_local std::string g_last_error;

void SetLastError(const arrow::Status& status) {
g_last_error = status.ToString();
}

arrow::Result<std::shared_ptr<arrow::RecordBatch>> MakeDemoRecordBatch() {
arrow::Int64Builder id_builder;
arrow::StringBuilder name_builder;

ARROW_RETURN_NOT_OK(id_builder.Append(1));
ARROW_RETURN_NOT_OK(id_builder.Append(2));
ARROW_RETURN_NOT_OK(id_builder.Append(3));
ARROW_RETURN_NOT_OK(id_builder.Append(4));

ARROW_RETURN_NOT_OK(name_builder.Append("alpha"));
ARROW_RETURN_NOT_OK(name_builder.Append("beta"));
ARROW_RETURN_NOT_OK(name_builder.Append("gamma"));
ARROW_RETURN_NOT_OK(name_builder.Append("delta"));

std::shared_ptr<arrow::Array> ids;
std::shared_ptr<arrow::Array> names;
ARROW_RETURN_NOT_OK(id_builder.Finish(&ids));
ARROW_RETURN_NOT_OK(name_builder.Finish(&names));
const int64_t num_rows = ids->length();

auto schema = arrow::schema({
arrow::field("id", arrow::int64()),
arrow::field("name", arrow::utf8()),
});
return arrow::RecordBatch::Make(schema, num_rows, {std::move(ids), std::move(names)});
}

arrow::Status ExportDemoRecordBatch(ArrowArray* out_array, ArrowSchema* out_schema) {
if (out_array == nullptr || out_schema == nullptr) {
return arrow::Status::Invalid("output ArrowArray and ArrowSchema pointers must not be null");
}

std::memset(out_array, 0, sizeof(*out_array));
std::memset(out_schema, 0, sizeof(*out_schema));

auto maybe_batch = MakeDemoRecordBatch();
if (!maybe_batch.ok()) {
return maybe_batch.status();
}
ARROW_RETURN_NOT_OK(arrow::ExportRecordBatch(*maybe_batch.ValueOrDie(), out_array, out_schema));
return arrow::Status::OK();
}

arrow::Status ConsumeDemoRecordBatch(ArrowArray* array, ArrowSchema* schema,
int64_t* out_row_count, int64_t* out_id_sum) {
if (array == nullptr || schema == nullptr || out_row_count == nullptr ||
out_id_sum == nullptr) {
return arrow::Status::Invalid("input and output pointers must not be null");
}

ARROW_ASSIGN_OR_RAISE(auto batch, arrow::ImportRecordBatch(array, schema));
if (batch->num_columns() != 2) {
return arrow::Status::Invalid("expected two columns");
}
if (batch->schema()->field(0)->name() != "id" ||
batch->schema()->field(1)->name() != "name") {
return arrow::Status::Invalid("unexpected schema");
}

auto ids = std::static_pointer_cast<arrow::Int64Array>(batch->column(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the id column type before casting

When a caller supplies a valid C Data record batch whose fields are named id and name but whose first column is not int64 (for example, UTF-8), the name-only schema check passes and this unchecked static_pointer_cast treats another array implementation as Int64Array. Subsequent Value() calls can read the wrong buffer layout, producing incorrect sums or undefined behavior instead of returning an error; check the field type before casting.

Useful? React with 👍 / 👎.

int64_t sum = 0;
for (int64_t index = 0; index < ids->length(); ++index) {
if (!ids->IsNull(index)) {
sum += ids->Value(index);
}
}

*out_row_count = batch->num_rows();
*out_id_sum = sum;
return arrow::Status::OK();
}

} // namespace

extern "C" UDF_RUNNER_CPP_V2_EXPORT int udf_runner_cpp_v2_demo_export_record_batch(
ArrowArray* out_array, ArrowSchema* out_schema) {
const arrow::Status status = ExportDemoRecordBatch(out_array, out_schema);
if (!status.ok()) {
SetLastError(status);
return 1;
}
g_last_error.clear();
return 0;
}

extern "C" UDF_RUNNER_CPP_V2_EXPORT int udf_runner_cpp_v2_demo_consume_record_batch(
ArrowArray* array, ArrowSchema* schema, int64_t* out_row_count,
int64_t* out_id_sum) {
const arrow::Status status =
ConsumeDemoRecordBatch(array, schema, out_row_count, out_id_sum);
if (!status.ok()) {
SetLastError(status);
return 1;
}
g_last_error.clear();
return 0;
}

extern "C" UDF_RUNNER_CPP_V2_EXPORT const char* udf_runner_cpp_v2_demo_last_error(void) {
return g_last_error.c_str();
}
9 changes: 9 additions & 0 deletions udf-runner-cpp/v2/arrow_c_data_demo.exports.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
global:
udf_runner_cpp_v2_demo_export_record_batch;
udf_runner_cpp_v2_demo_consume_record_batch;
udf_runner_cpp_v2_demo_last_error;
local:
*;
};

29 changes: 29 additions & 0 deletions udf-runner-cpp/v2/arrow_c_data_demo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <arrow/c/abi.h>

#include <stdint.h>

#if defined(_WIN32)
#define UDF_RUNNER_CPP_V2_EXPORT __declspec(dllexport)
#else
#define UDF_RUNNER_CPP_V2_EXPORT __attribute__((visibility("default")))
#endif

#ifdef __cplusplus
extern "C" {
#endif

UDF_RUNNER_CPP_V2_EXPORT int udf_runner_cpp_v2_demo_export_record_batch(
struct ArrowArray* out_array, struct ArrowSchema* out_schema);

UDF_RUNNER_CPP_V2_EXPORT int udf_runner_cpp_v2_demo_consume_record_batch(
struct ArrowArray* array, struct ArrowSchema* schema, int64_t* out_row_count,
int64_t* out_id_sum);

UDF_RUNNER_CPP_V2_EXPORT const char* udf_runner_cpp_v2_demo_last_error(void);

#ifdef __cplusplus
} // extern "C"
#endif

Loading