Skip to content

Commit af8de1d

Browse files
yanghuawestonpace
andauthored
feat: expose num_rows api for python LanceFileReader (#4053)
Co-authored-by: Weston Pace <weston.pace@gmail.com>
1 parent 1afdf3f commit af8de1d

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

python/python/lance/file.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def read_global_buffer(self, index: int) -> bytes:
179179
"""
180180
return self._reader.read_global_buffer(index)
181181

182+
def num_rows(self) -> int:
183+
"""Return the number of rows belonging to the data file."""
184+
return self._reader.num_rows()
185+
182186

183187
class LanceFileWriter:
184188
"""

python/python/lance/lance/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class LanceFileReader:
121121
def read_global_buffer(self, index: int) -> bytes: ...
122122
def metadata(self) -> LanceFileMetadata: ...
123123
def file_statistics(self) -> LanceFileStatistics: ...
124+
def num_rows(self): ...
124125

125126
class LanceBufferDescriptor:
126127
position: int

python/python/tests/test_file.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ def test_take(tmp_path):
110110
assert table == pa.table({"a": [0, 77, 83]})
111111

112112

113+
def test_num_rows(tmp_path):
114+
path = tmp_path / "foo.lance"
115+
schema = pa.schema([pa.field("a", pa.int64())])
116+
writer = LanceFileWriter(str(path), schema)
117+
writer.write_batch(pa.table({"a": [i for i in range(100)]}))
118+
writer.close()
119+
120+
reader = LanceFileReader(str(path))
121+
assert reader.num_rows() == 100
122+
123+
113124
def check_round_trip(tmp_path, table):
114125
path = tmp_path / "foo.lance"
115126
with LanceFileWriter(str(path), table.schema) as writer:

python/src/file.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,10 @@ impl LanceFileReader {
553553
.infer_error()?;
554554
Ok(buffer_bytes.to_vec())
555555
}
556+
557+
pub fn num_rows(&mut self) -> u64 {
558+
self.inner.num_rows()
559+
}
556560
}
557561

558562
#[cfg(test)]

0 commit comments

Comments
 (0)