Skip to content

Conversation

@Dandandan
Copy link
Contributor

@Dandandan Dandandan commented Nov 11, 2025

0## Which issue does this PR close?

Rationale for this change

Cleans up the plan by removing CoalesceBatchesExec. I do not expect any performance improvement.

Making plans better to read and avoid useless CoalesceBatchesExec

Also adds back fetch support by adding it to FilterExec

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@Dandandan Dandandan marked this pull request as draft November 11, 2025 18:40
@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Nov 11, 2025
@github-actions github-actions bot added the core Core DataFusion crate label Nov 11, 2025
@Dandandan Dandandan marked this pull request as ready for review November 11, 2025 20:09
@alamb
Copy link
Contributor

alamb commented Nov 11, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.14.0-1018-gcp #19~24.04.1-Ubuntu SMP Wed Sep 24 23:23:09 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing remove_coalesce_batches_exec (44e760d) to 65bd13d diff using: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor

alamb commented Nov 11, 2025

🤖: Benchmark completed

Details

Comparing HEAD and remove_coalesce_batches_exec
--------------------
Benchmark clickbench_extended.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ remove_coalesce_batches_exec ┃    Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ QQuery 0     │  2670.46 ms │                   2657.23 ms │ no change │
│ QQuery 1     │  1243.06 ms │                   1204.17 ms │ no change │
│ QQuery 2     │  2381.81 ms │                   2323.41 ms │ no change │
│ QQuery 3     │  1145.79 ms │                   1189.36 ms │ no change │
│ QQuery 4     │  2299.90 ms │                   2326.13 ms │ no change │
│ QQuery 5     │ 28212.09 ms │                  28505.68 ms │ no change │
│ QQuery 6     │  4084.83 ms │                   4093.30 ms │ no change │
│ QQuery 7     │  3918.20 ms │                   3727.52 ms │ no change │
└──────────────┴─────────────┴──────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 45956.14ms │
│ Total Time (remove_coalesce_batches_exec)   │ 46026.80ms │
│ Average Time (HEAD)                         │  5744.52ms │
│ Average Time (remove_coalesce_batches_exec) │  5753.35ms │
│ Queries Faster                              │          0 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │          8 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ remove_coalesce_batches_exec ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │     2.48 ms │                      2.30 ms │ +1.07x faster │
│ QQuery 1     │    48.39 ms │                     49.15 ms │     no change │
│ QQuery 2     │   135.72 ms │                    135.85 ms │     no change │
│ QQuery 3     │   165.47 ms │                    161.61 ms │     no change │
│ QQuery 4     │  1086.85 ms │                   1119.89 ms │     no change │
│ QQuery 5     │  1506.33 ms │                   1487.53 ms │     no change │
│ QQuery 6     │     2.16 ms │                      2.21 ms │     no change │
│ QQuery 7     │    54.58 ms │                     54.09 ms │     no change │
│ QQuery 8     │  1475.66 ms │                   1475.06 ms │     no change │
│ QQuery 9     │  1881.42 ms │                   1867.77 ms │     no change │
│ QQuery 10    │   375.25 ms │                    373.88 ms │     no change │
│ QQuery 11    │   429.07 ms │                    427.62 ms │     no change │
│ QQuery 12    │  1392.53 ms │                   1388.39 ms │     no change │
│ QQuery 13    │  2156.21 ms │                   2180.46 ms │     no change │
│ QQuery 14    │  1269.83 ms │                   1296.29 ms │     no change │
│ QQuery 15    │  1251.27 ms │                   1258.87 ms │     no change │
│ QQuery 16    │  2716.46 ms │                   2710.00 ms │     no change │
│ QQuery 17    │  2709.59 ms │                   2700.38 ms │     no change │
│ QQuery 18    │  5375.51 ms │                   5071.53 ms │ +1.06x faster │
│ QQuery 19    │   128.07 ms │                    126.90 ms │     no change │
│ QQuery 20    │  2070.97 ms │                   2018.10 ms │     no change │
│ QQuery 21    │  2326.97 ms │                   2317.70 ms │     no change │
│ QQuery 22    │  4057.61 ms │                   3962.54 ms │     no change │
│ QQuery 23    │ 13805.97 ms │                  12818.80 ms │ +1.08x faster │
│ QQuery 24    │   221.72 ms │                    218.87 ms │     no change │
│ QQuery 25    │   478.06 ms │                    464.19 ms │     no change │
│ QQuery 26    │   214.85 ms │                    223.12 ms │     no change │
│ QQuery 27    │  2938.93 ms │                   2808.73 ms │     no change │
│ QQuery 28    │ 24088.76 ms │                  23292.17 ms │     no change │
│ QQuery 29    │   979.98 ms │                    995.75 ms │     no change │
│ QQuery 30    │  1362.53 ms │                   1381.05 ms │     no change │
│ QQuery 31    │  1421.59 ms │                   1410.02 ms │     no change │
│ QQuery 32    │  4854.31 ms │                   5124.55 ms │  1.06x slower │
│ QQuery 33    │  6018.23 ms │                   5940.22 ms │     no change │
│ QQuery 34    │  6248.02 ms │                   6329.03 ms │     no change │
│ QQuery 35    │  2046.65 ms │                   2038.98 ms │     no change │
│ QQuery 36    │   120.12 ms │                    123.98 ms │     no change │
│ QQuery 37    │    50.82 ms │                     52.41 ms │     no change │
│ QQuery 38    │   122.36 ms │                    119.81 ms │     no change │
│ QQuery 39    │   199.94 ms │                    197.47 ms │     no change │
│ QQuery 40    │    43.31 ms │                     41.78 ms │     no change │
│ QQuery 41    │    39.33 ms │                     38.60 ms │     no change │
│ QQuery 42    │    32.63 ms │                     32.59 ms │     no change │
└──────────────┴─────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 97906.49ms │
│ Total Time (remove_coalesce_batches_exec)   │ 95840.28ms │
│ Average Time (HEAD)                         │  2276.90ms │
│ Average Time (remove_coalesce_batches_exec) │  2228.84ms │
│ Queries Faster                              │          3 │
│ Queries Slower                              │          1 │
│ Queries with No Change                      │         39 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark tpch_mem_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃      HEAD ┃ remove_coalesce_batches_exec ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 135.38 ms │                    137.85 ms │     no change │
│ QQuery 2     │  28.16 ms │                     29.11 ms │     no change │
│ QQuery 3     │  38.56 ms │                     39.32 ms │     no change │
│ QQuery 4     │  29.19 ms │                     29.46 ms │     no change │
│ QQuery 5     │  86.81 ms │                     90.77 ms │     no change │
│ QQuery 6     │  20.07 ms │                     19.93 ms │     no change │
│ QQuery 7     │ 228.98 ms │                    227.64 ms │     no change │
│ QQuery 8     │  33.05 ms │                     30.96 ms │ +1.07x faster │
│ QQuery 9     │ 108.12 ms │                    105.94 ms │     no change │
│ QQuery 10    │  64.79 ms │                     64.03 ms │     no change │
│ QQuery 11    │  17.38 ms │                     17.72 ms │     no change │
│ QQuery 12    │  51.45 ms │                     51.79 ms │     no change │
│ QQuery 13    │  46.24 ms │                     47.67 ms │     no change │
│ QQuery 14    │  13.65 ms │                     14.12 ms │     no change │
│ QQuery 15    │  25.05 ms │                     24.88 ms │     no change │
│ QQuery 16    │  25.12 ms │                     25.46 ms │     no change │
│ QQuery 17    │ 149.77 ms │                    151.86 ms │     no change │
│ QQuery 18    │ 277.54 ms │                    285.03 ms │     no change │
│ QQuery 19    │  38.29 ms │                     37.44 ms │     no change │
│ QQuery 20    │  50.32 ms │                     50.53 ms │     no change │
│ QQuery 21    │ 326.69 ms │                    322.22 ms │     no change │
│ QQuery 22    │  20.46 ms │                     21.00 ms │     no change │
└──────────────┴───────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 1815.09ms │
│ Total Time (remove_coalesce_batches_exec)   │ 1824.70ms │
│ Average Time (HEAD)                         │   82.50ms │
│ Average Time (remove_coalesce_batches_exec) │   82.94ms │
│ Queries Faster                              │         1 │
│ Queries Slower                              │         0 │
│ Queries with No Change                      │        21 │
│ Queries with Failure                        │         0 │
└─────────────────────────────────────────────┴───────────┘

@alamb
Copy link
Contributor

alamb commented Nov 12, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.14.0-1018-gcp #19~24.04.1-Ubuntu SMP Wed Sep 24 23:23:09 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing remove_coalesce_batches_exec (44e760d) to 65bd13d diff using: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor

alamb commented Nov 12, 2025

🤖: Benchmark completed

Details

Comparing HEAD and remove_coalesce_batches_exec
--------------------
Benchmark clickbench_extended.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ remove_coalesce_batches_exec ┃    Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ QQuery 0     │  2675.68 ms │                   2614.60 ms │ no change │
│ QQuery 1     │  1234.03 ms │                   1244.32 ms │ no change │
│ QQuery 2     │  2312.70 ms │                   2383.14 ms │ no change │
│ QQuery 3     │  1172.72 ms │                   1186.10 ms │ no change │
│ QQuery 4     │  2360.06 ms │                   2323.54 ms │ no change │
│ QQuery 5     │ 28322.13 ms │                  28219.12 ms │ no change │
│ QQuery 6     │  4088.67 ms │                   4069.83 ms │ no change │
│ QQuery 7     │  3776.14 ms │                   3649.89 ms │ no change │
└──────────────┴─────────────┴──────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 45942.12ms │
│ Total Time (remove_coalesce_batches_exec)   │ 45690.52ms │
│ Average Time (HEAD)                         │  5742.77ms │
│ Average Time (remove_coalesce_batches_exec) │  5711.32ms │
│ Queries Faster                              │          0 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │          8 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ remove_coalesce_batches_exec ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │     2.05 ms │                      2.19 ms │  1.06x slower │
│ QQuery 1     │    48.75 ms │                     49.77 ms │     no change │
│ QQuery 2     │   135.04 ms │                    137.08 ms │     no change │
│ QQuery 3     │   155.33 ms │                    164.84 ms │  1.06x slower │
│ QQuery 4     │  1004.65 ms │                   1085.44 ms │  1.08x slower │
│ QQuery 5     │  1477.51 ms │                   1505.85 ms │     no change │
│ QQuery 6     │     2.15 ms │                      2.18 ms │     no change │
│ QQuery 7     │    53.93 ms │                     53.85 ms │     no change │
│ QQuery 8     │  1385.11 ms │                   1461.98 ms │  1.06x slower │
│ QQuery 9     │  1766.73 ms │                   1871.82 ms │  1.06x slower │
│ QQuery 10    │   366.06 ms │                    376.94 ms │     no change │
│ QQuery 11    │   416.04 ms │                    429.99 ms │     no change │
│ QQuery 12    │  1302.53 ms │                   1348.98 ms │     no change │
│ QQuery 13    │  2099.13 ms │                   2139.58 ms │     no change │
│ QQuery 14    │  1251.61 ms │                   1284.96 ms │     no change │
│ QQuery 15    │  1191.87 ms │                   1260.65 ms │  1.06x slower │
│ QQuery 16    │  2629.93 ms │                   2717.71 ms │     no change │
│ QQuery 17    │  2629.61 ms │                   2687.75 ms │     no change │
│ QQuery 18    │  5321.32 ms │                   5106.49 ms │     no change │
│ QQuery 19    │   126.48 ms │                    128.87 ms │     no change │
│ QQuery 20    │  2034.30 ms │                   2034.67 ms │     no change │
│ QQuery 21    │  2300.72 ms │                   2326.82 ms │     no change │
│ QQuery 22    │  3980.04 ms │                   3953.86 ms │     no change │
│ QQuery 23    │ 17017.84 ms │                  12824.44 ms │ +1.33x faster │
│ QQuery 24    │   205.83 ms │                    207.69 ms │     no change │
│ QQuery 25    │   468.70 ms │                    481.03 ms │     no change │
│ QQuery 26    │   210.59 ms │                    221.57 ms │  1.05x slower │
│ QQuery 27    │  2823.73 ms │                   2794.70 ms │     no change │
│ QQuery 28    │ 23453.34 ms │                  23466.55 ms │     no change │
│ QQuery 29    │   967.18 ms │                    954.60 ms │     no change │
│ QQuery 30    │  1326.19 ms │                   1345.24 ms │     no change │
│ QQuery 31    │  1375.54 ms │                   1395.71 ms │     no change │
│ QQuery 32    │  4861.42 ms │                   5216.29 ms │  1.07x slower │
│ QQuery 33    │  6022.24 ms │                   6045.05 ms │     no change │
│ QQuery 34    │  6059.00 ms │                   6105.78 ms │     no change │
│ QQuery 35    │  1982.67 ms │                   2024.41 ms │     no change │
│ QQuery 36    │   122.56 ms │                    120.62 ms │     no change │
│ QQuery 37    │    51.02 ms │                     51.50 ms │     no change │
│ QQuery 38    │   121.72 ms │                    120.57 ms │     no change │
│ QQuery 39    │   199.74 ms │                    199.69 ms │     no change │
│ QQuery 40    │    41.73 ms │                     45.17 ms │  1.08x slower │
│ QQuery 41    │    39.58 ms │                     39.92 ms │     no change │
│ QQuery 42    │    34.09 ms │                     31.81 ms │ +1.07x faster │
└──────────────┴─────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 99065.61ms │
│ Total Time (remove_coalesce_batches_exec)   │ 95824.61ms │
│ Average Time (HEAD)                         │  2303.85ms │
│ Average Time (remove_coalesce_batches_exec) │  2228.48ms │
│ Queries Faster                              │          2 │
│ Queries Slower                              │          9 │
│ Queries with No Change                      │         32 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark tpch_mem_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃      HEAD ┃ remove_coalesce_batches_exec ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 138.67 ms │                    137.78 ms │     no change │
│ QQuery 2     │  29.16 ms │                     27.73 ms │     no change │
│ QQuery 3     │  39.64 ms │                     33.99 ms │ +1.17x faster │
│ QQuery 4     │  29.61 ms │                     29.72 ms │     no change │
│ QQuery 5     │  88.22 ms │                     87.65 ms │     no change │
│ QQuery 6     │  19.68 ms │                     19.56 ms │     no change │
│ QQuery 7     │ 229.95 ms │                    223.66 ms │     no change │
│ QQuery 8     │  35.25 ms │                     32.23 ms │ +1.09x faster │
│ QQuery 9     │  98.51 ms │                    110.97 ms │  1.13x slower │
│ QQuery 10    │  64.44 ms │                     65.40 ms │     no change │
│ QQuery 11    │  17.98 ms │                     17.36 ms │     no change │
│ QQuery 12    │  51.23 ms │                     52.44 ms │     no change │
│ QQuery 13    │  47.52 ms │                     46.10 ms │     no change │
│ QQuery 14    │  14.28 ms │                     13.93 ms │     no change │
│ QQuery 15    │  25.11 ms │                     25.19 ms │     no change │
│ QQuery 16    │  25.79 ms │                     25.32 ms │     no change │
│ QQuery 17    │ 148.79 ms │                    147.69 ms │     no change │
│ QQuery 18    │ 280.70 ms │                    277.03 ms │     no change │
│ QQuery 19    │  50.80 ms │                     38.85 ms │ +1.31x faster │
│ QQuery 20    │  61.22 ms │                     49.21 ms │ +1.24x faster │
│ QQuery 21    │ 326.73 ms │                    326.79 ms │     no change │
│ QQuery 22    │  21.33 ms │                     20.40 ms │     no change │
└──────────────┴───────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 1844.60ms │
│ Total Time (remove_coalesce_batches_exec)   │ 1808.99ms │
│ Average Time (HEAD)                         │   83.85ms │
│ Average Time (remove_coalesce_batches_exec) │   82.23ms │
│ Queries Faster                              │         4 │
│ Queries Slower                              │         1 │
│ Queries with No Change                      │        17 │
│ Queries with Failure                        │         0 │
└─────────────────────────────────────────────┴───────────┘

@Dandandan
Copy link
Contributor Author

Looks like performance (as expected) is roughly the same.

@github-actions github-actions bot added the physical-plan Changes to the physical-plan crate label Nov 12, 2025
@Dandandan Dandandan changed the title Remove FilterExec from CoalesceBatches optimization rule Remove FilterExec from CoalesceBatches optimization rule, add fetch support Nov 13, 2025
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Dandandan -- these plans are so much nicer to read

Image

I feel there may be some follow ons. I can file some tickets if you agree

  1. Somehow standardize on LimitedBatchCoalescer
  2. Support LimitedBatchCoalescer::push_batch_with_filter
  3. Apply this same pattern to HashJoinExec and RepartitionExec and then remove CoalesceBatches entirely 🤔


self.batch_coalescer.push_batch_with_filter(batch.clone(), filter_array)?;
Ok(())
// TODO: support push_batch_with_filter in LimitedBatchCoalescer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good todo -- it would be good to file as a follow on ticket / PR perhaps

I can do it if you like

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that would be nice

// The goal here is to detect operators that could produce small batches and only
// wrap those ones with a CoalesceBatchesExec operator. An alternate approach here
// would be to build the coalescing logic directly into the operators
// See https://github.com/apache/datafusion/issues/139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment could probably be removed now (the ticket is long since closed)

// See https://github.com/apache/datafusion/issues/139
let wrap_in_coalesce = plan_any.downcast_ref::<FilterExec>().is_some()
|| plan_any.downcast_ref::<HashJoinExec>().is_some()
let wrap_in_coalesce = plan_any.downcast_ref::<HashJoinExec>().is_some()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we only have HashJoinExec and RepartitionExec to update before we could remove CoalesceBatches entirely 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I tried to apply this optimization to RepartitionExec but unfortunately got some regressions (my feeling is due to some decreased parallelism / work distribution).

HashJoinExec would be a great candidate as it might be possible to avoid the take followed by concat which might speed up things.

@alamb
Copy link
Contributor

alamb commented Nov 13, 2025

Thank you for the review @geoffreyclaude and @martin-g

@Dandandan Dandandan added this pull request to the merge queue Nov 14, 2025
Merged via the queue into apache:main with commit f17cc09 Nov 14, 2025
32 checks passed
jizezhang pushed a commit to jizezhang/datafusion that referenced this pull request Nov 15, 2025
…upport (apache#18630)

0## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18646

## Rationale for this change

Cleans up the plan by removing `CoalesceBatchesExec`. I do not expect
any performance improvement.

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

Making plans better to read and avoid useless `CoalesceBatchesExec`

Also adds back fetch support by adding it to FilterExec

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## 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)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate optimizer Optimizer rules physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove FilterExec from CoalesceBatches optimization rule

4 participants