Skip to content

Commit 90261d6

Browse files
authored
GH-48084: [C++][FlightRPC] Replace boost::optional with std::optional (#48323)
### Rationale for this change Replace boost::optional with std::optional to resolve build failures on MSVC Windows CI. This PR is extracted from #48313. ### What changes are included in this PR? Replace boost::optional with std::optional in ODBC. ### Are these changes tested? Yes, locally on MSVC ### Are there any user-facing changes? N/A * GitHub Issue: #48084 Lead-authored-by: Alina (Xi) Li <[email protected]> Co-authored-by: Alina (Xi) Li <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent b2e8f25 commit 90261d6

15 files changed

+76
-60
lines changed

cpp/src/arrow/flight/sql/odbc/odbc_impl/blocking_queue.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#pragma once
1919

2020
#include <atomic>
21-
#include <boost/optional.hpp>
2221
#include <condition_variable>
2322
#include <mutex>
23+
#include <optional>
2424
#include <thread>
2525
#include <vector>
2626

@@ -43,7 +43,7 @@ class BlockingQueue {
4343
std::atomic<bool> closed_{false};
4444

4545
public:
46-
typedef std::function<boost::optional<T>(void)> Supplier;
46+
typedef std::function<std::optional<T>(void)> Supplier;
4747

4848
explicit BlockingQueue(size_t capacity) : capacity_(capacity), buffer_(capacity) {}
4949

@@ -58,7 +58,7 @@ class BlockingQueue {
5858

5959
// Only one thread at a time be notified and call supplier
6060
auto item = supplier();
61-
if (!item) break;
61+
if (!item.has_value()) break;
6262

6363
Push(*item);
6464
}

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_auth_method.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "arrow/result.h"
2525
#include "arrow/status.h"
2626

27+
#include <optional>
2728
#include <utility>
2829

2930
namespace arrow::flight::sql::odbc {
@@ -63,7 +64,7 @@ class UserPasswordAuthMethod : public FlightSqlAuthMethod {
6364
void Authenticate(FlightSqlConnection& connection,
6465
FlightCallOptions& call_options) override {
6566
FlightCallOptions auth_call_options;
66-
const boost::optional<Connection::Attribute>& login_timeout =
67+
const std::optional<Connection::Attribute>& login_timeout =
6768
connection.GetAttribute(Connection::LOGIN_TIMEOUT);
6869
if (login_timeout && boost::get<uint32_t>(*login_timeout) > 0) {
6970
// ODBC's LOGIN_TIMEOUT attribute and FlightCallOptions.timeout use

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <boost/algorithm/string/join.hpp>
3232
#include <boost/asio/ip/address.hpp>
3333
#include <boost/lexical_cast.hpp>
34-
#include <boost/optional.hpp>
3534
#include "arrow/flight/sql/odbc/odbc_impl/exceptions.h"
3635

3736
#include <sql.h>
@@ -194,7 +193,7 @@ void FlightSqlConnection::PopulateMetadataSettings(
194193
metadata_settings_.chunk_buffer_capacity = GetChunkBufferCapacity(conn_property_map);
195194
}
196195

197-
boost::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
196+
std::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
198197
const Connection::ConnPropertyMap& conn_property_map) {
199198
const int32_t min_string_column_length = 1;
200199

@@ -209,7 +208,7 @@ boost::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
209208
"01000", ODBCErrorCodes_GENERAL_WARNING);
210209
}
211210

212-
return boost::none;
211+
return std::nullopt;
213212
}
214213

215214
bool FlightSqlConnection::GetUseWideChar(const ConnPropertyMap& conn_property_map) {
@@ -245,7 +244,7 @@ const FlightCallOptions& FlightSqlConnection::PopulateCallOptions(
245244
const ConnPropertyMap& props) {
246245
// Set CONNECTION_TIMEOUT attribute or LOGIN_TIMEOUT depending on if this
247246
// is the first request.
248-
const boost::optional<Connection::Attribute>& connection_timeout =
247+
const std::optional<Connection::Attribute>& connection_timeout =
249248
closed_ ? GetAttribute(LOGIN_TIMEOUT) : GetAttribute(CONNECTION_TIMEOUT);
250249
if (connection_timeout && boost::get<uint32_t>(*connection_timeout) > 0) {
251250
call_options_.timeout =
@@ -383,17 +382,21 @@ bool FlightSqlConnection::SetAttribute(Connection::AttributeId attribute,
383382
}
384383
}
385384

386-
boost::optional<Connection::Attribute> FlightSqlConnection::GetAttribute(
385+
std::optional<Connection::Attribute> FlightSqlConnection::GetAttribute(
387386
Connection::AttributeId attribute) {
388387
switch (attribute) {
389388
case ACCESS_MODE:
390389
// FlightSQL does not provide this metadata.
391-
return boost::make_optional(Attribute(static_cast<uint32_t>(SQL_MODE_READ_WRITE)));
390+
return std::make_optional(Attribute(static_cast<uint32_t>(SQL_MODE_READ_WRITE)));
392391
case PACKET_SIZE:
393-
return boost::make_optional(Attribute(static_cast<uint32_t>(0)));
392+
return std::make_optional(Attribute(static_cast<uint32_t>(0)));
394393
default:
395394
const auto& it = attribute_.find(attribute);
396-
return boost::make_optional(it != attribute_.end(), it->second);
395+
if (it != attribute_.end()) {
396+
return std::make_optional(it->second);
397+
} else {
398+
return std::nullopt;
399+
}
397400
}
398401
}
399402

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "arrow/flight/sql/odbc/odbc_impl/spi/connection.h"
2121

22+
#include <optional>
2223
#include <vector>
2324
#include "arrow/flight/api.h"
2425
#include "arrow/flight/sql/api.h"
@@ -84,7 +85,7 @@ class FlightSqlConnection : public Connection {
8485

8586
bool SetAttribute(AttributeId attribute, const Attribute& value) override;
8687

87-
boost::optional<Connection::Attribute> GetAttribute(
88+
std::optional<Connection::Attribute> GetAttribute(
8889
Connection::AttributeId attribute) override;
8990

9091
Info GetInfo(uint16_t info_type) override;
@@ -111,8 +112,7 @@ class FlightSqlConnection : public Connection {
111112
/// \note Visible for testing
112113
void SetClosed(bool is_closed);
113114

114-
boost::optional<int32_t> GetStringColumnLength(
115-
const ConnPropertyMap& conn_property_map);
115+
std::optional<int32_t> GetStringColumnLength(const ConnPropertyMap& conn_property_map);
116116

117117
bool GetUseWideChar(const ConnPropertyMap& conn_property_map);
118118

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_connection_test.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
#include "arrow/flight/types.h"
2222
#include "gtest/gtest.h"
2323

24+
#include <optional>
25+
2426
namespace arrow::flight::sql::odbc {
2527

2628
TEST(AttributeTests, SetAndGetAttribute) {
2729
FlightSqlConnection connection(OdbcVersion::V_3);
2830
connection.SetClosed(false);
2931

3032
connection.SetAttribute(Connection::CONNECTION_TIMEOUT, static_cast<uint32_t>(200));
31-
const boost::optional<Connection::Attribute> first_value =
33+
const std::optional<Connection::Attribute> first_value =
3234
connection.GetAttribute(Connection::CONNECTION_TIMEOUT);
3335

3436
EXPECT_TRUE(first_value);
@@ -37,7 +39,7 @@ TEST(AttributeTests, SetAndGetAttribute) {
3739

3840
connection.SetAttribute(Connection::CONNECTION_TIMEOUT, static_cast<uint32_t>(300));
3941

40-
const boost::optional<Connection::Attribute> change_value =
42+
const std::optional<Connection::Attribute> change_value =
4143
connection.GetAttribute(Connection::CONNECTION_TIMEOUT);
4244

4345
EXPECT_TRUE(change_value);
@@ -49,7 +51,7 @@ TEST(AttributeTests, SetAndGetAttribute) {
4951
TEST(AttributeTests, GetAttributeWithoutSetting) {
5052
FlightSqlConnection connection(OdbcVersion::V_3);
5153

52-
const boost::optional<Connection::Attribute> optional =
54+
const std::optional<Connection::Attribute> optional =
5355
connection.GetAttribute(Connection::CONNECTION_TIMEOUT);
5456
connection.SetClosed(false);
5557

@@ -72,7 +74,7 @@ TEST(MetadataSettingsTest, StringColumnLengthTest) {
7274
std::to_string(expected_string_column_length)},
7375
};
7476

75-
const boost::optional<int32_t> actual_string_column_length =
77+
const std::optional<int32_t> actual_string_column_length =
7678
connection.GetStringColumnLength(properties);
7779

7880
EXPECT_TRUE(actual_string_column_length);

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_statement.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "arrow/flight/sql/odbc/odbc_impl/util.h"
3030
#include "arrow/io/memory.h"
3131

32-
#include <boost/optional.hpp>
32+
#include <optional>
3333
#include <utility>
3434
#include "arrow/flight/sql/odbc/odbc_impl/exceptions.h"
3535

@@ -96,13 +96,17 @@ bool FlightSqlStatement::SetAttribute(StatementAttributeId attribute,
9696
}
9797
}
9898

99-
boost::optional<Statement::Attribute> FlightSqlStatement::GetAttribute(
99+
std::optional<Statement::Attribute> FlightSqlStatement::GetAttribute(
100100
StatementAttributeId attribute) {
101101
const auto& it = attribute_.find(attribute);
102-
return boost::make_optional(it != attribute_.end(), it->second);
102+
if (it != attribute_.end()) {
103+
return std::make_optional(it->second);
104+
} else {
105+
return std::nullopt;
106+
}
103107
}
104108

105-
boost::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
109+
std::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
106110
const std::string& query) {
107111
ClosePreparedStatementIfAny(prepared_statement_, call_options_);
108112

@@ -114,7 +118,7 @@ boost::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
114118

115119
const auto& result_set_metadata = std::make_shared<FlightSqlResultSetMetadata>(
116120
prepared_statement_->dataset_schema(), metadata_settings_);
117-
return boost::optional<std::shared_ptr<ResultSetMetadata>>(result_set_metadata);
121+
return std::optional<std::shared_ptr<ResultSetMetadata>>(result_set_metadata);
118122
}
119123

120124
bool FlightSqlStatement::ExecutePrepared() {

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_statement.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "arrow/flight/sql/api.h"
2727
#include "arrow/flight/types.h"
2828

29+
#include <optional>
30+
2931
namespace arrow::flight::sql::odbc {
3032

3133
class FlightSqlStatement : public Statement {
@@ -53,9 +55,9 @@ class FlightSqlStatement : public Statement {
5355

5456
bool SetAttribute(StatementAttributeId attribute, const Attribute& value) override;
5557

56-
boost::optional<Attribute> GetAttribute(StatementAttributeId attribute) override;
58+
std::optional<Attribute> GetAttribute(StatementAttributeId attribute) override;
5759

58-
boost::optional<std::shared_ptr<ResultSetMetadata>> Prepare(
60+
std::optional<std::shared_ptr<ResultSetMetadata>> Prepare(
5961
const std::string& query) override;
6062

6163
bool ExecutePrepared() override;

cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_stream_chunk_buffer.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ FlightStreamChunkBuffer::FlightStreamChunkBuffer(
5959
std::shared_ptr<FlightStreamReader> stream_reader_ptr(std::move(result.ValueOrDie()));
6060

6161
BlockingQueue<std::pair<Result<FlightStreamChunk>,
62-
std::shared_ptr<FlightSqlClient>>>::Supplier supplier = [=] {
62+
std::shared_ptr<FlightSqlClient>>>::Supplier supplier = [=]()
63+
-> std::optional<
64+
std::pair<Result<FlightStreamChunk>, std::shared_ptr<FlightSqlClient>>> {
6365
auto result = stream_reader_ptr->Next();
6466
bool is_not_ok = !result.ok();
6567
bool is_not_empty = result.ok() && (result.ValueOrDie().data != nullptr);
@@ -68,11 +70,12 @@ FlightStreamChunkBuffer::FlightStreamChunkBuffer(
6870
// call. temp_flight_sql_client is intentionally null if the list of endpoint
6971
// Locations is empty.
7072
// After all data is fetched from reader, the temp client is closed.
71-
72-
// gh-48084 Replace boost::optional with std::optional
73-
return boost::make_optional(
74-
is_not_ok || is_not_empty,
75-
std::make_pair(std::move(result), temp_flight_sql_client));
73+
if (is_not_ok || is_not_empty) {
74+
return std::make_optional(
75+
std::make_pair(std::move(result), temp_flight_sql_client));
76+
} else {
77+
return std::nullopt;
78+
}
7679
};
7780
queue_.AddProducer(std::move(supplier));
7881
}

cpp/src/arrow/flight/sql/odbc/odbc_impl/odbc_connection.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <boost/xpressive/xpressive.hpp>
3737
#include <iterator>
3838
#include <memory>
39+
#include <optional>
3940
#include <utility>
4041

4142
using ODBC::ODBCConnection;
@@ -531,7 +532,7 @@ void ODBCConnection::SetConnectAttr(SQLINTEGER attribute, SQLPOINTER value,
531532
SQLRETURN ODBCConnection::GetConnectAttr(SQLINTEGER attribute, SQLPOINTER value,
532533
SQLINTEGER buffer_length,
533534
SQLINTEGER* output_length, bool is_unicode) {
534-
boost::optional<Connection::Attribute> spi_attribute;
535+
std::optional<Connection::Attribute> spi_attribute;
535536

536537
switch (attribute) {
537538
// Internal connection attributes

cpp/src/arrow/flight/sql/odbc/odbc_impl/odbc_statement.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#include <sql.h>
3030
#include <sqlext.h>
3131
#include <sqltypes.h>
32-
#include <boost/optional.hpp>
3332
#include <boost/variant.hpp>
33+
#include <optional>
3434
#include <utility>
3535

3636
using ODBC::DescriptorRecord;
@@ -273,7 +273,7 @@ void ODBCStatement::CopyAttributesFromConnection(ODBCConnection& connection) {
273273
bool ODBCStatement::IsPrepared() const { return is_prepared_; }
274274

275275
void ODBCStatement::Prepare(const std::string& query) {
276-
boost::optional<std::shared_ptr<ResultSetMetadata> > metadata =
276+
std::optional<std::shared_ptr<ResultSetMetadata> > metadata =
277277
spi_statement_->Prepare(query);
278278

279279
if (metadata) {
@@ -352,7 +352,7 @@ bool ODBCStatement::Fetch(size_t rows) {
352352
void ODBCStatement::GetStmtAttr(SQLINTEGER statement_attribute, SQLPOINTER output,
353353
SQLINTEGER buffer_size, SQLINTEGER* str_len_ptr,
354354
bool is_unicode) {
355-
boost::optional<Statement::Attribute> spi_attribute;
355+
std::optional<Statement::Attribute> spi_attribute;
356356
switch (statement_attribute) {
357357
// Descriptor accessor attributes
358358
case SQL_ATTR_APP_PARAM_DESC:

0 commit comments

Comments
 (0)