Commit e913557
authored
Fix: MemTable LIMIT ignored with reordered projections (#21177)
## Which issue does this PR close?
- Closes #21176
## Rationale for this change
When `SELECT` projects columns in a different order than the table
schema (e.g., `SELECT col_c, col_b` vs schema order `col_a, col_b,
col_c`), the `LIMIT` clause is silently ignored — all rows are returned
instead of the requested limit.
The physical optimizer runs `LimitPushdown` (which correctly sets
`fetch=N` on `MemorySourceConfig`) **before** `ProjectionPushdown`. When
the projection is pushed down, `try_swapping_with_projection()` creates
a new `MemorySourceConfig` via `try_new()`, which resets `fetch` to
`None`, silently dropping the limit.
## What changes are included in this PR?
One-line fix in `MemorySourceConfig::try_swapping_with_projection` to
preserve `self.fetch` on the newly created config:
```rust
// Before:
.map(|s| Arc::new(s) as Arc<dyn DataSource>)
// After:
.map(|s| Arc::new(s.with_limit(self.fetch)) as Arc<dyn DataSource>)
```
## Are these changes tested?
Yes.
- **Unit test** (`try_swapping_with_projection_preserves_fetch`):
directly verifies `fetch` is preserved after projection pushdown.
Confirmed it fails without the fix (`None` vs `Some(5)`).
- **SQL logic test** (`limit.slt`, table `t21176`): end-to-end
regression test for `SELECT col_c, col_b FROM t LIMIT 5` with reverse
column order.
## Are there any user-facing changes?
No API changes. This is a correctness fix — `LIMIT` now works correctly
regardless of `SELECT` column order against in-memory tables.1 parent 37c1b75 commit e913557
2 files changed
+95
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
253 | 262 | | |
254 | 263 | | |
255 | 264 | | |
| |||
897 | 906 | | |
898 | 907 | | |
899 | 908 | | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
900 | 945 | | |
901 | 946 | | |
902 | 947 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
927 | 927 | | |
928 | 928 | | |
929 | 929 | | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
0 commit comments