use polars_buffer::Buffer;
use polars_core::prelude::*;
use polars_io::RowIndex;
use polars_io::cloud::CloudOptions;
use polars_plan::prelude::UnionArgs;
use polars_utils::pl_path::PlRefPath;
use crate::prelude::*;
pub trait LazyFileListReader: Clone {
fn finish(self) -> PolarsResult<LazyFrame> {
if !self.glob() {
return self.finish_no_glob();
}
let ScanSources::Paths(paths) = self.sources() else {
unreachable!("opened-files or in-memory buffers should never be globbed");
};
let lfs = paths
.iter()
.map(|path| {
self.clone()
.with_n_rows(None)
.with_row_index(None)
.with_paths(Buffer::from_iter([path.clone()]))
.with_rechunk(false)
.finish_no_glob()
.map_err(|e| {
polars_err!(
ComputeError: "error while reading {}: {}", path, e
)
})
})
.collect::<PolarsResult<Vec<_>>>()?;
polars_ensure!(
!lfs.is_empty(),
ComputeError: "no matching files found in {:?}", paths.iter().map(|x| x.as_str()).collect::<Vec<_>>()
);
let mut lf = self.concat_impl(lfs)?;
if let Some(n_rows) = self.n_rows() {
lf = lf.slice(0, n_rows as IdxSize)
};
if let Some(rc) = self.row_index() {
lf = lf.with_row_index(rc.name.clone(), Some(rc.offset))
};
Ok(lf)
}
fn concat_impl(&self, lfs: Vec<LazyFrame>) -> PolarsResult<LazyFrame> {
let args = UnionArgs {
rechunk: self.rechunk(),
parallel: true,
to_supertypes: false,
from_partitioned_ds: true,
..Default::default()
};
concat_impl(&lfs, args)
}
fn finish_no_glob(self) -> PolarsResult<LazyFrame>;
fn glob(&self) -> bool {
true
}
fn sources(&self) -> &ScanSources;
#[must_use]
fn with_sources(self, source: ScanSources) -> Self;
#[must_use]
fn with_paths(self, paths: Buffer<PlRefPath>) -> Self {
self.with_sources(ScanSources::Paths(paths))
}
fn with_n_rows(self, n_rows: impl Into<Option<usize>>) -> Self;
fn with_row_index(self, row_index: impl Into<Option<RowIndex>>) -> Self;
fn rechunk(&self) -> bool;
#[must_use]
fn with_rechunk(self, toggle: bool) -> Self;
fn n_rows(&self) -> Option<usize>;
fn row_index(&self) -> Option<&RowIndex>;
fn cloud_options(&self) -> Option<&CloudOptions> {
None
}
}