Commit 6954497
committed
fix(accumulators): preserve state in evaluate() for window frame queries
This commit fixes issue #19612 where accumulators that don't implement
retract_batch exhibit buggy behavior in window frame queries.
## Problem
When aggregate functions are used with window frames like
`ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`, DataFusion uses
PlainAggregateWindowExpr which calls evaluate() multiple times on the
same accumulator instance. Accumulators that use std::mem::take() in
their evaluate() method consume their internal state, causing incorrect
results on subsequent calls.
## Solution
1. **percentile_cont**: Modified evaluate() to use mutable reference
instead of consuming the Vec. Added retract_batch() support for
both PercentileContAccumulator and DistinctPercentileContAccumulator.
2. **string_agg**: Changed SimpleStringAggAccumulator::evaluate() to
clone the accumulated string instead of taking it.
## Changes
- datafusion/functions-aggregate/src/percentile_cont.rs:
- Changed calculate_percentile() to take &mut [T::Native] instead of Vec<T::Native>
- Updated PercentileContAccumulator::evaluate() to pass reference
- Updated DistinctPercentileContAccumulator::evaluate() to clone values
- Added retract_batch() implementation using HashMap for efficient removal
- Updated PercentileContGroupsAccumulator::evaluate() for consistency
- datafusion/functions-aggregate/src/string_agg.rs:
- Changed evaluate() to use clone() instead of std::mem::take()
- datafusion/sqllogictest/test_files/aggregate.slt:
- Added test cases for percentile_cont with window frames
- Added test comparing median() vs percentile_cont(0.5) behavior
- Added test for string_agg cumulative window frame
- docs/source/library-user-guide/functions/adding-udfs.md:
- Added documentation about window-compatible accumulators
- Explained evaluate() state preservation requirements
- Documented retract_batch() implementation guidance
Closes #196121 parent 8809dae commit 6954497
File tree
4 files changed
+280
-18
lines changed- datafusion
- functions-aggregate/src
- sqllogictest/test_files
- docs/source/library-user-guide/functions
4 files changed
+280
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | | - | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
533 | 534 | | |
534 | 535 | | |
535 | 536 | | |
536 | | - | |
537 | | - | |
| 537 | + | |
538 | 538 | | |
539 | 539 | | |
540 | 540 | | |
541 | 541 | | |
542 | 542 | | |
543 | 543 | | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
544 | 588 | | |
545 | 589 | | |
546 | 590 | | |
| |||
665 | 709 | | |
666 | 710 | | |
667 | 711 | | |
668 | | - | |
| 712 | + | |
669 | 713 | | |
670 | 714 | | |
671 | 715 | | |
672 | 716 | | |
673 | | - | |
674 | | - | |
| 717 | + | |
| 718 | + | |
675 | 719 | | |
676 | 720 | | |
677 | 721 | | |
| |||
768 | 812 | | |
769 | 813 | | |
770 | 814 | | |
771 | | - | |
772 | | - | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
773 | 819 | | |
774 | | - | |
775 | | - | |
| 820 | + | |
| 821 | + | |
776 | 822 | | |
777 | 823 | | |
778 | 824 | | |
779 | 825 | | |
780 | 826 | | |
781 | 827 | | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
782 | 844 | | |
783 | 845 | | |
784 | 846 | | |
| |||
788 | 850 | | |
789 | 851 | | |
790 | 852 | | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
791 | 857 | | |
792 | | - | |
| 858 | + | |
793 | 859 | | |
794 | 860 | | |
795 | 861 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
387 | | - | |
388 | | - | |
| 387 | + | |
| 388 | + | |
389 | 389 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
| 390 | + | |
| 391 | + | |
395 | 392 | | |
396 | 393 | | |
397 | 394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8241 | 8241 | | |
8242 | 8242 | | |
8243 | 8243 | | |
| 8244 | + | |
| 8245 | + | |
| 8246 | + | |
| 8247 | + | |
| 8248 | + | |
| 8249 | + | |
| 8250 | + | |
| 8251 | + | |
| 8252 | + | |
| 8253 | + | |
| 8254 | + | |
| 8255 | + | |
| 8256 | + | |
| 8257 | + | |
| 8258 | + | |
| 8259 | + | |
| 8260 | + | |
| 8261 | + | |
| 8262 | + | |
| 8263 | + | |
| 8264 | + | |
| 8265 | + | |
| 8266 | + | |
| 8267 | + | |
| 8268 | + | |
| 8269 | + | |
| 8270 | + | |
| 8271 | + | |
| 8272 | + | |
| 8273 | + | |
| 8274 | + | |
| 8275 | + | |
| 8276 | + | |
| 8277 | + | |
| 8278 | + | |
| 8279 | + | |
| 8280 | + | |
| 8281 | + | |
| 8282 | + | |
| 8283 | + | |
| 8284 | + | |
| 8285 | + | |
| 8286 | + | |
| 8287 | + | |
| 8288 | + | |
| 8289 | + | |
| 8290 | + | |
| 8291 | + | |
| 8292 | + | |
| 8293 | + | |
| 8294 | + | |
| 8295 | + | |
| 8296 | + | |
| 8297 | + | |
| 8298 | + | |
| 8299 | + | |
| 8300 | + | |
| 8301 | + | |
| 8302 | + | |
| 8303 | + | |
| 8304 | + | |
| 8305 | + | |
| 8306 | + | |
| 8307 | + | |
| 8308 | + | |
| 8309 | + | |
| 8310 | + | |
| 8311 | + | |
| 8312 | + | |
| 8313 | + | |
| 8314 | + | |
| 8315 | + | |
| 8316 | + | |
| 8317 | + | |
| 8318 | + | |
| 8319 | + | |
| 8320 | + | |
| 8321 | + | |
| 8322 | + | |
| 8323 | + | |
| 8324 | + | |
| 8325 | + | |
| 8326 | + | |
| 8327 | + | |
| 8328 | + | |
| 8329 | + | |
| 8330 | + | |
| 8331 | + | |
| 8332 | + | |
| 8333 | + | |
| 8334 | + | |
| 8335 | + | |
| 8336 | + | |
| 8337 | + | |
| 8338 | + | |
| 8339 | + | |
| 8340 | + | |
| 8341 | + | |
| 8342 | + | |
| 8343 | + | |
| 8344 | + | |
| 8345 | + | |
| 8346 | + | |
| 8347 | + | |
| 8348 | + | |
| 8349 | + | |
| 8350 | + | |
| 8351 | + | |
| 8352 | + | |
| 8353 | + | |
| 8354 | + | |
| 8355 | + | |
| 8356 | + | |
| 8357 | + | |
| 8358 | + | |
| 8359 | + | |
| 8360 | + | |
| 8361 | + | |
| 8362 | + | |
| 8363 | + | |
| 8364 | + | |
| 8365 | + | |
| 8366 | + | |
| 8367 | + | |
| 8368 | + | |
| 8369 | + | |
| 8370 | + | |
| 8371 | + | |
| 8372 | + | |
| 8373 | + | |
| 8374 | + | |
| 8375 | + | |
| 8376 | + | |
| 8377 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1350 | 1350 | | |
1351 | 1351 | | |
1352 | 1352 | | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
1353 | 1418 | | |
1354 | 1419 | | |
1355 | 1420 | | |
| |||
0 commit comments