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: 7 additions & 0 deletions cpp/src/arrow/adapters/orc/adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ class ORCFileReader::Impl {
std::unique_ptr<liborc::RowReader> row_reader;
std::unique_ptr<liborc::ColumnVectorBatch> batch;
std::unique_ptr<RecordBatchBuilder> builder;
int64_t rows_read = 0;

ORC_BEGIN_CATCH_NOT_OK
row_reader = reader_->createRowReader(opts);
Expand All @@ -489,13 +490,19 @@ class ORCFileReader::Impl {

const liborc::Type& type = row_reader->getSelectedType();
while (row_reader->next(*batch)) {
if (schema->num_fields() == 0) {
rows_read += static_cast<int64_t>(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)));
}
}
ORC_END_CATCH_NOT_OK

if (schema->num_fields() == 0) {
return RecordBatch::Make(schema, rows_read, std::vector<std::shared_ptr<Array>>{});
}
return builder->Flush();
}

Expand Down
36 changes: 36 additions & 0 deletions cpp/src/arrow/adapters/orc/adapter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,42 @@ std::shared_ptr<Buffer> MakeStripeBatchFile(
return Buffer::FromString(std::string(stream.getData(), stream.getLength()));
}

TEST(TestAdapterRead, EmptyProjectionPreservesRowCount) {
auto file = std::make_shared<io::BufferReader>(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<std::string>{}));
ASSERT_OK_AND_ASSIGN(auto stripe_by_index, reader->ReadStripe(1, std::vector<int>{}));
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<std::string>{}));
ASSERT_OK_AND_ASSIGN(auto by_index, reader->Read(std::vector<int>{}));
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<int>{}));
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<io::BufferReader>(MakeStripeBatchFile(16384, 4));
ASSERT_OK_AND_ASSIGN(auto reader,
Expand Down
39 changes: 38 additions & 1 deletion python/pyarrow/tests/test_orc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Loading