Skip to content

[EPIC] Improved Externalized / Spilling / Large than Memory Hash Aggregation #13123

Description

@alamb

This is a collection of items to improve external (spilling) aggregation

Background

Abstract—Analytical database systems offer high-performance in-memory aggregation. If there are many unique groups, temporary query intermediates may not fit RAM, requiring the use of external storage. However, switching from an in-memory to an external algorithm can degrade performance sharply

DataFusion has supported memory limited / spilling hash aggregation since @kazuyukitanimura added it last year in #7400.

We can likely improve this feature and @2010YOUY01 is considering working on it

Tasks the solution you'd like

Activity

  1. alamb commented on Oct 26, 2024

    @alamb
    ContributorAuthor

    @2010YOUY01 says in #13090 (comment)

    Really nice paper, we can implement the same benchmark and compare in the future 😄 They implemented a unified buffer pool for both table data cache and operator (like aggregation) intermediate results, to easily support spilling in various operators. I think they didn't mention any optimization specific to the spilling part of aggregation, and just use simple LRU policy in the buffer pool. Maybe there are some spilling and merging specific optimizations we can explore (all of memory-limited aggregate/SortMergeJoin/Sort can benefit from)

    DF doesn't have a buffer pool in the traditional sense, and the way arrow-rs allocates memory directly from the system allocator makes it quite hard to implement. However, I think the fact that we have arrow-rs and the arrow IPC offers lots of opportunity.

    Also, are you interested in improving DataFusion's external aggregation capabilities? I think it is a non trivial gap at the moment and would be great to improve (and I would be interested in helping do so).
    if you are, I can start organizing the work into some tickets to see if we can get some others to check it out too

    Yes, I'm start to look at related components now. Perhaps we can start with making memory-limited SQL queries more stable (e.g. more tests, make sure TPCH-SF1000 is able to run on laptop correctly), and later optimize.

    I think starting with stability and then optimizing is a great idea 💯

    Note that one challenge of TPCH specifically is that it contains many joins and is largely focused on that, so in order to run TPCH-SF1000 we would also need to implement spilling joins

    Another potential option would be to work on running clickbench with a very small memory (100MB)?

    Or maybe we could figure out another large dataset 🤔

  2. alamb commented on Oct 26, 2024

    @alamb
    ContributorAuthor

    Note that one challenge of TPCH specifically is that it contains many joins and is largely focused on that, so in order to run TPCH-SF1000 we would also need to implement spilling joins

    Maybe @comphead 's work to get SMJ working in #13111 will help this (e.g. we could always use SMJ for the large TPCH queries 🤔 )

  3. 2010YOUY01 commented on Oct 27, 2024

    @2010YOUY01
    Contributor

    Another potential option would be to work on running clickbench with a very small memory (100MB)?

    This is a good idea, we should get clickbench work under memory constraints before TPCH

  4. changed the title [-][EPIC] Improved Externalized / Spilling / Out of core Hash Aggregation[/-] [+][EPIC] Improved Externalized / Spilling / Large than Memory Hash Aggregation[/+] on Jan 10, 2025
  5. alamb commented on Jan 10, 2025

    @alamb
    ContributorAuthor

    Here is a PR to optimize the spill format:

  6. added
    EPICA larger project, actively underway, with sub tasks
    PROPOSAL EPICA proposal being discussed that is not yet fully underway
    and removed
    EPICA larger project, actively underway, with sub tasks
    on Aug 12, 2025
  7. pepijnve commented on Dec 11, 2025

    @pepijnve
    Contributor

    As a followup to #19287 I'm thinking of working on spilling support for GroupOrdering values other than None. Does anyone know if there are specific pitfalls to look out for when implementing that?

  8. alamb commented on Dec 11, 2025

    @alamb
    ContributorAuthor

    As a followup to #19287 I'm thinking of working on spilling support for GroupOrdering values other than None. Does anyone know if there are specific pitfalls to look out for when implementing that?

    Not that I know of

  9. pepijnve commented on Dec 14, 2025

    @pepijnve
    Contributor

    Not that I know of

    I ended up implementing this in the linked PR. The only gotcha I came across is that in case of partial or full ordering, the output ordering is reported as being that ordering. The implication for spilling is that the spill files need to be sorted and merge accordingly.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    PROPOSAL EPICA proposal being discussed that is not yet fully underwayenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions