Commit 5b65e70
authored
Cap SortPreservingMerge statistics by fetch (apache#23359)
## Which issue does this PR close?
- Closes none.
## Rationale for this change
`SortPreservingMergeExec` supports `fetch`, but its statistics currently
pass through child statistics unchanged. This can make cost-based
optimizers overestimate top-k merge outputs. For example, a
`SortPreservingMergeExec(fetch=1)` side can still look as large as its
input to `JoinSelection`, causing hash join build/probe choices to miss
the small top-k side.
Other fetch-aware operators such as `SortExec`,
`CoalescePartitionsExec`, and limit execs already cap their statistics
by fetch. This PR aligns `SortPreservingMergeExec` with that behavior.
## What changes are included in this PR?
- Cap `SortPreservingMergeExec` statistics with
`Statistics::with_fetch(self.fetch, 0, 1)`.
- Preserve no-op statistics for `fetch = None` and `skip = 0`.
- Avoid scaling byte-size estimates by `0.0` when input row count is
unknown; use absent byte-size stats instead.
- Add regression tests for SPM fetch statistics and for `JoinSelection`
swapping an SPM(fetch=1) side to the hash join build side.
## Are these changes tested?
Yes.
- `cargo test -p datafusion-common test_with_fetch`
- `cargo test -p datafusion-physical-plan sort_preserving_merge`
- `cargo test -p datafusion join_selection --test core_integration`
- `cargo check -p datafusion --test core_integration`
- `cargo fmt --all -- --check`
- `cargo clippy --all-targets --all-features -- -D warnings`
## Are there any user-facing changes?
No user-facing API changes. This improves optimizer statistics for
fetch-limited sort-preserving merge plans.1 parent d75742e commit 5b65e70
3 files changed
Lines changed: 189 additions & 14 deletions
File tree
- datafusion
- common/src
- core/tests/physical_optimizer
- physical-plan/src/sorts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
545 | 545 | | |
546 | 546 | | |
547 | 547 | | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
548 | 552 | | |
549 | 553 | | |
550 | 554 | | |
| |||
598 | 602 | | |
599 | 603 | | |
600 | 604 | | |
601 | | - | |
| 605 | + | |
602 | 606 | | |
603 | 607 | | |
604 | 608 | | |
605 | 609 | | |
606 | 610 | | |
607 | | - | |
| 611 | + | |
608 | 612 | | |
609 | | - | |
| 613 | + | |
610 | 614 | | |
611 | 615 | | |
612 | | - | |
| 616 | + | |
613 | 617 | | |
614 | 618 | | |
615 | 619 | | |
616 | 620 | | |
617 | 621 | | |
618 | 622 | | |
619 | 623 | | |
620 | | - | |
621 | | - | |
| 624 | + | |
| 625 | + | |
622 | 626 | | |
623 | 627 | | |
624 | | - | |
| 628 | + | |
625 | 629 | | |
626 | 630 | | |
627 | 631 | | |
| |||
643 | 647 | | |
644 | 648 | | |
645 | 649 | | |
646 | | - | |
647 | | - | |
| 650 | + | |
| 651 | + | |
648 | 652 | | |
649 | 653 | | |
650 | | - | |
| 654 | + | |
651 | 655 | | |
652 | 656 | | |
653 | 657 | | |
| |||
2376 | 2380 | | |
2377 | 2381 | | |
2378 | 2382 | | |
| 2383 | + | |
| 2384 | + | |
| 2385 | + | |
| 2386 | + | |
| 2387 | + | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
2379 | 2415 | | |
2380 | 2416 | | |
2381 | 2417 | | |
| |||
Lines changed: 80 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
264 | 266 | | |
265 | 267 | | |
266 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
267 | 347 | | |
268 | 348 | | |
269 | 349 | | |
| |||
Lines changed: 63 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
399 | | - | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
400 | 409 | | |
401 | 410 | | |
402 | 411 | | |
| |||
446 | 455 | | |
447 | 456 | | |
448 | 457 | | |
| 458 | + | |
449 | 459 | | |
450 | 460 | | |
451 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
452 | 464 | | |
453 | 465 | | |
454 | 466 | | |
| |||
458 | 470 | | |
459 | 471 | | |
460 | 472 | | |
| 473 | + | |
461 | 474 | | |
462 | | - | |
| 475 | + | |
463 | 476 | | |
464 | 477 | | |
465 | 478 | | |
| |||
524 | 537 | | |
525 | 538 | | |
526 | 539 | | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
527 | 586 | | |
528 | 587 | | |
529 | 588 | | |
| |||
0 commit comments