Skip to content

Fix for auto-categoricals in fastparquet - #2113

Merged
martindurant merged 3 commits into
dask:masterfrom
martindurant:parquet_autocat
Mar 29, 2017
Merged

martindurant merged 3 commits into
dask:masterfrom
martindurant:parquet_autocat

Conversation

@martindurant

Copy link
Copy Markdown
Member

@mrocklin

Copy link
Copy Markdown
Member

Looks fine to me. This makes me curious what Parquet's metadata solution is. What kind of data can we put in there? Are there conventions that other projects have adopted? Is this in the metadata file or in each parquet file?

@martindurant

Copy link
Copy Markdown
Member Author

There is key-value area of the global metadata for arbitrary information, and also key-value areas for each column chunk of each row group. All of this is stored in the global _metadata file, if using multiple files, and replicated to every data file (but on load we only read it once).
In this case, I make a key fastparquet.cats with a value of JSON encoded {'catfield1': num_cats, ...}.

@mrocklin

mrocklin commented Mar 24, 2017 via email

Copy link
Copy Markdown
Member

@martindurant

Copy link
Copy Markdown
Member Author

I don't mind, but this is not meant for public consumption, and "cats" is a frequent variable name for the same purpose in the code.

Was there anything else you thought should be stored in a similar way?

@mrocklin

Copy link
Copy Markdown
Member

We call it cats internally, but we're putting this into data that other projects may be asked to understand. I think it is better to choose names that follow more standard conventions if possible. Short names should probably be discouraged. Ideally we would be able to find a name that other projects might say "oh hey, that is a good idea" and then choose to adopt on their own. Adding fastparquet to the name might preclude that.

@mrocklin

Copy link
Copy Markdown
Member

Perhaps ask on a Parquet mailing list? It might be wise to ask somewhere where the broader community can weigh in on the right conventions here.

cc @wesm, presumably you have run into this problem. Thoughts?

@wesm

wesm commented Mar 24, 2017

Copy link
Copy Markdown
Contributor

There aren't a lot of conventions I'm aware of surrounding the key-value metadata, but can definitely ask on the Parquet mailing list.

I will say surrounding this that, semantically, dictionary-encoded data in Parquet is not the same as pandas.Categorical, so other systems in general will decode such data as though it were PLAIN encoded. Because of the RLE-encoding benefits of the dictionary encoding, most implementations have dictionary encoding turned on for all types and fail over to plain encoding mid-stream when the dictionaries exceed a certain size threshold.

@mrocklin

Copy link
Copy Markdown
Member

@wesm presumably Arrow will also have this problem, where you'll have to decide if you want to round-trip the fact that a column is categorical. Do you have any intention of propagating this through the parquet-cpp bindings?

@wesm

wesm commented Mar 25, 2017

Copy link
Copy Markdown
Contributor

Yes, we will eventually need to store metadata to indicate when a particular Parquet logical type is used to represent more than one Arrow type (e.g. Arrow supports time as seconds through nanoseconds, but Parquet only has millis and micros). I figure as long as the stored data isn't "wrong", i.e. the decoded data on other platforms won't be misinterpreted, then it's okay.

Since we recently augmented the date and time types in Arrow I suspect in the next month or two we'll address faithful roundtripping of all these types to Parquet.

@mrocklin

Copy link
Copy Markdown
Member

Do you have any intention of roundtripping the fact that a pandas column is a categorical to parquet? If so then do you want to establish a shared convention now?

@martindurant

Copy link
Copy Markdown
Member Author

Note that we also want to keep track of how many labels in total the categorical can have, so that we need only assign an array of codes big enough to the values.

@wesm

wesm commented Mar 26, 2017

Copy link
Copy Markdown
Contributor

Yep, pandas.Categorical becomes arrow::DictionaryArray, so we will want to persist that metadata -- would be good to standardize the "pandas metadata". We'll want to be able to hash back to Dictionary/Categorical in the event that the Parquet reader failed over to PLAIN encoding -- cc @xhochy

I created PARQUET-924 to capture this. There is also ARROW-462 which I need to implement in the next couple months

@xhochy

xhochy commented Mar 26, 2017

Copy link
Copy Markdown
Contributor

After thinking a bit about this, we should move this discussion to the parquet ML or our bi-weekly hangouts. Off the top of my head I could think of the following possibilities:

  1. Add an additional flag / metadata to the schema in https://github.com/apache/parquet-format/blob/4bddbadf79e20a32152076fbedae0c3ce77fb531/src/main/thrift/parquet.thrift#L220
  2. Add a new ConvertedType UTF8_CATEGORICAL
  3. Add a new type for categoricals (this would equal the implementation in Arrow)

Number 1 is the only option that would work well with old readers, 2+3 would produce files that cannot be read correctly by older implementations.

As we might also want to support R's factor, we should not abuse the metadata but augment the actual format to support this. Currently old readers will benefit from neither implementation, so adding a backward-compatible format change should be the best option.

@martindurant

Copy link
Copy Markdown
Member Author

The notes above are interesting - please let me know if this discussion has moved elsewhere.
From the point of view of this PR it is moot, however, how fastparquet decides which columns are categorical if passed the now-default categories=None is just an implementation detail from dask's PoV.

@wesm

wesm commented Mar 27, 2017

Copy link
Copy Markdown
Contributor

Per @xhochy's note, any discussions about the format will happen on the Parquet mailing list or in the biweekly syncs (with meeting notes sent to the ML), please feel free to join us.

I'd like for dask to be able to use parquet-cpp+pyarrow for Parquet reads at some point, so it would be good to not have divergent semantics.

@mrocklin

Copy link
Copy Markdown
Member

I'd like for dask to be able to use parquet-cpp+pyarrow for Parquet reads at some point, so it would be good to not have divergent semantics.

+1

@martindurant martindurant mentioned this pull request Mar 28, 2017
Comment thread dask/dataframe/io/tests/test_parquet.py Outdated
(pd.DataFrame({'-': [3., 2., None]}), {}, {}),
(pd.DataFrame({'.': [3., 2., None]}), {}, {}),
# (pd.DataFrame({'.': [3., 2., None]}), {}, {}),
# period is not allowed in a field name

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.

This is an accident of implementation in fastparquet and should be fixed.

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.

@mrocklin

Copy link
Copy Markdown
Member

+1 from me

@martindurant
martindurant merged commit 2821ff1 into dask:master Mar 29, 2017
@martindurant
martindurant deleted the parquet_autocat branch March 29, 2017 00:39
@xhochy

xhochy commented Apr 6, 2017

Copy link
Copy Markdown
Contributor

@sinhrks sinhrks added this to the 0.14.2 milestone May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants