Skip to content

[C++] data corruption when using group_by and aggregate on large data sets #36295

Closed

Description

@adams-brian

Describe the bug, including details regarding any error messages, version, and platform.

We recently found some data corruption issues when using group_by and aggregate with large data sets.

I was able to create a minimal reproducible example:

import pyarrow as pa
import pyarrow.compute as pc

COLUMN_COUNT = 5  # <- 4 works fine, 5 causes data corruption
LENGTH = 100_000_000
data = {}
# 'index' = [0, 1, 2, ... , LENGTH-2, LENGTH-1]
data['index'] = pc.indices_nonzero(pc.if_else(True, True, pa.nulls(LENGTH, pa.bool_())))
for i in range(COLUMN_COUNT):
    # fill 'i' with i (ex: '3' = [3, 3, 3, ... 3, 3])
    data[f'{i}'] = pa.nulls(LENGTH, pa.uint64()).fill_null(i)
t = pa.table(data)  # <- create table from data
print('-------------------- ORIGINAL --------------------')
print(t)
a = t.group_by(t.column_names).aggregate([])  # <- should behave like a no-op
a = a.combine_chunks()  # <- not necessary, just improves the print formatting
print('-------------- GROUP_BY / AGGREGATE --------------')
print(a)

In this example the group_by and aggregate are set up to behave like a no-op and everything works fine with COLUMN_COUNT <= 4:

-------------------- ORIGINAL --------------------
pyarrow.Table
index: uint64
0: uint64
1: uint64
2: uint64
3: uint64
----
index: [[0,1,2,3,4,...,99999995,99999996,99999997,99999998,99999999]]
0: [[0,0,0,0,0,...,0,0,0,0,0]]
1: [[1,1,1,1,1,...,1,1,1,1,1]]
2: [[2,2,2,2,2,...,2,2,2,2,2]]
3: [[3,3,3,3,3,...,3,3,3,3,3]]
-------------- GROUP_BY / AGGREGATE --------------
pyarrow.Table
index: uint64
0: uint64
1: uint64
2: uint64
3: uint64
----
index: [[0,1,2,3,4,...,99999995,99999996,99999997,99999998,99999999]]
0: [[0,0,0,0,0,...,0,0,0,0,0]]
1: [[1,1,1,1,1,...,1,1,1,1,1]]
2: [[2,2,2,2,2,...,2,2,2,2,2]]
3: [[3,3,3,3,3,...,3,3,3,3,3]]

...but results in data corruption if COLUMN_COUNT >= 5:

-------------------- ORIGINAL --------------------
pyarrow.Table
index: uint64
0: uint64
1: uint64
2: uint64
3: uint64
4: uint64
----
index: [[0,1,2,3,4,...,99999995,99999996,99999997,99999998,99999999]]
0: [[0,0,0,0,0,...,0,0,0,0,0]]
1: [[1,1,1,1,1,...,1,1,1,1,1]]
2: [[2,2,2,2,2,...,2,2,2,2,2]]
3: [[3,3,3,3,3,...,3,3,3,3,3]]
4: [[4,4,4,4,4,...,4,4,4,4,4]]
-------------- GROUP_BY / AGGREGATE --------------
pyarrow.Table
index: uint64
0: uint64
1: uint64
2: uint64
3: uint64
4: uint64
----
index: [[0,1,2,3,4,...,3,3,3,3,3]]
0: [[0,0,0,0,0,...,4,4,4,4,4]]
1: [[1,1,1,1,1,...,10521510,10521511,10521512,10521513,10521514]]
2: [[2,2,2,2,2,...,0,0,0,0,0]]
3: [[3,3,3,3,3,...,1,1,1,1,1]]
4: [[4,4,4,4,4,...,2,2,2,2,2]]

Component(s)

Python

Version

pyarrow==12.0.1. I downgraded to pyarrow==11, pyarrow==10, pyarrow==9, and pyarrow==8 and observed the above behavior in those versions as well.

Platform

Linux (Ubuntu 22.04 LTS)

Activity

  1. westonpace commented on Jun 29, 2023

    @westonpace
    Member

    Hmm...given how stable this is I wonder if we're overflowing an int32 somewhere.

  2. westonpace commented on Jun 30, 2023

    @westonpace
    Member

    Indeed, this was the case. In the internals of the "group by" code is a hash map. This hash map stores keys in row-major order using the row-format (row-major order is commonly used with hash-maps since it's expected they will be accessed non-sequentially). This structure is called the RowTableImpl:

    class ARROW_EXPORT RowTableImpl {

    In the example we are using 6 columns (if we include the index) of int8 as the hash keys. This means each row in our row table is 48 bytes large. The test uses 100,000,000 rows. So we end up with 4,800,000,000 bytes which is over the int32 limit of 4,294,967,296. With 5 columns we only have 4,000,000,000 bytes and so it fits.

    This should be ok. The RowTableImpl appears to have been designed to store 2^64 bytes of data. In practice we do not have any unit tests covering this case and so, unsurprisingly, we have mistakes. The following patch fixes the reproduction from the OP: c901d1e

    Unfortunately, I still have pretty low confidence in the robustness of this code when working with tables this large. There are different paths for boolean fields, dictionary fields, string-like fields, and all of these paths are futher parameterized by AVX2 vs. not-AVX2 and we no longer have CI for AVX2.

    We are missing a set of unit tests to cover these scenarios and I don't think I'll have time to add them for a while. So I'm uncertain at the moment whether it would be better to put in this short-term fix or to simply reject and fail a plan once the keys table exceeds 4GB. If anyone is interested in taking on the effort of adding these unit tests I'd be happy to explain further. In the meantime I'm not sure when I'll get to it. I'm marking this blocker to force myself to make some kind of decision on this in the 13.0.0 timeframe.

  3. added
    Priority: Blocker
    Critical Fix
    on Jun 30, 2023
  4. added this to the 13.0.0 milestone on Jul 4, 2023
  5. raulcd commented on Jul 4, 2023

    @raulcd
    Member

    @westonpace do you think we should prioritize this to have a fix for 13.0.0? or is this not a blocker for the release?

  6. raulcd commented on Jul 7, 2023

    @raulcd
    Member

    @jorisvandenbossche Should this be a release blocker?

  7. jorisvandenbossche commented on Jul 7, 2023

    @jorisvandenbossche
    Member

    @westonpace I assume this is not a new issue? In which case I don't think it would be a blocker

  8. westonpace commented on Jul 7, 2023

    @westonpace
    Member

    It would be good to at least change this to return an error in time for 13.0.0. It's not a new issue but it is "invalid data" which is pretty bad. I agree it is probably not a blocker.

  9. removed
    Priority: Blocker
    on Jul 7, 2023
  10. modified the milestones: 13.0.0, 14.0.0 on Jul 12, 2023
  11. removed this from the 14.0.0 milestone on Oct 6, 2023
  12. 2 remaining items

  13. AlenkaF commented on Nov 15, 2023

    @AlenkaF
    Member

    Might someone with more acero knowledge have time and be willing to work on this issue? cc @ildipo @rtpsw

  14. modified the milestones: 15.0.0, 16.0.0 on Jan 8, 2024
  15. modified the milestones: 16.0.0, 17.0.0 on Apr 8, 2024
  16. modified the milestones: 17.0.0, 18.0.0 on Jun 28, 2024
  17. zanmato1984 commented on Sep 25, 2024

    @zanmato1984
    Contributor

    This issue may have already been fixed by #43389.

    I'm however unfortunately having troubles running pyarrow in my local at the moment so I can't verify. If anyone can help on that (running the python test on master branch), it'll be much appreciated.

  18. amoeba commented on Sep 25, 2024

    @amoeba
    Member

    Hey @zanmato1984, I think you're right that this is now fixed on main. I can reproduce the issue with PyArrow 17 but with PyArrow built from main I no longer get the data corruption.

    >>> pyarrow.__version__
    '18.0.0.dev372+gfc2e018ef1.d20240925'
    

    And when I run the script with COLUMN_COUNT = 5:

    ❯ python repro.py
    -------------------- ORIGINAL --------------------
    pyarrow.Table
    index: uint64
    0: uint64
    1: uint64
    2: uint64
    3: uint64
    4: uint64
    ----
    index: [[0,1,2,3,4,...,99999995,99999996,99999997,99999998,99999999]]
    0: [[0,0,0,0,0,...,0,0,0,0,0]]
    1: [[1,1,1,1,1,...,1,1,1,1,1]]
    2: [[2,2,2,2,2,...,2,2,2,2,2]]
    3: [[3,3,3,3,3,...,3,3,3,3,3]]
    4: [[4,4,4,4,4,...,4,4,4,4,4]]
    -------------- GROUP_BY / AGGREGATE --------------
    pyarrow.Table
    index: uint64
    0: uint64
    1: uint64
    2: uint64
    3: uint64
    4: uint64
    ----
    index: [[0,1,2,3,4,...,98566139,98566140,98566141,98566142,98566143]] 
    0: [[0,0,0,0,0,...,0,0,0,0,0]]
    1: [[1,1,1,1,1,...,1,1,1,1,1]]     <---- this looks good now where it didn't before ✅
    2: [[2,2,2,2,2,...,2,2,2,2,2]]
    3: [[3,3,3,3,3,...,3,3,3,3,3]]
    4: [[4,4,4,4,4,...,4,4,4,4,4]]
    
  19. zanmato1984 commented on Sep 26, 2024

    @zanmato1984
    Contributor

    Thanks a lot @amoeba for verifying. Really appreciate it!

    Let's close this as fixed by #43389 :)

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

    Component: C++Component: PythonCritical FixType: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions