@@ -313,12 +313,13 @@ class ReaderMixin {
313313 public:
314314 ReaderMixin (MemoryPool* pool, std::shared_ptr<io::InputStream> input,
315315 const ReadOptions& read_options, const ParseOptions& parse_options,
316- const ConvertOptions& convert_options)
316+ const ConvertOptions& convert_options, StopToken stop_token )
317317 : pool_(pool),
318318 read_options_ (read_options),
319319 parse_options_(parse_options),
320320 convert_options_(convert_options),
321- input_(std::move(input)) {}
321+ input_(std::move(input)),
322+ stop_token_(std::move(stop_token)) {}
322323
323324 protected:
324325 // Read header and column names from buffer, create column builders
@@ -500,6 +501,7 @@ class ReaderMixin {
500501
501502 std::shared_ptr<io::InputStream> input_;
502503 std::shared_ptr<internal::TaskGroup> task_group_;
504+ StopToken stop_token_;
503505};
504506
505507// ///////////////////////////////////////////////////////////////////////
@@ -697,7 +699,7 @@ class SerialStreamingReader : public BaseStreamingReader {
697699 ARROW_ASSIGN_OR_RAISE (auto rh_it,
698700 MakeReadaheadIterator (std::move (istream_it), block_queue_size));
699701 buffer_iterator_ = CSVBufferIterator::Make (std::move (rh_it));
700- task_group_ = internal::TaskGroup::MakeSerial ();
702+ task_group_ = internal::TaskGroup::MakeSerial (stop_token_ );
701703
702704 // Read schema from first batch
703705 ARROW_ASSIGN_OR_RAISE (pending_batch_, ReadNext ());
@@ -710,6 +712,10 @@ class SerialStreamingReader : public BaseStreamingReader {
710712 if (eof_) {
711713 return nullptr ;
712714 }
715+ if (stop_token_.IsStopRequested ()) {
716+ eof_ = true ;
717+ return stop_token_.Poll ();
718+ }
713719 if (!block_iterator_) {
714720 Status st = SetupReader ();
715721 if (!st.ok ()) {
@@ -790,7 +796,7 @@ class SerialTableReader : public BaseTableReader {
790796 }
791797
792798 Result<std::shared_ptr<Table>> Read () override {
793- task_group_ = internal::TaskGroup::MakeSerial ();
799+ task_group_ = internal::TaskGroup::MakeSerial (stop_token_ );
794800
795801 // First block
796802 ARROW_ASSIGN_OR_RAISE (auto first_buffer, buffer_iterator_.Next ());
@@ -804,6 +810,8 @@ class SerialTableReader : public BaseTableReader {
804810 MakeChunker (parse_options_),
805811 std::move (first_buffer));
806812 while (true ) {
813+ RETURN_NOT_OK (stop_token_.Poll ());
814+
807815 ARROW_ASSIGN_OR_RAISE (auto maybe_block, block_iterator.Next ());
808816 if (maybe_block == IterationTraits<CSVBlock>::End ()) {
809817 // EOF
@@ -833,9 +841,10 @@ class AsyncThreadedTableReader
833841 AsyncThreadedTableReader (MemoryPool* pool, std::shared_ptr<io::InputStream> input,
834842 const ReadOptions& read_options,
835843 const ParseOptions& parse_options,
836- const ConvertOptions& convert_options, Executor* cpu_executor,
837- Executor* io_executor)
838- : BaseTableReader(pool, input, read_options, parse_options, convert_options),
844+ const ConvertOptions& convert_options, StopToken stop_token,
845+ Executor* cpu_executor, Executor* io_executor)
846+ : BaseTableReader(pool, input, read_options, parse_options, convert_options,
847+ std::move (stop_token)),
839848 cpu_executor_(cpu_executor),
840849 io_executor_(io_executor) {}
841850
@@ -870,7 +879,7 @@ class AsyncThreadedTableReader
870879 Result<std::shared_ptr<Table>> Read () override { return ReadAsync ().result (); }
871880
872881 Future<std::shared_ptr<Table>> ReadAsync () override {
873- task_group_ = internal::TaskGroup::MakeThreaded (cpu_executor_);
882+ task_group_ = internal::TaskGroup::MakeThreaded (cpu_executor_, stop_token_ );
874883
875884 auto self = shared_from_this ();
876885 return ProcessFirstBuffer ().Then ([self](std::shared_ptr<Buffer> first_buffer) {
@@ -939,17 +948,30 @@ Result<std::shared_ptr<TableReader>> MakeTableReader(
939948 if (read_options.use_threads ) {
940949 auto cpu_executor = internal::GetCpuThreadPool ();
941950 auto io_executor = io_context.executor ();
942- reader = std::make_shared<AsyncThreadedTableReader>(pool, input, read_options,
943- parse_options, convert_options,
944- cpu_executor, io_executor);
951+ reader = std::make_shared<AsyncThreadedTableReader>(
952+ pool, input, read_options, parse_options, convert_options,
953+ io_context. stop_token (), cpu_executor, io_executor);
945954 } else {
946- reader = std::make_shared<SerialTableReader>(pool, input, read_options, parse_options,
947- convert_options);
955+ reader =
956+ std::make_shared<SerialTableReader>(pool, input, read_options, parse_options,
957+ convert_options, io_context.stop_token ());
948958 }
949959 RETURN_NOT_OK (reader->Init ());
950960 return reader;
951961}
952962
963+ Result<std::shared_ptr<StreamingReader>> MakeStreamingReader (
964+ io::IOContext io_context, std::shared_ptr<io::InputStream> input,
965+ const ReadOptions& read_options, const ParseOptions& parse_options,
966+ const ConvertOptions& convert_options) {
967+ std::shared_ptr<BaseStreamingReader> reader;
968+ reader = std::make_shared<SerialStreamingReader>(io_context.pool (), input, read_options,
969+ parse_options, convert_options,
970+ io_context.stop_token ());
971+ RETURN_NOT_OK (reader->Init ());
972+ return reader;
973+ }
974+
953975} // namespace
954976
955977// ///////////////////////////////////////////////////////////////////////
@@ -975,13 +997,17 @@ Result<std::shared_ptr<StreamingReader>> StreamingReader::Make(
975997 MemoryPool* pool, std::shared_ptr<io::InputStream> input,
976998 const ReadOptions& read_options, const ParseOptions& parse_options,
977999 const ConvertOptions& convert_options) {
978- std::shared_ptr<BaseStreamingReader> reader;
979- reader = std::make_shared<SerialStreamingReader>(pool, input, read_options,
980- parse_options, convert_options);
981- RETURN_NOT_OK (reader->Init ());
982- return reader;
1000+ return MakeStreamingReader (io::IOContext (pool), std::move (input), read_options,
1001+ parse_options, convert_options);
9831002}
9841003
985- } // namespace csv
1004+ Result<std::shared_ptr<StreamingReader>> StreamingReader::Make (
1005+ io::IOContext io_context, std::shared_ptr<io::InputStream> input,
1006+ const ReadOptions& read_options, const ParseOptions& parse_options,
1007+ const ConvertOptions& convert_options) {
1008+ return MakeStreamingReader (io_context, std::move (input), read_options, parse_options,
1009+ convert_options);
1010+ }
9861011
1012+ } // namespace csv
9871013} // namespace arrow
0 commit comments