Skip to content

Commit 6961176

Browse files
committed
[apacheGH-48084] Replace boost::optional with std::optional
Addresses comment apache#40939 (comment) Replace boost::optional with std::optional in ODBC codebase apache#48084 Fix lint
1 parent 5b83635 commit 6961176

16 files changed

+77
-56
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>
@@ -199,7 +198,7 @@ void FlightSqlConnection::PopulateMetadataSettings(
199198
metadata_settings_.chunk_buffer_capacity = GetChunkBufferCapacity(conn_property_map);
200199
}
201200

202-
boost::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
201+
std::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
203202
const Connection::ConnPropertyMap& conn_property_map) {
204203
const int32_t min_string_column_length = 1;
205204

@@ -214,7 +213,7 @@ boost::optional<int32_t> FlightSqlConnection::GetStringColumnLength(
214213
"01000", ODBCErrorCodes_GENERAL_WARNING);
215214
}
216215

217-
return boost::none;
216+
return std::nullopt;
218217
}
219218

220219
bool FlightSqlConnection::GetUseWideChar(const ConnPropertyMap& conn_property_map) {
@@ -250,7 +249,7 @@ const FlightCallOptions& FlightSqlConnection::PopulateCallOptions(
250249
const ConnPropertyMap& props) {
251250
// Set CONNECTION_TIMEOUT attribute or LOGIN_TIMEOUT depending on if this
252251
// is the first request.
253-
const boost::optional<Connection::Attribute>& connection_timeout =
252+
const std::optional<Connection::Attribute>& connection_timeout =
254253
closed_ ? GetAttribute(LOGIN_TIMEOUT) : GetAttribute(CONNECTION_TIMEOUT);
255254
if (connection_timeout && boost::get<uint32_t>(*connection_timeout) > 0) {
256255
call_options_.timeout =
@@ -388,17 +387,21 @@ bool FlightSqlConnection::SetAttribute(Connection::AttributeId attribute,
388387
}
389388
}
390389

391-
boost::optional<Connection::Attribute> FlightSqlConnection::GetAttribute(
390+
std::optional<Connection::Attribute> FlightSqlConnection::GetAttribute(
392391
Connection::AttributeId attribute) {
393392
switch (attribute) {
394393
case ACCESS_MODE:
395394
// FlightSQL does not provide this metadata.
396-
return boost::make_optional(Attribute(static_cast<uint32_t>(SQL_MODE_READ_WRITE)));
395+
return std::make_optional(Attribute(static_cast<uint32_t>(SQL_MODE_READ_WRITE)));
397396
case PACKET_SIZE:
398-
return boost::make_optional(Attribute(static_cast<uint32_t>(0)));
397+
return std::make_optional(Attribute(static_cast<uint32_t>(0)));
399398
default:
400399
const auto& it = attribute_.find(attribute);
401-
return boost::make_optional(it != attribute_.end(), it->second);
400+
if (it != attribute_.end()) {
401+
return std::make_optional(it->second);
402+
} else {
403+
return std::nullopt;
404+
}
402405
}
403406
}
404407

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

@@ -89,13 +89,17 @@ bool FlightSqlStatement::SetAttribute(StatementAttributeId attribute,
8989
}
9090
}
9191

92-
boost::optional<Statement::Attribute> FlightSqlStatement::GetAttribute(
92+
std::optional<Statement::Attribute> FlightSqlStatement::GetAttribute(
9393
StatementAttributeId attribute) {
9494
const auto& it = attribute_.find(attribute);
95-
return boost::make_optional(it != attribute_.end(), it->second);
95+
if (it != attribute_.end()) {
96+
return std::make_optional(it->second);
97+
} else {
98+
return std::nullopt;
99+
}
96100
}
97101

98-
boost::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
102+
std::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
99103
const std::string& query) {
100104
ClosePreparedStatementIfAny(prepared_statement_);
101105

@@ -107,7 +111,7 @@ boost::optional<std::shared_ptr<ResultSetMetadata>> FlightSqlStatement::Prepare(
107111

108112
const auto& result_set_metadata = std::make_shared<FlightSqlResultSetMetadata>(
109113
prepared_statement_->dataset_schema(), metadata_settings_);
110-
return boost::optional<std::shared_ptr<ResultSetMetadata>>(result_set_metadata);
114+
return std::optional<std::shared_ptr<ResultSetMetadata>>(result_set_metadata);
111115
}
112116

113117
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 {
@@ -51,9 +53,9 @@ class FlightSqlStatement : public Statement {
5153

5254
bool SetAttribute(StatementAttributeId attribute, const Attribute& value) override;
5355

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

56-
boost::optional<std::shared_ptr<ResultSetMetadata>> Prepare(
58+
std::optional<std::shared_ptr<ResultSetMetadata>> Prepare(
5759
const std::string& query) override;
5860

5961
bool ExecutePrepared() override;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ FlightStreamChunkBuffer::FlightStreamChunkBuffer(
3939
bool is_not_ok = !result.ok();
4040
bool is_not_empty = result.ok() && (result.ValueOrDie().data != nullptr);
4141

42-
return boost::make_optional(is_not_ok || is_not_empty, std::move(result));
42+
// If result is valid, save the temp Flight SQL Client for future stream reader
43+
// call. temp_flight_sql_client is intentionally null if the list of endpoint
44+
// Locations is empty.
45+
// After all data is fetched from reader, the temp client is closed.
46+
if (is_not_ok || is_not_empty) {
47+
return std::make_optional(std::move(result));
48+
} else {
49+
return std::optional<Result<FlightStreamChunk>>{};
50+
}
4351
};
4452
queue_.AddProducer(std::move(supplier));
4553
}

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
void ODBCConnection::GetConnectAttr(SQLINTEGER attribute, SQLPOINTER value,
532533
SQLINTEGER buffer_length, SQLINTEGER* output_length,
533534
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)