Skip to content

fix(topk): avoid overflow panic in interleave emission for Utf8#20494

Open
aviralgarg05 wants to merge 7 commits intoapache:mainfrom
aviralgarg05:fix/topk-interleave-overflow
Open

fix(topk): avoid overflow panic in interleave emission for Utf8#20494
aviralgarg05 wants to merge 7 commits intoapache:mainfrom
aviralgarg05:fix/topk-interleave-overflow

Conversation

@aviralgarg05
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

While testing, I found that ORDER BY ... LIMIT could panic in TopK when interleaving very large Utf8 values. The problem was that TopK tried to interleave all selected rows in a single call, which can overflow Arrow’s i32 string offsets in extreme cases. This PR removes that panic path so the operator behaves safely under large-string workloads.

What changes are included in this PR?

  • I changed TopK emission to interleave in bounded chunks instead of one unbounded interleave.
  • I added panic-safe handling around Arrow interleave so overflow panics are handled and surfaced as normal DataFusion errors.
  • I added adaptive chunk splitting: if a chunk overflows, I retry with a smaller chunk until it succeeds (or return an error for the single-row edge case).
  • I updated TopK compaction to use the same chunked path and keep row-to-batch remapping correct.
  • I removed the now-unused TopK.batch_size field.
  • I added a focused unit test: test_topk_heap_emit_with_state_respects_batch_size.

Are these changes tested?

Yes.

  • I added a new unit test in datafusion/physical-plan/src/topk/mod.rs for chunked TopK emission behavior.
  • I ran cargo fmt --all --check.
  • I ran cargo clippy -p datafusion-physical-plan --lib --tests -- -D warnings.
  • I ran cargo test -p datafusion-physical-plan topk::tests:: -- --nocapture three times after final changes.

Are there any user-facing changes?

Yes. For extreme large-string cases in ORDER BY ... LIMIT, TopK no longer panics with overflow; it now follows normal error/execution handling.

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Feb 23, 2026
Copy link
Copy Markdown
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

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

@aviralgarg05
Thanks for working on this

Comment thread datafusion/physical-plan/src/topk/mod.rs Outdated
Comment thread datafusion/physical-plan/src/topk/mod.rs
Comment thread datafusion/physical-plan/src/topk/mod.rs
Copy link
Copy Markdown
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

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

lgtm

@xudong963
Copy link
Copy Markdown
Member

FYI, I encountered a similar case in sort merge: #20922, and there is a PR changing panic to error for interleavehttps://github.com/apache/arrow-rs/pull/9549

Comment thread datafusion/physical-plan/src/topk/mod.rs
Comment thread datafusion/physical-plan/src/topk/mod.rs
Comment thread datafusion/physical-plan/src/topk/mod.rs Outdated
@alamb
Copy link
Copy Markdown
Contributor

alamb commented Mar 23, 2026

run benchmarks

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Linux bench-c4114009474-500-khghl 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux
Comparing fix/topk-interleave-overflow (78d69df) to 878b879 (merge-base) diff using: clickbench_partitioned
Results will be posted here when complete

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Linux bench-c4114009474-501-7fvkh 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux
Comparing fix/topk-interleave-overflow (78d69df) to 878b879 (merge-base) diff using: tpcds
Results will be posted here when complete

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Linux bench-c4114009474-502-tzrxl 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux
Comparing fix/topk-interleave-overflow (78d69df) to 878b879 (merge-base) diff using: tpch
Results will be posted here when complete

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Details

Comparing HEAD and fix_topk-interleave-overflow
--------------------
Benchmark tpch_sf1.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query     ┃                           HEAD ┃   fix_topk-interleave-overflow ┃    Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ QQuery 1  │ 45.41 / 45.94 ±0.75 / 47.40 ms │ 45.68 / 46.44 ±0.67 / 47.67 ms │ no change │
│ QQuery 2  │ 21.18 / 21.23 ±0.06 / 21.33 ms │ 21.15 / 21.55 ±0.35 / 22.18 ms │ no change │
│ QQuery 3  │ 31.72 / 31.87 ±0.15 / 32.07 ms │ 31.84 / 32.22 ±0.50 / 33.20 ms │ no change │
│ QQuery 4  │ 20.19 / 20.97 ±0.75 / 22.31 ms │ 20.25 / 21.85 ±1.12 / 23.55 ms │ no change │
│ QQuery 5  │ 48.40 / 50.81 ±2.96 / 56.31 ms │ 48.88 / 50.49 ±1.64 / 52.61 ms │ no change │
│ QQuery 6  │ 17.13 / 17.49 ±0.33 / 17.97 ms │ 17.17 / 18.30 ±1.17 / 19.79 ms │ no change │
│ QQuery 7  │ 54.87 / 55.95 ±0.83 / 57.29 ms │ 54.83 / 56.10 ±1.19 / 58.34 ms │ no change │
│ QQuery 8  │ 48.36 / 48.88 ±0.45 / 49.40 ms │ 49.03 / 49.72 ±0.59 / 50.71 ms │ no change │
│ QQuery 9  │ 53.80 / 55.84 ±2.27 / 60.15 ms │ 55.29 / 55.52 ±0.21 / 55.84 ms │ no change │
│ QQuery 10 │ 71.71 / 72.35 ±0.47 / 73.09 ms │ 71.31 / 72.98 ±1.42 / 75.45 ms │ no change │
│ QQuery 11 │ 14.29 / 14.84 ±0.82 / 16.46 ms │ 14.31 / 14.62 ±0.25 / 15.04 ms │ no change │
│ QQuery 12 │ 28.31 / 28.58 ±0.26 / 29.00 ms │ 28.17 / 28.70 ±0.30 / 29.02 ms │ no change │
│ QQuery 13 │ 38.34 / 39.14 ±0.71 / 40.45 ms │ 39.27 / 40.04 ±0.72 / 41.14 ms │ no change │
│ QQuery 14 │ 28.76 / 29.05 ±0.28 / 29.53 ms │ 28.96 / 29.31 ±0.28 / 29.73 ms │ no change │
│ QQuery 15 │ 33.67 / 33.91 ±0.16 / 34.10 ms │ 34.10 / 34.36 ±0.18 / 34.62 ms │ no change │
│ QQuery 16 │ 16.02 / 16.47 ±0.48 / 17.06 ms │ 16.18 / 16.46 ±0.19 / 16.76 ms │ no change │
│ QQuery 17 │ 73.90 / 75.02 ±1.16 / 77.14 ms │ 74.26 / 75.49 ±1.23 / 77.76 ms │ no change │
│ QQuery 18 │ 77.80 / 78.95 ±1.29 / 81.40 ms │ 77.61 / 80.13 ±1.42 / 81.58 ms │ no change │
│ QQuery 19 │ 37.54 / 37.97 ±0.47 / 38.83 ms │ 38.39 / 38.93 ±0.46 / 39.74 ms │ no change │
│ QQuery 20 │ 39.76 / 40.91 ±0.78 / 41.84 ms │ 41.37 / 42.02 ±0.57 / 43.06 ms │ no change │
│ QQuery 21 │ 64.53 / 66.23 ±1.33 / 67.52 ms │ 65.10 / 67.70 ±2.16 / 70.54 ms │ no change │
│ QQuery 22 │ 18.03 / 18.31 ±0.23 / 18.66 ms │ 18.32 / 18.73 ±0.42 / 19.43 ms │ no change │
└───────────┴────────────────────────────────┴────────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Benchmark Summary                           ┃          ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 900.72ms │
│ Total Time (fix_topk-interleave-overflow)   │ 911.66ms │
│ Average Time (HEAD)                         │  40.94ms │
│ Average Time (fix_topk-interleave-overflow) │  41.44ms │
│ Queries Faster                              │        0 │
│ Queries Slower                              │        0 │
│ Queries with No Change                      │       22 │
│ Queries with Failure                        │        0 │
└─────────────────────────────────────────────┴──────────┘

Resource Usage

tpch — base (merge-base)

Metric Value
Wall time 4.8s
Peak memory 4.2 GiB
Avg memory 3.7 GiB
CPU user 33.4s
CPU sys 3.1s
Disk read 0 B
Disk write 136.0 KiB

tpch — branch

Metric Value
Wall time 4.8s
Peak memory 4.2 GiB
Avg memory 3.7 GiB
CPU user 33.9s
CPU sys 3.0s
Disk read 0 B
Disk write 56.0 KiB

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Details

Comparing HEAD and fix_topk-interleave-overflow
--------------------
Benchmark tpcds_sf1.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Query     ┃                                     HEAD ┃             fix_topk-interleave-overflow ┃       Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
│ QQuery 1  │           43.31 / 44.09 ±0.83 / 45.54 ms │           42.97 / 43.85 ±0.85 / 45.39 ms │    no change │
│ QQuery 2  │        145.36 / 145.84 ±0.46 / 146.65 ms │        146.31 / 147.60 ±1.06 / 149.17 ms │    no change │
│ QQuery 3  │        114.33 / 115.42 ±0.83 / 116.65 ms │        115.23 / 115.92 ±0.57 / 116.65 ms │    no change │
│ QQuery 4  │    1311.03 / 1341.99 ±23.59 / 1373.31 ms │    1343.48 / 1373.93 ±23.51 / 1407.43 ms │    no change │
│ QQuery 5  │        173.31 / 175.66 ±1.35 / 177.06 ms │        174.31 / 175.57 ±1.14 / 177.39 ms │    no change │
│ QQuery 6  │     990.44 / 1027.11 ±42.21 / 1095.32 ms │    1023.74 / 1048.68 ±17.71 / 1072.65 ms │    no change │
│ QQuery 7  │        352.43 / 355.22 ±3.33 / 361.57 ms │        357.07 / 361.49 ±3.45 / 367.03 ms │    no change │
│ QQuery 8  │        116.48 / 117.25 ±0.54 / 117.83 ms │        114.74 / 116.91 ±1.40 / 118.86 ms │    no change │
│ QQuery 9  │        101.16 / 103.23 ±2.10 / 105.84 ms │        102.56 / 108.65 ±4.58 / 116.76 ms │ 1.05x slower │
│ QQuery 10 │        107.96 / 108.42 ±0.39 / 109.06 ms │        108.00 / 109.87 ±0.98 / 110.86 ms │    no change │
│ QQuery 11 │        943.64 / 955.88 ±8.65 / 965.09 ms │       939.01 / 953.81 ±10.12 / 967.30 ms │    no change │
│ QQuery 12 │           44.78 / 46.62 ±1.32 / 48.16 ms │           45.38 / 46.52 ±0.71 / 47.18 ms │    no change │
│ QQuery 13 │        395.62 / 402.25 ±5.07 / 410.74 ms │        403.40 / 406.20 ±2.52 / 409.83 ms │    no change │
│ QQuery 14 │     1012.14 / 1023.81 ±8.44 / 1036.33 ms │    1005.70 / 1022.91 ±13.44 / 1046.97 ms │    no change │
│ QQuery 15 │           16.32 / 16.87 ±0.65 / 17.99 ms │           16.93 / 17.58 ±0.41 / 18.03 ms │    no change │
│ QQuery 16 │           40.04 / 41.52 ±1.39 / 43.92 ms │           41.82 / 42.44 ±0.70 / 43.77 ms │    no change │
│ QQuery 17 │        238.72 / 243.02 ±4.00 / 249.38 ms │        241.72 / 243.35 ±0.84 / 244.00 ms │    no change │
│ QQuery 18 │        129.50 / 130.79 ±0.72 / 131.66 ms │        129.71 / 131.83 ±1.61 / 133.54 ms │    no change │
│ QQuery 19 │        155.68 / 156.50 ±0.49 / 157.19 ms │        156.49 / 157.44 ±0.80 / 158.77 ms │    no change │
│ QQuery 20 │           13.76 / 14.31 ±0.37 / 14.83 ms │           13.52 / 14.45 ±0.68 / 15.49 ms │    no change │
│ QQuery 21 │           19.30 / 20.16 ±0.50 / 20.69 ms │           19.66 / 20.53 ±0.56 / 21.19 ms │    no change │
│ QQuery 22 │        486.50 / 489.84 ±2.41 / 494.04 ms │        485.24 / 493.67 ±7.73 / 504.32 ms │    no change │
│ QQuery 23 │        912.09 / 916.96 ±3.36 / 921.34 ms │       909.98 / 926.21 ±13.62 / 949.47 ms │    no change │
│ QQuery 24 │        415.20 / 417.50 ±1.79 / 420.43 ms │        418.89 / 425.22 ±6.26 / 433.27 ms │    no change │
│ QQuery 25 │        355.28 / 358.11 ±2.14 / 361.88 ms │        358.89 / 360.65 ±1.43 / 362.77 ms │    no change │
│ QQuery 26 │           82.53 / 84.36 ±1.30 / 85.91 ms │           83.29 / 86.16 ±2.34 / 89.25 ms │    no change │
│ QQuery 27 │        348.30 / 349.83 ±1.23 / 351.13 ms │        351.26 / 354.02 ±2.06 / 356.57 ms │    no change │
│ QQuery 28 │        149.48 / 151.78 ±1.60 / 153.92 ms │        149.11 / 150.27 ±1.13 / 152.21 ms │    no change │
│ QQuery 29 │        299.17 / 302.96 ±3.42 / 307.70 ms │        303.80 / 306.32 ±2.39 / 310.61 ms │    no change │
│ QQuery 30 │           43.73 / 44.98 ±1.01 / 46.50 ms │           43.72 / 44.89 ±1.36 / 47.55 ms │    no change │
│ QQuery 31 │        170.53 / 171.65 ±1.11 / 173.74 ms │        170.45 / 171.29 ±0.82 / 172.63 ms │    no change │
│ QQuery 32 │          57.36 / 65.93 ±16.19 / 98.29 ms │          58.23 / 66.77 ±16.19 / 99.15 ms │    no change │
│ QQuery 33 │        140.05 / 141.62 ±1.22 / 143.42 ms │        141.67 / 143.14 ±0.86 / 144.22 ms │    no change │
│ QQuery 34 │        105.03 / 106.43 ±0.98 / 107.92 ms │        106.80 / 107.53 ±0.77 / 108.70 ms │    no change │
│ QQuery 35 │        108.54 / 109.87 ±0.70 / 110.50 ms │        106.58 / 109.50 ±1.52 / 110.97 ms │    no change │
│ QQuery 36 │        216.17 / 221.86 ±3.07 / 224.96 ms │        216.66 / 224.24 ±5.57 / 232.13 ms │    no change │
│ QQuery 37 │        178.62 / 181.87 ±2.66 / 185.02 ms │        179.65 / 181.79 ±1.74 / 183.90 ms │    no change │
│ QQuery 38 │           86.86 / 89.59 ±2.65 / 93.81 ms │           88.09 / 90.25 ±1.27 / 91.79 ms │    no change │
│ QQuery 39 │        126.50 / 128.99 ±2.38 / 133.49 ms │        124.45 / 128.78 ±3.04 / 133.68 ms │    no change │
│ QQuery 40 │        112.14 / 117.60 ±5.47 / 128.04 ms │        111.90 / 118.93 ±9.10 / 136.63 ms │    no change │
│ QQuery 41 │           14.53 / 15.95 ±0.88 / 16.94 ms │           14.36 / 15.79 ±1.29 / 18.09 ms │    no change │
│ QQuery 42 │        107.11 / 108.21 ±1.32 / 110.79 ms │        108.27 / 110.86 ±1.54 / 112.58 ms │    no change │
│ QQuery 43 │           83.32 / 83.82 ±0.39 / 84.38 ms │           84.71 / 86.12 ±1.05 / 87.62 ms │    no change │
│ QQuery 44 │           11.29 / 11.52 ±0.21 / 11.85 ms │           11.71 / 12.27 ±0.69 / 13.62 ms │ 1.06x slower │
│ QQuery 45 │           53.60 / 55.27 ±1.46 / 57.91 ms │           53.50 / 54.82 ±1.74 / 58.24 ms │    no change │
│ QQuery 46 │        230.78 / 233.90 ±2.37 / 237.99 ms │        233.08 / 237.91 ±3.04 / 241.32 ms │    no change │
│ QQuery 47 │       694.47 / 707.71 ±11.56 / 725.27 ms │        718.40 / 727.53 ±4.75 / 730.99 ms │    no change │
│ QQuery 48 │        289.81 / 295.24 ±4.71 / 302.38 ms │        287.83 / 298.02 ±5.15 / 301.53 ms │    no change │
│ QQuery 49 │        253.33 / 255.88 ±2.07 / 259.41 ms │        257.38 / 259.24 ±2.09 / 262.68 ms │    no change │
│ QQuery 50 │        226.26 / 232.90 ±4.75 / 238.08 ms │        229.18 / 236.35 ±5.07 / 244.64 ms │    no change │
│ QQuery 51 │        182.96 / 185.81 ±1.99 / 187.94 ms │        179.68 / 187.26 ±3.87 / 190.62 ms │    no change │
│ QQuery 52 │        108.38 / 109.24 ±0.82 / 110.21 ms │        108.49 / 110.94 ±1.33 / 112.49 ms │    no change │
│ QQuery 53 │        103.15 / 104.73 ±1.76 / 108.05 ms │        104.18 / 105.16 ±0.63 / 106.07 ms │    no change │
│ QQuery 54 │        148.11 / 149.57 ±1.14 / 151.42 ms │        148.74 / 150.06 ±1.53 / 152.86 ms │    no change │
│ QQuery 55 │        106.52 / 108.57 ±1.31 / 110.49 ms │        108.84 / 109.83 ±0.60 / 110.65 ms │    no change │
│ QQuery 56 │        141.54 / 142.47 ±0.79 / 143.85 ms │        141.35 / 142.85 ±0.86 / 143.91 ms │    no change │
│ QQuery 57 │        168.99 / 174.19 ±2.73 / 176.84 ms │        174.16 / 177.26 ±1.95 / 179.71 ms │    no change │
│ QQuery 58 │        289.28 / 298.02 ±4.87 / 302.67 ms │        299.98 / 306.57 ±5.51 / 315.09 ms │    no change │
│ QQuery 59 │        197.11 / 200.28 ±2.83 / 204.94 ms │        201.47 / 204.23 ±2.04 / 207.37 ms │    no change │
│ QQuery 60 │        143.72 / 145.24 ±0.96 / 146.68 ms │        143.15 / 146.21 ±1.94 / 148.92 ms │    no change │
│ QQuery 61 │        170.05 / 172.49 ±1.68 / 174.47 ms │        171.28 / 175.48 ±2.55 / 178.30 ms │    no change │
│ QQuery 62 │       912.09 / 954.72 ±34.07 / 990.73 ms │       913.71 / 933.21 ±17.36 / 954.91 ms │    no change │
│ QQuery 63 │        102.45 / 105.09 ±1.50 / 106.65 ms │        106.59 / 107.85 ±1.03 / 109.56 ms │    no change │
│ QQuery 64 │        699.35 / 711.70 ±8.83 / 724.95 ms │        699.31 / 704.54 ±3.99 / 708.52 ms │    no change │
│ QQuery 65 │        253.42 / 255.91 ±1.64 / 258.39 ms │        255.08 / 258.18 ±2.44 / 262.11 ms │    no change │
│ QQuery 66 │        242.27 / 252.12 ±7.10 / 262.16 ms │       239.51 / 254.92 ±12.68 / 277.21 ms │    no change │
│ QQuery 67 │        313.24 / 319.88 ±3.54 / 323.80 ms │        309.67 / 316.17 ±6.25 / 327.46 ms │    no change │
│ QQuery 68 │        280.99 / 284.73 ±3.60 / 290.55 ms │        279.83 / 287.11 ±6.41 / 299.06 ms │    no change │
│ QQuery 69 │        102.96 / 104.41 ±0.92 / 105.76 ms │        104.31 / 105.80 ±1.29 / 108.03 ms │    no change │
│ QQuery 70 │       333.54 / 347.48 ±10.54 / 359.66 ms │        341.84 / 352.18 ±9.18 / 365.39 ms │    no change │
│ QQuery 71 │        135.51 / 139.04 ±3.83 / 145.12 ms │        136.27 / 138.55 ±1.47 / 140.86 ms │    no change │
│ QQuery 72 │       717.61 / 737.34 ±12.83 / 753.46 ms │       715.36 / 726.83 ±10.96 / 744.69 ms │    no change │
│ QQuery 73 │        101.97 / 104.10 ±2.06 / 106.62 ms │        103.81 / 106.18 ±2.04 / 109.09 ms │    no change │
│ QQuery 74 │        576.26 / 594.63 ±9.40 / 603.10 ms │        592.69 / 595.41 ±2.25 / 599.41 ms │    no change │
│ QQuery 75 │        275.36 / 277.47 ±2.82 / 282.97 ms │        282.65 / 284.01 ±1.75 / 287.40 ms │    no change │
│ QQuery 76 │        132.05 / 134.23 ±1.43 / 135.86 ms │        138.09 / 139.73 ±1.41 / 141.53 ms │    no change │
│ QQuery 77 │        188.22 / 189.25 ±0.65 / 190.04 ms │        189.33 / 191.15 ±1.38 / 193.33 ms │    no change │
│ QQuery 78 │        349.13 / 353.46 ±4.39 / 361.20 ms │        352.19 / 360.11 ±5.18 / 365.21 ms │    no change │
│ QQuery 79 │        231.29 / 237.26 ±3.75 / 242.51 ms │        235.95 / 241.08 ±4.78 / 249.60 ms │    no change │
│ QQuery 80 │        330.93 / 332.61 ±1.97 / 336.42 ms │        328.73 / 336.00 ±4.50 / 341.92 ms │    no change │
│ QQuery 81 │           26.42 / 28.19 ±1.29 / 30.00 ms │           27.02 / 27.97 ±0.93 / 29.42 ms │    no change │
│ QQuery 82 │        202.88 / 206.48 ±2.56 / 209.68 ms │        204.42 / 206.50 ±1.76 / 209.64 ms │    no change │
│ QQuery 83 │           39.75 / 40.97 ±1.59 / 44.06 ms │           39.73 / 40.96 ±0.84 / 41.96 ms │    no change │
│ QQuery 84 │           49.52 / 50.07 ±0.58 / 51.11 ms │           48.12 / 49.93 ±1.16 / 51.71 ms │    no change │
│ QQuery 85 │        148.97 / 152.34 ±2.82 / 156.48 ms │        149.37 / 152.02 ±1.83 / 154.88 ms │    no change │
│ QQuery 86 │           39.61 / 41.00 ±1.06 / 42.41 ms │           38.96 / 39.92 ±0.98 / 41.66 ms │    no change │
│ QQuery 87 │           86.43 / 90.84 ±3.45 / 95.09 ms │           87.57 / 90.56 ±3.98 / 98.35 ms │    no change │
│ QQuery 88 │        100.77 / 102.20 ±0.86 / 103.44 ms │         99.59 / 100.40 ±0.65 / 101.54 ms │    no change │
│ QQuery 89 │        120.37 / 121.60 ±1.11 / 123.62 ms │        119.12 / 121.47 ±1.25 / 122.71 ms │    no change │
│ QQuery 90 │           23.91 / 24.59 ±0.65 / 25.73 ms │           23.70 / 24.45 ±0.60 / 25.45 ms │    no change │
│ QQuery 91 │           64.00 / 67.40 ±2.99 / 71.04 ms │           65.55 / 66.98 ±0.84 / 68.01 ms │    no change │
│ QQuery 92 │           58.68 / 59.41 ±0.60 / 60.37 ms │           58.59 / 59.23 ±0.56 / 60.25 ms │    no change │
│ QQuery 93 │        195.99 / 197.30 ±1.55 / 200.33 ms │        192.69 / 195.27 ±1.87 / 198.37 ms │    no change │
│ QQuery 94 │           61.98 / 63.01 ±0.63 / 63.87 ms │           62.18 / 62.64 ±0.34 / 63.04 ms │    no change │
│ QQuery 95 │        135.07 / 136.90 ±1.12 / 138.06 ms │        136.11 / 137.75 ±1.30 / 140.01 ms │    no change │
│ QQuery 96 │           74.32 / 74.94 ±0.57 / 75.80 ms │           74.59 / 75.18 ±0.48 / 75.82 ms │    no change │
│ QQuery 97 │        131.91 / 134.89 ±1.76 / 136.54 ms │        131.82 / 133.33 ±1.40 / 135.32 ms │    no change │
│ QQuery 98 │        156.30 / 158.10 ±1.38 / 159.93 ms │        155.82 / 156.86 ±0.86 / 158.32 ms │    no change │
│ QQuery 99 │ 10731.98 / 10768.22 ±28.49 / 10819.28 ms │ 10713.83 / 10792.67 ±47.00 / 10855.65 ms │    no change │
└───────────┴──────────────────────────────────────────┴──────────────────────────────────────────┴──────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 33782.62ms │
│ Total Time (fix_topk-interleave-overflow)   │ 33977.01ms │
│ Average Time (HEAD)                         │   341.24ms │
│ Average Time (fix_topk-interleave-overflow) │   343.20ms │
│ Queries Faster                              │          0 │
│ Queries Slower                              │          2 │
│ Queries with No Change                      │         97 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘

Resource Usage

tpcds — base (merge-base)

Metric Value
Wall time 169.2s
Peak memory 5.8 GiB
Avg memory 4.6 GiB
CPU user 271.3s
CPU sys 19.0s
Disk read 0 B
Disk write 705.8 MiB

tpcds — branch

Metric Value
Wall time 170.2s
Peak memory 5.9 GiB
Avg memory 4.9 GiB
CPU user 273.0s
CPU sys 18.5s
Disk read 0 B
Disk write 808.0 KiB

@adriangbot
Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Details

Comparing HEAD and fix_topk-interleave-overflow
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query     ┃                                  HEAD ┃          fix_topk-interleave-overflow ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0  │          1.30 / 4.58 ±6.42 / 17.41 ms │          1.30 / 4.56 ±6.38 / 17.31 ms │     no change │
│ QQuery 1  │        14.56 / 14.88 ±0.19 / 15.09 ms │        14.61 / 15.01 ±0.24 / 15.24 ms │     no change │
│ QQuery 2  │        58.93 / 59.09 ±0.11 / 59.23 ms │        56.39 / 57.08 ±0.72 / 58.45 ms │     no change │
│ QQuery 3  │        49.88 / 50.33 ±0.38 / 51.02 ms │        49.57 / 50.73 ±0.92 / 52.13 ms │     no change │
│ QQuery 4  │     292.48 / 295.04 ±2.31 / 299.04 ms │    291.01 / 301.08 ±10.44 / 320.60 ms │     no change │
│ QQuery 5  │     342.77 / 347.86 ±3.41 / 351.98 ms │     344.18 / 347.49 ±2.19 / 350.36 ms │     no change │
│ QQuery 6  │           5.78 / 6.26 ±0.51 / 7.07 ms │           5.08 / 5.88 ±0.78 / 7.04 ms │ +1.06x faster │
│ QQuery 7  │        17.42 / 17.66 ±0.29 / 18.18 ms │        17.01 / 18.77 ±2.82 / 24.39 ms │  1.06x slower │
│ QQuery 8  │     417.18 / 425.57 ±5.23 / 432.22 ms │    424.61 / 441.65 ±14.25 / 458.88 ms │     no change │
│ QQuery 9  │     654.42 / 671.66 ±9.20 / 679.94 ms │     658.31 / 665.05 ±6.77 / 674.53 ms │     no change │
│ QQuery 10 │        90.78 / 93.86 ±2.44 / 97.25 ms │       92.90 / 96.21 ±4.45 / 104.92 ms │     no change │
│ QQuery 11 │     104.93 / 106.40 ±1.14 / 107.87 ms │     108.42 / 109.60 ±0.78 / 110.65 ms │     no change │
│ QQuery 12 │     345.69 / 348.40 ±1.97 / 351.68 ms │     342.29 / 352.58 ±7.87 / 361.86 ms │     no change │
│ QQuery 13 │    459.74 / 473.24 ±17.06 / 506.65 ms │     455.23 / 470.37 ±9.22 / 483.10 ms │     no change │
│ QQuery 14 │     352.12 / 358.05 ±5.21 / 367.74 ms │     348.56 / 359.17 ±6.49 / 364.86 ms │     no change │
│ QQuery 15 │    366.64 / 380.63 ±17.76 / 414.91 ms │    363.24 / 373.67 ±11.55 / 392.09 ms │     no change │
│ QQuery 16 │    736.63 / 754.01 ±16.17 / 781.46 ms │    741.03 / 760.00 ±12.89 / 780.99 ms │     no change │
│ QQuery 17 │     717.36 / 728.44 ±6.88 / 738.15 ms │    724.64 / 733.49 ±11.76 / 756.72 ms │     no change │
│ QQuery 18 │ 1401.76 / 1496.11 ±50.34 / 1537.27 ms │ 1414.34 / 1490.88 ±46.86 / 1554.99 ms │     no change │
│ QQuery 19 │       35.77 / 47.18 ±11.58 / 67.48 ms │        35.34 / 38.18 ±2.00 / 41.18 ms │ +1.24x faster │
│ QQuery 20 │    718.14 / 740.69 ±24.57 / 779.78 ms │    714.75 / 726.89 ±10.70 / 746.64 ms │     no change │
│ QQuery 21 │    768.09 / 776.38 ±12.40 / 801.00 ms │     765.20 / 768.89 ±1.99 / 771.15 ms │     no change │
│ QQuery 22 │  1137.10 / 1141.90 ±3.72 / 1147.58 ms │  1129.85 / 1133.90 ±2.96 / 1138.47 ms │     no change │
│ QQuery 23 │ 3060.72 / 3092.57 ±21.92 / 3115.25 ms │ 3097.68 / 3140.16 ±34.43 / 3192.63 ms │     no change │
│ QQuery 24 │      97.97 / 100.19 ±1.60 / 102.43 ms │      98.62 / 105.42 ±4.32 / 111.26 ms │  1.05x slower │
│ QQuery 25 │     139.27 / 140.22 ±0.69 / 140.98 ms │     138.78 / 141.95 ±2.23 / 144.54 ms │     no change │
│ QQuery 26 │      99.91 / 103.03 ±1.88 / 105.28 ms │     100.00 / 102.47 ±2.22 / 105.96 ms │     no change │
│ QQuery 27 │     851.17 / 854.73 ±2.97 / 858.94 ms │     846.49 / 850.58 ±3.71 / 856.79 ms │     no change │
│ QQuery 28 │ 7707.31 / 7780.74 ±37.55 / 7813.36 ms │ 7705.97 / 7780.18 ±39.67 / 7814.79 ms │     no change │
│ QQuery 29 │        57.82 / 61.15 ±3.91 / 67.85 ms │        55.64 / 60.89 ±4.69 / 67.94 ms │     no change │
│ QQuery 30 │     366.50 / 373.90 ±6.78 / 386.21 ms │     365.10 / 369.21 ±5.26 / 379.44 ms │     no change │
│ QQuery 31 │    371.90 / 388.32 ±10.61 / 402.80 ms │     369.80 / 381.93 ±9.38 / 392.97 ms │     no change │
│ QQuery 32 │ 1048.52 / 1077.86 ±25.09 / 1122.46 ms │ 1057.59 / 1070.90 ±13.16 / 1093.20 ms │     no change │
│ QQuery 33 │  1471.86 / 1481.29 ±7.40 / 1493.90 ms │  1478.32 / 1487.26 ±6.67 / 1494.91 ms │     no change │
│ QQuery 34 │  1469.36 / 1475.25 ±4.72 / 1483.07 ms │ 1508.40 / 1586.11 ±41.47 / 1620.04 ms │  1.08x slower │
│ QQuery 35 │     388.40 / 394.72 ±5.27 / 404.33 ms │    390.63 / 436.65 ±67.72 / 570.07 ms │  1.11x slower │
│ QQuery 36 │     120.78 / 123.86 ±2.14 / 127.19 ms │     115.35 / 122.03 ±5.19 / 129.72 ms │     no change │
│ QQuery 37 │        48.56 / 51.72 ±2.46 / 55.58 ms │        48.36 / 49.73 ±0.92 / 50.54 ms │     no change │
│ QQuery 38 │        76.82 / 78.33 ±1.83 / 81.80 ms │        74.86 / 77.50 ±1.39 / 78.74 ms │     no change │
│ QQuery 39 │     215.97 / 224.17 ±5.10 / 231.25 ms │     212.77 / 219.76 ±6.72 / 231.09 ms │     no change │
│ QQuery 40 │        24.71 / 26.31 ±1.54 / 28.88 ms │        24.60 / 26.50 ±1.55 / 29.26 ms │     no change │
│ QQuery 41 │        20.42 / 22.12 ±1.71 / 25.05 ms │        20.35 / 20.98 ±0.55 / 21.82 ms │ +1.05x faster │
│ QQuery 42 │        19.55 / 20.17 ±0.34 / 20.54 ms │        20.24 / 20.50 ±0.22 / 20.76 ms │     no change │
└───────────┴───────────────────────────────────────┴───────────────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 27208.88ms │
│ Total Time (fix_topk-interleave-overflow)   │ 27371.84ms │
│ Average Time (HEAD)                         │   632.76ms │
│ Average Time (fix_topk-interleave-overflow) │   636.55ms │
│ Queries Faster                              │          3 │
│ Queries Slower                              │          4 │
│ Queries with No Change                      │         36 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘

Resource Usage

clickbench_partitioned — base (merge-base)

Metric Value
Wall time 137.3s
Peak memory 41.5 GiB
Avg memory 31.7 GiB
CPU user 1289.5s
CPU sys 91.4s
Disk read 0 B
Disk write 3.8 GiB

clickbench_partitioned — branch

Metric Value
Wall time 137.6s
Peak memory 39.0 GiB
Avg memory 32.4 GiB
CPU user 1292.4s
CPU sys 94.8s
Disk read 0 B
Disk write 752.0 KiB

Copy link
Copy Markdown
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 for this contirbution @aviralgarg05 and @xudong963 -- I am a little worried about the complexity added in this patch for two reasons:

  1. It doesn't appear to be adequately tested
  2. It may introduce a performance regression when generating output

Typically when people hit offset errors related to using Utf8 (because of offset overflows) our advice is either:

  1. Use Utf8View ("String View")
  2. Use LargeUtf8

I am not sure it is a good tradeoff to slow down / make the Utf8 path more complex

Comment thread datafusion/physical-plan/src/topk/mod.rs
Comment thread datafusion/physical-plan/src/topk/mod.rs
Comment thread datafusion/physical-plan/src/topk/mod.rs
@alamb alamb changed the title fix(topk): avoid overflow panic in interleave emission fix(topk): avoid overflow panic in interleave emission for Utf8 Mar 23, 2026
@xudong963
Copy link
Copy Markdown
Member

I am a little worried about the complexity added in this patch for two reasons:

Yes, the changes are in the hot path, for all cases, it'll do some checks.

I encountered a similar overflow in the SPM path #20922, and introduced a different way to fix it: if we get the panic info, capture it, then progressively retry until no overflow, which only affects the overflow path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TopK operator panics with "overflow" when interleaving large Utf8 string columns

5 participants