Skip to content

Draft: Use upstream arrow coalesce kernel in DataFusion #16249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Jun 4, 2025

Which issue does this PR close?

Rationale for this change

I am trying to move the coalesce operation upstream into arrow-rs so that it can be reused in parquet filter pushdown and more highly optimized. See :

The proposed upstream coalescer API is slightly different (guaranteed batch size, and doesn't have limit) so we must adapt the DataFusion code to handle this

What changes are included in this PR?

This PR refactors the BatchCoalescer in DataFusion to use the proposed upstream API to show that it

  1. Can be used (api is complete enough)
  2. Is not any slower

Are these changes tested?

By CI tests

Are there any user-facing changes?

No

@github-actions github-actions bot added the physical-plan Changes to the physical-plan crate label Jun 4, 2025
@@ -98,197 +49,93 @@ impl BatchCoalescer {
fetch: Option<usize>,
) -> Self {
Self {
schema,
target_batch_size,
inner: BatchCoalescer::new(schema, target_batch_size),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the key change here -- move all the buffer management upstream into arrow

/// [`BatchCoalescer::push_batch()`] operation.
///
/// The caller should take different actions, depending on the variant returned.
pub enum CoalescerState {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the buffering is all managed upstream now, so there is no need to expose buffering details to the user of the coalescer

@@ -488,110 +329,6 @@ mod tests {
.unwrap()
}

#[test]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

all moved upstream

/// Execution metrics
baseline_metrics: BaselineMetrics,
/// The current inner state of the stream. This state dictates the current
/// action or operation to be performed in the streaming process.
inner_state: CoalesceBatchesStreamState,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the Coalescer can now buffer batches internally, this enum can be reduced to "complete" or not

@alamb
Copy link
Contributor Author

alamb commented Jun 4, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.11.0-1013-gcp #13~24.04.1-Ubuntu SMP Wed Apr 2 16:34:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing alamb/test_upstream_coalesce (e79454f) to 992d156 diff
Benchmarks: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor Author

alamb commented Jun 4, 2025

🤖: Benchmark completed

Details

Comparing HEAD and alamb_test_upstream_coalesce
--------------------
Benchmark clickbench_extended.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query        ┃       HEAD ┃ alamb_test_upstream_coalesce ┃    Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ QQuery 0     │  1903.28ms │                    1910.47ms │ no change │
│ QQuery 1     │   705.78ms │                     705.39ms │ no change │
│ QQuery 2     │  1416.79ms │                    1427.68ms │ no change │
│ QQuery 3     │   721.59ms │                     695.62ms │ no change │
│ QQuery 4     │  1438.61ms │                    1459.22ms │ no change │
│ QQuery 5     │ 15147.47ms │                   15620.61ms │ no change │
│ QQuery 6     │  2025.79ms │                    2037.42ms │ no change │
│ QQuery 7     │  2088.81ms │                    2069.21ms │ no change │
│ QQuery 8     │   852.36ms │                     834.76ms │ no change │
└──────────────┴────────────┴──────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 26300.48ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 26760.37ms │
│ Average Time (HEAD)                         │  2922.28ms │
│ Average Time (alamb_test_upstream_coalesce) │  2973.37ms │
│ Queries Faster                              │          0 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │          9 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃       HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │    15.39ms │                      16.12ms │     no change │
│ QQuery 1     │    33.30ms │                      33.27ms │     no change │
│ QQuery 2     │    81.95ms │                      81.53ms │     no change │
│ QQuery 3     │    99.01ms │                      95.19ms │     no change │
│ QQuery 4     │   680.72ms │                     583.09ms │ +1.17x faster │
│ QQuery 5     │   901.62ms │                     859.72ms │     no change │
│ QQuery 6     │    24.40ms │                      23.86ms │     no change │
│ QQuery 7     │    37.45ms │                      36.23ms │     no change │
│ QQuery 8     │   951.27ms │                     922.01ms │     no change │
│ QQuery 9     │  1259.35ms │                    1187.14ms │ +1.06x faster │
│ QQuery 10    │   270.07ms │                     260.94ms │     no change │
│ QQuery 11    │   301.01ms │                     298.82ms │     no change │
│ QQuery 12    │   929.60ms │                     920.78ms │     no change │
│ QQuery 13    │  1352.62ms │                    1355.67ms │     no change │
│ QQuery 14    │   843.74ms │                     858.27ms │     no change │
│ QQuery 15    │   842.01ms │                     841.71ms │     no change │
│ QQuery 16    │  1725.31ms │                    1755.26ms │     no change │
│ QQuery 17    │  1605.81ms │                    1607.33ms │     no change │
│ QQuery 18    │  3061.08ms │                    3122.06ms │     no change │
│ QQuery 19    │    84.26ms │                      81.68ms │     no change │
│ QQuery 20    │  1134.75ms │                    1120.94ms │     no change │
│ QQuery 21    │  1327.65ms │                    1341.47ms │     no change │
│ QQuery 22    │  2173.10ms │                    2220.13ms │     no change │
│ QQuery 23    │  8004.42ms │                    7989.85ms │     no change │
│ QQuery 24    │   468.53ms │                     466.85ms │     no change │
│ QQuery 25    │   383.28ms │                     389.97ms │     no change │
│ QQuery 26    │   538.11ms │                     527.43ms │     no change │
│ QQuery 27    │  1606.15ms │                    1622.52ms │     no change │
│ QQuery 28    │ 13703.82ms │                   13070.74ms │     no change │
│ QQuery 29    │   520.31ms │                     525.56ms │     no change │
│ QQuery 30    │   796.51ms │                     839.14ms │  1.05x slower │
│ QQuery 31    │   851.63ms │                     884.19ms │     no change │
│ QQuery 32    │  2618.12ms │                    2636.26ms │     no change │
│ QQuery 33    │  3372.88ms │                    3288.44ms │     no change │
│ QQuery 34    │  3403.04ms │                    3349.20ms │     no change │
│ QQuery 35    │  1296.66ms │                    1313.52ms │     no change │
│ QQuery 36    │   121.58ms │                     126.46ms │     no change │
│ QQuery 37    │    56.05ms │                      56.07ms │     no change │
│ QQuery 38    │   122.31ms │                     124.83ms │     no change │
│ QQuery 39    │   197.01ms │                     198.32ms │     no change │
│ QQuery 40    │    50.25ms │                      47.22ms │ +1.06x faster │
│ QQuery 41    │    43.47ms │                      45.99ms │  1.06x slower │
│ QQuery 42    │    37.67ms │                      38.74ms │     no change │
└──────────────┴────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 57927.27ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 57164.53ms │
│ Average Time (HEAD)                         │  1347.15ms │
│ Average Time (alamb_test_upstream_coalesce) │  1329.41ms │
│ Queries Faster                              │          3 │
│ Queries Slower                              │          2 │
│ Queries with No Change                      │         38 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark tpch_mem_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Query        ┃     HEAD ┃ alamb_test_upstream_coalesce ┃       Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
│ QQuery 1     │ 116.93ms │                     116.77ms │    no change │
│ QQuery 2     │  22.28ms │                      22.65ms │    no change │
│ QQuery 3     │  34.38ms │                      35.42ms │    no change │
│ QQuery 4     │  19.66ms │                      20.79ms │ 1.06x slower │
│ QQuery 5     │  54.59ms │                      53.23ms │    no change │
│ QQuery 6     │  11.97ms │                      11.95ms │    no change │
│ QQuery 7     │  96.84ms │                     100.06ms │    no change │
│ QQuery 8     │  25.69ms │                      25.35ms │    no change │
│ QQuery 9     │  60.26ms │                      60.42ms │    no change │
│ QQuery 10    │  49.39ms │                      47.08ms │    no change │
│ QQuery 11    │  11.41ms │                      12.24ms │ 1.07x slower │
│ QQuery 12    │  41.31ms │                      40.07ms │    no change │
│ QQuery 13    │  27.65ms │                      28.44ms │    no change │
│ QQuery 14    │   9.80ms │                       9.94ms │    no change │
│ QQuery 15    │  23.05ms │                      22.78ms │    no change │
│ QQuery 16    │  21.49ms │                      20.98ms │    no change │
│ QQuery 17    │  98.87ms │                      98.30ms │    no change │
│ QQuery 18    │ 217.87ms │                     208.15ms │    no change │
│ QQuery 19    │  26.67ms │                      26.21ms │    no change │
│ QQuery 20    │  34.27ms │                      36.07ms │ 1.05x slower │
│ QQuery 21    │ 160.60ms │                     163.49ms │    no change │
│ QQuery 22    │  16.66ms │                      16.30ms │    no change │
└──────────────┴──────────┴──────────────────────────────┴──────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 1181.64ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 1176.69ms │
│ Average Time (HEAD)                         │   53.71ms │
│ Average Time (alamb_test_upstream_coalesce) │   53.49ms │
│ Queries Faster                              │         0 │
│ Queries Slower                              │         3 │
│ Queries with No Change                      │        19 │
└─────────────────────────────────────────────┴───────────┘

@alamb
Copy link
Contributor Author

alamb commented Jun 4, 2025

TLDR is that the performance looks good. I'll fixup the tests shortly

@github-actions github-actions bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Jun 4, 2025
@alamb
Copy link
Contributor Author

alamb commented Jun 4, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.11.0-1013-gcp #13~24.04.1-Ubuntu SMP Wed Apr 2 16:34:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing alamb/test_upstream_coalesce (9e20973) to 992d156 diff
Benchmarks: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor Author

alamb commented Jun 4, 2025

🤖: Benchmark completed

Details

Comparing HEAD and alamb_test_upstream_coalesce
--------------------
Benchmark clickbench_extended.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query        ┃       HEAD ┃ alamb_test_upstream_coalesce ┃    Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ QQuery 0     │  1914.38ms │                    1938.51ms │ no change │
│ QQuery 1     │   711.81ms │                     718.29ms │ no change │
│ QQuery 2     │  1455.52ms │                    1470.28ms │ no change │
│ QQuery 3     │   712.37ms │                     694.68ms │ no change │
│ QQuery 4     │  1449.70ms │                    1448.88ms │ no change │
│ QQuery 5     │ 15655.93ms │                   15613.27ms │ no change │
│ QQuery 6     │  2014.71ms │                    2072.03ms │ no change │
│ QQuery 7     │  2080.97ms │                    2169.52ms │ no change │
│ QQuery 8     │   856.77ms │                     841.42ms │ no change │
└──────────────┴────────────┴──────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 26852.14ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 26966.88ms │
│ Average Time (HEAD)                         │  2983.57ms │
│ Average Time (alamb_test_upstream_coalesce) │  2996.32ms │
│ Queries Faster                              │          0 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │          9 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃       HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │    15.50ms │                      15.07ms │     no change │
│ QQuery 1     │    32.04ms │                      32.46ms │     no change │
│ QQuery 2     │    85.71ms │                      81.49ms │     no change │
│ QQuery 3     │   101.37ms │                      96.91ms │     no change │
│ QQuery 4     │   635.37ms │                     605.83ms │     no change │
│ QQuery 5     │   846.37ms │                     857.35ms │     no change │
│ QQuery 6     │    25.51ms │                      22.94ms │ +1.11x faster │
│ QQuery 7     │    36.44ms │                      36.85ms │     no change │
│ QQuery 8     │   905.00ms │                     899.47ms │     no change │
│ QQuery 9     │  1221.22ms │                    1203.33ms │     no change │
│ QQuery 10    │   256.84ms │                     261.35ms │     no change │
│ QQuery 11    │   298.30ms │                     293.02ms │     no change │
│ QQuery 12    │   896.38ms │                     900.13ms │     no change │
│ QQuery 13    │  1319.11ms │                    1229.16ms │ +1.07x faster │
│ QQuery 14    │   821.82ms │                     840.52ms │     no change │
│ QQuery 15    │   808.55ms │                     832.04ms │     no change │
│ QQuery 16    │  1708.26ms │                    1717.94ms │     no change │
│ QQuery 17    │  1564.42ms │                    1600.58ms │     no change │
│ QQuery 18    │  3036.53ms │                    3090.72ms │     no change │
│ QQuery 19    │    83.52ms │                      83.60ms │     no change │
│ QQuery 20    │  1120.03ms │                    1142.33ms │     no change │
│ QQuery 21    │  1327.78ms │                    1340.28ms │     no change │
│ QQuery 22    │  2180.54ms │                    2211.59ms │     no change │
│ QQuery 23    │  8051.83ms │                    8092.34ms │     no change │
│ QQuery 24    │   467.15ms │                     463.58ms │     no change │
│ QQuery 25    │   393.84ms │                     390.05ms │     no change │
│ QQuery 26    │   540.91ms │                     522.58ms │     no change │
│ QQuery 27    │  1583.35ms │                    1648.93ms │     no change │
│ QQuery 28    │ 13535.80ms │                   14249.61ms │  1.05x slower │
│ QQuery 29    │   517.83ms │                     530.48ms │     no change │
│ QQuery 30    │   794.70ms │                     826.55ms │     no change │
│ QQuery 31    │   845.08ms │                     885.81ms │     no change │
│ QQuery 32    │  2690.84ms │                    2696.00ms │     no change │
│ QQuery 33    │  3326.15ms │                    3318.91ms │     no change │
│ QQuery 34    │  3395.28ms │                    3349.97ms │     no change │
│ QQuery 35    │  1271.25ms │                    1254.09ms │     no change │
│ QQuery 36    │   126.96ms │                     133.46ms │  1.05x slower │
│ QQuery 37    │    55.58ms │                      55.47ms │     no change │
│ QQuery 38    │   121.97ms │                     123.72ms │     no change │
│ QQuery 39    │   197.25ms │                     197.97ms │     no change │
│ QQuery 40    │    46.12ms │                      47.56ms │     no change │
│ QQuery 41    │    45.89ms │                      45.86ms │     no change │
│ QQuery 42    │    37.90ms │                      38.37ms │     no change │
└──────────────┴────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 57372.28ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 58266.22ms │
│ Average Time (HEAD)                         │  1334.24ms │
│ Average Time (alamb_test_upstream_coalesce) │  1355.03ms │
│ Queries Faster                              │          2 │
│ Queries Slower                              │          2 │
│ Queries with No Change                      │         39 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark tpch_mem_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃     HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 118.13ms │                     113.29ms │     no change │
│ QQuery 2     │  22.88ms │                      21.65ms │ +1.06x faster │
│ QQuery 3     │  34.71ms │                      34.51ms │     no change │
│ QQuery 4     │  19.51ms │                      20.75ms │  1.06x slower │
│ QQuery 5     │  52.61ms │                      52.95ms │     no change │
│ QQuery 6     │  11.99ms │                      12.01ms │     no change │
│ QQuery 7     │  95.89ms │                      90.98ms │ +1.05x faster │
│ QQuery 8     │  25.59ms │                      25.43ms │     no change │
│ QQuery 9     │  59.49ms │                      57.39ms │     no change │
│ QQuery 10    │  47.76ms │                      47.09ms │     no change │
│ QQuery 11    │  11.30ms │                      11.17ms │     no change │
│ QQuery 12    │  40.26ms │                      41.39ms │     no change │
│ QQuery 13    │  26.70ms │                      27.66ms │     no change │
│ QQuery 14    │   9.68ms │                       9.63ms │     no change │
│ QQuery 15    │  22.07ms │                      22.57ms │     no change │
│ QQuery 16    │  21.15ms │                      20.92ms │     no change │
│ QQuery 17    │  95.43ms │                      96.63ms │     no change │
│ QQuery 18    │ 214.02ms │                     191.75ms │ +1.12x faster │
│ QQuery 19    │  28.84ms │                      25.39ms │ +1.14x faster │
│ QQuery 20    │  35.16ms │                      34.99ms │     no change │
│ QQuery 21    │ 157.53ms │                     161.74ms │     no change │
│ QQuery 22    │  15.87ms │                      15.50ms │     no change │
└──────────────┴──────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 1166.59ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 1135.38ms │
│ Average Time (HEAD)                         │   53.03ms │
│ Average Time (alamb_test_upstream_coalesce) │   51.61ms │
│ Queries Faster                              │         4 │
│ Queries Slower                              │         1 │
│ Queries with No Change                      │        17 │
└─────────────────────────────────────────────┴───────────┘

@@ -2107,9 +2107,9 @@ RIGHT JOIN (select t2_id from join_t2 where join_t2.t2_id > 11) as join_t2
ON join_t1.t1_id < join_t2.t2_id
----
33 44
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this query has no ORDER BY so it is ok if the output emerges in a different order

Dandandan pushed a commit to apache/arrow-rs that referenced this pull request Jun 5, 2025
…lected b…atches: (#7597)

# Which issue does this PR close?

- Part of #6692
- Part of #7589


# Rationale for this change

The pattern of combining multiple small RecordBatches to form one larger
one for
subsequent processing is common in query engines like DataFusion which
filter or
partition incoming Arrays. Current best practice is to use the `filter`
or `take` kernels and then
`concat` kernels as explained in
- #6692

This pattern also appears in my attempt to improve parquet filter
performance (to cache the result
of applying a filter rather than re-decoding the results). See
- apache/datafusion#3463
- #7513

The current pattern is non optimal as it requires:
1. At least 2x peak memory (holding the input and output of `concat`)
2. 2 copies of the data (to create the output of `filter` and then
create the output of `concat`)

The theory is that with sufficient optimization we can reduce the peak
memory
requirements and (possibly) make it faster as well.

However, to add a benchmark for this filter+concat, I basically had
nothing to
benchmark. Specifically, there needed to be an API to call.

- Note I also made a PR to DataFusion showing this API can be used and
it is not slower: apache/datafusion#16249


# What changes are included in this PR?

I ported the code from DataFusion downstream upstream into arrow-rs so
1. We can use it in the parquet reader
2. We can benchmark and optimize it appropriately

1. Add `BatchCoalescer` to `arrow-select`, and tests
2. Update documentation
2. Add examples
3. Add a `pub` export in `arrow`
4. Add Benchmark

# Are there any user-facing changes?

This is a new API. 

I next plan to make an benchmark for this particular
@alamb
Copy link
Contributor Author

alamb commented Jun 7, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.11.0-1013-gcp #13~24.04.1-Ubuntu SMP Wed Apr 2 16:34:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing alamb/test_upstream_coalesce (7d6471b) to 1daa5ed diff
Benchmarks: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor Author

alamb commented Jun 7, 2025

https://github.com/apache/datafusion/actions/runs/15506836203/job/43662848611?pr=16249

Caused by:
process didn't exit successfully: /home/runner/work/datafusion/datafusion/target/ci/deps/fuzz-a456cc2e1bdd6367 (signal: 11, SIGSEGV: invalid memory reference)
Error: Process completed with exit code 101.

🤔 that isn't good

@Dandandan
Copy link
Contributor

I added a PR for reverting the changes in arrow-rs apache/arrow-rs#7623 - probably something subtle with one of the fast paths that isn't tested in arrow-rs.

@alamb
Copy link
Contributor Author

alamb commented Jun 7, 2025

I added a PR for reverting the changes in arrow-rs apache/arrow-rs#7623 - probably something subtle with one of the fast paths that isn't tested in arrow-rs.

Thank you. I have a theory: apache/arrow-rs#7623 (comment)

@alamb
Copy link
Contributor Author

alamb commented Jun 7, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.11.0-1013-gcp #13~24.04.1-Ubuntu SMP Wed Apr 2 16:34:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing alamb/test_upstream_coalesce (7d6471b) to 1daa5ed diff
Benchmarks: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

alamb pushed a commit to apache/arrow-rs that referenced this pull request Jun 7, 2025
#7623)

This reverts commit 7739a83.

# Which issue does this PR close?



# Rationale for this change
I found this errors in DataFusion (see
apache/datafusion#16249 (comment)),
so let's revert it and find the error.


# What changes are included in this PR?


# Are there any user-facing changes?
@Dandandan Dandandan force-pushed the alamb/test_upstream_coalesce branch from 7d6471b to 49cb62e Compare June 7, 2025 17:16
@Dandandan
Copy link
Contributor

@alamb benchmark runs ok now

@alamb
Copy link
Contributor Author

alamb commented Jun 8, 2025

🤖 ./gh_compare_branch.sh Benchmark Script Running
Linux aal-dev 6.11.0-1013-gcp #13~24.04.1-Ubuntu SMP Wed Apr 2 16:34:16 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing alamb/test_upstream_coalesce (ea8b700) to 992d156 diff
Benchmarks: tpch_mem clickbench_partitioned clickbench_extended
Results will be posted here when complete

@alamb
Copy link
Contributor Author

alamb commented Jun 8, 2025

@alamb benchmark runs ok now

Awesome -- thanks -- I restarted it now

@alamb
Copy link
Contributor Author

alamb commented Jun 8, 2025

🤖: Benchmark completed

Details

Comparing HEAD and alamb_test_upstream_coalesce
--------------------
Benchmark clickbench_extended.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │  1889.06 ms │                   1880.96 ms │     no change │
│ QQuery 1     │   716.77 ms │                    667.48 ms │ +1.07x faster │
│ QQuery 2     │  1442.19 ms │                   1381.98 ms │     no change │
│ QQuery 3     │   712.29 ms │                    707.44 ms │     no change │
│ QQuery 4     │  1455.16 ms │                   1475.63 ms │     no change │
│ QQuery 5     │ 15533.57 ms │                  15474.07 ms │     no change │
│ QQuery 6     │  2038.22 ms │                   2036.86 ms │     no change │
│ QQuery 7     │  2061.78 ms │                   2037.80 ms │     no change │
│ QQuery 8     │   842.37 ms │                    847.71 ms │     no change │
└──────────────┴─────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 26691.41ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 26509.93ms │
│ Average Time (HEAD)                         │  2965.71ms │
│ Average Time (alamb_test_upstream_coalesce) │  2945.55ms │
│ Queries Faster                              │          1 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │          8 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark clickbench_partitioned.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃        HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 0     │    15.65 ms │                     15.15 ms │     no change │
│ QQuery 1     │    32.30 ms │                     33.35 ms │     no change │
│ QQuery 2     │    81.46 ms │                     81.55 ms │     no change │
│ QQuery 3     │    96.00 ms │                     95.49 ms │     no change │
│ QQuery 4     │   590.78 ms │                    601.11 ms │     no change │
│ QQuery 5     │   854.48 ms │                    848.50 ms │     no change │
│ QQuery 6     │    23.10 ms │                     22.76 ms │     no change │
│ QQuery 7     │    37.06 ms │                     36.92 ms │     no change │
│ QQuery 8     │   896.05 ms │                    900.08 ms │     no change │
│ QQuery 9     │  1200.36 ms │                   1191.28 ms │     no change │
│ QQuery 10    │   262.49 ms │                    251.45 ms │     no change │
│ QQuery 11    │   291.65 ms │                    280.36 ms │     no change │
│ QQuery 12    │   904.38 ms │                    889.86 ms │     no change │
│ QQuery 13    │  1315.11 ms │                   1315.16 ms │     no change │
│ QQuery 14    │   837.89 ms │                    834.22 ms │     no change │
│ QQuery 15    │   821.30 ms │                    832.64 ms │     no change │
│ QQuery 16    │  1705.68 ms │                   1693.59 ms │     no change │
│ QQuery 17    │  1592.92 ms │                   1569.47 ms │     no change │
│ QQuery 18    │  3045.50 ms │                   3002.20 ms │     no change │
│ QQuery 19    │    84.78 ms │                     83.02 ms │     no change │
│ QQuery 20    │  1120.03 ms │                   1133.34 ms │     no change │
│ QQuery 21    │  1315.09 ms │                   1347.90 ms │     no change │
│ QQuery 22    │  2178.63 ms │                   2211.92 ms │     no change │
│ QQuery 23    │  8029.68 ms │                   8083.35 ms │     no change │
│ QQuery 24    │   461.71 ms │                    449.41 ms │     no change │
│ QQuery 25    │   382.66 ms │                    390.38 ms │     no change │
│ QQuery 26    │   529.64 ms │                    522.68 ms │     no change │
│ QQuery 27    │  1625.55 ms │                   1625.82 ms │     no change │
│ QQuery 28    │ 13784.35 ms │                  12932.17 ms │ +1.07x faster │
│ QQuery 29    │   521.18 ms │                    524.09 ms │     no change │
│ QQuery 30    │   801.66 ms │                    823.84 ms │     no change │
│ QQuery 31    │   851.85 ms │                    852.68 ms │     no change │
│ QQuery 32    │  2690.63 ms │                   2635.40 ms │     no change │
│ QQuery 33    │  3345.68 ms │                   3295.22 ms │     no change │
│ QQuery 34    │  3342.67 ms │                   3308.73 ms │     no change │
│ QQuery 35    │  1263.11 ms │                   1267.54 ms │     no change │
│ QQuery 36    │   123.69 ms │                    124.38 ms │     no change │
│ QQuery 37    │    56.37 ms │                     58.61 ms │     no change │
│ QQuery 38    │   125.62 ms │                    124.93 ms │     no change │
│ QQuery 39    │   196.76 ms │                    196.00 ms │     no change │
│ QQuery 40    │    46.66 ms │                     46.58 ms │     no change │
│ QQuery 41    │    45.72 ms │                     46.90 ms │     no change │
│ QQuery 42    │    38.86 ms │                     37.39 ms │     no change │
└──────────────┴─────────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 57566.75ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 56617.44ms │
│ Average Time (HEAD)                         │  1338.76ms │
│ Average Time (alamb_test_upstream_coalesce) │  1316.68ms │
│ Queries Faster                              │          1 │
│ Queries Slower                              │          0 │
│ Queries with No Change                      │         42 │
│ Queries with Failure                        │          0 │
└─────────────────────────────────────────────┴────────────┘
--------------------
Benchmark tpch_mem_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃      HEAD ┃ alamb_test_upstream_coalesce ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 118.73 ms │                    102.68 ms │ +1.16x faster │
│ QQuery 2     │  22.86 ms │                     21.30 ms │ +1.07x faster │
│ QQuery 3     │  34.24 ms │                     33.92 ms │     no change │
│ QQuery 4     │  20.26 ms │                     20.20 ms │     no change │
│ QQuery 5     │  53.26 ms │                     50.18 ms │ +1.06x faster │
│ QQuery 6     │  12.20 ms │                     11.80 ms │     no change │
│ QQuery 7     │  98.01 ms │                     90.17 ms │ +1.09x faster │
│ QQuery 8     │  26.39 ms │                     25.29 ms │     no change │
│ QQuery 9     │  57.58 ms │                     57.16 ms │     no change │
│ QQuery 10    │  49.82 ms │                     45.13 ms │ +1.10x faster │
│ QQuery 11    │  11.63 ms │                     11.14 ms │     no change │
│ QQuery 12    │  40.85 ms │                     38.18 ms │ +1.07x faster │
│ QQuery 13    │  28.41 ms │                     28.02 ms │     no change │
│ QQuery 14    │  10.05 ms │                      9.69 ms │     no change │
│ QQuery 15    │  22.60 ms │                     22.41 ms │     no change │
│ QQuery 16    │  21.50 ms │                     20.11 ms │ +1.07x faster │
│ QQuery 17    │  97.15 ms │                     95.22 ms │     no change │
│ QQuery 18    │ 210.39 ms │                    176.64 ms │ +1.19x faster │
│ QQuery 19    │  26.46 ms │                     25.16 ms │     no change │
│ QQuery 20    │  34.93 ms │                     33.86 ms │     no change │
│ QQuery 21    │ 160.28 ms │                    144.59 ms │ +1.11x faster │
│ QQuery 22    │  17.04 ms │                     15.31 ms │ +1.11x faster │
└──────────────┴───────────┴──────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                           ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                           │ 1174.64ms │
│ Total Time (alamb_test_upstream_coalesce)   │ 1078.14ms │
│ Average Time (HEAD)                         │   53.39ms │
│ Average Time (alamb_test_upstream_coalesce) │   49.01ms │
│ Queries Faster                              │        10 │
│ Queries Slower                              │         0 │
│ Queries with No Change                      │        12 │
│ Queries with Failure                        │         0 │
└─────────────────────────────────────────────┴───────────┘

@Dandandan Dandandan marked this pull request as ready for review June 8, 2025 15:12
@Dandandan Dandandan marked this pull request as draft June 8, 2025 15:13
@Dandandan
Copy link
Contributor

🤖: Benchmark completed

Details

This is similar to my results, some larger gains on TPC-H, smaller gains (spreaded out over different queries) for clickbench.

@alamb
Copy link
Contributor Author

alamb commented Jun 8, 2025

This is similar to my results, some larger gains on TPC-H, smaller gains (spreaded out over different queries) for clickbench.

SWEET!

Just wait until we get rid of the intermediate copy ;) it is going to be amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate 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.

2 participants