Commit 6f1040b
authored
Fix PushdownSort dropping LIMIT when eliminating SortExec (#21744)
## Which issue does this PR close?
When `PushdownSort` removes a `SortExec` because a source returns
`Exact` (guaranteeing ordering), any `fetch` (LIMIT) on the `SortExec`
is silently dropped if the underlying plan does not support
`with_fetch()`.
For example, `ProjectionExec` supports `try_pushdown_sort` (delegating
to its child) but does not implement `with_fetch()`. A plan like
`SortExec(fetch=10) → ProjectionExec → source` that gets sort-eliminated
loses the limit.
## What changes are included in this PR?
In the `Exact` branch of `PushdownSort`, when the eliminated `SortExec`
carried a `fetch`:
1. Try `with_fetch()` on the pushed-down source first
2. If `with_fetch()` returns `None`, fall back to wrapping with
`GlobalLimitExec`
## Are these changes tested?
Yes. Three new unit tests:
- `test_sort_pushdown_exact_no_fetch_no_limit` — Exact elimination
without fetch: no limit wrapper added
- `test_sort_pushdown_exact_preserves_fetch_with_global_limit` — Exact
elimination with fetch, source does NOT support `with_fetch()`:
`GlobalLimitExec` wrapper added
- `test_sort_pushdown_exact_preserves_fetch_with_source_support` — Exact
elimination with fetch, source supports `with_fetch()`: limit pushed
into source directly
## Are there any user-facing changes?
No.1 parent aab4263 commit 6f1040b
3 files changed
Lines changed: 185 additions & 22 deletions
File tree
- datafusion
- core/tests/physical_optimizer
- physical-optimizer/src
Lines changed: 92 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
996 | 996 | | |
997 | 997 | | |
998 | 998 | | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
830 | 830 | | |
831 | 831 | | |
832 | 832 | | |
833 | | - | |
834 | | - | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
835 | 844 | | |
836 | 845 | | |
837 | 846 | | |
838 | 847 | | |
839 | 848 | | |
840 | 849 | | |
841 | 850 | | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
842 | 857 | | |
843 | 858 | | |
844 | 859 | | |
| |||
872 | 887 | | |
873 | 888 | | |
874 | 889 | | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
875 | 893 | | |
876 | 894 | | |
877 | 895 | | |
878 | 896 | | |
879 | 897 | | |
880 | 898 | | |
881 | 899 | | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
882 | 912 | | |
883 | 913 | | |
884 | 914 | | |
885 | 915 | | |
886 | 916 | | |
887 | 917 | | |
888 | 918 | | |
| 919 | + | |
889 | 920 | | |
890 | | - | |
891 | | - | |
| 921 | + | |
892 | 922 | | |
893 | 923 | | |
894 | 924 | | |
895 | 925 | | |
896 | 926 | | |
897 | 927 | | |
898 | 928 | | |
| 929 | + | |
899 | 930 | | |
900 | | - | |
901 | 931 | | |
902 | | - | |
| 932 | + | |
903 | 933 | | |
904 | 934 | | |
905 | 935 | | |
906 | 936 | | |
907 | 937 | | |
908 | 938 | | |
909 | 939 | | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
910 | 944 | | |
911 | 945 | | |
912 | 946 | | |
| |||
953 | 987 | | |
954 | 988 | | |
955 | 989 | | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
956 | 1004 | | |
957 | 1005 | | |
958 | 1006 | | |
| |||
965 | 1013 | | |
966 | 1014 | | |
967 | 1015 | | |
968 | | - | |
969 | | - | |
970 | | - | |
971 | | - | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
972 | 1037 | | |
973 | 1038 | | |
974 | 1039 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| 108 | + | |
| 109 | + | |
107 | 110 | | |
108 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
109 | 114 | | |
110 | 115 | | |
111 | 116 | | |
| |||
149 | 154 | | |
150 | 155 | | |
151 | 156 | | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
156 | 167 | | |
157 | | - | |
158 | | - | |
159 | | - | |
| 168 | + | |
| 169 | + | |
160 | 170 | | |
161 | 171 | | |
162 | 172 | | |
| |||
0 commit comments