diff --git a/c_glib/gandiva-glib/node.cpp b/c_glib/gandiva-glib/node.cpp index d42d4801b7e58..1ced7754a707a 100644 --- a/c_glib/gandiva-glib/node.cpp +++ b/c_glib/gandiva-glib/node.cpp @@ -29,7 +29,7 @@ ggandiva_literal_node_get(GGandivaLiteralNode *node) { auto gandiva_literal_node = std::static_pointer_cast(ggandiva_node_get_raw(GGANDIVA_NODE(node))); - return arrow::util::get(gandiva_literal_node->holder()); + return std::get(gandiva_literal_node->holder()); } G_BEGIN_DECLS diff --git a/cpp/gdb_arrow.py b/cpp/gdb_arrow.py index 5421b4ffb15d8..5d7e1719afee3 100644 --- a/cpp/gdb_arrow.py +++ b/cpp/gdb_arrow.py @@ -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: @@ -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. @@ -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, } diff --git a/cpp/src/arrow/compute/exec.h b/cpp/src/arrow/compute/exec.h index d03b073bb88a0..12cce42038d71 100644 --- a/cpp/src/arrow/compute/exec.h +++ b/cpp/src/arrow/compute/exec.h @@ -313,7 +313,7 @@ struct ExecValue { struct ARROW_EXPORT ExecResult { // The default value of the variant is ArraySpan - util::Variant> value; + std::variant> value; int64_t length() const { if (this->is_array_span()) { @@ -332,12 +332,12 @@ struct ARROW_EXPORT ExecResult { } ArraySpan* array_span() const { - return const_cast(&util::get(this->value)); + return const_cast(&std::get(this->value)); } bool is_array_span() const { return this->value.index() == 0; } const std::shared_ptr& array_data() const { - return util::get>(this->value); + return std::get>(this->value); } bool is_array_data() const { return this->value.index() == 1; } diff --git a/cpp/src/arrow/compute/exec/exec_plan.cc b/cpp/src/arrow/compute/exec/exec_plan.cc index 322b5f5e456c6..057e1ace5cd0e 100644 --- a/cpp/src/arrow/compute/exec/exec_plan.cc +++ b/cpp/src/arrow/compute/exec/exec_plan.cc @@ -612,12 +612,12 @@ Result Declaration::AddToPlan(ExecPlan* plan, size_t i = 0; for (const Input& input : this->inputs) { - if (auto node = util::get_if(&input)) { + if (auto node = std::get_if(&input)) { inputs[i++] = *node; continue; } ARROW_ASSIGN_OR_RAISE(inputs[i++], - util::get(input).AddToPlan(plan, registry)); + std::get(input).AddToPlan(plan, registry)); } ARROW_ASSIGN_OR_RAISE( @@ -638,7 +638,7 @@ Declaration Declaration::Sequence(std::vector decls) { decls.pop_back(); receiver->inputs.emplace_back(std::move(input)); - receiver = &util::get(receiver->inputs.front()); + receiver = &std::get(receiver->inputs.front()); } return out; } diff --git a/cpp/src/arrow/compute/exec/exec_plan.h b/cpp/src/arrow/compute/exec/exec_plan.h index 93d0655124114..3ff2340856f94 100644 --- a/cpp/src/arrow/compute/exec/exec_plan.h +++ b/cpp/src/arrow/compute/exec/exec_plan.h @@ -449,7 +449,7 @@ inline Result 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; + using Input = std::variant; Declaration() {} diff --git a/cpp/src/arrow/compute/exec/expression.cc b/cpp/src/arrow/compute/exec/expression.cc index 16942a0f80f11..d23838303f7fd 100644 --- a/cpp/src/arrow/compute/exec/expression.cc +++ b/cpp/src/arrow/compute/exec/expression.cc @@ -76,10 +76,10 @@ Expression call(std::string function, std::vector arguments, return Expression(std::move(call)); } -const Datum* Expression::literal() const { return util::get_if(impl_.get()); } +const Datum* Expression::literal() const { return std::get_if(impl_.get()); } const Expression::Parameter* Expression::parameter() const { - return util::get_if(impl_.get()); + return std::get_if(impl_.get()); } const FieldRef* Expression::field_ref() const { @@ -90,7 +90,7 @@ const FieldRef* Expression::field_ref() const { } const Expression::Call* Expression::call() const { - return util::get_if(impl_.get()); + return std::get_if(impl_.get()); } const DataType* Expression::type() const { diff --git a/cpp/src/arrow/compute/exec/expression.h b/cpp/src/arrow/compute/exec/expression.h index a872e799597c0..d49fe5c893ec9 100644 --- a/cpp/src/arrow/compute/exec/expression.h +++ b/cpp/src/arrow/compute/exec/expression.h @@ -22,13 +22,13 @@ #include #include #include +#include #include #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 { @@ -127,7 +127,7 @@ class ARROW_EXPORT Expression { explicit Expression(Parameter parameter); private: - using Impl = util::Variant; + using Impl = std::variant; std::shared_ptr impl_; ARROW_EXPORT friend bool Identical(const Expression& l, const Expression& r); diff --git a/cpp/src/arrow/compute/exec/test_util.cc b/cpp/src/arrow/compute/exec/test_util.cc index 8c8c3f6b3b2f6..2abe6e9e0290f 100644 --- a/cpp/src/arrow/compute/exec/test_util.cc +++ b/cpp/src/arrow/compute/exec/test_util.cc @@ -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(&input)) { + if (auto decl = std::get_if(&input)) { PrintTo(*decl, os); } } diff --git a/cpp/src/arrow/dataset/discovery.h b/cpp/src/arrow/dataset/discovery.h index 40c020519555f..238b33e40fe25 100644 --- a/cpp/src/arrow/dataset/discovery.h +++ b/cpp/src/arrow/dataset/discovery.h @@ -25,6 +25,7 @@ #include #include +#include #include #include "arrow/dataset/partition.h" @@ -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 { diff --git a/cpp/src/arrow/dataset/file_base.cc b/cpp/src/arrow/dataset/file_base.cc index 64daf08fd0317..23f4d09a9d2fa 100644 --- a/cpp/src/arrow/dataset/file_base.cc +++ b/cpp/src/arrow/dataset/file_base.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include "arrow/compute/api_scalar.h" @@ -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 { @@ -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> fragments_and_subtrees; + std::vector> fragments_and_subtrees; }; Result> FileSystemDataset::Make( @@ -242,13 +242,13 @@ Result FileSystemDataset::GetFragmentsImpl( RETURN_NOT_OK(subtrees_->forest.Visit( [&](compute::Forest::Ref ref) -> Result { if (auto fragment_index = - util::get_if(&subtrees_->fragments_and_subtrees[ref.i])) { + std::get_if(&subtrees_->fragments_and_subtrees[ref.i])) { fragment_indices.push_back(*fragment_index); return false; } const auto& subtree_expr = - util::get(subtrees_->fragments_and_subtrees[ref.i]); + std::get(subtrees_->fragments_and_subtrees[ref.i]); ARROW_ASSIGN_OR_RAISE(auto simplified, SimplifyWithGuarantee(predicates.back(), subtree_expr)); diff --git a/cpp/src/arrow/datum.cc b/cpp/src/arrow/datum.cc index f06e97a20ec4b..d0b5cf62c61be 100644 --- a/cpp/src/arrow/datum.cc +++ b/cpp/src/arrow/datum.cc @@ -69,18 +69,18 @@ Datum::Datum(const RecordBatch& value) std::shared_ptr Datum::make_array() const { DCHECK_EQ(Datum::ARRAY, this->kind()); - return MakeArray(util::get>(this->value)); + return MakeArray(std::get>(this->value)); } const std::shared_ptr& Datum::type() const { if (this->kind() == Datum::ARRAY) { - return util::get>(this->value)->type; + return std::get>(this->value)->type; } if (this->kind() == Datum::CHUNKED_ARRAY) { - return util::get>(this->value)->type(); + return std::get>(this->value)->type(); } if (this->kind() == Datum::SCALAR) { - return util::get>(this->value)->type; + return std::get>(this->value)->type; } static std::shared_ptr no_type; return no_type; @@ -88,10 +88,10 @@ const std::shared_ptr& Datum::type() const { const std::shared_ptr& Datum::schema() const { if (this->kind() == Datum::RECORD_BATCH) { - return util::get>(this->value)->schema(); + return std::get>(this->value)->schema(); } if (this->kind() == Datum::TABLE) { - return util::get>(this->value)->schema(); + return std::get>(this->value)->schema(); } static std::shared_ptr no_schema; return no_schema; @@ -100,13 +100,13 @@ const std::shared_ptr& Datum::schema() const { int64_t Datum::length() const { switch (this->kind()) { case Datum::ARRAY: - return util::get>(this->value)->length; + return std::get>(this->value)->length; case Datum::CHUNKED_ARRAY: - return util::get>(this->value)->length(); + return std::get>(this->value)->length(); case Datum::RECORD_BATCH: - return util::get>(this->value)->num_rows(); + return std::get>(this->value)->num_rows(); case Datum::TABLE: - return util::get>(this->value)->num_rows(); + return std::get>(this->value)->num_rows(); case Datum::SCALAR: return 1; default: @@ -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>(this->value)); + return util::TotalBufferSize(*std::get>(this->value)); case Datum::CHUNKED_ARRAY: - return util::TotalBufferSize( - *util::get>(this->value)); + return util::TotalBufferSize(*std::get>(this->value)); case Datum::RECORD_BATCH: - return util::TotalBufferSize(*util::get>(this->value)); + return util::TotalBufferSize(*std::get>(this->value)); case Datum::TABLE: - return util::TotalBufferSize(*util::get>(this->value)); + return util::TotalBufferSize(*std::get>(this->value)); case Datum::SCALAR: return 0; default: @@ -135,11 +134,11 @@ int64_t Datum::TotalBufferSize() const { int64_t Datum::null_count() const { if (this->kind() == Datum::ARRAY) { - return util::get>(this->value)->GetNullCount(); + return std::get>(this->value)->GetNullCount(); } else if (this->kind() == Datum::CHUNKED_ARRAY) { - return util::get>(this->value)->null_count(); + return std::get>(this->value)->null_count(); } else if (this->kind() == Datum::SCALAR) { - const auto& val = *util::get>(this->value); + const auto& val = *std::get>(this->value); return val.is_valid ? 0 : 1; } else { DCHECK(false) << "This function only valid for array-like values"; diff --git a/cpp/src/arrow/datum.h b/cpp/src/arrow/datum.h index d4aaff22ce3b1..ffea7800ecfc6 100644 --- a/cpp/src/arrow/datum.h +++ b/cpp/src/arrow/datum.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "arrow/array/data.h" @@ -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 { @@ -51,9 +51,9 @@ struct ARROW_EXPORT Datum { // current variant does not have a length. static constexpr int64_t kUnknownLength = -1; - util::Variant, std::shared_ptr, - std::shared_ptr, std::shared_ptr, - std::shared_ptr> + std::variant, std::shared_ptr, + std::shared_ptr, std::shared_ptr, + std::shared_ptr
> value; /// \brief Empty datum, to be populated elsewhere @@ -136,7 +136,7 @@ struct ARROW_EXPORT Datum { } const std::shared_ptr& array() const { - return util::get>(this->value); + return std::get>(this->value); } /// \brief The sum of bytes in each buffer referenced by the datum @@ -149,19 +149,19 @@ struct ARROW_EXPORT Datum { std::shared_ptr make_array() const; const std::shared_ptr& chunked_array() const { - return util::get>(this->value); + return std::get>(this->value); } const std::shared_ptr& record_batch() const { - return util::get>(this->value); + return std::get>(this->value); } const std::shared_ptr
& table() const { - return util::get>(this->value); + return std::get>(this->value); } const std::shared_ptr& scalar() const { - return util::get>(this->value); + return std::get>(this->value); } template diff --git a/cpp/src/arrow/engine/substrait/relation_internal.cc b/cpp/src/arrow/engine/substrait/relation_internal.cc index 3911373b7b74b..00427fc4c9e0a 100644 --- a/cpp/src/arrow/engine/substrait/relation_internal.cc +++ b/cpp/src/arrow/engine/substrait/relation_internal.cc @@ -558,7 +558,7 @@ Result> ExtractSchemaToBind(const compute::Declaration& const auto& opts = checked_cast(*(declr.options)); bind_schema = opts.dataset->schema(); } else if (declr.factory_name == "filter") { - auto input_declr = util::get(declr.inputs[0]); + auto input_declr = std::get(declr.inputs[0]); ARROW_ASSIGN_OR_RAISE(bind_schema, ExtractSchemaToBind(input_declr)); } else if (declr.factory_name == "sink") { // Note that the sink has no output_schema @@ -633,7 +633,7 @@ Result> FilterRelationConverter( auto declr_input = declaration.inputs[0]; ARROW_ASSIGN_OR_RAISE( auto input_rel, - ToProto(util::get(declr_input), ext_set, conversion_options)); + ToProto(std::get(declr_input), ext_set, conversion_options)); filter_rel->set_allocated_input(input_rel.release()); ARROW_ASSIGN_OR_RAISE(auto subs_expr, @@ -667,7 +667,7 @@ Status SerializeAndCombineRelations(const compute::Declaration& declaration, // Generally when a plan is deserialized the declaration will be a sink declaration. // Since there is no Sink relation in substrait, this function would be recursively // called on the input of the Sink declaration. - auto sink_input_decl = util::get(declaration.inputs[0]); + auto sink_input_decl = std::get(declaration.inputs[0]); RETURN_NOT_OK( SerializeAndCombineRelations(sink_input_decl, ext_set, rel, conversion_options)); } else { diff --git a/cpp/src/arrow/engine/substrait/serde_test.cc b/cpp/src/arrow/engine/substrait/serde_test.cc index cb1eadcdbdf80..afa676a40957c 100644 --- a/cpp/src/arrow/engine/substrait/serde_test.cc +++ b/cpp/src/arrow/engine/substrait/serde_test.cc @@ -185,10 +185,10 @@ void CheckRoundTripResult(const std::shared_ptr output_schema, ASSERT_OK_AND_ASSIGN(auto sink_decls, DeserializePlans( *buf, [] { return kNullConsumer; }, ext_id_reg, &ext_set, conversion_options)); - auto other_declrs = sink_decls[0].inputs[0].get(); + auto& other_declrs = std::get(sink_decls[0].inputs[0]); ASSERT_OK_AND_ASSIGN(auto output_table, - GetTableFromPlan(*other_declrs, exec_context, output_schema)); + GetTableFromPlan(other_declrs, exec_context, output_schema)); if (!include_columns.empty()) { ASSERT_OK_AND_ASSIGN(output_table, output_table->SelectColumns(include_columns)); } @@ -1045,7 +1045,7 @@ TEST(Substrait, DeserializeWithWriteOptionsFactory) { compute::Declaration* decl = &declarations[0]; ASSERT_EQ(decl->factory_name, "write"); ASSERT_EQ(decl->inputs.size(), 1); - decl = util::get_if(&decl->inputs[0]); + decl = std::get_if(&decl->inputs[0]); ASSERT_NE(decl, nullptr); ASSERT_EQ(decl->factory_name, "scan"); ASSERT_OK_AND_ASSIGN(auto plan, compute::ExecPlan::Make()); @@ -1216,21 +1216,21 @@ TEST(Substrait, JoinPlanBasic) { auto join_decl = sink_decls[0].inputs[0]; - const auto& join_rel = join_decl.get(); + const auto& join_rel = std::get(join_decl); const auto& join_options = - checked_cast(*join_rel->options); + checked_cast(*join_rel.options); - EXPECT_EQ(join_rel->factory_name, "hashjoin"); + EXPECT_EQ(join_rel.factory_name, "hashjoin"); EXPECT_EQ(join_options.join_type, compute::JoinType::INNER); - const auto& left_rel = join_rel->inputs[0].get(); - const auto& right_rel = join_rel->inputs[1].get(); + const auto& left_rel = std::get(join_rel.inputs[0]); + const auto& right_rel = std::get(join_rel.inputs[1]); const auto& l_options = - checked_cast(*left_rel->options); + checked_cast(*left_rel.options); const auto& r_options = - checked_cast(*right_rel->options); + checked_cast(*right_rel.options); AssertSchemaEqual( l_options.dataset->schema(), @@ -1582,12 +1582,12 @@ TEST(Substrait, AggregateBasic) { DeserializePlans(*buf, [] { return kNullConsumer; })); auto agg_decl = sink_decls[0].inputs[0]; - const auto& agg_rel = agg_decl.get(); + const auto& agg_rel = std::get(agg_decl); const auto& agg_options = - checked_cast(*agg_rel->options); + checked_cast(*agg_rel.options); - EXPECT_EQ(agg_rel->factory_name, "aggregate"); + EXPECT_EQ(agg_rel.factory_name, "aggregate"); EXPECT_EQ(agg_options.aggregates[0].name, ""); EXPECT_EQ(agg_options.aggregates[0].function, "hash_sum"); } @@ -1968,9 +1968,10 @@ TEST(Substrait, BasicPlanRoundTripping) { DeserializePlans( *serialized_plan, [] { return kNullConsumer; }, ext_id_reg, &ext_set)); // filter declaration - auto roundtripped_filter = sink_decls[0].inputs[0].get(); + const auto& roundtripped_filter = + std::get(sink_decls[0].inputs[0]); const auto& filter_opts = - checked_cast(*(roundtripped_filter->options)); + checked_cast(*(roundtripped_filter.options)); auto roundtripped_expr = filter_opts.filter_expression; if (auto* call = roundtripped_expr.call()) { @@ -1982,9 +1983,10 @@ TEST(Substrait, BasicPlanRoundTripping) { EXPECT_EQ(dummy_schema->field_names()[right_index], filter_col_right); } // scan declaration - auto roundtripped_scan = roundtripped_filter->inputs[0].get(); + const auto& roundtripped_scan = + std::get(roundtripped_filter.inputs[0]); const auto& dataset_opts = - checked_cast(*(roundtripped_scan->options)); + checked_cast(*(roundtripped_scan.options)); const auto& roundripped_ds = dataset_opts.dataset; EXPECT_TRUE(roundripped_ds->schema()->Equals(*dummy_schema)); ASSERT_OK_AND_ASSIGN(auto roundtripped_frgs, roundripped_ds->GetFragments()); @@ -2080,9 +2082,9 @@ TEST(Substrait, BasicPlanRoundTrippingEndToEnd) { DeserializePlans( *serialized_plan, [] { return kNullConsumer; }, ext_id_reg, &ext_set)); // filter declaration - auto roundtripped_filter = sink_decls[0].inputs[0].get(); + auto& roundtripped_filter = std::get(sink_decls[0].inputs[0]); const auto& filter_opts = - checked_cast(*(roundtripped_filter->options)); + checked_cast(*(roundtripped_filter.options)); auto roundtripped_expr = filter_opts.filter_expression; if (auto* call = roundtripped_expr.call()) { @@ -2094,9 +2096,10 @@ TEST(Substrait, BasicPlanRoundTrippingEndToEnd) { EXPECT_EQ(dummy_schema->field_names()[right_index], filter_col_right); } // scan declaration - auto roundtripped_scan = roundtripped_filter->inputs[0].get(); + const auto& roundtripped_scan = + std::get(roundtripped_filter.inputs[0]); const auto& dataset_opts = - checked_cast(*(roundtripped_scan->options)); + checked_cast(*(roundtripped_scan.options)); const auto& roundripped_ds = dataset_opts.dataset; EXPECT_TRUE(roundripped_ds->schema()->Equals(*dummy_schema)); ASSERT_OK_AND_ASSIGN(auto roundtripped_frgs, roundripped_ds->GetFragments()); @@ -2112,8 +2115,8 @@ TEST(Substrait, BasicPlanRoundTrippingEndToEnd) { checked_cast(roundtrip_frg_vec[idx++].get()); EXPECT_TRUE(l_frag->Equals(*r_frag)); } - ASSERT_OK_AND_ASSIGN(auto rnd_trp_table, GetTableFromPlan(*roundtripped_filter, - exec_context, dummy_schema)); + ASSERT_OK_AND_ASSIGN(auto rnd_trp_table, + GetTableFromPlan(roundtripped_filter, exec_context, dummy_schema)); EXPECT_TRUE(expected_table->Equals(*rnd_trp_table)); } diff --git a/cpp/src/arrow/filesystem/mockfs.cc b/cpp/src/arrow/filesystem/mockfs.cc index 69e49b320439d..d8302bed47149 100644 --- a/cpp/src/arrow/filesystem/mockfs.cc +++ b/cpp/src/arrow/filesystem/mockfs.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "arrow/buffer.h" @@ -35,7 +36,6 @@ #include "arrow/util/future.h" #include "arrow/util/logging.h" #include "arrow/util/string_view.h" -#include "arrow/util/variant.h" #include "arrow/util/windows_fixup.h" namespace arrow { @@ -120,7 +120,7 @@ struct Directory { }; // A filesystem entry -using EntryBase = util::Variant; +using EntryBase = std::variant; class Entry : public EntryBase { public: @@ -129,13 +129,13 @@ class Entry : public EntryBase { explicit Entry(Directory&& v) : EntryBase(std::move(v)) {} explicit Entry(File&& v) : EntryBase(std::move(v)) {} - bool is_dir() const { return util::holds_alternative(*this); } + bool is_dir() const { return std::holds_alternative(*this); } - bool is_file() const { return util::holds_alternative(*this); } + bool is_file() const { return std::holds_alternative(*this); } - Directory& as_dir() { return util::get(*this); } + Directory& as_dir() { return std::get(*this); } - File& as_file() { return util::get(*this); } + File& as_file() { return std::get(*this); } // Get info for this entry. Note the path() property isn't set. FileInfo GetInfo() { diff --git a/cpp/src/arrow/flight/client.h b/cpp/src/arrow/flight/client.h index 0298abe366d0e..61fa6e9d0c405 100644 --- a/cpp/src/arrow/flight/client.h +++ b/cpp/src/arrow/flight/client.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "arrow/ipc/options.h" @@ -32,7 +33,6 @@ #include "arrow/result.h" #include "arrow/status.h" #include "arrow/util/cancel.h" -#include "arrow/util/variant.h" #include "arrow/flight/type_fwd.h" #include "arrow/flight/types.h" // IWYU pragma: keep @@ -118,7 +118,7 @@ struct ARROW_FLIGHT_EXPORT FlightClientOptions { /// \brief Generic connection options, passed to the underlying /// transport; interpretation is implementation-dependent. - std::vector>> generic_options; + std::vector>> generic_options; /// \brief Use TLS without validating the server certificate. Use with caution. bool disable_server_verification = false; diff --git a/cpp/src/arrow/flight/sql/server.cc b/cpp/src/arrow/flight/sql/server.cc index a8f3ed8a80c24..584a017f1e5af 100644 --- a/cpp/src/arrow/flight/sql/server.cc +++ b/cpp/src/arrow/flight/sql/server.cc @@ -680,7 +680,7 @@ arrow::Result> FlightSqlServerBase::DoGetSqlIn return Status::KeyError("No information for SQL info number ", info); } ARROW_RETURN_NOT_OK(name_field_builder.Append(info)); - ARROW_RETURN_NOT_OK(arrow::util::visit(sql_info_result_appender, it->second)); + ARROW_RETURN_NOT_OK(std::visit(sql_info_result_appender, it->second)); } std::shared_ptr name; diff --git a/cpp/src/arrow/flight/sql/server_test.cc b/cpp/src/arrow/flight/sql/server_test.cc index 7ba3ca4a24364..516b2fa1d0a0e 100644 --- a/cpp/src/arrow/flight/sql/server_test.cc +++ b/cpp/src/arrow/flight/sql/server_test.cc @@ -788,7 +788,7 @@ TEST_F(TestFlightSqlServer, TestCommandGetSqlInfo) { reinterpret_cast(*scalar)); const auto& expected_result = sql_info_expected_results.at(col_name_chunk_data[row]); - arrow::util::visit(validator, expected_result); + std::visit(validator, expected_result); } } } diff --git a/cpp/src/arrow/flight/sql/types.h b/cpp/src/arrow/flight/sql/types.h index 20c7952d8d710..b0598c11688dd 100644 --- a/cpp/src/arrow/flight/sql/types.h +++ b/cpp/src/arrow/flight/sql/types.h @@ -21,11 +21,11 @@ #include #include #include +#include #include #include "arrow/flight/sql/visibility.h" #include "arrow/type_fwd.h" -#include "arrow/util/variant.h" namespace arrow { namespace flight { @@ -37,8 +37,8 @@ namespace sql { /// \brief Variant supporting all possible types on SQL info. using SqlInfoResult = - arrow::util::Variant, - std::unordered_map>>; + std::variant, + std::unordered_map>>; /// \brief Map SQL info identifier to its value. using SqlInfoResultMap = std::unordered_map; diff --git a/cpp/src/arrow/flight/transport/grpc/grpc_client.cc b/cpp/src/arrow/flight/transport/grpc/grpc_client.cc index 8fe1e1bae792e..e6f5016960739 100644 --- a/cpp/src/arrow/flight/transport/grpc/grpc_client.cc +++ b/cpp/src/arrow/flight/transport/grpc/grpc_client.cc @@ -631,10 +631,10 @@ class GrpcClientImpl : public internal::ClientTransport { // Allow setting generic gRPC options. for (const auto& arg : options.generic_options) { - if (util::holds_alternative(arg.second)) { - default_args[arg.first] = util::get(arg.second); - } else if (util::holds_alternative(arg.second)) { - args.SetString(arg.first, util::get(arg.second)); + if (std::holds_alternative(arg.second)) { + default_args[arg.first] = std::get(arg.second); + } else if (std::holds_alternative(arg.second)) { + args.SetString(arg.first, std::get(arg.second)); } // Otherwise unimplemented } diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc index efff07db6671f..c91fa234e0af8 100644 --- a/cpp/src/arrow/type.cc +++ b/cpp/src/arrow/type.cc @@ -1110,28 +1110,29 @@ Result> FieldPath::Get(const ArrayData& data) const { } FieldRef::FieldRef(FieldPath indices) : impl_(std::move(indices)) { - DCHECK_GT(util::get(impl_).indices().size(), 0); + DCHECK_GT(std::get(impl_).indices().size(), 0); } void FieldRef::Flatten(std::vector children) { // flatten children struct Visitor { - void operator()(std::string* name) { *out++ = FieldRef(std::move(*name)); } + void operator()(std::string&& name) { out->push_back(FieldRef(std::move(name))); } - void operator()(FieldPath* indices) { *out++ = FieldRef(std::move(*indices)); } + void operator()(FieldPath&& indices) { out->push_back(FieldRef(std::move(indices))); } - void operator()(std::vector* children) { - for (auto& child : *children) { - util::visit(*this, &child.impl_); + void operator()(std::vector&& children) { + out->reserve(out->size() + children.size()); + for (auto&& child : children) { + std::visit(*this, std::move(child.impl_)); } } - std::back_insert_iterator> out; + std::vector* out; }; std::vector out; - Visitor visitor{std::back_inserter(out)}; - visitor(&children); + Visitor visitor{&out}; + visitor(std::move(children)); DCHECK(!out.empty()); DCHECK(std::none_of(out.begin(), out.end(), @@ -1237,7 +1238,7 @@ std::string FieldRef::ToDotPath() const { } }; - return util::visit(Visitor{}, impl_); + return std::visit(Visitor{}, impl_); } size_t FieldRef::hash() const { @@ -1257,7 +1258,7 @@ size_t FieldRef::hash() const { } }; - return util::visit(Visitor{}, impl_); + return std::visit(Visitor{}, impl_); } std::string FieldRef::ToString() const { @@ -1277,7 +1278,7 @@ std::string FieldRef::ToString() const { } }; - return "FieldRef." + util::visit(Visitor{}, impl_); + return "FieldRef." + std::visit(Visitor{}, impl_); } std::vector FieldRef::FindAll(const Schema& schema) const { @@ -1379,7 +1380,7 @@ std::vector FieldRef::FindAll(const FieldVector& fields) const { const FieldVector& fields_; }; - return util::visit(Visitor{fields}, impl_); + return std::visit(Visitor{fields}, impl_); } std::vector FieldRef::FindAll(const ArrayData& array) const { diff --git a/cpp/src/arrow/type.h b/cpp/src/arrow/type.h index 209c5e52e129b..663c476512746 100644 --- a/cpp/src/arrow/type.h +++ b/cpp/src/arrow/type.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "arrow/result.h" @@ -32,7 +33,6 @@ #include "arrow/util/checked_cast.h" #include "arrow/util/endian.h" #include "arrow/util/macros.h" -#include "arrow/util/variant.h" #include "arrow/util/visibility.h" #include "arrow/visitor.h" // IWYU pragma: keep @@ -1735,23 +1735,23 @@ class ARROW_EXPORT FieldRef : public util::EqualityComparable { explicit operator bool() const { return Equals(FieldPath{}); } bool operator!() const { return !Equals(FieldPath{}); } - bool IsFieldPath() const { return util::holds_alternative(impl_); } - bool IsName() const { return util::holds_alternative(impl_); } + bool IsFieldPath() const { return std::holds_alternative(impl_); } + bool IsName() const { return std::holds_alternative(impl_); } bool IsNested() const { if (IsName()) return false; - if (IsFieldPath()) return util::get(impl_).indices().size() > 1; + if (IsFieldPath()) return std::get(impl_).indices().size() > 1; return true; } const FieldPath* field_path() const { - return IsFieldPath() ? &util::get(impl_) : NULLPTR; + return IsFieldPath() ? &std::get(impl_) : NULLPTR; } const std::string* name() const { - return IsName() ? &util::get(impl_) : NULLPTR; + return IsName() ? &std::get(impl_) : NULLPTR; } const std::vector* nested_refs() const { - return util::holds_alternative>(impl_) - ? &util::get>(impl_) + return std::holds_alternative>(impl_) + ? &std::get>(impl_) : NULLPTR; } @@ -1843,7 +1843,7 @@ class ARROW_EXPORT FieldRef : public util::EqualityComparable { private: void Flatten(std::vector children); - util::Variant> impl_; + std::variant> impl_; ARROW_EXPORT friend void PrintTo(const FieldRef& ref, std::ostream* os); }; diff --git a/cpp/src/arrow/util/CMakeLists.txt b/cpp/src/arrow/util/CMakeLists.txt index cd1a8967eeb19..051138a002bd5 100644 --- a/cpp/src/arrow/util/CMakeLists.txt +++ b/cpp/src/arrow/util/CMakeLists.txt @@ -71,8 +71,7 @@ add_arrow_test(utility-test trie_test.cc uri_test.cc utf8_util_test.cc - value_parsing_test.cc - variant_test.cc) + value_parsing_test.cc) add_arrow_test(threading-utility-test SOURCES @@ -100,4 +99,3 @@ add_arrow_benchmark(thread_pool_benchmark) add_arrow_benchmark(trie_benchmark) add_arrow_benchmark(utf8_util_benchmark) add_arrow_benchmark(value_parsing_benchmark) -add_arrow_benchmark(variant_benchmark) diff --git a/cpp/src/arrow/util/variant.h b/cpp/src/arrow/util/variant.h deleted file mode 100644 index 8bbce52517859..0000000000000 --- a/cpp/src/arrow/util/variant.h +++ /dev/null @@ -1,443 +0,0 @@ -// 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. - -#pragma once - -#include -#include -#include -#include - -#include "arrow/util/macros.h" -#include "arrow/util/type_traits.h" - -namespace arrow { -namespace util { - -/// \brief a std::variant-like discriminated union -/// -/// Simplifications from std::variant: -/// -/// - Strictly defaultable. The first type of T... should be nothrow default constructible -/// and it will be used for default Variants. -/// -/// - Never valueless_by_exception. std::variant supports a state outside those specified -/// by T... to which it can return in the event that a constructor throws. If a Variant -/// would become valueless_by_exception it will instead return to its default state. -/// -/// - Strictly nothrow move constructible and assignable -/// -/// - Less sophisticated type deduction. std::variant("hello") will -/// intelligently construct std::string while Variant("hello") will -/// construct bool. -/// -/// - Either both copy constructible and assignable or neither (std::variant independently -/// enables copy construction and copy assignment). Variant is copy constructible if -/// each of T... is copy constructible and assignable. -/// -/// - Slimmer interface; several members of std::variant are omitted. -/// -/// - Throws no exceptions; if a bad_variant_access would be thrown Variant will instead -/// segfault (nullptr dereference). -/// -/// - Mutable visit takes a pointer instead of mutable reference or rvalue reference, -/// which is more conformant with our code style. -template -class Variant; - -namespace detail { - -template -struct is_equality_comparable : std::false_type {}; - -template -struct is_equality_comparable< - T, typename std::enable_if() == std::declval()), bool>::value>::type> - : std::true_type {}; - -template -using conditional_t = typename std::conditional::type; - -template -struct type_constant { - using type = T; -}; - -template -struct first; - -template -struct first { - using type = H; -}; - -template -using decay_t = typename std::decay::type; - -template -struct all : std::true_type {}; - -template -struct all : conditional_t, std::false_type> {}; - -struct delete_copy_constructor { - template - struct type { - type() = default; - type(const type& other) = delete; - type& operator=(const type& other) = delete; - }; -}; - -struct explicit_copy_constructor { - template - struct type { - type() = default; - type(const type& other) { static_cast(other).copy_to(this); } - type& operator=(const type& other) { - static_cast(this)->destroy(); - static_cast(other).copy_to(this); - return *this; - } - }; -}; - -template -struct VariantStorage { - VariantStorage() = default; - VariantStorage(const VariantStorage&) {} - VariantStorage& operator=(const VariantStorage&) { return *this; } - VariantStorage(VariantStorage&&) noexcept {} - VariantStorage& operator=(VariantStorage&&) noexcept { return *this; } - ~VariantStorage() { - static_assert(offsetof(VariantStorage, data_) == 0, - "(void*)&VariantStorage::data_ == (void*)this"); - } - - typename arrow::internal::aligned_union<0, T...>::type data_; - uint8_t index_ = 0; -}; - -template -struct VariantImpl; - -template -struct VariantImpl> : VariantStorage { - static void index_of() noexcept {} - void destroy() noexcept {} - void move_to(...) noexcept {} - void copy_to(...) const {} - - template - [[noreturn]] R visit_const(Visitor&& visitor) const { - std::terminate(); - } - template - [[noreturn]] R visit_mutable(Visitor&& visitor) { - std::terminate(); - } -}; - -template -struct VariantImpl, H, T...> : VariantImpl, T...> { - using VariantType = Variant; - using Impl = VariantImpl; - - static constexpr uint8_t kIndex = sizeof...(M) - sizeof...(T) - 1; - - VariantImpl() = default; - - using VariantImpl::VariantImpl; - using Impl::operator=; - using Impl::index_of; - - explicit VariantImpl(H value) { - new (this) H(std::move(value)); - this->index_ = kIndex; - } - - VariantImpl& operator=(H value) { - static_cast(this)->destroy(); - new (this) H(std::move(value)); - this->index_ = kIndex; - return *this; - } - - H& cast_this() { return *reinterpret_cast(this); } - const H& cast_this() const { return *reinterpret_cast(this); } - - void move_to(VariantType* target) noexcept { - if (this->index_ == kIndex) { - new (target) H(std::move(cast_this())); - target->index_ = kIndex; - } else { - Impl::move_to(target); - } - } - - // Templated to avoid instantiation in case H is not copy constructible - template - void copy_to(Void* generic_target) const { - const auto target = static_cast(generic_target); - try { - if (this->index_ == kIndex) { - new (target) H(cast_this()); - target->index_ = kIndex; - } else { - Impl::copy_to(target); - } - } catch (...) { - target->construct_default(); - throw; - } - } - - void destroy() noexcept { - if (this->index_ == kIndex) { - if (!std::is_trivially_destructible::value) { - cast_this().~H(); - } - } else { - Impl::destroy(); - } - } - - static constexpr std::integral_constant index_of( - const type_constant&) { - return {}; - } - - template - R visit_const(Visitor&& visitor) const { - if (this->index_ == kIndex) { - return std::forward(visitor)(cast_this()); - } - return Impl::template visit_const(std::forward(visitor)); - } - - template - R visit_mutable(Visitor&& visitor) { - if (this->index_ == kIndex) { - return std::forward(visitor)(&cast_this()); - } - return Impl::template visit_mutable(std::forward(visitor)); - } -}; - -} // namespace detail - -template -class Variant : detail::VariantImpl, T...>, - detail::conditional_t< - detail::all<(std::is_copy_constructible::value && - std::is_copy_assignable::value)...>::value, - detail::explicit_copy_constructor, - detail::delete_copy_constructor>::template type> { - template - static constexpr uint8_t index_of() { - return Impl::index_of(detail::type_constant{}); - } - - using Impl = detail::VariantImpl, T...>; - - public: - using default_type = typename util::detail::first::type; - - Variant() noexcept { construct_default(); } - - Variant(const Variant& other) = default; - Variant& operator=(const Variant& other) = default; - Variant& operator=(Variant&& other) noexcept { - this->destroy(); - other.move_to(this); - return *this; - } - - using Impl::Impl; - using Impl::operator=; - - Variant(Variant&& other) noexcept { other.move_to(this); } - - ~Variant() { - static_assert(offsetof(Variant, data_) == 0, "(void*)&Variant::data_ == (void*)this"); - this->destroy(); - } - - /// \brief Return the zero-based type index of the value held by the variant - uint8_t index() const noexcept { return this->index_; } - - /// \brief Get a const pointer to the value held by the variant - /// - /// If the type given as template argument doesn't match, a null pointer is returned. - template ()> - const U* get() const noexcept { - return index() == I ? reinterpret_cast(this) : NULLPTR; - } - - /// \brief Get a pointer to the value held by the variant - /// - /// If the type given as template argument doesn't match, a null pointer is returned. - template ()> - U* get() noexcept { - return index() == I ? reinterpret_cast(this) : NULLPTR; - } - - /// \brief Replace the value held by the variant - /// - /// The intended type must be given as a template argument. - /// The value is constructed in-place using the given function arguments. - template ()> - void emplace(A&&... args) { - try { - this->destroy(); - new (this) U(std::forward(args)...); - this->index_ = I; - } catch (...) { - construct_default(); - throw; - } - } - - template ()> - void emplace(std::initializer_list il, A&&... args) { - try { - this->destroy(); - new (this) U(il, std::forward(args)...); - this->index_ = I; - } catch (...) { - construct_default(); - throw; - } - } - - /// \brief Swap with another variant's contents - void swap(Variant& other) noexcept { // NOLINT google-runtime-references - Variant tmp = std::move(other); - other = std::move(*this); - *this = std::move(tmp); - } - - using Impl::visit_const; - using Impl::visit_mutable; - - private: - void construct_default() noexcept { - new (this) default_type(); - this->index_ = 0; - } - - template - friend struct detail::explicit_copy_constructor::type; - - template - friend struct detail::VariantImpl; -}; - -/// \brief Call polymorphic visitor on a const variant's value -/// -/// The visitor will receive a const reference to the value held by the variant. -/// It must define overloads for each possible variant type. -/// The overloads should all return the same type (no attempt -/// is made to find a generalized return type). -template ()( - std::declval::default_type&>()))> -R visit(Visitor&& visitor, const util::Variant& v) { - return v.template visit_const(std::forward(visitor)); -} - -/// \brief Call polymorphic visitor on a non-const variant's value -/// -/// The visitor will receive a pointer to the value held by the variant. -/// It must define overloads for each possible variant type. -/// The overloads should all return the same type (no attempt -/// is made to find a generalized return type). -template ()( - std::declval::default_type*>()))> -R visit(Visitor&& visitor, util::Variant* v) { - return v->template visit_mutable(std::forward(visitor)); -} - -/// \brief Get a const reference to the value held by the variant -/// -/// If the type given as template argument doesn't match, behavior is undefined -/// (a null pointer will be dereferenced). -template -const U& get(const Variant& v) { - return *v.template get(); -} - -/// \brief Get a reference to the value held by the variant -/// -/// If the type given as template argument doesn't match, behavior is undefined -/// (a null pointer will be dereferenced). -template -U& get(Variant& v) { - return *v.template get(); -} - -/// \brief Get a const pointer to the value held by the variant -/// -/// If the type given as template argument doesn't match, a nullptr is returned. -template -const U* get_if(const Variant* v) { - return v->template get(); -} - -/// \brief Get a pointer to the value held by the variant -/// -/// If the type given as template argument doesn't match, a nullptr is returned. -template -U* get_if(Variant* v) { - return v->template get(); -} - -namespace detail { - -template -struct VariantsEqual { - template - bool operator()(const U& r) const { - return get(l_) == r; - } - const Variant& l_; -}; - -} // namespace detail - -template ::value...>::value>> -bool operator==(const Variant& l, const Variant& r) { - if (l.index() != r.index()) return false; - return visit(detail::VariantsEqual{l}, r); -} - -template -auto operator!=(const Variant& l, const Variant& r) -> decltype(l == r) { - return !(l == r); -} - -/// \brief Return whether the variant holds a value of the given type -template -bool holds_alternative(const Variant& v) { - return v.template get(); -} - -} // namespace util -} // namespace arrow diff --git a/cpp/src/arrow/util/variant_benchmark.cc b/cpp/src/arrow/util/variant_benchmark.cc deleted file mode 100644 index af3fafb8b0e35..0000000000000 --- a/cpp/src/arrow/util/variant_benchmark.cc +++ /dev/null @@ -1,248 +0,0 @@ -// 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 "benchmark/benchmark.h" - -#include -#include -#include -#include -#include - -#include "arrow/array.h" -#include "arrow/chunked_array.h" -#include "arrow/datum.h" -#include "arrow/status.h" -#include "arrow/testing/gtest_util.h" -#include "arrow/testing/random.h" -#include "arrow/type.h" -#include "arrow/util/checked_cast.h" -#include "arrow/util/variant.h" - -namespace arrow { - -using internal::checked_pointer_cast; - -namespace util { - -using TrivialVariant = arrow::util::Variant; - -using NonTrivialVariant = arrow::util::Variant; - -std::vector MakeInts(int64_t nitems) { - auto rng = arrow::random::RandomArrayGenerator(42); - auto array = checked_pointer_cast(rng.Int32(nitems, 0, 1 << 30)); - std::vector items(nitems); - for (int64_t i = 0; i < nitems; ++i) { - items[i] = array->Value(i); - } - return items; -} - -std::vector MakeFloats(int64_t nitems) { - auto rng = arrow::random::RandomArrayGenerator(42); - auto array = checked_pointer_cast(rng.Float32(nitems, 0.0, 1.0)); - std::vector items(nitems); - for (int64_t i = 0; i < nitems; ++i) { - items[i] = array->Value(i); - } - return items; -} - -std::vector MakeStrings(int64_t nitems) { - auto rng = arrow::random::RandomArrayGenerator(42); - // Some std::string's will use short string optimization, but not all... - auto array = checked_pointer_cast(rng.String(nitems, 5, 40)); - std::vector items(nitems); - for (int64_t i = 0; i < nitems; ++i) { - items[i] = array->GetString(i); - } - return items; -} - -static void ConstructTrivialVariant(benchmark::State& state) { - const int64_t N = 10000; - const auto ints = MakeInts(N); - const auto floats = MakeFloats(N); - - for (auto _ : state) { - for (int64_t i = 0; i < N; ++i) { - // About type selection: we ensure 50% of each type, but try to avoid - // branch mispredictions by creating runs of the same type. - if (i & 0x10) { - TrivialVariant v{ints[i]}; - const int32_t* val = &arrow::util::get(v); - benchmark::DoNotOptimize(val); - } else { - TrivialVariant v{floats[i]}; - const float* val = &arrow::util::get(v); - benchmark::DoNotOptimize(val); - } - } - } - - state.SetItemsProcessed(state.iterations() * N); -} - -static void ConstructNonTrivialVariant(benchmark::State& state) { - const int64_t N = 10000; - const auto ints = MakeInts(N); - const auto strings = MakeStrings(N); - - for (auto _ : state) { - for (int64_t i = 0; i < N; ++i) { - if (i & 0x10) { - NonTrivialVariant v{ints[i]}; - const int32_t* val = &arrow::util::get(v); - benchmark::DoNotOptimize(val); - } else { - NonTrivialVariant v{strings[i]}; - const std::string* val = &arrow::util::get(v); - benchmark::DoNotOptimize(val); - } - } - } - - state.SetItemsProcessed(state.iterations() * N); -} - -struct VariantVisitor { - int64_t total = 0; - - void operator()(const int32_t& v) { total += v; } - void operator()(const float& v) { - // Avoid potentially costly float-to-int conversion - int32_t x; - memcpy(&x, &v, 4); - total += x; - } - void operator()(const std::string& v) { total += static_cast(v.length()); } -}; - -template -static void VisitVariant(benchmark::State& state, - const std::vector& variants) { - for (auto _ : state) { - VariantVisitor visitor; - for (const auto& v : variants) { - visit(visitor, v); - } - benchmark::DoNotOptimize(visitor.total); - } - - state.SetItemsProcessed(state.iterations() * variants.size()); -} - -static void VisitTrivialVariant(benchmark::State& state) { - const int64_t N = 10000; - const auto ints = MakeInts(N); - const auto floats = MakeFloats(N); - - std::vector variants; - variants.reserve(N); - for (int64_t i = 0; i < N; ++i) { - if (i & 0x10) { - variants.emplace_back(ints[i]); - } else { - variants.emplace_back(floats[i]); - } - } - - VisitVariant(state, variants); -} - -static void VisitNonTrivialVariant(benchmark::State& state) { - const int64_t N = 10000; - const auto ints = MakeInts(N); - const auto strings = MakeStrings(N); - - std::vector variants; - variants.reserve(N); - for (int64_t i = 0; i < N; ++i) { - if (i & 0x10) { - variants.emplace_back(ints[i]); - } else { - variants.emplace_back(strings[i]); - } - } - - VisitVariant(state, variants); -} - -static void ConstructDatum(benchmark::State& state) { - const int64_t N = 10000; - auto array = *MakeArrayOfNull(int8(), 100); - auto chunked_array = std::make_shared(ArrayVector{array, array}); - - for (auto _ : state) { - for (int64_t i = 0; i < N; ++i) { - if (i & 0x10) { - Datum datum{array}; - const ArrayData* val = datum.array().get(); - benchmark::DoNotOptimize(val); - } else { - Datum datum{chunked_array}; - const ChunkedArray* val = datum.chunked_array().get(); - benchmark::DoNotOptimize(val); - } - } - } - - state.SetItemsProcessed(state.iterations() * N); -} - -static void VisitDatum(benchmark::State& state) { - const int64_t N = 10000; - auto array = *MakeArrayOfNull(int8(), 100); - auto chunked_array = std::make_shared(ArrayVector{array, array}); - - std::vector datums; - datums.reserve(N); - for (int64_t i = 0; i < N; ++i) { - if (i & 0x10) { - datums.emplace_back(array); - } else { - datums.emplace_back(chunked_array); - } - } - - for (auto _ : state) { - int64_t total = 0; - for (const auto& datum : datums) { - // The .is_XXX() methods are the usual idiom when visiting a Datum, - // rather than the visit() function. - if (datum.is_array()) { - total += datum.array()->length; - } else { - total += datum.chunked_array()->length(); - } - } - benchmark::DoNotOptimize(total); - } - - state.SetItemsProcessed(state.iterations() * datums.size()); -} - -BENCHMARK(ConstructTrivialVariant); -BENCHMARK(ConstructNonTrivialVariant); -BENCHMARK(VisitTrivialVariant); -BENCHMARK(VisitNonTrivialVariant); -BENCHMARK(ConstructDatum); -BENCHMARK(VisitDatum); - -} // namespace util -} // namespace arrow diff --git a/cpp/src/arrow/util/variant_test.cc b/cpp/src/arrow/util/variant_test.cc deleted file mode 100644 index f94d1b6ccf867..0000000000000 --- a/cpp/src/arrow/util/variant_test.cc +++ /dev/null @@ -1,345 +0,0 @@ -// 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/util/variant.h" - -#include -#include -#include -#include -#include - -#include -#include - -#include "arrow/testing/gtest_compat.h" - -namespace arrow { - -namespace util { -namespace { - -using ::testing::Eq; - -template -void AssertDefaultConstruction() { - using variant_type = Variant; - - static_assert(std::is_nothrow_default_constructible::value, ""); - - variant_type v; - EXPECT_EQ(v.index(), 0); - EXPECT_EQ(get(v), H{}); -} - -TEST(Variant, DefaultConstruction) { - AssertDefaultConstruction(); - AssertDefaultConstruction(); - AssertDefaultConstruction(); - AssertDefaultConstruction>(); - AssertDefaultConstruction, int>(); - AssertDefaultConstruction, void*, - std::true_type>(); - AssertDefaultConstruction, void*, bool, - std::string, std::true_type>(); -} - -template -struct AssertCopyConstructionOne { - void operator()(uint8_t index) { - V v{member_}; - EXPECT_EQ(v.index(), index); - EXPECT_EQ(get(v), member_); - - V copy{v}; - EXPECT_EQ(copy.index(), v.index()); - EXPECT_EQ(get(copy), get(v)); - EXPECT_EQ(copy, v); - - V assigned; - assigned = member_; - EXPECT_EQ(assigned.index(), index); - EXPECT_EQ(get(assigned), member_); - - assigned = v; - EXPECT_EQ(assigned.index(), v.index()); - EXPECT_EQ(get(assigned), get(v)); - EXPECT_EQ(assigned, v); - } - - const T& member_; -}; - -template -void AssertCopyConstruction(T... member) { - uint8_t index = 0; - for (auto Assert : {std::function( - AssertCopyConstructionOne, T>{member})...}) { - Assert(index++); - } -} - -template -void AssertCopyConstructionDisabled() { - static_assert(!std::is_copy_constructible>::value, - "copy construction was not disabled"); -} - -TEST(Variant, CopyConstruction) { - // if any member is not copy constructible then Variant is not copy constructible - AssertCopyConstructionDisabled>(); - AssertCopyConstructionDisabled, std::string>(); - AssertCopyConstructionDisabled>(); - - AssertCopyConstruction(32, std::string("hello"), true); - AssertCopyConstruction(std::string("world"), false, 53); - AssertCopyConstruction(nullptr, std::true_type{}, std::string("!")); - AssertCopyConstruction(std::vector{1, 3, 3, 7}, "C string"); - - // copy assignment operator is not used - struct CopyAssignThrows { - CopyAssignThrows() = default; - CopyAssignThrows(const CopyAssignThrows&) = default; - - CopyAssignThrows& operator=(const CopyAssignThrows&) { throw 42; } - - CopyAssignThrows(CopyAssignThrows&&) = default; - CopyAssignThrows& operator=(CopyAssignThrows&&) = default; - - bool operator==(const CopyAssignThrows&) const { return true; } - }; - EXPECT_NO_THROW(AssertCopyConstruction(CopyAssignThrows{})); -} - -TEST(Variant, Emplace) { - using variant_type = Variant, int>; - variant_type v; - - v.emplace(); - EXPECT_EQ(v, variant_type{int{}}); - - v.emplace("hello"); - EXPECT_EQ(v, variant_type{std::string("hello")}); - - v.emplace>({1, 3, 3, 7}); - EXPECT_EQ(v, variant_type{std::vector({1, 3, 3, 7})}); -} - -TEST(Variant, MoveConstruction) { - struct noop_delete { - void operator()(...) const {} - }; - using ptr = std::unique_ptr; - static_assert(!std::is_copy_constructible::value, ""); - - using variant_type = Variant; - - int tag = 42; - auto ExpectIsTag = [&](const variant_type& v) { - EXPECT_EQ(v.index(), 1); - EXPECT_EQ(get(v).get(), &tag); - }; - - ptr p; - - // move construction from member - p.reset(&tag); - variant_type v0{std::move(p)}; - ExpectIsTag(v0); - - // move assignment from member - p.reset(&tag); - v0 = std::move(p); - ExpectIsTag(v0); - - // move construction from other variant - variant_type v1{std::move(v0)}; - ExpectIsTag(v1); - - // move assignment from other variant - p.reset(&tag); - variant_type v2{std::move(p)}; - v1 = std::move(v2); - ExpectIsTag(v1); - - // type changing move assignment from member - variant_type v3; - EXPECT_NE(v3.index(), 1); - p.reset(&tag); - v3 = std::move(p); - ExpectIsTag(v3); - - // type changing move assignment from other variant - variant_type v4; - EXPECT_NE(v4.index(), 1); - v4 = std::move(v3); - ExpectIsTag(v4); -} - -TEST(Variant, ExceptionSafety) { - struct { - } actually_throw; - - struct { - } dont_throw; - - struct ConstructorThrows { - explicit ConstructorThrows(decltype(actually_throw)) { throw 42; } - explicit ConstructorThrows(decltype(dont_throw)) {} - - ConstructorThrows(const ConstructorThrows&) { throw 42; } - - ConstructorThrows& operator=(const ConstructorThrows&) = default; - ConstructorThrows(ConstructorThrows&&) = default; - ConstructorThrows& operator=(ConstructorThrows&&) = default; - }; - - Variant v; - - // constructor throws during emplacement - EXPECT_THROW(v.emplace(actually_throw), int); - // safely returned to the default state - EXPECT_EQ(v.index(), 0); - - // constructor throws during copy assignment from member - EXPECT_THROW( - { - const ConstructorThrows throws(dont_throw); - v = throws; - }, - int); - // safely returned to the default state - EXPECT_EQ(v.index(), 0); -} - -// XXX GTest 1.11 exposes a `using std::visit` in its headers which -// somehow gets preferred to `arrow::util::visit`, even if there is -// a using clause (perhaps because of macros such as EXPECT_EQ). -template -void DoVisit(Args&&... args) { - return ::arrow::util::visit(std::forward(args)...); -} - -template -void AssertVisitedEquals(const T& expected, Args&&... args) { - const auto actual = ::arrow::util::visit(std::forward(args)...); - EXPECT_EQ(expected, actual); -} - -template -struct AssertVisitOne { - void operator()(const T& actual) { EXPECT_EQ(&actual, expected_); } - - void operator()(T* actual) { EXPECT_EQ(actual, expected_); } - - template - void operator()(const U&) { - FAIL() << "the expected type was not visited."; - } - - template - void operator()(U*) { - FAIL() << "the expected type was not visited."; - } - - explicit AssertVisitOne(T member) : member_(std::move(member)) {} - - void operator()() { - V v{member_}; - expected_ = &get(v); - DoVisit(*this, v); - DoVisit(*this, &v); - } - - T member_; - const T* expected_; -}; - -// Try visiting all alternatives on a Variant -template -void AssertVisitAll(T... member) { - for (auto Assert : - {std::function(AssertVisitOne, T>{member})...}) { - Assert(); - } -} - -TEST(VariantTest, Visit) { - AssertVisitAll(32, std::string("hello"), true); - AssertVisitAll(std::string("world"), false, 53); - AssertVisitAll(nullptr, std::true_type{}, std::string("!")); - AssertVisitAll(std::vector{1, 3, 3, 7}, "C string"); - - using int_or_string = Variant; - int_or_string v; - - // value returning visit: - struct { - int_or_string operator()(int i) { return int_or_string{i * 2}; } - int_or_string operator()(const std::string& s) { return int_or_string{s + s}; } - } Double; - - v = 7; - AssertVisitedEquals(int_or_string{14}, Double, v); - - v = "lolol"; - AssertVisitedEquals(int_or_string{"lolollolol"}, Double, v); - - // mutating visit: - struct { - void operator()(int* i) { *i *= 2; } - void operator()(std::string* s) { *s += *s; } - } DoubleInplace; - - v = 7; - DoVisit(DoubleInplace, &v); - EXPECT_EQ(v, int_or_string{14}); - - v = "lolol"; - DoVisit(DoubleInplace, &v); - EXPECT_EQ(v, int_or_string{"lolollolol"}); -} - -TEST(VariantTest, Equality) { - using int_or_double = Variant; - - auto eq = [](const int_or_double& a, const int_or_double& b) { - EXPECT_TRUE(a == b); - EXPECT_FALSE(a != b); - }; - auto ne = [](const int_or_double& a, const int_or_double& b) { - EXPECT_TRUE(a != b); - EXPECT_FALSE(a == b); - }; - - int_or_double u, v; - u.emplace(1); - v.emplace(1); - eq(u, v); - v.emplace(2); - ne(u, v); - v.emplace(1.0); - ne(u, v); - u.emplace(1.0); - eq(u, v); - u.emplace(2.0); - ne(u, v); -} - -} // namespace -} // namespace util -} // namespace arrow diff --git a/cpp/src/gandiva/interval_holder.h b/cpp/src/gandiva/interval_holder.h index 43f3401932811..e1a50bcf68302 100644 --- a/cpp/src/gandiva/interval_holder.h +++ b/cpp/src/gandiva/interval_holder.h @@ -60,7 +60,7 @@ class GANDIVA_EXPORT IntervalHolder : public FunctionHolder { " function needs to be an integer literal to indicate " "whether to suppress the error"); } - suppress_errors = arrow::util::get(literal_suppress_errors->holder()); + suppress_errors = std::get(literal_suppress_errors->holder()); } return Make(suppress_errors, holder); diff --git a/cpp/src/gandiva/literal_holder.cc b/cpp/src/gandiva/literal_holder.cc index beed8119cb1cb..a77140b727e44 100644 --- a/cpp/src/gandiva/literal_holder.cc +++ b/cpp/src/gandiva/literal_holder.cc @@ -38,7 +38,7 @@ struct LiteralToStream { std::string ToString(const LiteralHolder& holder) { std::stringstream ss; LiteralToStream visitor{ss}; - ::arrow::util::visit(visitor, holder); + ::std::visit(visitor, holder); return ss.str(); } diff --git a/cpp/src/gandiva/literal_holder.h b/cpp/src/gandiva/literal_holder.h index c4712aafc4bbd..40faf39e1f5e6 100644 --- a/cpp/src/gandiva/literal_holder.h +++ b/cpp/src/gandiva/literal_holder.h @@ -18,8 +18,7 @@ #pragma once #include - -#include +#include #include #include "gandiva/decimal_scalar.h" @@ -28,8 +27,8 @@ namespace gandiva { using LiteralHolder = - arrow::util::Variant; + std::variant; GANDIVA_EXPORT std::string ToString(const LiteralHolder& holder); diff --git a/cpp/src/gandiva/llvm_generator.cc b/cpp/src/gandiva/llvm_generator.cc index 42a05a7dff058..58efef9676f4b 100644 --- a/cpp/src/gandiva/llvm_generator.cc +++ b/cpp/src/gandiva/llvm_generator.cc @@ -664,44 +664,44 @@ void LLVMGenerator::Visitor::Visit(const LiteralDex& dex) { switch (dex.type()->id()) { case arrow::Type::BOOL: - value = types->i1_constant(arrow::util::get(dex.holder())); + value = types->i1_constant(std::get(dex.holder())); break; case arrow::Type::UINT8: - value = types->i8_constant(arrow::util::get(dex.holder())); + value = types->i8_constant(std::get(dex.holder())); break; case arrow::Type::UINT16: - value = types->i16_constant(arrow::util::get(dex.holder())); + value = types->i16_constant(std::get(dex.holder())); break; case arrow::Type::UINT32: - value = types->i32_constant(arrow::util::get(dex.holder())); + value = types->i32_constant(std::get(dex.holder())); break; case arrow::Type::UINT64: - value = types->i64_constant(arrow::util::get(dex.holder())); + value = types->i64_constant(std::get(dex.holder())); break; case arrow::Type::INT8: - value = types->i8_constant(arrow::util::get(dex.holder())); + value = types->i8_constant(std::get(dex.holder())); break; case arrow::Type::INT16: - value = types->i16_constant(arrow::util::get(dex.holder())); + value = types->i16_constant(std::get(dex.holder())); break; case arrow::Type::FLOAT: - value = types->float_constant(arrow::util::get(dex.holder())); + value = types->float_constant(std::get(dex.holder())); break; case arrow::Type::DOUBLE: - value = types->double_constant(arrow::util::get(dex.holder())); + value = types->double_constant(std::get(dex.holder())); break; case arrow::Type::STRING: case arrow::Type::BINARY: { - const std::string& str = arrow::util::get(dex.holder()); + const std::string& str = std::get(dex.holder()); value = ir_builder()->CreateGlobalStringPtr(str.c_str()); len = types->i32_constant(static_cast(str.length())); @@ -712,7 +712,7 @@ void LLVMGenerator::Visitor::Visit(const LiteralDex& dex) { case arrow::Type::DATE32: case arrow::Type::TIME32: case arrow::Type::INTERVAL_MONTHS: - value = types->i32_constant(arrow::util::get(dex.holder())); + value = types->i32_constant(std::get(dex.holder())); break; case arrow::Type::INT64: @@ -720,12 +720,12 @@ void LLVMGenerator::Visitor::Visit(const LiteralDex& dex) { case arrow::Type::TIME64: case arrow::Type::TIMESTAMP: case arrow::Type::INTERVAL_DAY_TIME: - value = types->i64_constant(arrow::util::get(dex.holder())); + value = types->i64_constant(std::get(dex.holder())); break; case arrow::Type::DECIMAL: { // build code for struct - auto scalar = arrow::util::get(dex.holder()); + auto scalar = std::get(dex.holder()); // ConstantInt doesn't have a get method that takes int128 or a pair of int64. so, // passing the string representation instead. auto int128_value = diff --git a/cpp/src/gandiva/node.h b/cpp/src/gandiva/node.h index 60bc38d367b93..858c657048986 100644 --- a/cpp/src/gandiva/node.h +++ b/cpp/src/gandiva/node.h @@ -86,12 +86,12 @@ class GANDIVA_EXPORT LiteralNode : public Node { // The default formatter prints in decimal can cause a loss in precision. so, // print in hex. Can't use hexfloat since gcc 4.9 doesn't support it. if (return_type()->id() == arrow::Type::DOUBLE) { - double dvalue = arrow::util::get(holder_); + double dvalue = std::get(holder_); uint64_t bits; memcpy(&bits, &dvalue, sizeof(bits)); ss << " raw(" << std::hex << bits << ")"; } else if (return_type()->id() == arrow::Type::FLOAT) { - float fvalue = arrow::util::get(holder_); + float fvalue = std::get(holder_); uint32_t bits; memcpy(&bits, &fvalue, sizeof(bits)); ss << " raw(" << std::hex << bits << ")"; diff --git a/cpp/src/gandiva/random_generator_holder.cc b/cpp/src/gandiva/random_generator_holder.cc index 3471c87d92b89..3d395741d70c2 100644 --- a/cpp/src/gandiva/random_generator_holder.cc +++ b/cpp/src/gandiva/random_generator_holder.cc @@ -39,7 +39,7 @@ Status RandomGeneratorHolder::Make(const FunctionNode& node, Status::Invalid("'random' function requires an int32 literal as parameter")); *holder = std::shared_ptr(new RandomGeneratorHolder( - literal->is_null() ? 0 : arrow::util::get(literal->holder()))); + literal->is_null() ? 0 : std::get(literal->holder()))); return Status::OK(); } } // namespace gandiva diff --git a/cpp/src/gandiva/regex_functions_holder.cc b/cpp/src/gandiva/regex_functions_holder.cc index b1e2e59cb2a05..d70c73f233a27 100644 --- a/cpp/src/gandiva/regex_functions_holder.cc +++ b/cpp/src/gandiva/regex_functions_holder.cc @@ -30,7 +30,7 @@ std::string& RemovePatternEscapeChars(const FunctionNode& node, std::string& pat if (node.children().size() != 2) { auto escape_char = dynamic_cast(node.children().at(2).get()); pattern.erase(std::remove(pattern.begin(), pattern.end(), - arrow::util::get(escape_char->holder()).at(0)), + std::get(escape_char->holder()).at(0)), pattern.end()); // remove escape chars } else { pattern.erase(std::remove(pattern.begin(), pattern.end(), '\\'), pattern.end()); @@ -95,10 +95,10 @@ Status LikeHolder::Make(const FunctionNode& node, std::shared_ptr* h if (node.descriptor()->name() == "ilike") { regex_op.set_case_sensitive(false); // set case-insensitive for ilike function. - return Make(arrow::util::get(literal->holder()), holder, regex_op); + return Make(std::get(literal->holder()), holder, regex_op); } if (node.children().size() == 2) { - return Make(arrow::util::get(literal->holder()), holder); + return Make(std::get(literal->holder()), holder); } else { auto escape_char = dynamic_cast(node.children().at(2).get()); ARROW_RETURN_IF( @@ -110,8 +110,8 @@ Status LikeHolder::Make(const FunctionNode& node, std::shared_ptr* h !IsArrowStringLiteral(escape_char_type), Status::Invalid( "'like' function requires a string literal as the third parameter")); - return Make(arrow::util::get(literal->holder()), - arrow::util::get(escape_char->holder()), holder); + return Make(std::get(literal->holder()), + std::get(escape_char->holder()), holder); } } @@ -180,7 +180,7 @@ Status ReplaceHolder::Make(const FunctionNode& node, Status::Invalid( "'replace' function requires a string literal as the second parameter")); - return Make(arrow::util::get(literal->holder()), holder); + return Make(std::get(literal->holder()), holder); } Status ReplaceHolder::Make(const std::string& sql_pattern, @@ -210,7 +210,7 @@ Status ExtractHolder::Make(const FunctionNode& node, literal == nullptr || !IsArrowStringLiteral(literal->return_type()->id()), Status::Invalid("'extract' function requires a literal as the second parameter")); - return ExtractHolder::Make(arrow::util::get(literal->holder()), holder); + return ExtractHolder::Make(std::get(literal->holder()), holder); } Status ExtractHolder::Make(const std::string& sql_pattern, diff --git a/cpp/src/gandiva/to_date_holder.cc b/cpp/src/gandiva/to_date_holder.cc index 1b7e2864f60fb..27a16d1779960 100644 --- a/cpp/src/gandiva/to_date_holder.cc +++ b/cpp/src/gandiva/to_date_holder.cc @@ -45,7 +45,7 @@ Status ToDateHolder::Make(const FunctionNode& node, return Status::Invalid( "'to_date' function requires a string literal as the second parameter"); } - auto pattern = arrow::util::get(literal_pattern->holder()); + auto pattern = std::get(literal_pattern->holder()); int suppress_errors = 0; if (node.children().size() == 3) { @@ -63,7 +63,7 @@ Status ToDateHolder::Make(const FunctionNode& node, "The (optional) third parameter to 'to_date' function needs to an integer " "literal to indicate whether to suppress the error"); } - suppress_errors = arrow::util::get(literal_suppress_errors->holder()); + suppress_errors = std::get(literal_suppress_errors->holder()); } return Make(pattern, suppress_errors, holder); diff --git a/cpp/src/parquet/arrow/path_internal.cc b/cpp/src/parquet/arrow/path_internal.cc index 8002f13e79941..0ef9eea1dabb7 100644 --- a/cpp/src/parquet/arrow/path_internal.cc +++ b/cpp/src/parquet/arrow/path_internal.cc @@ -89,6 +89,7 @@ #include #include #include +#include #include #include "arrow/array.h" @@ -104,7 +105,6 @@ #include "arrow/util/logging.h" #include "arrow/util/macros.h" #include "arrow/util/make_unique.h" -#include "arrow/util/variant.h" #include "arrow/visit_array_inline.h" #include "parquet/properties.h" @@ -519,9 +519,9 @@ struct PathInfo { // The vectors are expected to the same length info. // Note index order matters here. - using Node = ::arrow::util::Variant; + using Node = + std::variant; std::vector path; std::shared_ptr primitive_array; @@ -578,32 +578,32 @@ Status WritePath(ElementRange root_range, PathInfo* path_info, while (stack_position >= stack_base) { PathInfo::Node& node = path_info->path[stack_position - stack_base]; struct { - IterationResult operator()(NullableNode* node) { - return node->Run(stack_position, stack_position + 1, context); + IterationResult operator()(NullableNode& node) { + return node.Run(stack_position, stack_position + 1, context); } - IterationResult operator()(ListNode* node) { - return node->Run(stack_position, stack_position + 1, context); + IterationResult operator()(ListNode& node) { + return node.Run(stack_position, stack_position + 1, context); } - IterationResult operator()(NullableTerminalNode* node) { - return node->Run(*stack_position, context); + IterationResult operator()(NullableTerminalNode& node) { + return node.Run(*stack_position, context); } - IterationResult operator()(FixedSizeListNode* node) { - return node->Run(stack_position, stack_position + 1, context); + IterationResult operator()(FixedSizeListNode& node) { + return node.Run(stack_position, stack_position + 1, context); } - IterationResult operator()(AllPresentTerminalNode* node) { - return node->Run(*stack_position, context); + IterationResult operator()(AllPresentTerminalNode& node) { + return node.Run(*stack_position, context); } - IterationResult operator()(AllNullsTerminalNode* node) { - return node->Run(*stack_position, context); + IterationResult operator()(AllNullsTerminalNode& node) { + return node.Run(*stack_position, context); } - IterationResult operator()(LargeListNode* node) { - return node->Run(stack_position, stack_position + 1, context); + IterationResult operator()(LargeListNode& node) { + return node.Run(stack_position, stack_position + 1, context); } ElementRange* stack_position; PathWriteContext* context; } visitor = {stack_position, &context}; - IterationResult result = ::arrow::util::visit(visitor, &node); + IterationResult result = std::visit(visitor, node); if (ARROW_PREDICT_FALSE(result == kError)) { DCHECK(!context.last_status.ok()); @@ -640,39 +640,39 @@ struct FixupVisitor { int16_t rep_level_if_null = kLevelNotSet; template - void HandleListNode(T* arg) { - if (arg->rep_level() == max_rep_level) { - arg->SetLast(); + void HandleListNode(T& arg) { + if (arg.rep_level() == max_rep_level) { + arg.SetLast(); // after the last list node we don't need to fill // rep levels on null. rep_level_if_null = kLevelNotSet; } else { - rep_level_if_null = arg->rep_level(); + rep_level_if_null = arg.rep_level(); } } - void operator()(ListNode* node) { HandleListNode(node); } - void operator()(LargeListNode* node) { HandleListNode(node); } - void operator()(FixedSizeListNode* node) { HandleListNode(node); } + void operator()(ListNode& node) { HandleListNode(node); } + void operator()(LargeListNode& node) { HandleListNode(node); } + void operator()(FixedSizeListNode& node) { HandleListNode(node); } // For non-list intermediate nodes. template - void HandleIntermediateNode(T* arg) { + void HandleIntermediateNode(T& arg) { if (rep_level_if_null != kLevelNotSet) { - arg->SetRepLevelIfNull(rep_level_if_null); + arg.SetRepLevelIfNull(rep_level_if_null); } } - void operator()(NullableNode* arg) { HandleIntermediateNode(arg); } + void operator()(NullableNode& arg) { HandleIntermediateNode(arg); } - void operator()(AllNullsTerminalNode* arg) { + void operator()(AllNullsTerminalNode& arg) { // Even though no processing happens past this point we // still need to adjust it if a list occurred after an // all null array. HandleIntermediateNode(arg); } - void operator()(NullableTerminalNode*) {} - void operator()(AllPresentTerminalNode*) {} + void operator()(NullableTerminalNode&) {} + void operator()(AllPresentTerminalNode&) {} }; PathInfo Fixup(PathInfo info) { @@ -687,7 +687,7 @@ PathInfo Fixup(PathInfo info) { visitor.rep_level_if_null = 0; } for (size_t x = 0; x < info.path.size(); x++) { - ::arrow::util::visit(visitor, &info.path[x]); + std::visit(visitor, info.path[x]); } return info; } diff --git a/cpp/src/parquet/encryption/key_metadata.h b/cpp/src/parquet/encryption/key_metadata.h index 2281b96e60ec6..b6dc349f19bdf 100644 --- a/cpp/src/parquet/encryption/key_metadata.h +++ b/cpp/src/parquet/encryption/key_metadata.h @@ -18,8 +18,7 @@ #pragma once #include - -#include "arrow/util/variant.h" +#include #include "parquet/encryption/key_material.h" #include "parquet/exception.h" @@ -70,14 +69,14 @@ class PARQUET_EXPORT KeyMetadata { if (!is_internal_storage_) { throw ParquetException("key material is stored externally."); } - return ::arrow::util::get(key_material_or_reference_); + return ::std::get(key_material_or_reference_); } const std::string& key_reference() const { if (is_internal_storage_) { throw ParquetException("key material is stored internally."); } - return ::arrow::util::get(key_material_or_reference_); + return ::std::get(key_material_or_reference_); } private: @@ -87,7 +86,7 @@ class PARQUET_EXPORT KeyMetadata { bool is_internal_storage_; /// If is_internal_storage_ is true, KeyMaterial is set, /// else a string referencing to an outside "key material" is set. - ::arrow::util::Variant key_material_or_reference_; + ::std::variant key_material_or_reference_; }; } // namespace encryption diff --git a/python/pyarrow/includes/libarrow_flight.pxd b/python/pyarrow/includes/libarrow_flight.pxd index 3b9ac54fe9dee..d095217253849 100644 --- a/python/pyarrow/includes/libarrow_flight.pxd +++ b/python/pyarrow/includes/libarrow_flight.pxd @@ -577,8 +577,8 @@ cdef extern from "arrow/python/flight.h" namespace "arrow::py::flight" nogil: unique_ptr[CSchemaResult]* out) -cdef extern from "arrow/util/variant.h" namespace "arrow" nogil: - cdef cppclass CIntStringVariant" arrow::util::Variant": +cdef extern from "" namespace "std" nogil: + cdef cppclass CIntStringVariant" std::variant": CIntStringVariant() CIntStringVariant(int) CIntStringVariant(c_string) diff --git a/python/pyarrow/src/gdb.cc b/python/pyarrow/src/gdb.cc index 7541e52460903..c681dfe9caa83 100644 --- a/python/pyarrow/src/gdb.cc +++ b/python/pyarrow/src/gdb.cc @@ -35,7 +35,6 @@ #include "arrow/util/logging.h" #include "arrow/util/macros.h" #include "arrow/util/string_view.h" -#include "arrow/util/variant.h" namespace arrow { @@ -121,13 +120,6 @@ void TestSession() { auto error_result = Result(error_status); auto error_detail_result = Result(error_detail_status); - // Variants - using VariantType = util::Variant; - - VariantType int_variant{42}; - VariantType bool_variant{false}; - VariantType string_variant{std::string("hello")}; - // String views util::string_view string_view_empty{}; util::string_view string_view_abc{"abc"}; diff --git a/python/pyarrow/tests/test_gdb.py b/python/pyarrow/tests/test_gdb.py index 6b76d9b626e68..3056cb4326dc2 100644 --- a/python/pyarrow/tests/test_gdb.py +++ b/python/pyarrow/tests/test_gdb.py @@ -297,19 +297,6 @@ def test_buffer_heap(gdb_arrow): 'arrow::Buffer of size 3, mutable, "abc"') -def test_variants(gdb_arrow): - check_stack_repr( - gdb_arrow, "int_variant", - "arrow::util::Variant of index 0 (actual type int), value 42") - check_stack_repr( - gdb_arrow, "bool_variant", - "arrow::util::Variant of index 1 (actual type bool), value false") - check_stack_repr( - gdb_arrow, "string_variant", - re.compile(r'^arrow::util::Variant of index 2 \(actual type ' - r'std::.*string.*\), value .*"hello".*')) - - def test_decimals(gdb_arrow): v128 = "98765432109876543210987654321098765432" check_stack_repr(gdb_arrow, "decimal128_zero", "arrow::Decimal128(0)")