2828#include < cmath>
2929#include < cstdint>
3030#include < cstring>
31+ #include < limits>
3132#include < list>
3233#include < map>
3334#include < memory>
@@ -112,6 +113,10 @@ uint64_t orc_read_row_count(const ::orc::ReaderMetrics& metrics) {
112113 return OrcReadRowCountMetric<::orc::ReaderMetrics>::value (metrics);
113114}
114115
116+ bool is_orc_stop (const io::IOContext* io_ctx, const std::exception& e) {
117+ return io_ctx != nullptr && io_ctx->should_stop && std::string_view (e.what ()) == " stop" ;
118+ }
119+
115120bool is_hour_offset_timezone (std::string_view timezone) {
116121 return timezone.size () == 6 && (timezone[0 ] == ' +' || timezone[0 ] == ' -' ) &&
117122 std::isdigit (static_cast <unsigned char >(timezone[1 ])) &&
@@ -161,6 +166,9 @@ class DorisOrcInputStream final : public ::orc::InputStream {
161166 uint64_t bytes_read = 0 ;
162167 auto * out = static_cast <uint8_t *>(buf);
163168 while (bytes_read < length) {
169+ if (_io_ctx != nullptr && _io_ctx->should_stop ) {
170+ throw ::orc::ParseError (" stop" );
171+ }
164172 size_t loop_read = 0 ;
165173 Status st = _file_reader->read_at (
166174 static_cast <size_t >(offset + bytes_read),
@@ -1126,9 +1134,7 @@ void OrcReader::_collect_profile() const {
11261134}
11271135
11281136format::ColumnDefinition OrcReader::row_position_column_definition () {
1129- auto field = format::row_position_column_definition ();
1130- field.type = make_nullable (field.type );
1131- return field;
1137+ return format::row_position_column_definition ();
11321138}
11331139
11341140Status OrcReader::init (RuntimeState* state) {
@@ -1150,6 +1156,9 @@ Status OrcReader::init(RuntimeState* state) {
11501156 _state->reader = ::orc::createReader (std::move (input_stream), options);
11511157 _state->root_type = &_state->reader ->getType ();
11521158 } catch (const std::exception& e) {
1159+ if (is_orc_stop (_io_ctx.get (), e)) {
1160+ return Status::EndOfFile (" stop" );
1161+ }
11531162 return Status::InternalError (" Failed to open ORC file {}: {}" , _file_description->path ,
11541163 e.what ());
11551164 }
@@ -1723,7 +1732,10 @@ Status OrcReader::_create_row_reader() {
17231732 _state->orc_lazy_selection_valid = false ;
17241733 _state->orc_lazy_selected_rows .clear ();
17251734 _state->orc_lazy_input_rows = 0 ;
1726- _state->orc_lazy_next_batch_first_row = 0 ;
1735+ const auto initial_row_number = _state->row_reader ->getRowNumber ();
1736+ _state->orc_lazy_next_batch_first_row =
1737+ initial_row_number == std::numeric_limits<uint64_t >::max () ? 0
1738+ : initial_row_number + 1 ;
17271739 _state->column_to_selected_batch_index .clear ();
17281740 size_t physical_read_column_count = 0 ;
17291741 for (const auto file_column_id : _state->read_columns ) {
@@ -1767,18 +1779,28 @@ Status OrcReader::_filter_orc_batch(::orc::ColumnVectorBatch& data, uint16_t* se
17671779 return Status::InternalError (" New ORC lazy filter expects struct row batch" );
17681780 }
17691781
1782+ std::vector<std::optional<format::LocalColumnId>> position_to_column (
1783+ _request->local_positions .size ());
1784+ for (const auto & [file_column_id, local_position] : _request->local_positions ) {
1785+ const auto position = local_position.value ();
1786+ if (position >= position_to_column.size ()) {
1787+ return Status::InvalidArgument (" ORC scan local position {} is out of range {}" ,
1788+ position, position_to_column.size ());
1789+ }
1790+ if (position_to_column[position].has_value ()) {
1791+ return Status::InvalidArgument (" ORC scan local position {} is duplicated" , position);
1792+ }
1793+ position_to_column[position] = file_column_id;
1794+ }
1795+
17701796 Block filter_block;
1771- filter_block.reserve (_request->local_positions .size ());
1772- for (size_t position = 0 ; position < _request->local_positions .size (); ++position) {
1773- const auto local_position = format::LocalIndex (position);
1774- const auto entry_it = std::ranges::find_if (
1775- _request->local_positions ,
1776- [&](const auto & entry) { return entry.second == local_position; });
1777- if (entry_it == _request->local_positions .end ()) {
1797+ filter_block.reserve (position_to_column.size ());
1798+ for (size_t position = 0 ; position < position_to_column.size (); ++position) {
1799+ if (!position_to_column[position].has_value ()) {
17781800 return Status::InvalidArgument (" ORC scan local positions are not dense at {}" ,
17791801 position);
17801802 }
1781- const auto file_column_id = entry_it-> first ;
1803+ const auto file_column_id = *position_to_column[position] ;
17821804 if (is_row_position_column (file_column_id)) {
17831805 auto field = row_position_column_definition ();
17841806 filter_block.insert ({field.type ->create_column (), field.type , field.name });
@@ -2053,6 +2075,11 @@ Status OrcReader::get_block(Block* file_block, size_t* rows, bool* eof) {
20532075 }
20542076 *rows = 0 ;
20552077 file_block->clear_column_data (file_block->columns ());
2078+ if (_io_ctx != nullptr && _io_ctx->should_stop ) {
2079+ *eof = true ;
2080+ _eof = true ;
2081+ return Status::OK ();
2082+ }
20562083 if (_eof) {
20572084 *eof = true ;
20582085 return Status::OK ();
@@ -2067,9 +2094,15 @@ Status OrcReader::get_block(Block* file_block, size_t* rows, bool* eof) {
20672094 _state->orc_lazy_selection_valid = false ;
20682095 _state->orc_lazy_selected_rows .clear ();
20692096 _state->orc_lazy_input_rows = 0 ;
2070- _state->orc_lazy_next_batch_first_row = _state->row_reader ->getRowNumber () + 1 ;
20712097 has_next = _state->row_reader ->next (*_state->batch );
20722098 } catch (const std::exception& e) {
2099+ if (is_orc_stop (_io_ctx.get (), e)) {
2100+ file_block->clear_column_data (file_block->columns ());
2101+ *rows = 0 ;
2102+ *eof = true ;
2103+ _eof = true ;
2104+ return Status::OK ();
2105+ }
20732106 return Status::InternalError (" Failed to read ORC batch: {}" , e.what ());
20742107 }
20752108 if (has_next) {
0 commit comments