# Parquet The standard Python build includes the standalone Parquet reader and writer. In C++, link `Quixote::parquet` and include ``. ## Read, query and write Run `examples/parquet_roundtrip.py` to filter two files together, append new rows from memory with `union_all`, and write the combined rows to Parquet. ``` """Create, query and write Parquet with an installed Quixote package.""" from pathlib import Path from tempfile import TemporaryDirectory import pyarrow as pa import quixote as qe def run(): data = pa.record_batch({"id": [1, 2, 3], "value": [10, None, 30]}) with TemporaryDirectory() as directory: parts = [Path(directory) / f"part-{i}.parquet" for i in range(2)] output_path = Path(directory) / "output.parquet" with qe.Runtime(cpu_threads=2, io_threads=2) as runtime: with qe.Session(runtime) as session: for part, rows in zip(parts, [data.slice(0, 2), data.slice(2)], strict=True): session.from_arrow(rows).sink_parquet(part) selected = session.scan_parquet(parts).filter( qe.col("value").is_not_null() ).select("id", "value") arrivals = session.from_arrow(pa.record_batch({"id": [4], "value": [40]})) selected.union_all(arrivals).sink_parquet(output_path) result = session.scan_parquet(output_path).collect() table = pa.table(result) assert table.to_pydict() == {"id": [1, 3, 4], "value": [10, 30, 40]} return table if __name__ == "__main__": print(run()) ``` `session.scan_parquet(path, columns=["x", "y"])` reads the footer and exposes those fields in that order. Include fields used by filters as well as outputs. Omit `columns` to select every field. Use `columns=[]` to count rows without reading value columns. Pass a nonempty iterable of paths to scan several files in the order given. Each path can be a string or a path object such as `pathlib.Path`. Quixote reads each footer when you build the query and schedules row groups across files when you run it. Selected fields must have the same names and types. If any file allows nulls in a field, the result allows nulls there. Keep the files unchanged while their queries exist. `query.sink_parquet(path)` writes row groups without collecting the whole result. The defaults are Zstd compression, 131072 rows per group and 8192 rows per page. Use `compression="snappy"`, `"lz4"`, `"gzip"` or `"uncompressed"` to change the codec. Set `row_group_rows` and `page_rows` to positive sizes with at least as many rows per group as per page. The writer closes the temporary file before publishing it. To replace an existing destination, pass `overwrite=True`. In C++, pass a path or a `std::vector` of paths to `ScanParquet`. Write with `WriteParquet(query, path, write_options, execution_options)`. ## Supported files Struct, List, LargeList and Map can contain any supported scalar or nested type. A null struct, a struct with all-null fields, an empty list and a list with one null element each retain their meaning. Select a top-level column by name to read its children together. A filter on a sibling scalar column can skip whole row groups before reading those children. The writer uses standard Parquet LIST and MAP annotations. The reader also accepts older two-level lists and bare repeated fields. Embedded Arrow schema metadata preserves LargeList, LargeUtf8, LargeBinary and the sorted map key flag. Readers that ignore this metadata use their own offset widths. Each selected nested column is assembled one row group at a time against the query memory limit. Reduce the row group size when writing very large values. | Feature | Reader | Writer | | --- | --- | --- | | Boolean, all signed and unsigned integer widths, Float32, Float64, Utf8, Date32 | Required or optional fields | Preserves field nullability | | Binary, LargeBinary | Unannotated BYTE\_ARRAY | BYTE\_ARRAY | | LargeUtf8 | BYTE\_ARRAY with STRING or UTF8 annotation | BYTE\_ARRAY with STRING annotation | | Date64 | Parquet DATE reads as Date32 | INT32 with DATE annotation. Values must be midnight and fit Date32. | | Timestamp | INT64 with TIMESTAMP(MICROS) or legacy TIMESTAMP\_MICROS | TIMESTAMP(MICROS), preserving the UTC flag | | Decimal128 | Annotated INT32, INT64 or fixed-length bytes (1 to 16) | Chooses physical storage by precision | | Boolean encoding | PLAIN and RLE | PLAIN | | PLAIN and dictionary | Reads both for the other supported types, including dictionary fallback | PLAIN for all supported types. Dictionary for Int8, Int16, Int32, all unsigned integers, Float32, Float64, strings, bytes, Date64 and Timestamp when smaller | | BYTE\_STREAM\_SPLIT | FLOAT, DOUBLE, INT32, INT64 and fixed-length decimal bytes | For Float32 and Float64 pages without dictionary encoding, uses BYTE\_STREAM\_SPLIT when it compresses smaller than PLAIN | | DELTA\_BINARY\_PACKED | INT32 and INT64 storage, including dates and integer-backed decimals | Can choose delta for integer storage | | DELTA\_BYTE\_ARRAY and DELTA\_LENGTH\_BYTE\_ARRAY | Both offset widths for strings and bytes | Can choose either | | Data pages | V1 and V2 | V2 | | Compression | Uncompressed, Zstd, Snappy, LZ4\_RAW, Gzip | Same | Parquet stores dates in whole days. Writing a non-midnight Date64 raises an error. To discard the time explicitly, cast the column to Date32 before writing. The `"lz4"` option uses raw LZ4 blocks. The reader rejects legacy Hadoop-framed LZ4. The writer requires at least one output field. Encryption and external column files are unsupported. ## When a scan can skip data - Row-group null counts and usable min/max bounds can rule out entire groups before the scan reads their pages. For example, a date range can exclude a group whose latest date is too early. Quixote writes these statistics for narrow and unsigned integers, Binary, LargeBinary, LargeUtf8, Date64, Timestamp, Float32 and Float64. Unsigned pruning uses type-ordered bounds. Floating-point bounds exclude NaNs. The scan keeps groups that may contain a NaN matching the filter. - Quixote writes a page offset index for each column and a column index with per-page bounds and null counts. It omits the column index when a bound exceeds 256 bytes or the column chunk contains a NaN. A selective filter can use these indexes to skip pages in the filter and projected columns, even when their page boundaries differ. Unfiltered scans skip the indexes. - On dictionary-encoded Utf8 pages, the scan can test dictionary entries for equality, membership or a string pattern and filter rows by dictionary ID. It can still evaluate the predicate when a later page uses plain encoding. - Inner and semi joins on Int32, Int64 or Date32 keys can pass a runtime filter from the right input to the left scan. The scan can skip groups outside the key range and discard rows before reading other columns. A surviving row may still require reading and decompressing a whole page. A large page or dictionary can exceed the query's memory budget. ## Run TPC-H Q6 From the source tree, run the shipped query on the small lineitem fixture: ``` uv run --no-sync python examples/tpch/q6.py tests/fixtures/tpch_q6.parquet ``` The fixture returns one Decimal128 revenue value, `19.0000`. Pass your own lineitem file in place of the fixture to run the query on its date, quantity, price and discount columns. ``` """Compute TPC-H Q6 revenue from a lineitem Parquet file.""" import argparse import datetime as dt from decimal import Decimal import pyarrow as pa import quixote as qe def q6(session, path): scan = session.scan_parquet(path, columns=[ "l_shipdate", "l_quantity", "l_extendedprice", "l_discount", ]) shipdate = qe.col("l_shipdate") discount = qe.col("l_discount") selected = ( scan.filter(shipdate >= dt.date(1994, 1, 1)) .filter(shipdate < dt.date(1995, 1, 1)) .filter(discount >= Decimal("0.05")) .filter(discount <= Decimal("0.07")) .filter(qe.col("l_quantity") < Decimal("24")) ) revenue = (qe.col("l_extendedprice") * discount).sum().alias("revenue") return selected.select(revenue) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("lineitem") parser.add_argument("--cpu-executor", choices=("thread_pool", "tbb"), default="thread_pool") args = parser.parse_args() with qe.Runtime(cpu_threads=4, io_threads=2, cpu_executor=args.cpu_executor) as runtime: with qe.Session(runtime) as session: print(pa.table(q6(session, args.lineitem).collect())) ```