arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[C++][Parquet] Default to V2 data pages

Open pitrou opened this issue 1 month ago • 10 comments

Describe the enhancement requested

V2 data pages are very old and probably supported by every implementation post-2020. Given they allow selective compression of data pages and other useful stuff (such as num_nulls), we should probably write them by default.

Component(s)

C++, Parquet

pitrou avatar Oct 26 '26 16:10 pitrou

@wgtmac @HuaHuaY Thoughts?

pitrou avatar Oct 26 '26 16:10 pitrou

I just found https://stackoverflow.com/questions/77654784/what-is-the-difference-between-data-page-version-1-0-and-2-0-in-parquet-files which is answered by @emkornfield :)

We have enabled all supported encodings in our production environment for years but we still default data page to v1 for better compression ratio. I don't know if data page v2 has any potential bug but overall I'm in favor of switching the default to v2.

wgtmac avatar Oct 27 '26 14:10 wgtmac

we still default data page to v1 for better compression ratio

Can you provide some more details on this @wgtmac, why would v1 give better compression?

adamreeve avatar Oct 28 '26 08:10 adamreeve

It was several years ago when we ran TPC-H/TPC-DS/SSB benchmarks. If I remember correctly, v1 data pages have better compression ratio because of better compression on definition levels as they are all flat schemas.

wgtmac avatar Oct 28 '26 08:10 wgtmac

It'd be interesting to know by how much.

pitrou avatar Oct 28 '26 14:10 pitrou

I ran benchmarks comparing Parquet Data Page V1 vs V2 across TPC-H, SSB, and TPC-DS at SF 1.0 (PyArrow 26.0).

TL;DR: The file size penalty of V2 ranges from 0.0% on clean flat schemas to ~1.2% on wide tables with scattered nulls, while V2 reads 12% to 35% faster.

Benchmark (SF 1.0) Rows Codec V1 Size V2 Size Delta (%)
TPC-H lineitem (0 nulls) 6.0M Snappy / ZSTD 208 MB / 162 MB 208 MB / 162 MB +0.00% / -0.02% (V2 smaller)
SSB lineorder (clean) 6.0M Snappy / ZSTD 179 MB / 143 MB 179 MB / 143 MB +0.00% / -0.01% (V2 smaller)
SSB lineorder (2% nulls) 6.0M Snappy / ZSTD 180 MB / 144 MB 180 MB / 145 MB +0.24% / +0.33%
TPC-DS store_sales (sparse nulls) 2.88M Snappy / ZSTD 121 MB / 105 MB 123 MB / 107 MB +1.22% / +1.24%
TPC-DS catalog_sales 1.44M Snappy / ZSTD 101 MB / 86 MB 101 MB / 86 MB +0.16% / +0.13%

Why the small difference exists:

  • Per the Parquet specification, V2 stores definition/repetition levels uncompressed (only values are compressed).
  • In V1, definition levels are compressed together with values. For sparse/random nulls, bit-packed levels have low entropy, allowing Snappy/ZSTD to compress them by 1.2x to 50x in V1. For clean non-null schemas, RLE levels take only 2-3 bytes anyway, so V1 and V2 are practically identical.

Performance trade-off:

  • Read Throughput: In store_sales, V2 was ~12.1% faster on full-table scans and ~35.9% faster on single nullable column reads (ss_promo_sk) because uncompressed levels bypass decompression via memcpy.
  • Readers also benefit from explicit num_nulls in DataPageHeaderV2 to short-circuit null handling.

Defaulting to V2 seems well worth the minor <= 1.2% size trade-off.

Environment: Python 3.12, PyArrow 26.0.1, DuckDB 2.0.0 (Windows / x86_64)

Awesome06 avatar Nov 08 '26 09:11 Awesome06

@Awesome06 Thank you for the numbers. If you used AI to generate them, did you double-check?

pitrou avatar Nov 08 '26 12:11 pitrou

@pitrou Yes. These are measured on disk using PyArrow 26.0 and DuckDB 2.0 (not AI-generated numbers).

The datasets were generated via DuckDB's TPC-H/TPC-DS engines (dbgen/dsdgen) at SF 1.0, written to parquet files via pq.write_table() with data_page_version="1.0" and "2.0", and measured via os.path.getsize().

SSB lineorder (2% nulls) is not reproduced via the script to ensure deterministic results.

Python reproduction script (requires: pip install pyarrow duckdb)
import os
import tempfile
import duckdb
import pyarrow.parquet as pq

def compare(table, name, codecs=("snappy", "zstd")):
    results = []
    with tempfile.TemporaryDirectory() as tmpdir:
        for codec in codecs:
            p_v1 = os.path.join(tmpdir, f"{name}_{codec}_v1.parquet")
            p_v2 = os.path.join(tmpdir, f"{name}_{codec}_v2.parquet")
            
            pq.write_table(table, p_v1, data_page_version="1.0", compression=codec)
            pq.write_table(table, p_v2, data_page_version="2.0", compression=codec)
            
            s1 = os.path.getsize(p_v1)
            s2 = os.path.getsize(p_v2)
            delta = s2 - s1
            pct = (delta / s1) * 100.0
            results.append((codec, s1, s2, delta, pct))
    return results

def main():
    con = duckdb.connect()
    
    print("Generating TPC-H SF 1.0 (lineitem)...")
    con.execute("CALL dbgen(sf=1.0)")
    tbl_lineitem = con.sql("SELECT * FROM lineitem").to_arrow_table()
    res_lineitem = compare(tbl_lineitem, "tpch_lineitem")
    
    print("Generating SSB SF 1.0 (lineorder)...")
    con.execute("""
    CREATE TABLE lineorder AS
    SELECT
        l_orderkey AS lo_orderkey, l_linenumber AS lo_linenumber, o_custkey AS lo_custkey,
        l_partkey AS lo_partkey, l_suppkey AS lo_suppkey, o_orderdate AS lo_orderdate,
        o_orderpriority AS lo_orderpriority, o_shippriority AS lo_shippriority,
        l_quantity AS lo_quantity, l_extendedprice AS lo_extendedprice,
        o_totalprice AS lo_ordtotalprice, l_discount AS lo_discount,
        (l_extendedprice * (1 - l_discount))::INT64 AS lo_revenue,
        (l_extendedprice * (1 - l_tax))::INT64 AS lo_supplycost,
        l_tax AS lo_tax, l_commitdate AS lo_commitdate, l_shipmode AS lo_shipmode
    FROM lineitem JOIN orders ON l_orderkey = o_orderkey;
    """)
    tbl_lineorder = con.sql("SELECT * FROM lineorder").to_arrow_table()
    res_lineorder = compare(tbl_lineorder, "ssb_lineorder")
    
    print("Generating TPC-DS SF 1.0 (store_sales, catalog_sales)...")
    con_ds = duckdb.connect()
    con_ds.execute("CALL dsdgen(sf=1.0)")
    tbl_store_sales = con_ds.sql("SELECT * FROM store_sales").to_arrow_table()
    tbl_catalog_sales = con_ds.sql("SELECT * FROM catalog_sales").to_arrow_table()
    res_store_sales = compare(tbl_store_sales, "tpcds_store_sales")
    res_catalog_sales = compare(tbl_catalog_sales, "tpcds_catalog_sales")
    
    print(f"\n{'Benchmark (SF 1.0)':<25} {'Codec':<8} {'V1 Bytes':<14} {'V2 Bytes':<14} {'Delta Bytes':<12} {'Delta (%)':<10}")
    print("-" * 85)
    for name, res_list in [
        ("TPC-H lineitem", res_lineitem),
        ("SSB lineorder", res_lineorder),
        ("TPC-DS store_sales", res_store_sales),
        ("TPC-DS catalog_sales", res_catalog_sales),
    ]:
        for codec, s1, s2, delta, pct in res_list:
            print(f"{name:<25} {codec:<8} {s1:<14,d} {s2:<14,d} {delta:<+12,d} {pct:>+8.3f}%")

if __name__ == "__main__":
    main()

Awesome06 avatar Nov 08 '26 12:11 Awesome06

Ok, so I think we can just switch to V2 by default.

pitrou avatar Nov 09 '26 10:11 pitrou

I'll go ahead and open a PR switching the default to V2 across C++ and Python.

Awesome06 avatar Nov 09 '26 12:11 Awesome06