Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(py): support count rows with filter in a fragment #3318

Merged
merged 9 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions python/python/lance/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ def fragment_id(self):
def count_rows(
self, filter: Optional[Union[pa.compute.Expression, str]] = None
) -> int:
if filter is not None:
raise ValueError("Does not support filter at the moment")
return self._fragment.count_rows()
if isinstance(filter, pa.compute.Expression):
raise ValueError("Does not support pyarrow Expression at the moment")
return self._fragment.count_rows(filter)

@property
def num_deletions(self) -> int:
Expand Down
11 changes: 11 additions & 0 deletions python/python/tests/test_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,14 @@ def test_fragment_merge(tmp_path):
tmp_path, merge, read_version=dataset.latest_version
)
assert [f.name for f in dataset.schema] == ["a", "b", "c", "d"]


def test_fragment_count_rows(tmp_path: Path):
data = pa.table({"a": range(800), "b": range(800)})
ds = write_dataset(data, tmp_path)

fragments = ds.get_fragments()
assert len(fragments) == 1

assert fragments[0].count_rows() == 800
assert fragments[0].count_rows("a < 200") == 200
21 changes: 5 additions & 16 deletions python/src/fragment.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
// Copyright 2024 Lance Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

use std::fmt::Write as _;
use std::sync::Arc;
Expand Down Expand Up @@ -127,11 +116,11 @@ impl FileFragment {
PyLance(self.fragment.metadata().clone())
}

#[pyo3(signature=(_filter=None))]
fn count_rows(&self, _filter: Option<String>) -> PyResult<usize> {
#[pyo3(signature=(filter=None))]
fn count_rows(&self, filter: Option<String>) -> PyResult<usize> {
RT.runtime.block_on(async {
self.fragment
.count_rows()
.count_rows(filter)
.await
.map_err(|e| PyIOError::new_err(e.to_string()))
})
Expand Down
4 changes: 2 additions & 2 deletions rust/lance/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ impl Dataset {

pub(crate) async fn count_all_rows(&self) -> Result<usize> {
let cnts = stream::iter(self.get_fragments())
.map(|f| async move { f.count_rows().await })
.map(|f| async move { f.count_rows(None).await })
.buffer_unordered(16)
.try_collect::<Vec<_>>()
.await?;
Expand Down Expand Up @@ -2037,7 +2037,7 @@ mod tests {
assert_eq!(fragments.len(), 10);
assert_eq!(dataset.count_fragments(), 10);
for fragment in &fragments {
assert_eq!(fragment.count_rows().await.unwrap(), 100);
assert_eq!(fragment.count_rows(None).await.unwrap(), 100);
let reader = fragment
.open(dataset.schema(), FragReadConfig::default(), None)
.await
Expand Down
43 changes: 30 additions & 13 deletions rust/lance/src/dataset/fragment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: Apache-4.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Wraps a Fragment of the dataset.
Expand Down Expand Up @@ -710,7 +710,7 @@ impl FileFragment {
row_id_sequence,
opened_files,
ArrowSchema::from(projection),
self.count_rows().await?,
self.count_rows(None).await?,
num_physical_rows,
)?;

Expand Down Expand Up @@ -829,7 +829,7 @@ impl FileFragment {
}

// This should return immediately on modern datasets.
let num_rows = self.count_rows().await?;
let num_rows = self.count_rows(None).await?;

// Check if there are any fields that are not in any data files
let field_ids_in_files = opened_files
Expand All @@ -849,15 +849,24 @@ impl FileFragment {
}

/// Count the rows in this fragment.
pub async fn count_rows(&self) -> Result<usize> {
let total_rows = self.physical_rows();

let deletion_count = self.count_deletions();
pub async fn count_rows(&self, filter: Option<String>) -> Result<usize> {
match filter {
Some(expr) => self
.scan()
.filter(&expr)?
.count_rows()
.await
.map(|v| v as usize),
None => {
let total_rows = self.physical_rows();
let deletion_count = self.count_deletions();

let (total_rows, deletion_count) =
futures::future::try_join(total_rows, deletion_count).await?;
let (total_rows, deletion_count) =
futures::future::try_join(total_rows, deletion_count).await?;

Ok(total_rows - deletion_count)
Ok(total_rows - deletion_count)
}
}
}

/// Get the number of rows that have been deleted in this fragment.
Expand Down Expand Up @@ -2644,7 +2653,7 @@ mod tests {
assert_eq!(fragments.len(), 5);
for f in fragments {
assert_eq!(f.metadata.num_rows(), Some(40));
assert_eq!(f.count_rows().await.unwrap(), 40);
assert_eq!(f.count_rows(None).await.unwrap(), 40);
assert_eq!(f.metadata().deletion_file, None);
}
}
Expand All @@ -2660,10 +2669,18 @@ mod tests {
let dataset = create_dataset(test_uri, data_storage_version).await;
let fragment = dataset.get_fragments().pop().unwrap();

assert_eq!(fragment.count_rows().await.unwrap(), 40);
assert_eq!(fragment.count_rows(None).await.unwrap(), 40);
assert_eq!(fragment.physical_rows().await.unwrap(), 40);
assert!(fragment.metadata.deletion_file.is_none());

assert_eq!(
fragment
.count_rows(Some("i < 170".to_string()))
.await
.unwrap(),
34
);

let fragment = fragment
.delete("i >= 160 and i <= 172")
.await
Expand All @@ -2672,7 +2689,7 @@ mod tests {

fragment.validate().await.unwrap();

assert_eq!(fragment.count_rows().await.unwrap(), 27);
assert_eq!(fragment.count_rows(None).await.unwrap(), 27);
assert_eq!(fragment.physical_rows().await.unwrap(), 40);
assert!(fragment.metadata.deletion_file.is_some());
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions rust/lance/src/dataset/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn take(
let mut frag_iter = fragments.iter();
let mut cur_frag = frag_iter.next();
let mut cur_frag_rows = if let Some(cur_frag) = cur_frag {
cur_frag.count_rows().await? as u64
cur_frag.count_rows(None).await? as u64
} else {
0
};
Expand All @@ -57,7 +57,7 @@ pub async fn take(
frag_offset += cur_frag_rows;
cur_frag = frag_iter.next();
cur_frag_rows = if let Some(cur_frag) = cur_frag {
cur_frag.count_rows().await? as u64
cur_frag.count_rows(None).await? as u64
} else {
0
};
Expand Down
2 changes: 1 addition & 1 deletion rust/lance/src/io/exec/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl LanceStream {
if let Some(next_frag) = frags_iter.next() {
let num_rows_in_frag = next_frag
.fragment
.count_rows()
.count_rows(None)
// count_rows should be a fast operation in v2 files
.now_or_never()
.ok_or(Error::Internal {
Expand Down
Loading