Skip to content

GH-34884: [Python]: Support pickling pyarrow.dataset PartitioningFactory objects - #36550

Merged
jorisvandenbossche merged 2 commits into
apache:mainfrom
jorisvandenbossche:gh-gh-34884-factories
Jul 10, 2023
Merged

jorisvandenbossche merged 2 commits into
apache:mainfrom
jorisvandenbossche:gh-gh-34884-factories

Conversation

@jorisvandenbossche

@jorisvandenbossche jorisvandenbossche commented Jul 7, 2023 •

Copy link
Copy Markdown
Member

Rationale for this change

#36462 already added support for pickling Partitioning objects, but not yet the PartitioningFactory objects.

The problem for PartitioningFactory is that we currently don't really expose the full class hierarchy in python, just the base class PartitioningFactory. We also don't expose creating those factory objects, except through the discover methods of the Partitioning classes.
I think it would be nice to keep this minimal binding, but that means if we want to make them serializable with pickle, we need another way to do that (and if we don't want to add custom code for serialization on the C++ side).

In this PR, I went for the route of essentially storing the constructor (the discover static method) and the arguments that were passed to the constructor, on the factory object, so we can use this info for pickling. Not the nicest code, but the simplest solution I could think of.

Are these changes tested?

Yes

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As long as this isn't actually running discovery I don't see a problem. I think I see what you mean about an awkward pattern but it should work. Seems like the biggest downside is that these _constructor_filename_partitioning_factory become visible symbols that users should ignore?



def _constructor_hive_partitioning_factory(*args):
return HivePartitioning.discover(*args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this method actually run discovery or does it just create the factory?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It just creates the factory

if c_factory.get() == nullptr:
return None
return PartitioningFactory.wrap(c_factory)
return PartitioningFactory.wrap(c_factory, None, None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't this mean that this object can't be pickled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct.

But this is only if you first create an options object with a factory, and then decide the pickle the factory accessed from the options object instead of the factory you created to start with:

partitioning_factory = ds.HivePartitioning.discover(..)
options = ds.FileSystemFactoryOptions(partitioning=partitioning_factory)
pickle.dumps(options.partitioning_factory)  # instead of pickle.dumps(partitioning_factory)

So in practice, if you run into this, you can always pickle the original factory object you created (as there is no other way to get such an object than creating it yourself, i.e. the options object only has it when you passed it manually).

We could support pickling those options-accessed factory objects (and ideally that would also be the case, for consistency / avoiding confusion), but the current code only stores the C++ factory object on the options class and recreates the python factory object from the C++ object, and then we don't know anymore how this C++ object was created. We could add some workaround to also store the python factory object, but I am not sure this is worth the effort, given that as a user you can always pickle the original object (explanation above)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I agree this is probably not needed. I don't think many users create FileSystemFactoryOptions (I don't think I even knew it existed :)

if c_factory.get() == nullptr:
return None
return PartitioningFactory.wrap(c_factory)
return PartitioningFactory.wrap(c_factory, None, None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same question as above.

@jorisvandenbossche

Copy link
Copy Markdown
Member Author

Seems like the biggest downside is that these _constructor_filename_partitioning_factory become visible symbols that users should ignore?

I prefixed them with an underscore, so by default those shouldn't be visible (eg with tab completion)

@westonpace

Copy link
Copy Markdown
Member

I just noticed that there is a minor lint failure in the CI. Then I think it's good to go.

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit a63ead7.

There were 3 benchmark results indicating a performance regression:

The full Conbench report has more details.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Dataset PartitioningFactory cannot be serialized in Python

2 participants