Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast Parquet row count in Spark

The Parquet files contain a per-block row count field. Spark seems to read it at some point (SpecificParquetRecordReaderBase.java#L151).

I tried this in spark-shell:

sqlContext.read.load("x.parquet").count

And Spark ran two stages, showing various aggregation steps in the DAG. I figure this means it reads through the file normally instead of using the row counts. (I could be wrong.)

The question is: Is Spark already using the row count fields when I run count? Is there another API to use those fields? Is relying on those fields a bad idea for some reason?

like image 212
Daniel Darabos Avatar asked Nov 16 '16 10:11

Daniel Darabos


People also ask

Why Parquet is best fit for spark?

Parquet has higher execution speed compared to other standard file formats like Avro,JSON etc and it also consumes less disk space in compare to AVRO and JSON.

Can you use spark SQL to read a Parquet data?

Spark SQL provides support for both reading and writing Parquet files that automatically preserves the schema of the original data. When reading Parquet files, all columns are automatically converted to be nullable for compatibility reasons.


1 Answers

That is correct, Spark is already using the rowcounts field when you are running count.

Diving into the details a bit, the SpecificParquetRecordReaderBase.java references the Improve Parquet scan performance when using flat schemas commit as part of [SPARK-11787] Speed up parquet reader for flat schemas. Note, this commit was included as part of the Spark 1.6 branch.

If the query is a row count, it pretty much works the way you described it (i.e. reading the metadata). If the predicates are fully satisfied by the min/max values, that should work as well though that is not as fully verified. It's not a bad idea to use those Parquet fields but as implied in the previous statement, the key issue is to ensure that the predicate filtering matches the metadata so you are doing an accurate count.

To help understand why there are two stages, here's the DAG created when running the count() statement.

enter image description here

When digging into the two stages, notice that the first one (Stage 25) is running the file scan while the second stage (Stage 26) runs the shuffle for the count.

enter image description here enter image description here

Thanks to Nong Li (the author of the SpecificParquetRecordReaderBase.java commit) for validating!

 

Updated

To provide additional context on the bridge between Dataset.count and Parquet, the flow of the internal logic surrounding this is:

  • Spark does not read any Parquet columns to calculate the count
  • Passing of the Parquet schema to the VectorizedParquetRecordReader is actually an empty Parquet message
  • Computing the count using the metadata stored in the Parquet file footers. involves the wrapping of the above within an iterator that returns an InternalRow per InternalRow.scala.

To work with the Parquet File format, internally, Apache Spark wraps the logic with an iterator that returns an InternalRow; more information can be found in InternalRow.scala. Ultimately, the count() aggregate function interacts with the underlying Parquet data source using this iterator. BTW, this is true for both vectorized and non-vectorized Parquet reader.

Therefore, to bridge the Dataset.count() with the Parquet reader, the path is:

  • The Dataset.count() call is planned into an aggregate operator with a single count() aggregate function.
  • Java code is generated at planning time for the aggregate operator as well as the count() aggregate function.
  • The generated Java code interacts with the underlying data source ParquetFileFormat with an RecordReaderIterator, which is used internally by the Spark data source API.

For more information, please refer to Parquet Count Metadata Explanation.

like image 145
Denny Lee Avatar answered Sep 28 '22 07:09

Denny Lee