Skip to content

Commit

Permalink
ARROW-17695: [C++] Remove Variant class
Browse files Browse the repository at this point in the history
Use std::variant instead.
  • Loading branch information
pitrou committed Sep 15, 2022
1 parent 7cfdfbb commit 1647f0f
Show file tree
Hide file tree
Showing 39 changed files with 191 additions and 1,267 deletions.
2 changes: 1 addition & 1 deletion c_glib/gandiva-glib/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ggandiva_literal_node_get(GGandivaLiteralNode *node)
{
auto gandiva_literal_node =
std::static_pointer_cast<gandiva::LiteralNode>(ggandiva_node_get_raw(GGANDIVA_NODE(node)));
return arrow::util::get<Type>(gandiva_literal_node->holder());
return std::get<Type>(gandiva_literal_node->holder());
}

G_BEGIN_DECLS
Expand Down
32 changes: 7 additions & 25 deletions cpp/gdb_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,17 @@ def value(self):

class Variant:
"""
A arrow::util::Variant<...>.
A `std::variant<...>`.
"""

def __init__(self, val):
self.val = val
self.index = int(self.val['index_'])
try:
# libstdc++ internals
self.index = val['_M_index']
except gdb.error:
# fallback for other C++ standard libraries
self.index = gdb.parse_and_eval(f"{for_evaluation(val)}.index()")
try:
self.value_type = self.val.type.template_argument(self.index)
except RuntimeError:
Expand Down Expand Up @@ -2175,28 +2180,6 @@ def to_string(self):
return f"arrow::util::string_view of size {size}, {data}"


class VariantPrinter:
"""
Pretty-printer for arrow::util::Variant.
"""

def __init__(self, name, val):
self.val = val
self.variant = Variant(val)

def to_string(self):
if self.variant.value_type is None:
return "arrow::util::Variant (uninitialized or corrupt)"
type_desc = (f"arrow::util::Variant of index {self.variant.index} "
f"(actual type {self.variant.value_type})")

value = self.variant.value
if value is None:
return (f"{type_desc}, unavailable value")
else:
return (f"{type_desc}, value {value}")


class FieldPrinter:
"""
Pretty-printer for arrow::Field.
Expand Down Expand Up @@ -2415,7 +2398,6 @@ def to_string(self):
"arrow::Status": StatusPrinter,
"arrow::Table": TablePrinter,
"arrow::util::string_view": StringViewPrinter,
"arrow::util::Variant": VariantPrinter,
"nonstd::sv_lite::basic_string_view": StringViewPrinter,
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/compute/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ struct ExecValue {

struct ARROW_EXPORT ExecResult {
// The default value of the variant is ArraySpan
util::Variant<ArraySpan, std::shared_ptr<ArrayData>> value;
std::variant<ArraySpan, std::shared_ptr<ArrayData>> value;

int64_t length() const {
if (this->is_array_span()) {
Expand All @@ -332,12 +332,12 @@ struct ARROW_EXPORT ExecResult {
}

ArraySpan* array_span() const {
return const_cast<ArraySpan*>(&util::get<ArraySpan>(this->value));
return const_cast<ArraySpan*>(&std::get<ArraySpan>(this->value));
}
bool is_array_span() const { return this->value.index() == 0; }

const std::shared_ptr<ArrayData>& array_data() const {
return util::get<std::shared_ptr<ArrayData>>(this->value);
return std::get<std::shared_ptr<ArrayData>>(this->value);
}

bool is_array_data() const { return this->value.index() == 1; }
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/compute/exec/exec_plan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,12 @@ Result<ExecNode*> Declaration::AddToPlan(ExecPlan* plan,

size_t i = 0;
for (const Input& input : this->inputs) {
if (auto node = util::get_if<ExecNode*>(&input)) {
if (auto node = std::get_if<ExecNode*>(&input)) {
inputs[i++] = *node;
continue;
}
ARROW_ASSIGN_OR_RAISE(inputs[i++],
util::get<Declaration>(input).AddToPlan(plan, registry));
std::get<Declaration>(input).AddToPlan(plan, registry));
}

ARROW_ASSIGN_OR_RAISE(
Expand All @@ -638,7 +638,7 @@ Declaration Declaration::Sequence(std::vector<Declaration> decls) {
decls.pop_back();

receiver->inputs.emplace_back(std::move(input));
receiver = &util::get<Declaration>(receiver->inputs.front());
receiver = &std::get<Declaration>(receiver->inputs.front());
}
return out;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/compute/exec/exec_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ inline Result<ExecNode*> MakeExecNode(
/// inputs may also be Declarations). The node can be constructed and added to a plan
/// with Declaration::AddToPlan, which will recursively construct any inputs as necessary.
struct ARROW_EXPORT Declaration {
using Input = util::Variant<ExecNode*, Declaration>;
using Input = std::variant<ExecNode*, Declaration>;

Declaration() {}

Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/compute/exec/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Expression call(std::string function, std::vector<Expression> arguments,
return Expression(std::move(call));
}

const Datum* Expression::literal() const { return util::get_if<Datum>(impl_.get()); }
const Datum* Expression::literal() const { return std::get_if<Datum>(impl_.get()); }

const Expression::Parameter* Expression::parameter() const {
return util::get_if<Parameter>(impl_.get());
return std::get_if<Parameter>(impl_.get());
}

const FieldRef* Expression::field_ref() const {
Expand All @@ -90,7 +90,7 @@ const FieldRef* Expression::field_ref() const {
}

const Expression::Call* Expression::call() const {
return util::get_if<Call>(impl_.get());
return std::get_if<Call>(impl_.get());
}

const DataType* Expression::type() const {
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/arrow/compute/exec/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>

#include "arrow/compute/type_fwd.h"
#include "arrow/datum.h"
#include "arrow/type_fwd.h"
#include "arrow/util/small_vector.h"
#include "arrow/util/variant.h"

namespace arrow {
namespace compute {
Expand Down Expand Up @@ -127,7 +127,7 @@ class ARROW_EXPORT Expression {
explicit Expression(Parameter parameter);

private:
using Impl = util::Variant<Datum, Parameter, Call>;
using Impl = std::variant<Datum, Parameter, Call>;
std::shared_ptr<Impl> impl_;

ARROW_EXPORT friend bool Identical(const Expression& l, const Expression& r);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/compute/exec/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void PrintTo(const Declaration& decl, std::ostream* os) {

*os << "{";
for (const auto& input : decl.inputs) {
if (auto decl = util::get_if<Declaration>(&input)) {
if (auto decl = std::get_if<Declaration>(&input)) {
PrintTo(*decl, os);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/dataset/discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <memory>
#include <string>
#include <variant>
#include <vector>

#include "arrow/dataset/partition.h"
Expand All @@ -33,7 +34,6 @@
#include "arrow/filesystem/type_fwd.h"
#include "arrow/result.h"
#include "arrow/util/macros.h"
#include "arrow/util/variant.h"

namespace arrow {
namespace dataset {
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/arrow/dataset/file_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <unordered_map>
#include <variant>
#include <vector>

#include "arrow/compute/api_scalar.h"
Expand All @@ -43,7 +44,6 @@
#include "arrow/util/string.h"
#include "arrow/util/task_group.h"
#include "arrow/util/tracing_internal.h"
#include "arrow/util/variant.h"

namespace arrow {

Expand Down Expand Up @@ -154,7 +154,7 @@ struct FileSystemDataset::FragmentSubtrees {
// Forest for skipping fragments based on extracted subtree expressions
compute::Forest forest;
// fragment indices and subtree expressions in forest order
std::vector<util::Variant<int, compute::Expression>> fragments_and_subtrees;
std::vector<std::variant<int, compute::Expression>> fragments_and_subtrees;
};

Result<std::shared_ptr<FileSystemDataset>> FileSystemDataset::Make(
Expand Down Expand Up @@ -242,13 +242,13 @@ Result<FragmentIterator> FileSystemDataset::GetFragmentsImpl(
RETURN_NOT_OK(subtrees_->forest.Visit(
[&](compute::Forest::Ref ref) -> Result<bool> {
if (auto fragment_index =
util::get_if<int>(&subtrees_->fragments_and_subtrees[ref.i])) {
std::get_if<int>(&subtrees_->fragments_and_subtrees[ref.i])) {
fragment_indices.push_back(*fragment_index);
return false;
}

const auto& subtree_expr =
util::get<compute::Expression>(subtrees_->fragments_and_subtrees[ref.i]);
std::get<compute::Expression>(subtrees_->fragments_and_subtrees[ref.i]);
ARROW_ASSIGN_OR_RAISE(auto simplified,
SimplifyWithGuarantee(predicates.back(), subtree_expr));

Expand Down
35 changes: 17 additions & 18 deletions cpp/src/arrow/datum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ Datum::Datum(const RecordBatch& value)

std::shared_ptr<Array> Datum::make_array() const {
DCHECK_EQ(Datum::ARRAY, this->kind());
return MakeArray(util::get<std::shared_ptr<ArrayData>>(this->value));
return MakeArray(std::get<std::shared_ptr<ArrayData>>(this->value));
}

const std::shared_ptr<DataType>& Datum::type() const {
if (this->kind() == Datum::ARRAY) {
return util::get<std::shared_ptr<ArrayData>>(this->value)->type;
return std::get<std::shared_ptr<ArrayData>>(this->value)->type;
}
if (this->kind() == Datum::CHUNKED_ARRAY) {
return util::get<std::shared_ptr<ChunkedArray>>(this->value)->type();
return std::get<std::shared_ptr<ChunkedArray>>(this->value)->type();
}
if (this->kind() == Datum::SCALAR) {
return util::get<std::shared_ptr<Scalar>>(this->value)->type;
return std::get<std::shared_ptr<Scalar>>(this->value)->type;
}
static std::shared_ptr<DataType> no_type;
return no_type;
}

const std::shared_ptr<Schema>& Datum::schema() const {
if (this->kind() == Datum::RECORD_BATCH) {
return util::get<std::shared_ptr<RecordBatch>>(this->value)->schema();
return std::get<std::shared_ptr<RecordBatch>>(this->value)->schema();
}
if (this->kind() == Datum::TABLE) {
return util::get<std::shared_ptr<Table>>(this->value)->schema();
return std::get<std::shared_ptr<Table>>(this->value)->schema();
}
static std::shared_ptr<Schema> no_schema;
return no_schema;
Expand All @@ -100,13 +100,13 @@ const std::shared_ptr<Schema>& Datum::schema() const {
int64_t Datum::length() const {
switch (this->kind()) {
case Datum::ARRAY:
return util::get<std::shared_ptr<ArrayData>>(this->value)->length;
return std::get<std::shared_ptr<ArrayData>>(this->value)->length;
case Datum::CHUNKED_ARRAY:
return util::get<std::shared_ptr<ChunkedArray>>(this->value)->length();
return std::get<std::shared_ptr<ChunkedArray>>(this->value)->length();
case Datum::RECORD_BATCH:
return util::get<std::shared_ptr<RecordBatch>>(this->value)->num_rows();
return std::get<std::shared_ptr<RecordBatch>>(this->value)->num_rows();
case Datum::TABLE:
return util::get<std::shared_ptr<Table>>(this->value)->num_rows();
return std::get<std::shared_ptr<Table>>(this->value)->num_rows();
case Datum::SCALAR:
return 1;
default:
Expand All @@ -117,14 +117,13 @@ int64_t Datum::length() const {
int64_t Datum::TotalBufferSize() const {
switch (this->kind()) {
case Datum::ARRAY:
return util::TotalBufferSize(*util::get<std::shared_ptr<ArrayData>>(this->value));
return util::TotalBufferSize(*std::get<std::shared_ptr<ArrayData>>(this->value));
case Datum::CHUNKED_ARRAY:
return util::TotalBufferSize(
*util::get<std::shared_ptr<ChunkedArray>>(this->value));
return util::TotalBufferSize(*std::get<std::shared_ptr<ChunkedArray>>(this->value));
case Datum::RECORD_BATCH:
return util::TotalBufferSize(*util::get<std::shared_ptr<RecordBatch>>(this->value));
return util::TotalBufferSize(*std::get<std::shared_ptr<RecordBatch>>(this->value));
case Datum::TABLE:
return util::TotalBufferSize(*util::get<std::shared_ptr<Table>>(this->value));
return util::TotalBufferSize(*std::get<std::shared_ptr<Table>>(this->value));
case Datum::SCALAR:
return 0;
default:
Expand All @@ -135,11 +134,11 @@ int64_t Datum::TotalBufferSize() const {

int64_t Datum::null_count() const {
if (this->kind() == Datum::ARRAY) {
return util::get<std::shared_ptr<ArrayData>>(this->value)->GetNullCount();
return std::get<std::shared_ptr<ArrayData>>(this->value)->GetNullCount();
} else if (this->kind() == Datum::CHUNKED_ARRAY) {
return util::get<std::shared_ptr<ChunkedArray>>(this->value)->null_count();
return std::get<std::shared_ptr<ChunkedArray>>(this->value)->null_count();
} else if (this->kind() == Datum::SCALAR) {
const auto& val = *util::get<std::shared_ptr<Scalar>>(this->value);
const auto& val = *std::get<std::shared_ptr<Scalar>>(this->value);
return val.is_valid ? 0 : 1;
} else {
DCHECK(false) << "This function only valid for array-like values";
Expand Down
18 changes: 9 additions & 9 deletions cpp/src/arrow/datum.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>

#include "arrow/array/data.h"
Expand All @@ -30,7 +31,6 @@
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/macros.h"
#include "arrow/util/variant.h" // IWYU pragma: export
#include "arrow/util/visibility.h"

namespace arrow {
Expand All @@ -51,9 +51,9 @@ struct ARROW_EXPORT Datum {
// current variant does not have a length.
static constexpr int64_t kUnknownLength = -1;

util::Variant<Empty, std::shared_ptr<Scalar>, std::shared_ptr<ArrayData>,
std::shared_ptr<ChunkedArray>, std::shared_ptr<RecordBatch>,
std::shared_ptr<Table>>
std::variant<Empty, std::shared_ptr<Scalar>, std::shared_ptr<ArrayData>,
std::shared_ptr<ChunkedArray>, std::shared_ptr<RecordBatch>,
std::shared_ptr<Table>>
value;

/// \brief Empty datum, to be populated elsewhere
Expand Down Expand Up @@ -136,7 +136,7 @@ struct ARROW_EXPORT Datum {
}

const std::shared_ptr<ArrayData>& array() const {
return util::get<std::shared_ptr<ArrayData>>(this->value);
return std::get<std::shared_ptr<ArrayData>>(this->value);
}

/// \brief The sum of bytes in each buffer referenced by the datum
Expand All @@ -149,19 +149,19 @@ struct ARROW_EXPORT Datum {
std::shared_ptr<Array> make_array() const;

const std::shared_ptr<ChunkedArray>& chunked_array() const {
return util::get<std::shared_ptr<ChunkedArray>>(this->value);
return std::get<std::shared_ptr<ChunkedArray>>(this->value);
}

const std::shared_ptr<RecordBatch>& record_batch() const {
return util::get<std::shared_ptr<RecordBatch>>(this->value);
return std::get<std::shared_ptr<RecordBatch>>(this->value);
}

const std::shared_ptr<Table>& table() const {
return util::get<std::shared_ptr<Table>>(this->value);
return std::get<std::shared_ptr<Table>>(this->value);
}

const std::shared_ptr<Scalar>& scalar() const {
return util::get<std::shared_ptr<Scalar>>(this->value);
return std::get<std::shared_ptr<Scalar>>(this->value);
}

template <typename ExactType>
Expand Down
Loading

0 comments on commit 1647f0f

Please sign in to comment.