Commit 769f367
authored
fix: Track elapsed_compute metric for CSV scans (#18901)
## Which issue does this PR close?
- Closes #18795
## Rationale for this change
Currently, scanning CSV files does not correctly report the
`elapsed_compute` metric in `BaselineMetrics`. When running `EXPLAIN
ANALYZE` on a CSV table, the `elapsed_compute` time is reported as
negligible (e.g., `1ns` or `42ns`), even for large files, because the
time spent parsing the CSV data is not being measured.
This PR ensures that the time spent reading and parsing CSV batches is
correctly accounted for in the execution metrics.
## What changes are included in this PR?
- Modified `datafusion/datasource-csv/src/source.rs`.
- Updated `CsvOpener` to store the `partition_index`.
- Initialized `BaselineMetrics` within C`svOpener::open`.
- Wrapped the underlying CSV reader iterator using `std::iter::from_fn`.
This allows us to start a timer before calling `reader.next()` and stop
it immediately after, ensuring the CPU time spent parsing the CSV is
captured in `elapsed_compute`.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes. Existing unit tests passed (`cargo test -p
datafusion-datasource-csv`).
Additionally, I verified the fix manually using `datafusion-cli` and
`EXPLAIN ANALYZE`.
**Before the fix:** elapsed_compute was reported as 85ns (effectively
zero).
```
DataSourceExec: ... metrics=[output_rows=3, elapsed_compute=85ns, ... time_elapsed_processing=1.54ms ...]
```
**After the fix**: elapsed_compute is reported as 1.11ms, accurately
reflecting the parsing time.
```
DataSourceExec: ... metrics=[output_rows=3, elapsed_compute=1.11ms, ... time_elapsed_processing=1.26ms ...]
```
## Are there any user-facing changes?
Users viewing `EXPLAIN ANALYZE` output for queries involving CSV files
will now see accurate `elapsed_compute` values instead of near-zero
values.1 parent 4eb2933 commit 769f367
1 file changed
+19
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
| 216 | + | |
216 | 217 | | |
217 | 218 | | |
218 | 219 | | |
| |||
226 | 227 | | |
227 | 228 | | |
228 | 229 | | |
| 230 | + | |
229 | 231 | | |
230 | 232 | | |
231 | 233 | | |
| |||
241 | 243 | | |
242 | 244 | | |
243 | 245 | | |
244 | | - | |
| 246 | + | |
245 | 247 | | |
246 | 248 | | |
247 | 249 | | |
248 | 250 | | |
249 | 251 | | |
| 252 | + | |
250 | 253 | | |
251 | 254 | | |
252 | 255 | | |
| |||
352 | 355 | | |
353 | 356 | | |
354 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
355 | 361 | | |
356 | 362 | | |
357 | 363 | | |
| |||
391 | 397 | | |
392 | 398 | | |
393 | 399 | | |
394 | | - | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
395 | 411 | | |
396 | 412 | | |
397 | 413 | | |
| |||
0 commit comments