From 4b77d5debd8d61ae5ba457ea722090fb3928f467 Mon Sep 17 00:00:00 2001 From: jiangxt2 Date: Wed, 25 Nov 2026 11:34:17 +0800 Subject: [PATCH] [C++][Python] Preserve row counts for empty ORC projections Generated-by: OpenAI Codex Signed-off-by: jiangxt2 --- cpp/src/arrow/adapters/orc/adapter.cc | 7 ++++ cpp/src/arrow/adapters/orc/adapter_test.cc | 36 ++++++++++++++++++++ python/pyarrow/tests/test_orc.py | 39 +++++++++++++++++++++- 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/adapters/orc/adapter.cc b/cpp/src/arrow/adapters/orc/adapter.cc index 8ee73ececdd..2ee6ecac0d0 100644 --- a/cpp/src/arrow/adapters/orc/adapter.cc +++ b/cpp/src/arrow/adapters/orc/adapter.cc @@ -478,6 +478,7 @@ class ORCFileReader::Impl { std::unique_ptr row_reader; std::unique_ptr batch; std::unique_ptr builder; + int64_t rows_read = 0; ORC_BEGIN_CATCH_NOT_OK row_reader = reader_->createRowReader(opts); @@ -489,6 +490,9 @@ class ORCFileReader::Impl { const liborc::Type& type = row_reader->getSelectedType(); while (row_reader->next(*batch)) { + if (schema->num_fields() == 0) { + rows_read += static_cast(batch->numElements); + } for (int i = 0; i < builder->num_fields(); i++) { RETURN_NOT_OK(AppendBatch(type.getSubtype(i), struct_batch.fields[i], 0, batch->numElements, builder->GetField(i))); @@ -496,6 +500,9 @@ class ORCFileReader::Impl { } ORC_END_CATCH_NOT_OK + if (schema->num_fields() == 0) { + return RecordBatch::Make(schema, rows_read, std::vector>{}); + } return builder->Flush(); } diff --git a/cpp/src/arrow/adapters/orc/adapter_test.cc b/cpp/src/arrow/adapters/orc/adapter_test.cc index 72141e4895e..69ba5dfb15e 100644 --- a/cpp/src/arrow/adapters/orc/adapter_test.cc +++ b/cpp/src/arrow/adapters/orc/adapter_test.cc @@ -495,6 +495,42 @@ std::shared_ptr MakeStripeBatchFile( return Buffer::FromString(std::string(stream.getData(), stream.getLength())); } +TEST(TestAdapterRead, EmptyProjectionPreservesRowCount) { + auto file = std::make_shared(MakeStripeBatchFile(16384, 3)); + ASSERT_OK_AND_ASSIGN(auto reader, + adapters::orc::ORCFileReader::Open(file, default_memory_pool())); + ASSERT_EQ(3, reader->NumberOfStripes()); + + ASSERT_OK_AND_ASSIGN(auto stripe_by_name, + reader->ReadStripe(1, std::vector{})); + ASSERT_OK_AND_ASSIGN(auto stripe_by_index, reader->ReadStripe(1, std::vector{})); + for (const auto& batch : {stripe_by_name, stripe_by_index}) { + ASSERT_OK(batch->ValidateFull()); + ASSERT_EQ(0, batch->num_columns()); + ASSERT_EQ(16384, batch->num_rows()); + } + + ASSERT_OK_AND_ASSIGN(auto by_name, reader->Read(std::vector{})); + ASSERT_OK_AND_ASSIGN(auto by_index, reader->Read(std::vector{})); + for (const auto& table : {by_name, by_index}) { + ASSERT_OK(table->ValidateFull()); + ASSERT_EQ(0, table->num_columns()); + ASSERT_EQ(3 * 16384, table->num_rows()); + } + + auto metadata = key_value_metadata({"key"}, {"value"}); + auto empty_schema = ::arrow::schema({}, metadata); + ASSERT_OK_AND_ASSIGN(auto with_schema, reader->Read(empty_schema)); + ASSERT_OK_AND_ASSIGN(auto with_schema_and_indices, + reader->Read(empty_schema, std::vector{})); + for (const auto& table : {with_schema, with_schema_and_indices}) { + ASSERT_OK(table->ValidateFull()); + ASSERT_EQ(0, table->num_columns()); + ASSERT_EQ(3 * 16384, table->num_rows()); + AssertSchemaEqual(*empty_schema, *table->schema(), /*check_metadata=*/true); + } +} + TEST(TestAdapterRead, StripeBatchSelectionAndState) { auto file = std::make_shared(MakeStripeBatchFile(16384, 4)); ASSERT_OK_AND_ASSIGN(auto reader, diff --git a/python/pyarrow/tests/test_orc.py b/python/pyarrow/tests/test_orc.py index 8542c63b1ab..e9323ec1a29 100644 --- a/python/pyarrow/tests/test_orc.py +++ b/python/pyarrow/tests/test_orc.py @@ -217,7 +217,8 @@ def test_timezone_absent(datadir, tmpdir): def test_orcfile_empty(datadir): from pyarrow import orc - table = orc.ORCFile(datadir / "TestOrcFile.emptyFile.orc").read() + reader = orc.ORCFile(datadir / "TestOrcFile.emptyFile.orc") + table = reader.read() assert table.num_rows == 0 expected_schema = pa.schema([ @@ -245,6 +246,13 @@ def test_orcfile_empty(datadir): )), ]) assert table.schema == expected_schema + empty_result = reader.read(columns=[]) + assert empty_result.num_columns == 0 + assert empty_result.num_rows == 0 + assert empty_result.schema.equals(pa.schema([], metadata=reader.schema.metadata), + check_metadata=True) + with pytest.raises(ValueError): + reader.read_stripe(0, columns=[]) def test_filesystem_uri(tmpdir): @@ -700,6 +708,24 @@ def batch_file(request): return data, table +@pytest.mark.parametrize('batch_file', ['uncompressed'], indirect=True) +def test_empty_projection_preserves_row_count(batch_file): + from pyarrow import orc + + data, table = batch_file + reader = orc.ORCFile(data) + result = reader.read(columns=[]) + assert result.num_columns == 0 + assert result.num_rows == table.num_rows + assert result.schema.equals(pa.schema([], metadata=reader.schema.metadata), + check_metadata=True) + for stripe in range(reader.nstripes): + batch = reader.read_stripe(stripe, columns=[]) + assert batch.num_columns == 0 + assert batch.num_rows == 16384 + batch.validate(full=True) + + @pytest.mark.parametrize('batch_size', [1000, 16384, 65536]) @pytest.mark.parametrize('stripes', [None, [2, 0, 2]]) def test_iter_batches_selection(batch_file, batch_size, stripes): @@ -866,6 +892,17 @@ def readinto(self, buffer): return len(data) +@pytest.mark.parametrize('batch_file', ['uncompressed'], indirect=True) +def test_empty_projection_read_error(batch_file): + from pyarrow import orc + + source = BatchTrackingFile(batch_file[0].to_pybytes()) + reader = orc.ORCFile(source) + source.fail = True + with pytest.raises((OSError, pa.ArrowException), match='injected ORC read failure'): + reader.read_stripe(0, columns=[]) + + @pytest.mark.parametrize('stripes', [None, [2, 0]], ids=['all', 'selected']) def test_iter_batches_lazy_close_and_owner(batch_file, monkeypatch, stripes): from pyarrow import orc