Skip to main content

datafusion_datasource/file_stream/
builder.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::sync::Arc;
6
7use crate::file_scan_config::FileScanConfig;
8use crate::file_stream::scan_state::ScanState;
9use crate::file_stream::work_source::{SharedWorkSource, WorkSource};
10use crate::morsel::{FileOpenerMorselizer, Morselizer};
11use datafusion_common::{Result, internal_err};
12use datafusion_physical_plan::metrics::{BaselineMetrics, ExecutionPlanMetricsSet};
13
14use super::metrics::FileStreamMetrics;
15use super::{FileOpener, FileStream, FileStreamState, OnError};
16
17/// Builder for constructing a [`FileStream`].
18pub struct FileStreamBuilder<'a> {
19    config: &'a FileScanConfig,
20    partition: Option<usize>,
21    morselizer: Option<Box<dyn Morselizer>>,
22    metrics: Option<&'a ExecutionPlanMetricsSet>,
23    on_error: OnError,
24    shared_work_source: Option<SharedWorkSource>,
25}
26
27impl<'a> FileStreamBuilder<'a> {
28    /// Create a new builder for [`FileStream`].
29    pub fn new(config: &'a FileScanConfig) -> Self {
30        Self {
31            config,
32            partition: None,
33            morselizer: None,
34            metrics: None,
35            on_error: OnError::Fail,
36            shared_work_source: None,
37        }
38    }
39
40    /// Configure the partition to scan.
41    pub fn with_partition(mut self, partition: usize) -> Self {
42        self.partition = Some(partition);
43        self
44    }
45
46    /// Configure the [`FileOpener`] used to open files.
47    ///
48    /// This will overwrite any setting from [`Self::with_morselizer`]
49    pub fn with_file_opener(mut self, file_opener: Arc<dyn FileOpener>) -> Self {
50        self.morselizer = Some(Box::new(FileOpenerMorselizer::new(file_opener)));
51        self
52    }
53
54    /// Configure the [`Morselizer`] used to open files.
55    ///
56    /// This will overwrite any setting from [`Self::with_file_opener`]
57    pub fn with_morselizer(mut self, morselizer: Box<dyn Morselizer>) -> Self {
58        self.morselizer = Some(morselizer);
59        self
60    }
61
62    /// Configure the metrics set used by the stream.
63    pub fn with_metrics(mut self, metrics: &'a ExecutionPlanMetricsSet) -> Self {
64        self.metrics = Some(metrics);
65        self
66    }
67
68    /// Configure the behavior when opening or scanning a file fails.
69    pub fn with_on_error(mut self, on_error: OnError) -> Self {
70        self.on_error = on_error;
71        self
72    }
73
74    /// Configure the [`SharedWorkSource`] for sibling work stealing.
75    pub(crate) fn with_shared_work_source(
76        mut self,
77        shared_work_source: Option<SharedWorkSource>,
78    ) -> Self {
79        self.shared_work_source = shared_work_source;
80        self
81    }
82
83    /// Build the configured [`FileStream`].
84    pub fn build(self) -> Result<FileStream> {
85        let Self {
86            config,
87            partition,
88            morselizer,
89            metrics,
90            on_error,
91            shared_work_source,
92        } = self;
93
94        let Some(partition) = partition else {
95            return internal_err!("FileStreamBuilder missing required partition");
96        };
97        let Some(morselizer) = morselizer else {
98            return internal_err!("FileStreamBuilder missing required morselizer");
99        };
100        let Some(metrics) = metrics else {
101            return internal_err!("FileStreamBuilder missing required metrics");
102        };
103        let projected_schema = config.projected_schema()?;
104        let Some(file_group) = config.file_groups.get(partition).cloned() else {
105            return internal_err!(
106                "FileStreamBuilder invalid partition index: {partition}"
107            );
108        };
109        let work_source = match shared_work_source {
110            Some(shared) => WorkSource::Shared(shared),
111            None => WorkSource::Local(file_group.into_inner().into()),
112        };
113
114        let file_stream_metrics = FileStreamMetrics::new(metrics, partition);
115        let scan_state = Box::new(ScanState::new(
116            work_source,
117            config.limit,
118            morselizer,
119            on_error,
120            file_stream_metrics,
121        ));
122
123        Ok(FileStream {
124            projected_schema,
125            state: FileStreamState::Scan { scan_state },
126            baseline_metrics: BaselineMetrics::new(metrics, partition),
127        })
128    }
129}