Skip to content
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

GH-45344: [C++][Testing] Generic StepGenerator #45345

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 1 addition & 2 deletions cpp/src/arrow/acero/order_by_node_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ static constexpr int kRowsPerBatch = 4;
static constexpr int kNumBatches = 32;

std::shared_ptr<Table> TestTable() {
return gen::Gen({{"up", gen::Step()},
{"down", gen::Step(/*start=*/0, /*step=*/-1, /*signed_int=*/true)}})
return gen::Gen({{"up", gen::Step()}, {"down", gen::Step(/*start=*/0, /*step=*/-1)}})
->FailOnError()
->Table(kRowsPerBatch, kNumBatches);
}
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/acero/sorted_merge_node_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ namespace arrow::acero {

std::shared_ptr<Table> TestTable(int start, int step, int rows_per_batch,
int num_batches) {
return gen::Gen({{"timestamp", gen::Step(start, step, /*signed_int=*/true)},
{"str", gen::Random(utf8())}})
return gen::Gen({{"timestamp", gen::Step(start, step)}, {"str", gen::Random(utf8())}})
->FailOnError()
->Table(rows_per_batch, num_batches);
}
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
arrow_install_all_headers("arrow/testing")

if(ARROW_BUILD_TESTS)
add_arrow_test(random_test)
add_arrow_test(generator_test)
add_arrow_test(gtest_util_test)
add_arrow_test(random_test)

if(ARROW_FILESYSTEM)
add_library(arrow_filesystem_example MODULE examplefs.cc)
Expand Down
41 changes: 0 additions & 41 deletions cpp/src/arrow/testing/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include "arrow/array.h"
#include "arrow/buffer.h"
#include "arrow/builder.h"
#include "arrow/compute/exec.h"
#include "arrow/datum.h"
#include "arrow/record_batch.h"
Expand Down Expand Up @@ -220,42 +219,6 @@ class ConstantGenerator : public ArrayGenerator {
std::shared_ptr<Scalar> value_;
};

class StepGenerator : public ArrayGenerator {
public:
StepGenerator(uint32_t start, uint32_t step, bool signed_int)
: start_(start), step_(step), signed_int_(signed_int) {}

template <typename BuilderType, typename CType>
Result<std::shared_ptr<Array>> DoGenerate(int64_t num_rows) {
BuilderType builder;
ARROW_RETURN_NOT_OK(builder.Reserve(num_rows));
CType val = start_;
for (int64_t i = 0; i < num_rows; i++) {
builder.UnsafeAppend(val);
val += step_;
}
start_ = val;
return builder.Finish();
}

Result<std::shared_ptr<Array>> Generate(int64_t num_rows) override {
if (signed_int_) {
return DoGenerate<Int32Builder, int32_t>(num_rows);
} else {
return DoGenerate<UInt32Builder, uint32_t>(num_rows);
}
}

std::shared_ptr<DataType> type() const override {
return signed_int_ ? int32() : uint32();
}

private:
uint32_t start_;
uint32_t step_;
bool signed_int_;
};

static constexpr random::SeedType kTestSeed = 42;

class RandomGenerator : public ArrayGenerator {
Expand Down Expand Up @@ -405,10 +368,6 @@ std::shared_ptr<ArrayGenerator> Constant(std::shared_ptr<Scalar> value) {
return std::make_shared<ConstantGenerator>(std::move(value));
}

std::shared_ptr<ArrayGenerator> Step(uint32_t start, uint32_t step, bool signed_int) {
return std::make_shared<StepGenerator>(start, step, signed_int);
}

std::shared_ptr<ArrayGenerator> Random(std::shared_ptr<DataType> type) {
return std::make_shared<RandomGenerator>(std::move(type));
}
Expand Down
43 changes: 40 additions & 3 deletions cpp/src/arrow/testing/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>

#include "arrow/array/array_base.h"
#include "arrow/builder.h"
#include "arrow/compute/type_fwd.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/visibility.h"
Expand Down Expand Up @@ -301,12 +302,48 @@ ARROW_TESTING_EXPORT std::shared_ptr<DataGenerator> Gen(
/// make a generator that returns a constant value
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Constant(
std::shared_ptr<Scalar> value);

/// make a generator that returns an incrementing value
///
/// Note: overflow is not prevented standard unsigned integer overflow applies
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Step(uint32_t start = 0,
uint32_t step = 1,
bool signed_int = false);
template <typename T = uint32_t>
std::shared_ptr<ArrayGenerator> Step(T start = 0, T step = 1) {
class StepGenerator : public ArrayGenerator {
public:
// Use [[maybe_unused]] to avoid a compiler warning in Clang versions before 15 that
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is weird but it seems to be a clang bug.

CI failure: https://github.com/apache/arrow/actions/runs/12952919478/job/36131370510?pr=45345

/arrow/cpp/src/arrow/testing/generator.h:313:11: error: unused type alias 'ArrowType' [-Werror,-Wunused-local-typedef]
    using ArrowType = typename CTypeTraits<T>::ArrowType;
          ^

A SO thread: https://stackoverflow.com/questions/50205243/clang-emits-an-unused-type-alias-warning-for-a-type-alias-that-is-used

Reproduction on Clang 14: https://godbolt.org/z/hsnh6cGh8

Clang 15 (works fine): https://godbolt.org/z/x5zTe7j64

// incorrectly reports 'unused type alias'.
using ArrowType [[maybe_unused]] = typename CTypeTraits<T>::ArrowType;
static_assert(is_number_type<ArrowType>::value,
"Step generator only supports numeric types");

StepGenerator(T start, T step) : start_(start), step_(step) {}

Result<std::shared_ptr<Array>> Generate(int64_t num_rows) override {
using BuilderType = typename TypeTraits<ArrowType>::BuilderType;

BuilderType builder;
ARROW_RETURN_NOT_OK(builder.Reserve(num_rows));
T val = start_;
for (int64_t i = 0; i < num_rows; i++) {
builder.UnsafeAppend(val);
val += step_;
}
start_ = val;
return builder.Finish();
}

std::shared_ptr<DataType> type() const override {
return TypeTraits<ArrowType>::type_singleton();
}

private:
T start_;
T step_;
};

return std::make_shared<StepGenerator>(start, step);
}

/// make a generator that returns a random value
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Random(
std::shared_ptr<DataType> type);
Expand Down
73 changes: 73 additions & 0 deletions cpp/src/arrow/testing/generator_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 <gtest/gtest.h>

#include "arrow/testing/generator.h"

namespace arrow::gen {

template <typename CType>
void CheckStep(const Array& result, CType start, CType step, int64_t length) {
using ArrowType = typename CTypeTraits<CType>::ArrowType;

ASSERT_EQ(result.type_id(), TypeTraits<ArrowType>::type_singleton()->id());
ASSERT_EQ(result.length(), length);
ASSERT_EQ(result.null_bitmap(), nullptr);
auto data = result.data()->GetValues<CType>(1);
CType current = start;
for (int64_t i = 0; i < length; ++i) {
ASSERT_EQ(data[i], current);
current += step;
}
}

TEST(StepTest, Default) {
for (auto length : {0, 1, 1024}) {
ARROW_SCOPED_TRACE("length=" + std::to_string(length));
ASSERT_OK_AND_ASSIGN(auto array, Step()->Generate(length));
CheckStep<uint32_t>(*array, 0, 1, length);
}
}

using NumericCTypes = ::testing::Types<int8_t, uint8_t, int16_t, uint16_t, int32_t,
uint32_t, int64_t, uint64_t, float, double>;

template <typename CType>
class TypedStepTest : public ::testing::Test {};

TYPED_TEST_SUITE(TypedStepTest, NumericCTypes);

TYPED_TEST(TypedStepTest, Basic) {
for (auto length : {0, 1, 1024}) {
ARROW_SCOPED_TRACE("length=" + std::to_string(length));
for (TypeParam start :
{std::numeric_limits<TypeParam>::min(), static_cast<TypeParam>(0)}) {
ARROW_SCOPED_TRACE("start=" + std::to_string(start));
for (TypeParam step :
{static_cast<TypeParam>(0), std::numeric_limits<TypeParam>::epsilon(),
static_cast<TypeParam>(std::numeric_limits<TypeParam>::max() /
(length + 1))}) {
ARROW_SCOPED_TRACE("step=" + std::to_string(step));
ASSERT_OK_AND_ASSIGN(auto array, Step(start, step)->Generate(length));
CheckStep(*array, start, step, length);
}
}
}
}

} // namespace arrow::gen
Loading