Created
June 23, 2023 17:57
-
-
Save westonpace/a45738e5a356324d410cba2c2713b1fd to your computer and use it in GitHub Desktop.
Example of using compute functions to compare arrays
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
// Licensed to the Apache Software Foundation (ASF) under one | |
// or more contributor license agreements. See the NOTICE file | |
// distributed with this work for additional information | |
// regarding copyright ownership. The ASF licenses this file | |
// to you under the Apache License, Version 2.0 (the | |
// "License"); you may not use this file except in compliance | |
// with the License. You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, | |
// software distributed under the License is distributed on an | |
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
// KIND, either express or implied. See the License for the | |
// specific language governing permissions and limitations | |
// under the License. | |
#include <arrow/array/builder_binary.h> | |
#include <arrow/array/builder_decimal.h> | |
#include <arrow/array/builder_primitive.h> | |
#include <arrow/compute/api.h> | |
#include <arrow/compute/exec.h> | |
#include <arrow/result.h> | |
#include <arrow/status.h> | |
#include <arrow/table.h> | |
#include <arrow/type_fwd.h> | |
#include <iostream> | |
#include <memory> | |
#include <stdexcept> | |
namespace { | |
// Utility functions for converting arrow::Result & arrow::Status into | |
// exceptions | |
template <typename T> T throw_or_assign(arrow::Result<T> value) { | |
if (!value.ok()) { | |
throw std::runtime_error(value.status().ToString()); | |
} | |
return value.MoveValueUnsafe(); | |
} | |
void throw_not_ok(const arrow::Status &status) { | |
if (!status.ok()) { | |
throw std::runtime_error(status.ToString()); | |
} | |
} | |
std::shared_ptr<arrow::Table> | |
CreateSampleTable(const std::vector<std::optional<int32_t>> values) { | |
arrow::Int32Builder x_builder; | |
for (std::optional<int32_t> value : values) { | |
if (value.has_value()) { | |
throw_not_ok(x_builder.Append(*value)); | |
} else { | |
throw_not_ok(x_builder.AppendNull()); | |
} | |
} | |
std::shared_ptr<arrow::Array> x_arr = throw_or_assign(x_builder.Finish()); | |
std::shared_ptr<arrow::Schema> schema = | |
arrow::schema({arrow::field("x", arrow::int32())}); | |
return arrow::Table::Make(schema, {x_arr}, x_arr->length()); | |
} | |
void RunTest() { | |
std::shared_ptr<arrow::Table> table_a = CreateSampleTable({1, 5, 7}); | |
std::shared_ptr<arrow::Table> table_b = CreateSampleTable({1, {}, 4}); | |
std::shared_ptr<arrow::Array> array_a = table_a->column(0)->chunk(0); | |
std::shared_ptr<arrow::Array> array_b = table_b->column(0)->chunk(0); | |
std::cout << "LHS" << array_a->ToString() << std::endl; | |
std::cout << "RHS" << array_b->ToString() << std::endl; | |
auto call_and_print = [&](std::string_view op) { | |
arrow::Datum op_result = throw_or_assign( | |
arrow::compute::CallFunction(std::string(op), {array_a, array_b})); | |
std::cout << op << ": " << op_result.make_array()->ToString() << std::endl; | |
}; | |
call_and_print("equal"); | |
call_and_print("less"); | |
call_and_print("less_equal"); | |
call_and_print("greater"); | |
call_and_print("greater_equal"); | |
} | |
} // namespace | |
int main() { | |
try { | |
RunTest(); | |
} catch (std::runtime_error &err) { | |
std::cerr << "An error occurred: " << err.what() << std::endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment