Commit 560b10e
fix: panic in ListingTableFactory when session is not SessionState (#20139)
## Which issue does this PR close?
- Closes #20113.
## Rationale for this change
This PR fixes a potential panic in `ListingTableFactory::create` when
the provided
[Session](cci:2://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/session/src/session.rs:71:0-140:1)
instance is not a `SessionState`.
Previously, the code used `.unwrap()` on
`downcast_ref::<SessionState>()`. If a custom
[Session](cci:2://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/session/src/session.rs:71:0-140:1)
implementation was used (which is allowed by the trait), this would
cause a crash. This change replaces `.unwrap()` with `ok_or_else`,
returning a proper `DataFusionError::Internal` instead.
## What changes are included in this PR?
- Replaced `.unwrap()` with `ok_or_else` in
`ListingTableFactory::create` to safely handle session downcasting.
- Added a regression test
[test_create_with_invalid_session](cci:1://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/core/src/datasource/listing_table_factory.rs:554:4-638:5)
in
[datafusion/core/src/datasource/listing_table_factory.rs](cci:7://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/core/src/datasource/listing_table_factory.rs:0:0-0:0)
that uses a
[MockSession](cci:2://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/core/src/datasource/listing_table_factory.rs:570:8-570:27)
to verify the error is returned instead of panicking.
## Are these changes tested?
Yes.
- Added new unit test
[test_create_with_invalid_session](cci:1://file:///Users/evangelisilva/.gemini/antigravity/scratch/datafusion/datafusion/core/src/datasource/listing_table_factory.rs:554:4-638:5).
- Ran `cargo test -p datafusion --lib
datasource::listing_table_factory::tests::test_create_with_invalid_session`
and it passed.
## Are there any user-facing changes?
No.
---------
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>1 parent d544735 commit 560b10e
1 file changed
Lines changed: 108 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
| |||
546 | 554 | | |
547 | 555 | | |
548 | 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 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
549 | 656 | | |
0 commit comments