Create a mem table by reading from another data source
(
t: Arc<dyn TableProvider>,
output_partitions: Option<usize>,
state: &dyn Session,
)
| 140 | |
| 141 | /// Create a mem table by reading from another data source |
| 142 | pub async fn load( |
| 143 | t: Arc<dyn TableProvider>, |
| 144 | output_partitions: Option<usize>, |
| 145 | state: &dyn Session, |
| 146 | ) -> Result<Self> { |
| 147 | let schema = t.schema(); |
| 148 | let constraints = t.constraints(); |
| 149 | let exec = t.scan(state, None, &[], None).await?; |
| 150 | let partition_count = exec.output_partitioning().partition_count(); |
| 151 | |
| 152 | let mut join_set = JoinSet::new(); |
| 153 | |
| 154 | for part_idx in 0..partition_count { |
| 155 | let task = state.task_ctx(); |
| 156 | let exec = Arc::clone(&exec); |
| 157 | join_set.spawn(async move { |
| 158 | let stream = exec.execute(part_idx, task)?; |
| 159 | common::collect(stream).await |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | let mut data: Vec<Vec<RecordBatch>> = |
| 164 | Vec::with_capacity(exec.output_partitioning().partition_count()); |
| 165 | |
| 166 | while let Some(result) = join_set.join_next().await { |
| 167 | match result { |
| 168 | Ok(res) => data.push(res?), |
| 169 | Err(e) => { |
| 170 | if e.is_panic() { |
| 171 | std::panic::resume_unwind(e.into_panic()); |
| 172 | } else { |
| 173 | unreachable!(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | let mut exec = DataSourceExec::new(Arc::new(MemorySourceConfig::try_new( |
| 180 | &data, |
| 181 | Arc::clone(&schema), |
| 182 | None, |
| 183 | )?)); |
| 184 | if let Some(cons) = constraints { |
| 185 | exec = exec.with_constraints(cons.clone()); |
| 186 | } |
| 187 | |
| 188 | if let Some(num_partitions) = output_partitions { |
| 189 | let exec = RepartitionExec::try_new( |
| 190 | Arc::new(exec), |
| 191 | Partitioning::RoundRobinBatch(num_partitions), |
| 192 | )?; |
| 193 | |
| 194 | // execute and collect results |
| 195 | let mut output_partitions = vec![]; |
| 196 | for i in 0..exec.properties().output_partitioning().partition_count() { |
| 197 | // execute this *output* partition and collect all batches |
| 198 | let task_ctx = state.task_ctx(); |
| 199 | let mut stream = exec.execute(i, task_ctx)?; |