anuragmantri commented on code in PR #55:
URL: https://github.com/apache/datafusion-iceberg/pull/55#discussion_r4299374058
##########
crates/datafusion/src/read/scan.rs:
##########
@@ -93,13 +134,124 @@ impl IcebergDataSource {
pub fn fetch(&self) -> Option<usize> {
self.fetch
}
+
+ /// The data files the source merges in the order it reports, listed while
+ /// the scan was planned, or `None` if it reports no order and lists its
+ /// files when it runs.
+ pub fn sorted_tasks(&self) -> Option<&[FileScanTask]> {
+ self.sorted.as_ref().map(|sorted| sorted.tasks.as_slice())
+ }
+
+ /// Lists the source's data files and reports the sort order they all
+ /// record, if the session enables
`iceberg.planning.preserve_data_ordering`
+ /// and they are few enough to merge. Otherwise returns the source
unchanged.
+ pub(crate) async fn plan_sort_order(self, state: &dyn Session) ->
Result<Self> {
+ let config = state
+ .config()
+ .options()
+ .extensions
+ .get::<IcebergDataFusionConfig>()
+ .map(|config| config.planning.clone())
+ .unwrap_or_default();
+ if !config.preserve_data_ordering {
+ return Ok(self);
+ }
+
+ let metadata = self.table.metadata();
+ let snapshot = match self.snapshot_id {
+ Some(snapshot_id) => metadata.snapshot_by_id(snapshot_id),
+ None => metadata.current_snapshot(),
+ };
+ let Some(snapshot) = snapshot else {
+ return Ok(self);
+ };
+ let schema = snapshot.schema(metadata).map_err(to_datafusion_error)?;
+ let output = self.schema();
+ if !metadata
+ .sort_orders_iter()
+ .any(|order| lex_ordering(order, &schema, &output).is_some())
+ {
+ return Ok(self);
+ }
+
+ let tasks: Vec<FileScanTask> = build_table_scan(
+ &self.table,
+ self.snapshot_id,
+ Some(self.projection.clone()),
+ self.predicate.clone(),
+ )?
+ .plan_files()
+ .await
+ .map_err(to_datafusion_error)?
+ .take(config.max_merge_files.saturating_add(1))
+ .try_collect()
+ .await
+ .map_err(to_datafusion_error)?;
+ if tasks.len() > config.max_merge_files {
+ return Ok(self);
+ }
+ let mut source = self;
+ source.sorted = shared_ordering(&tasks, &output)
+ .map(|ordering| SortedTasks { tasks, ordering });
+ Ok(source)
+ }
+
+ /// Reads each sorted data file as its own stream and merges the streams in
+ /// the order the source reports.
+ fn merge_sorted(
+ &self,
+ sorted: &SortedTasks,
+ partition: usize,
+ context: &TaskContext,
+ ) -> Result<SendableRecordBatchStream> {
+ // The builder defaults to the reader settings `TableScan::to_arrow`
uses.
+ let reader = self.table.reader_builder().build();
+ let mut streams = sorted
+ .tasks
+ .iter()
+ .map(|task| {
+ let tasks = futures::stream::iter([Ok(task.clone())]).boxed();
+ let batches = reader
+ .clone()
+ .read(tasks)
+ .map_err(to_datafusion_error)?
+ .stream()
+ .map_err(to_datafusion_error);
+ Ok(
+ Box::pin(RecordBatchStreamAdapter::new(self.schema(),
batches))
+ as SendableRecordBatchStream,
+ )
+ })
+ .collect::<Result<Vec<_>>>()?;
+ if streams.len() == 1 {
+ return Ok(streams.remove(0));
+ }
Review Comment:
Good catch. Added `test_sorted_scan_of_one_file_and_of_no_files` in 6a0c8f1.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]