From c06f6f4f4b365e63de390164ad3f3d895fe395cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 26 Aug 2025 15:59:49 +0200 Subject: [PATCH 1/7] GH-47422: [Python][C++][Flight] Expose ipc::ReadStats in Flight MetadataRecordBatchReader --- cpp/src/arrow/flight/client.cc | 8 +- cpp/src/arrow/flight/client.h | 3 + cpp/src/arrow/flight/transport_server.cc | 7 +- cpp/src/arrow/flight/types.h | 4 + python/pyarrow/_flight.pyx | 13 ++++ python/pyarrow/includes/libarrow_flight.pxd | 1 + python/pyarrow/ipc.pxi | 1 - python/pyarrow/lib.pxd | 3 + python/pyarrow/tests/test_flight.py | 84 +++++++++++++++------ 9 files changed, 98 insertions(+), 26 deletions(-) diff --git a/cpp/src/arrow/flight/client.cc b/cpp/src/arrow/flight/client.cc index 2003d778052d..afb25fe99a79 100644 --- a/cpp/src/arrow/flight/client.cc +++ b/cpp/src/arrow/flight/client.cc @@ -259,6 +259,12 @@ class ClientStreamReader : public FlightStreamReader { } return batches; } + + arrow::ipc::ReadStats stats() const override { + ARROW_CHECK_NE(batch_reader_, nullptr); + return batch_reader_->stats(); + } + arrow::Result> ToTable() override { return ToTable(stop_token_); } @@ -278,7 +284,7 @@ class ClientStreamReader : public FlightStreamReader { StopToken stop_token_; std::shared_ptr memory_manager_; std::shared_ptr peekable_reader_; - std::shared_ptr batch_reader_; + std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; }; diff --git a/cpp/src/arrow/flight/client.h b/cpp/src/arrow/flight/client.h index ae6011b117aa..06e0c723d33d 100644 --- a/cpp/src/arrow/flight/client.h +++ b/cpp/src/arrow/flight/client.h @@ -141,6 +141,9 @@ class ARROW_FLIGHT_EXPORT FlightStreamReader : public MetadataRecordBatchReader using MetadataRecordBatchReader::ToTable; /// \brief Consume entire stream as a Table arrow::Result> ToTable(const StopToken& stop_token); + + /// \brief Return current read statistics + virtual arrow::ipc::ReadStats stats() const = 0; }; // Silence warning diff --git a/cpp/src/arrow/flight/transport_server.cc b/cpp/src/arrow/flight/transport_server.cc index b58e6a58b13a..c5276a9b0443 100644 --- a/cpp/src/arrow/flight/transport_server.cc +++ b/cpp/src/arrow/flight/transport_server.cc @@ -154,10 +154,15 @@ class TransportMessageReader final : public FlightMessageReader { return Status::OK(); } + ipc::ReadStats stats() const override { + ARROW_CHECK_NE(batch_reader_, nullptr); + return batch_reader_->stats(); + } + FlightDescriptor descriptor_; std::shared_ptr peekable_reader_; std::shared_ptr memory_manager_; - std::shared_ptr batch_reader_; + std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; }; diff --git a/cpp/src/arrow/flight/types.h b/cpp/src/arrow/flight/types.h index 656cc00e6764..2eef6eb53385 100644 --- a/cpp/src/arrow/flight/types.h +++ b/cpp/src/arrow/flight/types.h @@ -35,6 +35,7 @@ #include "arrow/flight/type_fwd.h" #include "arrow/flight/visibility.h" #include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" #include "arrow/ipc/writer.h" #include "arrow/result.h" #include "arrow/status.h" @@ -1179,6 +1180,9 @@ class ARROW_FLIGHT_EXPORT MetadataRecordBatchReader { /// \brief Consume entire stream as a Table virtual arrow::Result> ToTable(); + + /// \brief Return current read statistics + virtual arrow::ipc::ReadStats stats() const = 0; }; /// \brief Convert a MetadataRecordBatchReader to a regular RecordBatchReader. diff --git a/python/pyarrow/_flight.pyx b/python/pyarrow/_flight.pyx index fe2e1b3d6740..836e78c520d4 100644 --- a/python/pyarrow/_flight.pyx +++ b/python/pyarrow/_flight.pyx @@ -1118,6 +1118,19 @@ cdef class _MetadataRecordBatchReader(_Weakrefable, _ReadPandasMixin): return reader + @property + def stats(self): + """ + Current Flight read statistics. + + Returns + ------- + ReadStats + """ + if not self.reader: + raise ValueError("Operation on closed reader") + return _wrap_read_stats(( self.reader.get()).stats()) + cdef class MetadataRecordBatchReader(_MetadataRecordBatchReader): """The base class for readers for Flight streams. diff --git a/python/pyarrow/includes/libarrow_flight.pxd b/python/pyarrow/includes/libarrow_flight.pxd index b1af6bcb4fa3..caf1f67cf8b7 100644 --- a/python/pyarrow/includes/libarrow_flight.pxd +++ b/python/pyarrow/includes/libarrow_flight.pxd @@ -202,6 +202,7 @@ cdef extern from "arrow/flight/api.h" namespace "arrow" nogil: CResult[shared_ptr[CSchema]] GetSchema() CResult[CFlightStreamChunk] Next() CResult[shared_ptr[CTable]] ToTable() + CIpcReadStats stats() const CResult[shared_ptr[CRecordBatchReader]] MakeRecordBatchReader\ " arrow::flight::MakeRecordBatchReader"( diff --git a/python/pyarrow/ipc.pxi b/python/pyarrow/ipc.pxi index d57a899b58d2..9608194303da 100644 --- a/python/pyarrow/ipc.pxi +++ b/python/pyarrow/ipc.pxi @@ -125,7 +125,6 @@ class ReadStats(_ReadStats): __slots__ = () -@staticmethod cdef _wrap_read_stats(CIpcReadStats c): return ReadStats(c.num_messages, c.num_record_batches, c.num_dictionary_batches, c.num_dictionary_deltas, diff --git a/python/pyarrow/lib.pxd b/python/pyarrow/lib.pxd index cbdac7f10205..2e397cca0437 100644 --- a/python/pyarrow/lib.pxd +++ b/python/pyarrow/lib.pxd @@ -48,6 +48,9 @@ cdef class IpcReadOptions(_Weakrefable): CIpcReadOptions c_options +cdef _wrap_read_stats(CIpcReadStats c) + + cdef class Message(_Weakrefable): cdef: unique_ptr[CMessage] message diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index b1fa2fb310a7..eac0e904c4e1 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -36,7 +36,7 @@ import pytest import pyarrow as pa -from pyarrow.lib import IpcReadOptions, tobytes +from pyarrow.lib import IpcReadOptions, ReadStats, tobytes from pyarrow.util import find_free_port from pyarrow.tests import util @@ -185,6 +185,7 @@ def do_get(self, context, ticket): def do_put(self, context, descriptor, reader, writer): counter = 0 expected_data = [-10, -5, 0, 5, 10] + assert reader.stats.num_messages == 1 for batch, buf in reader: assert batch.equals(pa.RecordBatch.from_arrays( [pa.array([expected_data[counter]])], @@ -195,6 +196,8 @@ def do_put(self, context, descriptor, reader, writer): assert counter == client_counter writer.write(struct.pack(' Date: Tue, 26 Aug 2025 17:08:13 +0200 Subject: [PATCH 2/7] Minor fixes for MSVC --- cpp/src/arrow/flight/client.h | 1 + cpp/src/arrow/flight/types.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/flight/client.h b/cpp/src/arrow/flight/client.h index 06e0c723d33d..3ad9f26275b0 100644 --- a/cpp/src/arrow/flight/client.h +++ b/cpp/src/arrow/flight/client.h @@ -142,6 +142,7 @@ class ARROW_FLIGHT_EXPORT FlightStreamReader : public MetadataRecordBatchReader /// \brief Consume entire stream as a Table arrow::Result> ToTable(const StopToken& stop_token); + using MetadataRecordBatchReader::stats; /// \brief Return current read statistics virtual arrow::ipc::ReadStats stats() const = 0; }; diff --git a/cpp/src/arrow/flight/types.h b/cpp/src/arrow/flight/types.h index 2eef6eb53385..d498ac67f7a7 100644 --- a/cpp/src/arrow/flight/types.h +++ b/cpp/src/arrow/flight/types.h @@ -35,7 +35,6 @@ #include "arrow/flight/type_fwd.h" #include "arrow/flight/visibility.h" #include "arrow/ipc/options.h" -#include "arrow/ipc/reader.h" #include "arrow/ipc/writer.h" #include "arrow/result.h" #include "arrow/status.h" @@ -48,6 +47,7 @@ class Table; namespace ipc { class DictionaryMemo; +struct ReadStats; } // namespace ipc namespace util { From 28f673aafc7ff4870652e73e24444b07d3cd842c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Wed, 27 Aug 2025 13:39:34 +0200 Subject: [PATCH 3/7] Fix possible not initialized reader and add more stats tests --- cpp/src/arrow/flight/client.cc | 4 +++- cpp/src/arrow/flight/transport_server.cc | 12 +++++++----- python/pyarrow/tests/test_flight.py | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/flight/client.cc b/cpp/src/arrow/flight/client.cc index afb25fe99a79..e33b6c3fd071 100644 --- a/cpp/src/arrow/flight/client.cc +++ b/cpp/src/arrow/flight/client.cc @@ -261,7 +261,9 @@ class ClientStreamReader : public FlightStreamReader { } arrow::ipc::ReadStats stats() const override { - ARROW_CHECK_NE(batch_reader_, nullptr); + if (batch_reader_ == nullptr) { + return ipc::ReadStats{}; + } return batch_reader_->stats(); } diff --git a/cpp/src/arrow/flight/transport_server.cc b/cpp/src/arrow/flight/transport_server.cc index c5276a9b0443..782b1f764d1c 100644 --- a/cpp/src/arrow/flight/transport_server.cc +++ b/cpp/src/arrow/flight/transport_server.cc @@ -137,6 +137,13 @@ class TransportMessageReader final : public FlightMessageReader { return out; } + ipc::ReadStats stats() const override { + if (batch_reader_ == nullptr) { + return ipc::ReadStats{}; + } + return batch_reader_->stats(); + } + private: /// Ensure we are set up to read data. Status EnsureDataStarted() { @@ -154,11 +161,6 @@ class TransportMessageReader final : public FlightMessageReader { return Status::OK(); } - ipc::ReadStats stats() const override { - ARROW_CHECK_NE(batch_reader_, nullptr); - return batch_reader_->stats(); - } - FlightDescriptor descriptor_; std::shared_ptr peekable_reader_; std::shared_ptr memory_manager_; diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index eac0e904c4e1..2af9b5826620 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -424,6 +424,7 @@ def __init__(self, options=None, **kwargs): self.options = options def do_exchange(self, context, descriptor, reader, writer): + assert reader.stats.num_messages == 0 if descriptor.descriptor_type != flight.DescriptorType.CMD: raise pa.ArrowInvalid("Must provide a command descriptor") elif descriptor.command == b"echo": @@ -452,11 +453,14 @@ def exchange_do_put(self, context, reader, writer): for chunk in reader: if not chunk.data: raise pa.ArrowInvalid("All chunks must have data.") + assert reader.stats.num_messages != 0 num_batches += 1 + assert reader.stats.num_record_batches == num_batches writer.write_metadata(str(num_batches).encode("utf-8")) def exchange_echo(self, context, reader, writer): """Run a simple echo server.""" + assert reader.stats.num_messages == 0 started = False for chunk in reader: if not started and chunk.data: @@ -467,16 +471,19 @@ def exchange_echo(self, context, reader, writer): elif chunk.app_metadata: writer.write_metadata(chunk.app_metadata) elif chunk.data: + assert reader.stats.num_messages != 0 writer.write_batch(chunk.data) else: assert False, "Should not happen" def exchange_transform(self, context, reader, writer): """Sum rows in an uploaded table.""" + assert reader.stats.num_messages == 0 for field in reader.schema: if not pa.types.is_integer(field.type): raise pa.ArrowInvalid("Invalid field: " + repr(field)) table = reader.read_all() + assert reader.stats.num_messages != 0 sums = [0] * table.num_rows for column in table: for row, value in enumerate(column): @@ -2102,8 +2109,8 @@ def test_doexchange_put(): assert chunk.data is None expected_buf = str(len(batches)).encode("utf-8") assert chunk.app_metadata == expected_buf - # TODO: Investigate segfault - # assert reader.stats == {} + # Metadata only message is not counted as an ipc data message + assert reader.stats.num_messages == 0 def test_doexchange_echo(): @@ -2128,12 +2135,15 @@ def test_doexchange_echo(): # Now write data without metadata. writer.begin(data.schema) + num_batches = 0 for batch in batches: writer.write_batch(batch) assert reader.schema == data.schema chunk = reader.read_chunk() assert chunk.data == batch assert chunk.app_metadata is None + num_batches += 1 + assert reader.stats.num_record_batches == num_batches # And write data with metadata. for i, batch in enumerate(batches): @@ -2142,6 +2152,7 @@ def test_doexchange_echo(): chunk = reader.read_chunk() assert chunk.data == batch assert chunk.app_metadata == buf + assert reader.stats.num_record_batches == num_batches + i + 1 def test_doexchange_echo_v4(): From 0c1c0d4ac4d31bd30d57920e1efa2e8d0d6720f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Thu, 28 Aug 2025 16:17:57 +0200 Subject: [PATCH 4/7] nit fix for test as per review --- python/pyarrow/tests/test_flight.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index 2af9b5826620..95c974dcb094 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -2152,7 +2152,8 @@ def test_doexchange_echo(): chunk = reader.read_chunk() assert chunk.data == batch assert chunk.app_metadata == buf - assert reader.stats.num_record_batches == num_batches + i + 1 + num_batches += 1 + assert reader.stats.num_record_batches == num_batches def test_doexchange_echo_v4(): From c648fe09d4518bc2e4a507f50a6a175a2d91f145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Thu, 28 Aug 2025 16:19:35 +0200 Subject: [PATCH 5/7] Take into account metadata messages (not tracked by the batch_reader) --- cpp/src/arrow/flight/client.cc | 17 ++++++++++++++--- cpp/src/arrow/flight/transport_server.cc | 11 +++++++++-- python/pyarrow/tests/test_flight.py | 3 +-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cpp/src/arrow/flight/client.cc b/cpp/src/arrow/flight/client.cc index e33b6c3fd071..0752eb01505e 100644 --- a/cpp/src/arrow/flight/client.cc +++ b/cpp/src/arrow/flight/client.cc @@ -225,6 +225,7 @@ class ClientStreamReader : public FlightStreamReader { out.app_metadata = data->app_metadata; out.data = nullptr; peekable_reader_->Next(&data); + extra_messages_++; return out; } @@ -261,10 +262,15 @@ class ClientStreamReader : public FlightStreamReader { } arrow::ipc::ReadStats stats() const override { + ipc::ReadStats stats; if (batch_reader_ == nullptr) { - return ipc::ReadStats{}; + stats = ipc::ReadStats{}; + stats.num_messages = extra_messages_; + return stats; } - return batch_reader_->stats(); + stats = batch_reader_->stats(); + stats.num_messages += extra_messages_; + return stats; } arrow::Result> ToTable() override { @@ -288,6 +294,7 @@ class ClientStreamReader : public FlightStreamReader { std::shared_ptr peekable_reader_; std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; + int64_t extra_messages_ = 0; }; FlightMetadataReader::~FlightMetadataReader() = default; @@ -468,6 +475,7 @@ class ClientStreamWriter : public FlightStreamWriter { if (!success) { return Close(); } + extra_messages_++; return Status::OK(); } @@ -520,7 +528,9 @@ class ClientStreamWriter : public FlightStreamWriter { ipc::WriteStats stats() const override { ARROW_CHECK_NE(batch_writer_, nullptr); - return batch_writer_->stats(); + auto write_stats = batch_writer_->stats(); + write_stats.num_messages += extra_messages_; + return write_stats; } private: @@ -538,6 +548,7 @@ class ClientStreamWriter : public FlightStreamWriter { bool closed_; // Close() is expected to be idempotent Status final_status_; + int64_t extra_messages_ = 0; // Temporary state to construct the IPC payload writer ipc::IpcWriteOptions write_options_; diff --git a/cpp/src/arrow/flight/transport_server.cc b/cpp/src/arrow/flight/transport_server.cc index 782b1f764d1c..d99861328982 100644 --- a/cpp/src/arrow/flight/transport_server.cc +++ b/cpp/src/arrow/flight/transport_server.cc @@ -124,6 +124,7 @@ class TransportMessageReader final : public FlightMessageReader { out.app_metadata = data->app_metadata; out.data = nullptr; peekable_reader_->Next(&data); + extra_messages_++; return out; } @@ -138,10 +139,15 @@ class TransportMessageReader final : public FlightMessageReader { } ipc::ReadStats stats() const override { + ipc::ReadStats stats; if (batch_reader_ == nullptr) { - return ipc::ReadStats{}; + stats = ipc::ReadStats{}; + stats.num_messages = extra_messages_; + return stats; } - return batch_reader_->stats(); + stats = batch_reader_->stats(); + stats.num_messages += extra_messages_; + return stats; } private: @@ -166,6 +172,7 @@ class TransportMessageReader final : public FlightMessageReader { std::shared_ptr memory_manager_; std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; + int64_t extra_messages_ = 0; }; /// \brief An IpcPayloadWriter for ServerDataStream. diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index 95c974dcb094..c3aa089aa864 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -2109,8 +2109,7 @@ def test_doexchange_put(): assert chunk.data is None expected_buf = str(len(batches)).encode("utf-8") assert chunk.app_metadata == expected_buf - # Metadata only message is not counted as an ipc data message - assert reader.stats.num_messages == 0 + assert reader.stats.num_messages == 1 def test_doexchange_echo(): From 2553c387595c320b0e62ac98fd24967b923f3f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 2 Sep 2025 14:23:40 +0200 Subject: [PATCH 6/7] Never count metadata only messages --- cpp/src/arrow/flight/client.cc | 17 +++-------------- cpp/src/arrow/flight/transport_server.cc | 19 +++---------------- python/pyarrow/tests/test_flight.py | 3 ++- 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/cpp/src/arrow/flight/client.cc b/cpp/src/arrow/flight/client.cc index 0752eb01505e..e33b6c3fd071 100644 --- a/cpp/src/arrow/flight/client.cc +++ b/cpp/src/arrow/flight/client.cc @@ -225,7 +225,6 @@ class ClientStreamReader : public FlightStreamReader { out.app_metadata = data->app_metadata; out.data = nullptr; peekable_reader_->Next(&data); - extra_messages_++; return out; } @@ -262,15 +261,10 @@ class ClientStreamReader : public FlightStreamReader { } arrow::ipc::ReadStats stats() const override { - ipc::ReadStats stats; if (batch_reader_ == nullptr) { - stats = ipc::ReadStats{}; - stats.num_messages = extra_messages_; - return stats; + return ipc::ReadStats{}; } - stats = batch_reader_->stats(); - stats.num_messages += extra_messages_; - return stats; + return batch_reader_->stats(); } arrow::Result> ToTable() override { @@ -294,7 +288,6 @@ class ClientStreamReader : public FlightStreamReader { std::shared_ptr peekable_reader_; std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; - int64_t extra_messages_ = 0; }; FlightMetadataReader::~FlightMetadataReader() = default; @@ -475,7 +468,6 @@ class ClientStreamWriter : public FlightStreamWriter { if (!success) { return Close(); } - extra_messages_++; return Status::OK(); } @@ -528,9 +520,7 @@ class ClientStreamWriter : public FlightStreamWriter { ipc::WriteStats stats() const override { ARROW_CHECK_NE(batch_writer_, nullptr); - auto write_stats = batch_writer_->stats(); - write_stats.num_messages += extra_messages_; - return write_stats; + return batch_writer_->stats(); } private: @@ -548,7 +538,6 @@ class ClientStreamWriter : public FlightStreamWriter { bool closed_; // Close() is expected to be idempotent Status final_status_; - int64_t extra_messages_ = 0; // Temporary state to construct the IPC payload writer ipc::IpcWriteOptions write_options_; diff --git a/cpp/src/arrow/flight/transport_server.cc b/cpp/src/arrow/flight/transport_server.cc index d99861328982..197def7cabdc 100644 --- a/cpp/src/arrow/flight/transport_server.cc +++ b/cpp/src/arrow/flight/transport_server.cc @@ -124,7 +124,6 @@ class TransportMessageReader final : public FlightMessageReader { out.app_metadata = data->app_metadata; out.data = nullptr; peekable_reader_->Next(&data); - extra_messages_++; return out; } @@ -139,15 +138,10 @@ class TransportMessageReader final : public FlightMessageReader { } ipc::ReadStats stats() const override { - ipc::ReadStats stats; if (batch_reader_ == nullptr) { - stats = ipc::ReadStats{}; - stats.num_messages = extra_messages_; - return stats; + return ipc::ReadStats{}; } - stats = batch_reader_->stats(); - stats.num_messages += extra_messages_; - return stats; + return batch_reader_->stats(); } private: @@ -172,7 +166,6 @@ class TransportMessageReader final : public FlightMessageReader { std::shared_ptr memory_manager_; std::shared_ptr batch_reader_; std::shared_ptr app_metadata_; - int64_t extra_messages_ = 0; }; /// \brief An IpcPayloadWriter for ServerDataStream. @@ -247,9 +240,6 @@ class TransportMessageWriter final : public FlightMessageWriter { return MakeFlightError(FlightStatusCode::Internal, "Could not write metadata to stream (client disconnect?)"); } - // Those messages are not written through the batch writer, - // count them separately to include them in the stats. - extra_messages_++; return Status::OK(); } @@ -273,9 +263,7 @@ class TransportMessageWriter final : public FlightMessageWriter { ipc::WriteStats stats() const override { ARROW_CHECK_NE(batch_writer_, nullptr); - auto write_stats = batch_writer_->stats(); - write_stats.num_messages += extra_messages_; - return write_stats; + return batch_writer_->stats(); } private: @@ -290,7 +278,6 @@ class TransportMessageWriter final : public FlightMessageWriter { std::unique_ptr batch_writer_; std::shared_ptr app_metadata_; ::arrow::ipc::IpcWriteOptions ipc_options_; - int64_t extra_messages_ = 0; }; /// \brief Adapt TransportDataStream to the FlightMetadataWriter diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index c3aa089aa864..95c974dcb094 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -2109,7 +2109,8 @@ def test_doexchange_put(): assert chunk.data is None expected_buf = str(len(batches)).encode("utf-8") assert chunk.app_metadata == expected_buf - assert reader.stats.num_messages == 1 + # Metadata only message is not counted as an ipc data message + assert reader.stats.num_messages == 0 def test_doexchange_echo(): From 357033c53b72e904719169644115a53c0687b9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Wed, 3 Sep 2025 10:41:34 +0200 Subject: [PATCH 7/7] Fix test to avoid unnecessary if(s) --- python/pyarrow/tests/test_flight.py | 44 +++++++++++++---------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index 95c974dcb094..bcaf9dcad9bd 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -2565,42 +2565,41 @@ def received_headers(self, headers): def test_flight_dictionary_deltas_do_exchange(): - expected_dict_deltas_stats = ReadStats( - num_messages=6, - num_record_batches=3, - num_dictionary_batches=2, - num_dictionary_deltas=1, - num_replaced_dictionaries=0 - ) - expected_dict_replacement_stats = ReadStats( - num_messages=6, - num_record_batches=3, - num_dictionary_batches=2, - num_dictionary_deltas=0, - num_replaced_dictionaries=1 - ) + expected_stats = { + 'dict_deltas': ReadStats( + num_messages=6, + num_record_batches=3, + num_dictionary_batches=2, + num_dictionary_deltas=1, + num_replaced_dictionaries=0 + ), + 'dict_replacement': ReadStats( + num_messages=6, + num_record_batches=3, + num_dictionary_batches=2, + num_dictionary_deltas=0, + num_replaced_dictionaries=1 + ) + } class DeltaFlightServer(ConstantFlightServer): def do_exchange(self, context, descriptor, reader, writer): expected_table = simple_dicts_table() received_table = reader.read_all() assert received_table.equals(expected_table) + assert reader.stats == expected_stats[descriptor.command.decode()] if descriptor.command == b'dict_deltas': - assert reader.stats == expected_dict_deltas_stats - options = pa.ipc.IpcWriteOptions(emit_dictionary_deltas=True) writer.begin(expected_table.schema, options=options) writer.write_table(expected_table) if descriptor.command == b'dict_replacement': - assert reader.stats == expected_dict_replacement_stats - writer.begin(expected_table.schema) writer.write_table(expected_table) with DeltaFlightServer() as server, \ FlightClient(('localhost', server.port)) as client: expected_table = simple_dicts_table() - for command in [b"dict_deltas", b"dict_replacement"]: + for command in ["dict_deltas", "dict_replacement"]: descriptor = flight.FlightDescriptor.for_command(command) writer, reader = client.do_exchange( descriptor, @@ -2612,13 +2611,10 @@ def do_exchange(self, context, descriptor, reader, writer): # Send client table with dictionary updates with writer: writer.begin(expected_table.schema, options=pa.ipc.IpcWriteOptions( - emit_dictionary_deltas=(command == b"dict_deltas"))) + emit_dictionary_deltas=(command == "dict_deltas"))) writer.write_table(expected_table) writer.done_writing() received_table = reader.read_all() assert received_table.equals(expected_table) - if command == b"dict_deltas": - assert reader.stats == expected_dict_deltas_stats - elif command == b"dict_replacement": - assert reader.stats == expected_dict_replacement_stats + assert reader.stats == expected_stats[command]