Executes the `run` queries, optionally saving results for later verification. If there are multiple queries only the results for the last query are saved. When `save_results` is `true`, it collects `SELECT`/`WITH` query results and stores them in `last_results`. When `save_results` is `false`, it streams results and counts rows without buffering them. If an 'expect' string is defined this metho
(&mut self, ctx: &SessionContext, save_results: bool)
| 202 | /// Returns an error if a `run` query fails or if expected plan strings |
| 203 | /// are not found. |
| 204 | pub async fn run(&mut self, ctx: &SessionContext, save_results: bool) -> Result<()> { |
| 205 | let run_queries = self |
| 206 | .queries |
| 207 | .get(&QueryDirective::Run) |
| 208 | .ok_or_else(|| exec_datafusion_err!("Run query should be loaded by now"))?; |
| 209 | |
| 210 | let mut result_count = 0; |
| 211 | |
| 212 | let result: Vec<RecordBatch> = { |
| 213 | let mut local_result = vec![]; |
| 214 | |
| 215 | for query in run_queries { |
| 216 | match save_results { |
| 217 | true => { |
| 218 | debug!( |
| 219 | "Running query (saving results) {}-{}: {query}", |
| 220 | self.group, self.subgroup |
| 221 | ); |
| 222 | |
| 223 | let df = ctx.sql(query).await?; |
| 224 | if !self.expect.is_empty() { |
| 225 | let physical_plan = df.create_physical_plan().await?; |
| 226 | self.validate_expected_plan(&physical_plan)?; |
| 227 | } |
| 228 | |
| 229 | let result_schema = Arc::new(df.schema().as_arrow().clone()); |
| 230 | let mut batches = df.collect().await?; |
| 231 | let trimmed = query.trim_start(); |
| 232 | |
| 233 | // save the output for select/with queries |
| 234 | if starts_with_ignore_ascii_case(trimmed, "select") |
| 235 | || starts_with_ignore_ascii_case(trimmed, "with") |
| 236 | { |
| 237 | if batches.is_empty() { |
| 238 | batches.push(RecordBatch::new_empty(result_schema)); |
| 239 | } |
| 240 | let row_count_for_query = |
| 241 | batches.iter().map(RecordBatch::num_rows).sum::<usize>(); |
| 242 | debug!( |
| 243 | "Persisting {} batches ({} rows)...", |
| 244 | batches.len(), |
| 245 | row_count_for_query |
| 246 | ); |
| 247 | |
| 248 | result_count = row_count_for_query; |
| 249 | local_result = batches; |
| 250 | } |
| 251 | } |
| 252 | false => { |
| 253 | debug!( |
| 254 | "Running query (ignoring results) {}-{}: {query}", |
| 255 | self.group, self.subgroup |
| 256 | ); |
| 257 | |
| 258 | result_count = self |
| 259 | .execute_sql_without_result_buffering(query, ctx) |
| 260 | .await?; |
| 261 | } |
no test coverage detected