// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * @file Promise-based parquet reads. The iterator-based reads live in `./streams`. * * {@linkcode readParquetRows} throws when the file is missing. {@linkcode tryReadParquetRows} returns `null`. * Both throw for an unreadable file, so a caller cannot mistake a read failure for an empty file. */ import { pathExists } from "@mailwoman/core/fs/readers/stat" import type { PathBuilderLike } from "path-ts" import { escapeSQLString, openDuckDB } from "#parquet/duckdb" import { openParquetRowStream, type ParquetRowStreamOptions, validateRowLimit } from "#parquet/streams" /** * Reads every row of a parquet file into memory. * * Use {@linkcode openParquetRowStream} for a file too large to hold in memory. * * @throws When the file is missing or `columns` includes a column the file lacks. */ export async function readParquetRows(path: PathBuilderLike, options: ParquetRowStreamOptions = {}): Promise { if (!(await pathExists(path))) { throw new Error(`No parquet file at ${path}`) } return Array.fromAsync(openParquetRowStream(path, options)) } /** * Reads every row of a parquet file, or returns `null` when the file is missing. * * @throws When the file cannot be parsed or `columns` includes a column the file lacks. */ export async function tryReadParquetRows( path: PathBuilderLike, options: ParquetRowStreamOptions = {} ): Promise { if (!(await pathExists(path))) return null return Array.fromAsync(openParquetRowStream(path, options)) } /** * Counts the rows of a parquet file from its metadata, so the cost stays flat as the row count grows. * * The function opens its own DuckDB connection. * * @throws When the file is missing. */ export async function countParquetRows(path: PathBuilderLike): Promise { if (!(await pathExists(path))) { throw new Error(`No parquet file at ${path}`) } using db = await openDuckDB() const result = await db.runAndReadAll(`SELECT count(*) AS n FROM read_parquet('${escapeSQLString(path.toString())}')`) const rows = result.getRowObjects() as Array<{ n: unknown }> return Number(rows[0]?.n ?? 0) } /** * Returns a parquet file's column names in file order. * * The query uses `DESCRIBE`, which lists logical columns. * `parquet_schema` lists physical leaves instead, where a list column such as * `tokens` appears only as its `element` child. * * @throws When the file is missing. */ export async function parquetColumnNames(path: PathBuilderLike): Promise { if (!(await pathExists(path))) { throw new Error(`No parquet file at ${path}`) } using db = await openDuckDB() const result = await db.runAndReadAll(`DESCRIBE SELECT * FROM read_parquet('${escapeSQLString(path.toString())}')`) return (result.getRowObjects() as Array<{ column_name: unknown }>).map((row) => String(row.column_name)) } /** * The number of rows in a parquet file's first `rowGroups` row groups, * so a smoke read can stop where they end. * * @throws When the file is missing. */ export async function parquetRowGroupRows(path: PathBuilderLike, rowGroups: number): Promise { if (!(await pathExists(path))) { throw new Error(`No parquet file at ${path}`) } using db = await openDuckDB() const result = await db.runAndReadAll( `SELECT coalesce(sum(n), 0) AS rows FROM (SELECT DISTINCT row_group_id, row_group_num_rows AS n ` + `FROM parquet_metadata('${escapeSQLString(path.toString())}') WHERE row_group_id < ${validateRowLimit(rowGroups)})` ) return Number((result.getRowObjects() as Array<{ rows: unknown }>)[0]?.rows ?? 0) }