Skip to content

dataframe merge returns incorrect result when data is from multi-partition parquet #2730

Closed

Description

@ningsean

Similar to issue #2237 , I am seeing dask dataframe merge result can be incorrect when data is from multi-partition parquet. pandas merge always produces correct results.

I have put my test data in https://github.com/ningsean/dask_merge_issue
I am running a local cluster at localhost:9898

My setup is Python 3.6.1, with the following module versions.

dask.__version__
'0.15.2+25.ga1a75a7'

numpy.__version__
'1.13.1'

partd.__version__
'0.3.8'

pandas.__version__
'0.20.1'

Here is the code that shows the problem.

from dask.distributed import Client
import dask.dataframe as dd
import pandas as pd

client = Client("localhost:9898") 

ddf1 = dd.read_parquet("keys_133m.parq")
ddf1.columns
#Index(['KEY'], dtype='object')
ddf1.dtypes
#KEY    int64

ddf2 = dd.read_parquet("key_ids_133m.parq")
ddf2.columns
#Index(['KEY', 'ID'], dtype='object')
ddf2.dtypes
#KEY    int64
#ID     int64

pandas inner merge result has about 500k rows

df = ddf1.compute().merge(ddf2.compute(),how='inner',on='KEY')
df.shape
#(505847, 2)

dask inner merge has only about 272k rows

ddf = ddf1.merge(ddf2,how='inner',on='KEY')
len(ddf)
#272051

One example of missing row in ddf is KEY==135337982:

#dask merge result does not contain the row
ddf.loc[ddf.KEY==135337982].compute()
#Empty DataFrame
#Columns: [KEY, ID]
#Index: []

#pandas merge result contains the row
df.loc[df.KEY==135337982]
#              KEY         ID
#122842  135337982  149107799

Single-partition parquet works

#use nlargest to write to a single-partition parquet
ddf1.nlargest(n=505850,columns='KEY').to_parquet('keys_one_partition.parq')
ddf2.nlargest(n=1960255,columns='KEY').to_parquet('key_ids_one_partition.parq')

ddf1_one = dd.read_parquet('keys_one_partition.parq')
ddf2_one = dd.read_parquet('key_ids_one_partition.parq')

ddf_one = ddf1_one.merge(ddf2_one,how='inner',on='KEY')
len(ddf_one)
#505847

df_one = ddf1_one.compute().merge(ddf2_one.compute(),how='inner',on='KEY')
df_one.shape
#(505847, 2)

Activity

  1. added a commit that references this issue on Sep 29, 2017
  2. martindurant commented on Sep 29, 2017

    @martindurant
    Member

    Interestingly, dask returns the correct value on the threaded scheduler.

  3. ningsean commented on Oct 2, 2017

    @ningsean
    Author

    Hmmm, I get incorrect result with different schedulers, even with a single-threaded scheduler (get=dask.get). The only way that I can get the correct result is to do the merge with pandas dataframes.

    To summarize it again, here are the different merges I have tried, and only pandas merge worked.

    ddf = ddf1.merge(ddf2,how='inner',on='KEY')
    
    #single-threaded scheduler returns incorrect result
    ddf.loc[ddf.KEY==135337982].compute(get=dask.get)
    #Empty DataFrame
    #Columns: [KEY, ID]
    #Index: []
    
    #multi-process scheduler returns incorrect result
    #ddf.loc[ddf.KEY==135337982].compute(get=dask.multiprocessing.get)
    #Empty DataFrame
    #Columns: [KEY, ID]
    #Index: []
    
    #multi-threaded scheduler returns incorrect result
    ddf.loc[ddf.KEY==135337982].compute(get=dask.threaded.get)
    #Empty DataFrame
    #Columns: [KEY, ID]
    #Index: []
    
    #distributed scheduler returns incorrect result
    ddf.loc[ddf.KEY==135337982].compute(get=client.get)
    #Empty DataFrame
    #Columns: [KEY, ID]
    #Index: []
    
    
    #pandas dataframe merge returns correct result
    df = ddf1.compute().merge(ddf2.compute(),how='inner',on='KEY')
    df.loc[df.KEY==135337982]
    #              KEY         ID
    #122842  135337982  149107799
    
    
  4. ningsean commented on Oct 4, 2017

    @ningsean
    Author

    Strange, if I init a distributed client, the single-threaded scheduler gives the wrong result.

    from dask.distributed import Client
    import dask.dataframe as dd
    import dask
    client = Client("localhost:9898")
    ddf1 = dd.read_parquet("keys_133m.parq")
    ddf2 = dd.read_parquet("key_ids_133m.parq")
    ddf = ddf1.merge(ddf2,how='inner',on='KEY')
    df = ddf.compute(get=dask.get)
    print(df.loc[df.KEY==135337982])
    
    #result:
    #Empty DataFrame
    #Columns: [KEY, ID]
    #Index: []
    

    But if I don't init the client at all, the single-threaded scheduler returns the correct result (and a few times slower):

    from dask.distributed import Client
    import dask.dataframe as dd
    import dask
    ############# do not init the distributed client ############### client = Client("localhost:9898")
    ddf1 = dd.read_parquet("keys_133m.parq")
    ddf2 = dd.read_parquet("key_ids_133m.parq")
    ddf = ddf1.merge(ddf2,how='inner',on='KEY')
    df = ddf.compute(get=dask.get)
    print(df.loc[df.KEY==135337982])
    
    #result:
    #          KEY         ID
    #62  135337982  149107799
    

    In both cases, I checked the status bokeh app and scheduler and worker log, there didn't seem to be any activities with the local cluster.

    Another that I noticed is that, with the client init'ed, the ddf.dask has 28044 tasks with len(ddf.dask), while without it the ddf.dask as only 4779 tasks.

    So, the results seem to indicate that even though not used, a client to the local cluster affects the task graph, the correctness of the merge results as well as the speed. This is totally unexpected to me.

  5. mrocklin commented on Oct 4, 2017

    @mrocklin
    Member

    When dask.distributed starts up it changes the global shuffle mechanism from "disk" to "tasks". You can also control this with the dask.set_options(shuffle='tasks') context manager. It might be useful to try that out on your problem to see if you can recreate your issue without starting a client.

    with dask.set_options(shuffle='tasks'):
        ...

    If so then that will help to isolate the error.

  6. ningsean commented on Oct 4, 2017

    @ningsean
    Author

    Yes, dask.set_options(shuffle='tasks') has the same effect as starting a client. Now the error happens even without starting the client.

  7. ningsean commented on Nov 8, 2017

    @ningsean
    Author

    Interesting. If I make the "KEY" column as the index for both dask dataframes, then use join, the result would come out correct. It is slightly slower than doing merge directly due to the overhead of set_index.

    If I persist the indexed dataframes and the non-indexed dataframes, the join on the indexed would be much faster than the merge on the non-indexed, with the additional benefit of the result being correct!

    I tried to debug and pin-point the issue with the merge but haven't been able to get anywhere.

    client = Client('localhost:9898')
    keys = ddf1.set_index('KEY', drop=False) 
    keyids = ddf2.set_index('KEY', drop=False)
    ddf = keys.join(keyids, rsuffix='_y') 
    ddf.loc[ddf.KEY==135337982].compute()
    
    # result:
    #                 KEY      KEY_y         ID
    #KEY                                       
    #135337982  135337982  135337982  149107799
    
    len(ddf)
    
    # result:
    # 505850
    
    
  8. hussainsultan commented on Dec 11, 2017

    @hussainsultan
    Contributor

    bump - this is something that i am seeing as well.

  9. brendancol commented on Dec 11, 2017

    @brendancol

    @mrocklin @jcrist Anybody actively working on this one? Just ran into this too.

  10. mrocklin commented on Dec 12, 2017

    @mrocklin
    Member

    @brendancol I'm not working on it currently, nor is anyone that I know of. Generally people write on an issue when they start work. I agree that this is important. I don't personally plan to work on it any time in the next week or two. If anyone wants to take it on that would be great.

  11. hussainsultan commented on Dec 12, 2017

    @hussainsultan
    Contributor

    @mrocklin i am working on creating a reproducible example. I haven't been able to reproduce this issue outside of the production environment though. Will update this thread.

  12. dennis-ec commented on Feb 21, 2018

    @dennis-ec

    bump - Any news on this? I also find this issue very important because merge is a very common operation and forced to set the index first slows me down quite a bit.

  13. mrocklin commented on Feb 21, 2018

    @mrocklin
    Member

    It looks like this is waiting on a reproducible example that demonstrates the failure in a simple way. Perhaps you are able to provide such an example @dennis-ec ?

  14. hussainsultan commented on Feb 21, 2018

    @hussainsultan
    Contributor

    I haven't been able to reproduce this using synthetic data. However, the issue persists with the parquet files that @ningsean uploaded.

    import requests
    import dask
    import dask.dataframe as dd
    import zipfile
    r = requests.get("https://github.com/ningsean/dask_merge_issue/raw/master/data.zip" )
    with open('data.zip', 'wb') as f:
        f.write(r.content)
    
    zipfile.ZipFile('data.zip', 'r').extractall('.')
    
    dd_right = dd.read_parquet('keys_133m.parq/', index=False)
    dd_left = dd.read_parquet('key_ids_133m.parq/', columns = ['KEY', 'ID'], index=False)
    with dask.set_options(shuffle='tasks'):
        dd_merged = dd_left.merge(dd_right, how='inner', on='KEY')
        df_computed = dd_merged.compute()
    
    computed_from_pandas = dd_left.compute().merge(dd_right.compute(), how='inner', on='KEY')
    assert len(df_computed) == len(computed_from_pandas)

    Output

    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-41-d28889968375> in <module>()
    ----> 1 assert len(df_computed) == len(computed_from_pandas)
    
    AssertionError: 

    Hope this is helpful.

    In [7]: dask.__version__
    Out[7]: '0.17.0'
    
    In [8]: pd.__version__
    Out[8]: '0.22.0'
    
  15. mrocklin commented on Feb 21, 2018

    @mrocklin
    Member

    Unfortunately we won't be able to include these files in tests. We really do need something that can be generated automatically.

  16. hussainsultan commented on Feb 21, 2018

    @hussainsultan
    Contributor

    yea i will play around some more to reproduce this with synthetic data

  17. hussainsultan commented on Feb 23, 2018

    @hussainsultan
    Contributor

    This is a really weird one. It appears that the task shuffle breaks down when number of tasks increase to a big number. here is a reproducible example (perhaps not minimal):

    In [14]: import numpy as np
        ...: import pandas as pd
        ...: import dask
        ...: import dask.dataframe as dd
        ...: large = pd.DataFrame({'KEY': np.arange(0,50000)})  
        ...: small = pd.DataFrame({'KEY': np.arange(2500,50000)})
        ...: dd_large = dd.from_pandas(large,npartitions=257)
        ...: dd_small = dd.from_pandas(small, npartitions=3)
        ...: with dask.set_options(shuffle='tasks'):
        ...:     dd_merged = dd_large.merge(dd_small, how='inner', on='KEY')
        ...:     result = dd_merged.compute()
        ...:     
        ...: expected = large.merge(small, how='inner', on='KEY')
        ...: pd.testing.assert_frame_equal(result.sort_values('KEY').reset_index(drop=True), expected)
        ...: 
    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-14-eca716bc6f44> in <module>()
         12 
         13 expected = large.merge(small, how='inner', on='KEY')
    ---> 14 pd.testing.assert_frame_equal(result.sort_values('KEY').reset_index(drop=True), expected)
    
    ~/miniconda3/envs/dask_join_dev/lib/python3.6/site-packages/pandas/util/testing.py in assert_frame_equal(left, right, check_dtype, check_index_type, check_column_type, check_frame_type, check_less_precise, check_names, by_blocks, check_exact, check_datetimelike_compat, check_categorical, check_like, obj)
       1351                             'DataFrame shape mismatch',
       1352                             '{shape!r}'.format(shape=left.shape),
    -> 1353                             '{shape!r}'.format(shape=right.shape))
       1354 
       1355     if check_like:
    
    ~/miniconda3/envs/dask_join_dev/lib/python3.6/site-packages/pandas/util/testing.py in raise_assert_detail(obj, message, left, right, diff)
       1091         msg += "\n[diff]: {diff}".format(diff=diff)
       1092 
    -> 1093     raise AssertionError(msg)
       1094 
       1095 
    
    AssertionError: DataFrame are different
    
    DataFrame shape mismatch
    [left]:  (47322, 1)
    [right]: (47500, 1)

    however, the following works just fine:

    In [12]: import numpy as np
        ...: import pandas as pd
        ...: import dask
        ...: import dask.dataframe as dd
        ...: large = pd.DataFrame({'KEY': np.arange(0,50000)})  
        ...: small = pd.DataFrame({'KEY': np.arange(2500,50000)})
        ...: dd_large = dd.from_pandas(large,npartitions=256)
        ...: dd_small = dd.from_pandas(small, npartitions=3)
        ...: with dask.set_options(shuffle='tasks'):
        ...:     dd_merged = dd_large.merge(dd_small, how='inner', on='KEY')
        ...:     result = dd_merged.compute()
        ...:     
        ...: expected = large.merge(small, how='inner', on='KEY')
        ...: 
    
    In [13]: pd.testing.assert_frame_equal(result.sort_values('KEY').reset_index(drop=True), expected)
  18. hussainsultan commented on Feb 23, 2018

    @hussainsultan
    Contributor
  19. mrocklin commented on Feb 23, 2018

    @mrocklin
    Member

    Thank you for this @hussainsultan . Most of the work to resolve this problem is probably in the reproducible example that you just made. I really appreciate your effort here.

    I will try to take a look at this later this morning.

  20. added 2 commits that reference this issue on Feb 23, 2018
    8588a27
    366b143
  21. mrocklin commented on Feb 23, 2018

    @mrocklin
    Member
  22. added a commit that references this issue on Feb 23, 2018
    3aad810
  23. hussainsultan commented on Feb 23, 2018

    @hussainsultan
    Contributor

    Thank you for the fix! This also works with the original data that @ningsean posted.

  24. added a commit that references this issue on Feb 24, 2018
    3ad05d6
  25. ningsean commented on Feb 24, 2018

    @ningsean
    Author
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions