Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cpp/src/arrow/flight/sql/odbc/entry_points.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,7 @@ SQLRETURN SQL_API SQLBindCol(SQLHSTMT stmt, SQLUSMALLINT record_number,
}

SQLRETURN SQL_API SQLCancel(SQLHSTMT stmt) {
ARROW_LOG(DEBUG) << "SQLCancel called with stmt: " << stmt;
return ODBC::ODBCStatement::ExecuteWithDiagnostics(stmt, SQL_ERROR, [=]() {
throw arrow::flight::sql::odbc::DriverException("SQLCancel is not implemented",
"IM001");
return SQL_ERROR;
});
return arrow::flight::sql::odbc::SQLCancel(stmt);
}

SQLRETURN SQL_API SQLCloseCursor(SQLHSTMT stmt) {
Expand Down
22 changes: 22 additions & 0 deletions cpp/src/arrow/flight/sql/odbc/odbc_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,28 @@ SQLRETURN SQLExecute(SQLHSTMT stmt) {
});
}

SQLRETURN SQLCancel(SQLHSTMT stmt) {
ARROW_LOG(DEBUG) << "SQLCancel called with stmt: " << stmt;

using ODBC::ODBCStatement;

if (!stmt) {
return SQL_INVALID_HANDLE;
}

// Bypass ExecuteWithDiagnostics: it would lock the same mutex SQLFetch holds while
// blocked, so SQLCancel would wait for the very call it's meant to interrupt. It
// also shares Diagnostics with that in-progress SQLFetch.
// GH-55816 TODO: post a diagnostic record here on failure instead of swallowing
// the exception, without reintroducing the lock contention bypassed above.
try {
ODBCStatement::Of(stmt)->Cancel();
} catch (...) {
return SQL_ERROR;
}
return SQL_SUCCESS;
}

SQLRETURN SQLFetch(SQLHSTMT stmt) {
ARROW_LOG(DEBUG) << "SQLFetch called with stmt: " << stmt;

Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/flight/sql/odbc/odbc_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace arrow::flight::sql::odbc {
[[nodiscard]] SQLRETURN SQLPrepare(SQLHSTMT stmt, SQLWCHAR* queryText,
SQLINTEGER text_length);
[[nodiscard]] SQLRETURN SQLExecute(SQLHSTMT stmt);
[[nodiscard]] SQLRETURN SQLCancel(SQLHSTMT stmt);
[[nodiscard]] SQLRETURN SQLFetch(SQLHSTMT stmt);
[[nodiscard]] SQLRETURN SQLExtendedFetch(SQLHSTMT stmt, SQLUSMALLINT fetch_orientation,
SQLLEN fetch_offset, SQLULEN* row_count_ptr,
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/blocking_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class BlockingQueue {
return true;
}

bool HasActiveProducers() const { return active_threads_ > 0; }

void Close() {
std::unique_lock<std::mutex> unique_lock(mtx_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ size_t FlightSqlResultSet::Move(size_t rows, size_t bind_offset, size_t bind_typ
// Consider it might be the first call to Move() and current_chunk is not
// populated yet
assert(rows > 0);
// Mark the whole call, not just whichever GetNext() calls happen inside it, as an
// in-progress fetch: Cancel() must be able to interrupt rows being read out of an
// already-buffered chunk too, not only a GetNext() blocked waiting for the next one.
FlightStreamChunkBuffer::FetchGuard fetch_guard(chunk_buffer_);
if (current_chunk_.data == nullptr) {
if (!chunk_buffer_.GetNext(&current_chunk_)) {
return 0;
Expand Down Expand Up @@ -212,8 +216,9 @@ void FlightSqlResultSet::Close() {
}

void FlightSqlResultSet::Cancel() {
chunk_buffer_.Close();
current_chunk_.data = nullptr;
// GH-55800 TODO: Cancel the query on the server via the Flight SQL protocol's
// CancelFlightInfo, instead of only cancelling the client-side stream below.
chunk_buffer_.Cancel();
}

SQLRETURN FlightSqlResultSet::GetData(int column_n, int16_t target_type, int precision,
Expand Down
67 changes: 35 additions & 32 deletions cpp/src/arrow/flight/sql/odbc/odbc_impl/flight_sql_statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ bool FlightSqlStatement::ExecutePrepared() {
prepared_statement_->Execute(call_options_);
ThrowIfNotOK(result.status());

current_result_set_ = std::make_shared<FlightSqlResultSet>(
SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, result.ValueOrDie(), nullptr,
diagnostics_, metadata_settings_);
diagnostics_, metadata_settings_));

return true;
}
Expand All @@ -142,14 +142,22 @@ bool FlightSqlStatement::Execute(const std::string& query) {
Result<std::shared_ptr<FlightInfo>> result = sql_client_.Execute(call_options_, query);
ThrowIfNotOK(result.status());

current_result_set_ = std::make_shared<FlightSqlResultSet>(
SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, result.ValueOrDie(), nullptr,
diagnostics_, metadata_settings_);
diagnostics_, metadata_settings_));

return true;
}

std::shared_ptr<ResultSet> FlightSqlStatement::GetResultSet() {
std::lock_guard<std::mutex> guard(current_result_set_mutex_);
return current_result_set_;
}

std::shared_ptr<ResultSet> FlightSqlStatement::SetCurrentResultSet(
std::shared_ptr<ResultSet> result_set) {
std::lock_guard<std::mutex> guard(current_result_set_mutex_);
current_result_set_ = std::move(result_set);
return current_result_set_;
}

Expand All @@ -165,32 +173,30 @@ std::shared_ptr<ResultSet> FlightSqlStatement::GetTables(

if ((catalog_name && *catalog_name == "%") && (schema_name && schema_name->empty()) &&
(table_name && table_name->empty())) {
current_result_set_ =
return SetCurrentResultSet(
GetTablesForSQLAllCatalogs(column_names, client_options_, call_options_,
sql_client_, diagnostics_, metadata_settings_);
sql_client_, diagnostics_, metadata_settings_));
} else if ((catalog_name && catalog_name->empty()) &&
(schema_name && *schema_name == "%") &&
(table_name && table_name->empty())) {
current_result_set_ = GetTablesForSQLAllDbSchemas(
return SetCurrentResultSet(GetTablesForSQLAllDbSchemas(
column_names, client_options_, call_options_, sql_client_, schema_name,
diagnostics_, metadata_settings_);
diagnostics_, metadata_settings_));
} else if ((catalog_name && catalog_name->empty()) &&
(schema_name && schema_name->empty()) &&
(table_name && table_name->empty()) && (table_type && *table_type == "%")) {
current_result_set_ =
return SetCurrentResultSet(
GetTablesForSQLAllTableTypes(column_names, client_options_, call_options_,
sql_client_, diagnostics_, metadata_settings_);
sql_client_, diagnostics_, metadata_settings_));
} else {
if (table_type) {
ParseTableTypes(*table_type, table_types);
}

current_result_set_ = GetTablesForGenericUse(
return SetCurrentResultSet(GetTablesForGenericUse(
column_names, client_options_, call_options_, sql_client_, catalog_name,
schema_name, table_name, table_types, diagnostics_, metadata_settings_);
schema_name, table_name, table_types, diagnostics_, metadata_settings_));
}

return current_result_set_;
}

std::shared_ptr<ResultSet> FlightSqlStatement::GetTables_V2(
Expand Down Expand Up @@ -225,11 +231,9 @@ std::shared_ptr<ResultSet> FlightSqlStatement::GetColumns_V2(
auto transformer = std::make_shared<GetColumns_Transformer>(
metadata_settings_, OdbcVersion::V_2, column_name);

current_result_set_ = std::make_shared<FlightSqlResultSet>(
return SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, flight_info, transformer, diagnostics_,
metadata_settings_);

return current_result_set_;
metadata_settings_));
}

std::shared_ptr<ResultSet> FlightSqlStatement::GetColumns_V3(
Expand All @@ -246,11 +250,9 @@ std::shared_ptr<ResultSet> FlightSqlStatement::GetColumns_V3(
auto transformer = std::make_shared<GetColumns_Transformer>(
metadata_settings_, OdbcVersion::V_3, column_name);

current_result_set_ = std::make_shared<FlightSqlResultSet>(
return SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, flight_info, transformer, diagnostics_,
metadata_settings_);

return current_result_set_;
metadata_settings_));
}

std::shared_ptr<ResultSet> FlightSqlStatement::GetTypeInfo_V2(int16_t data_type) {
Expand All @@ -264,11 +266,9 @@ std::shared_ptr<ResultSet> FlightSqlStatement::GetTypeInfo_V2(int16_t data_type)
auto transformer = std::make_shared<GetTypeInfoTransformer>(
metadata_settings_, OdbcVersion::V_2, data_type);

current_result_set_ = std::make_shared<FlightSqlResultSet>(
return SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, flight_info, transformer, diagnostics_,
metadata_settings_);

return current_result_set_;
metadata_settings_));
}

std::shared_ptr<ResultSet> FlightSqlStatement::GetTypeInfo_V3(int16_t data_type) {
Expand All @@ -282,18 +282,21 @@ std::shared_ptr<ResultSet> FlightSqlStatement::GetTypeInfo_V3(int16_t data_type)
auto transformer = std::make_shared<GetTypeInfoTransformer>(
metadata_settings_, OdbcVersion::V_3, data_type);

current_result_set_ = std::make_shared<FlightSqlResultSet>(
return SetCurrentResultSet(std::make_shared<FlightSqlResultSet>(
sql_client_, client_options_, call_options_, flight_info, transformer, diagnostics_,
metadata_settings_);

return current_result_set_;
metadata_settings_));
}

Diagnostics& FlightSqlStatement::GetDiagnostics() { return diagnostics_; }

void FlightSqlStatement::Cancel() {
if (!current_result_set_) return;
current_result_set_->Cancel();
std::shared_ptr<ResultSet> result_set;
{
std::lock_guard<std::mutex> guard(current_result_set_mutex_);
result_set = current_result_set_;
}
if (!result_set) return;
result_set->Cancel();
}

} // namespace arrow::flight::sql::odbc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "arrow/flight/sql/api.h"
#include "arrow/flight/types.h"

#include <mutex>
#include <optional>

namespace arrow::flight::sql::odbc {
Expand All @@ -37,6 +38,9 @@ class FlightSqlStatement : public Statement {
FlightClientOptions client_options_;
FlightCallOptions call_options_;
FlightSqlClient& sql_client_;
// Guards current_result_set_, since Cancel() (SQLCancel) must be safe to call
// from a thread other than the one currently executing/fetching this statement.
std::mutex current_result_set_mutex_;
std::shared_ptr<ResultSet> current_result_set_;
std::shared_ptr<PreparedStatement> prepared_statement_;
const MetadataSettings& metadata_settings_;
Expand All @@ -47,6 +51,9 @@ class FlightSqlStatement : public Statement {
const std::string* table_type,
const ColumnNames& column_names);

// Thread-safe setter/getter for current_result_set_
std::shared_ptr<ResultSet> SetCurrentResultSet(std::shared_ptr<ResultSet> result_set);

public:
FlightSqlStatement(const Diagnostics& diagnostics, FlightSqlClient& sql_client,
FlightClientOptions client_options, FlightCallOptions call_options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ FlightStreamChunkBuffer::FlightStreamChunkBuffer(
util::ThrowIfNotOK(result.status());
std::shared_ptr<FlightStreamReader> stream_reader_ptr(std::move(result.ValueOrDie()));

{
std::lock_guard<std::mutex> guard(readers_mutex_);
stream_readers_.push_back(stream_reader_ptr);
}

BlockingQueue<std::pair<Result<FlightStreamChunk>,
std::shared_ptr<FlightSqlClient>>>::Supplier supplier = [=]()
-> std::optional<
Expand All @@ -82,15 +87,24 @@ FlightStreamChunkBuffer::FlightStreamChunkBuffer(
}

bool FlightStreamChunkBuffer::GetNext(FlightStreamChunk* chunk) {
// fetch_in_progress_ is set by the caller's FetchGuard for the whole fetch
// operation, which may span several GetNext() calls; this call itself does not
// own that flag.
std::pair<Result<FlightStreamChunk>, std::shared_ptr<FlightSqlClient>>
closeable_endpoint_stream_pair;
if (!queue_.Pop(&closeable_endpoint_stream_pair)) {
if (cancelled_) {
throw DriverException("Query was canceled", "HY008");
}
return false;
}

Result<FlightStreamChunk> result = closeable_endpoint_stream_pair.first;
if (!result.status().ok()) {
Close();
if (cancelled_) {
throw DriverException("Query was canceled", "HY008");
}
throw DriverException(result.status().message());
}
*chunk = std::move(result.ValueOrDie());
Expand All @@ -99,6 +113,22 @@ bool FlightStreamChunkBuffer::GetNext(FlightStreamChunk* chunk) {

void FlightStreamChunkBuffer::Close() { queue_.Close(); }

void FlightStreamChunkBuffer::Cancel() {
std::lock_guard<std::mutex> guard(readers_mutex_);
// Per ODBC 3.5, SQLCancel has no effect when no processing is active: only
// interrupt a GetNext() that is actually blocked waiting for a chunk, and
// otherwise leave the producers and any already-buffered rows alone.
if (!fetch_in_progress_) {
return;
}

cancelled_ = true;
for (const auto& reader : stream_readers_) {
reader->Cancel();
}
queue_.Close();
}

FlightStreamChunkBuffer::~FlightStreamChunkBuffer() { Close(); }

} // namespace arrow::flight::sql::odbc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#pragma once

#include <atomic>
#include <mutex>
#include <vector>

#include "arrow/flight/client.h"
#include "arrow/flight/sql/client.h"
#include "arrow/flight/sql/odbc/odbc_impl/blocking_queue.h"
Expand All @@ -28,6 +32,16 @@ class FlightStreamChunkBuffer {
std::pair<arrow::Result<FlightStreamChunk>, std::shared_ptr<FlightSqlClient>>>
queue_;

// Keep each endpoint’s DoGet stream reader so Cancel() can interrupt a producer
// blocked in Next() while waiting for the network.
std::mutex readers_mutex_;
std::vector<std::shared_ptr<FlightStreamReader>> stream_readers_;
// Set only when Cancel() actually interrupts an in-progress fetch (see Cancel()),
// so GetNext() can tell a cancellation apart from a normal end of stream.
std::atomic<bool> cancelled_{false};
// It's true when fetch is operating.
std::atomic<bool> fetch_in_progress_{false};

public:
FlightStreamChunkBuffer(FlightSqlClient& flight_sql_client,
const FlightClientOptions& client_options,
Expand All @@ -39,7 +53,29 @@ class FlightStreamChunkBuffer {

void Close();

// Cancels the underlying DoGet stream(s) so any producer thread blocked on a
// network read returns promptly, then closes the queue. Safe to call from a
// different thread than the one fetching rows.
void Cancel();

bool GetNext(FlightStreamChunk* chunk);

// This guard marks the fetch as active while it exists, even if the fetch
// reads several chunks with multiple `GetNext()` calls. This allows `Cancel()`
// to stop the fetch between chunks, not just while `GetNext()` is waiting
// for the next chunk.
class FetchGuard {
public:
explicit FetchGuard(FlightStreamChunkBuffer& buffer) : buffer_(buffer) {
buffer_.fetch_in_progress_ = true;
}
FetchGuard(const FetchGuard&) = delete;
FetchGuard& operator=(const FetchGuard&) = delete;
~FetchGuard() { buffer_.fetch_in_progress_ = false; }

private:
FlightStreamChunkBuffer& buffer_;
};
};

} // namespace arrow::flight::sql::odbc
Loading