Hello,
In the spirit of trying to make wide Parquet files faster to read when
projecting onto a couple columns, I have been experimenting with lazy
parsing of Thrift ColumnChunk structures.
The idea is that, when parsing the Parquet footer, ColumnChunk parsing
is skipped and a placeholder is recorded that points at the range of
Thrift bytes that encode the ColumnChunk. Then, when the ColumnChunk
is actually required, the range of bytes is decoded on the fly and the
decoded ColumnChunk is stored instead.
Thrift being a stack-based bytecode format, this is not trivial. Thrift
C++ comes with a generic skip() function that is too slow. However, a
hand-written skipping function (still generic but carefully tuned)
turns out to give good results.
You can find a PoC branch on Arrow C++ here:
https://github.com/apache/arrow/compare/main...pitrou:thrift-exp
Here are some benchmark results:
* before: https://gist.github.com/pitrou/772001440bdb3ed38279c075e34e59eb
* after: https://gist.github.com/pitrou/86e4e6ce89810252f9943e31722591b0
I got an AI to distill those results as a Markdown table, hopefully
the numbers are right 🙃:
https://gist.github.com/pitrou/6e668f747edc50a5eec63841c8a6b5d3
The results should be almost self-explanatory: `ReadFileMetadata`
just parses the Parquet footer while `ReadFileAndColumnMetadata`
also accesses a subset of the column chunk metadata, triggering
lazy parsing.
Bottom line:
1. Lazy parsing can yield up to a 9x speedup with 100,000 column
chunks in the file.
2. Selecting (== decoding the metadata for) a moderate amount of
columns ("sel=5%") still yields interesting speedups, up to 5x.
3. Selecting all columns, on the other hand, is significantly slower
(up to 30%) because the initial Thrift skipping adds to the total
footer parsing time.
Profiling the most extreme benchmarking cases gives interesting insights:
1. For `ReadFileMetadata/num_columns:1000/num_row_groups:100`, Thrift
decoding and skipping accounts for 87% of the benchmark time; this
implies that Thrift is really the bottleneck when there is a large
number of row groups *and* columns;
2. However, for `ReadFileMetadata/num_columns:1000/num_row_groups:1`,
Thrift deserialization is only 17% of the benchmark time - the rest
is Parquet C++ schema construction and destruction. Therefore,
more work may be warranted in the Parquet C++ codebase itself in
order to reduce inefficiencies, regardless of whether Thrift is
involved at all.
A couple technical details about the PoC:
* I had to modify the Thrift-generated C++ files, so this is definitely
not maintainable. A clean implementation probably requires a custom
C++ Thrift parser (which would also probably be faster).
* The core Thrift skipping function is non-recursive and allows for
aggressive inlining (*). On my machine (a AMD Zen 2 CPU), it seems to
skip at around 1.5 GB/s.
(*) See `SkipListOfStructCompact` in `parquet_types.tcc`.
This shouldn't discourage more radical Parquet footer experiments, but
may provide interesting optimization opportunities in the shorter term,
including with existing Parquet datasets.
Regards
Antoine.