import { type PathBuilderLike } from "path-ts"; import { type DuckDBLimits } from "#parquet/duckdb"; import { type FileDerivation, type ParquetFileRecord } from "#parquet/record"; import { PARQUET_COMPRESSION, type ParquetRow } from "#parquet/schema"; import { type RowLicenses } from "#source-register/row-license"; import type { LabeledRow } from "#types"; import type { SplitName } from "#utils/split"; /** * The logical type of one column {@linkcode writeColumnParquetFile} writes. */ export declare const ParquetColumnKind: { readonly String: "string"; readonly StringList: "string[]"; readonly Int32List: "int32[]"; readonly Int64List: "int64[]"; /** * A column whose every value is null, the type pyarrow infers for one. */ readonly Null: "null"; }; export type ParquetColumnKind = (typeof ParquetColumnKind)[keyof typeof ParquetColumnKind]; /** * A file's columns in file order, each with its logical type. */ export type ParquetColumns = ReadonlyArray; /** * Writes rows held in memory as one parquet file with exactly `columns`, in their order. * * It is the writer for an output whose schema is fixed by an earlier writer * rather than by {@linkcode LABELED_ROW_SCHEMA}, such as a CJK corpus. * A row missing a column writes `null` there. * * It stamps no license and writes no record. * The caller stamps each row first. */ export declare function writeColumnParquetFile(rows: readonly object[], columns: ParquetColumns, path: PathBuilderLike): Promise; /** * Replace a row's `license` with its source's register license id, counting the statement it replaced. * * Every parquet writer calls it before a row reaches a file, so the column holds an SPDX * or `LicenseRef-*` id and the producer's prose moves to the file's record. * * @throws When the row's source has no license id, so no row is written with a placeholder. */ export declare function stampRowLicense(row: ParquetRow, licenses: RowLicenses, statements: Map, context: string): ParquetRow; /** * One row as a parquet part's staging file holds it, with the fields the writer reads from it. * * A parallel build's align worker stages each row once, so the finish worker shuffles * and writes lines without parsing a row or serializing it again. */ export interface StagedRow { source: string; source_id: string; /** * The register license id the row's `license` column holds. */ license: string; /** * The producer's license statement the register id replaced, counted in the file's record. */ statement: string | null; /** * The row's parquet columns as one JSON object, without a trailing newline. */ line: string; } /** * Convert a labeled row to the line a staging file holds, its license replaced by its source's register id. * * @throws When the row fails {@linkcode rowToParquet}'s checks or its source has no license id. */ export declare function stageRow(row: LabeledRow, licenses: RowLicenses, context: string): StagedRow; /** * One staged row as a line of a staged-rows file: its `source_id` and statement * as a JSON array, a tab, and its parquet line. * * JSON escapes a tab inside a string, so the first tab ends the array. * The source and license id are the same for every row of one source's file, * so the file doesn't repeat them. */ export declare function encodeStagedRow(row: StagedRow): string; /** * Read one line {@linkcode encodeStagedRow} wrote, for rows of `source` whose license id is `license`. */ export declare function decodeStagedRow(text: string, source: string, license: string): StagedRow; /** * Options for {@linkcode writeParquetFile}. */ export interface WriteParquetFileOptions { /** * @defaultValue {@linkcode committedRowLicenses} */ licenses?: RowLicenses; } /** * Write one parquet file from rows held in memory, and its record beside it. * * Each row's `license` is replaced with its source's register license id. * {@linkcode stampRowLicense} does the replacement. * * @throws When the rows hold more than one source, because a record describes a file of one source. * @throws When the source has no license id. */ export declare function writeParquetFile(input: readonly ParquetRow[], path: PathBuilderLike, derivation: FileDerivation, options?: WriteParquetFileOptions): Promise; /** * Per-file metadata captured in `manifest.json`, one entry per `.parquet` file of a split. * * The writer lists them under `files`. * Corpora written earlier list them under a retired key, so every reader goes through * `baseManifestFiles` here or `manifest_files` in `corpus_files.py`, which accept each spelling. */ export interface ParquetFileDescriptor { split: SplitName; path: string; format: "parquet"; compression: typeof PARQUET_COMPRESSION; rows: number; bytes: number; sha256: string; first_source_id: string; last_source_id: string; /** * The file's corpus source slug. * * {@linkcode writeParquetSplits} writes one source per file and always sets it. * A manifest written before it did lists multi-source `part-NNNN.parquet` files * without one, so a reader of an older manifest finds it absent and `audit.ts` * infers the source from `first_source_id`'s prefix. */ source?: string; /** * The file's derivation hash. * * This value identifies its store directory. * Absent from manifests written before the store. */ derivation_hash?: string; } export interface ParquetManifest { corpus_version: string; schema: readonly string[]; rows_per_file: number; row_group_size: number; files: ParquetFileDescriptor[]; counts: Record; total_rows: number; } export interface WriteParquetSplitsOptions { /** * Root output directory. * * The corpus version directory and the `store/` directory are created beneath it. */ outputDir: PathBuilderLike; /** * The extract hash, code hash and config of one source's files. * * Each file holds one source's rows, so its derivation covers only that source's inputs and code. * The writer adds the file's source, split, part and row cap to the config. * * Rows hold no corpus version, so the version isn't a derivation input. * It calls this once per source. */ derivationFor: (source: string) => FileDerivation | Promise; /** * Corpus version recorded in the manifest and the output directory name. */ corpusVersion: string; /** * @defaultValue {@linkcode committedRowLicenses} */ licenses?: RowLicenses; /** * Max rows per `.parquet` file. * * @defaultValue `1,000,000` */ rowsPerFile?: number; /** * Continue from the files `/MANIFEST.json` already records. * * The manifest is rewritten after each finished file. * A descriptor is appended only after its `.parquet` file closes, so each recorded file is complete. * * A resumed run rereads each descriptor's size. * For each split and source it discards the rows the recorded files hold and opens the next part. * * The caller therefore has to supply the same iterables in the same order. * For `buildCorpus` that means the same labeled files and the same shuffle seed. */ resume?: boolean; /** * Where the manifest of the files this call writes is kept and, under `resume`, read back. * * A parallel build's source worker keeps its own and the coordinator assembles * the corpus manifest from them. * * @defaultValue `/MANIFEST.json` */ manifestPath?: PathBuilderLike; /** * Limits for the DuckDB instance that converts each staged part. * * @defaultValue {@linkcode openDuckDB}'s defaults */ duckdb?: DuckDBLimits; /** * Converts one staged NDJSON part to the parquet file at `outputPath`. * * It exists for tests and for callers that wrap the conversion, such as to * observe each part or to fail one. * A wrapper calls {@linkcode writeStagedParquet} to perform the conversion itself. * * @defaultValue {@linkcode writeStagedParquet} */ convertStagedPart?: (stagePath: string, outputPath: string, limits?: DuckDBLimits) => Promise; } /** * The manifest that a set of finished files describes. * * `counts` and `total_rows` are summed from the descriptors rather than from a running row counter, * so a manifest written between two files never claims the rows of the file being written. * Files are listed by split, then path, so the order doesn't depend on which file finished first. */ export declare function parquetManifestOf(files: readonly ParquetFileDescriptor[], opts: { corpusVersion: string; rowsPerFile: number; }): ParquetManifest; /** * Pre-partitioned labeled-row streams, one per split. * * Callers (`buildCorpus`) decide each row's split inline at align time via `splitForRow` * and route rows to the matching stream. * Splits with no rows can be omitted or passed as an empty iterable. * {@linkcode writeParquetSplits} skips them. */ export type PerSplitRows = Partial>>; /** * DuckDB threads for converting a staged part, whatever the instance's own limit. */ export declare const CONVERSION_THREADS = 1; /** * Convert one staged NDJSON part to a parquet file at `outputPath`. * * @internal Exported for the test that converts one part under several instance thread limits. */ export declare function writeStagedParquet(stagePath: string, outputPath: string, limits?: DuckDBLimits): Promise; /** * Streams labeled rows into `.parquet` files, with one set per split and source. * * It writes the manifest that describes them, and a record beside every file. * * Splits are processed sequentially. * Within a split one file is open per source, since rows arrive interleaved * across sources after the shuffle. * * Rows are staged to newline-delimited JSON with backpressure, then DuckDB * writes the Parquet file from disk. * A staged part is removed as soon as its conversion succeeds. * * One conversion runs while the rows of the next parts are staged, so the split's staging * directory holds at most one open part per source and the part being converted. * * Each source's final part stays open until the split's rows end. * Peak disk demand is therefore the unfinished staged parts, plus the conversion * output being written, plus DuckDB's spill during that conversion. * * A failed conversion records no file and adds no manifest entry, so a resumed run writes that part again. * The writer keeps the staged part on failure and removes its own partial output. * * The error states the source, part, row count, source-id range and staged path. * The staged part goes when the split's staging directory is disposed. * Its rows can be regenerated from the deterministic input. * * Each conversion writes `data.parquet.partial..` and renames it into place, * so two writers of one derivation never write the same file. * * A file whose store path already holds a file is kept when the two sha256 values agree, * since the path identifies its derivation and the bytes are the same. * A disagreement throws, because one derivation wrote two different files. */ export declare function writeParquetSplits(perSplit: PerSplitRows, opts: WriteParquetSplitsOptions): Promise; /** * Each split's staged rows, in the order {@linkcode writeStagedParquetSplits} writes them. */ export type PerSplitStagedRows = Partial>>; /** * {@linkcode writeParquetSplits} over rows already staged by {@linkcode stageRow}. * * `opts.licenses` is unread: each staged row holds its license id. */ export declare function writeStagedParquetSplits(perSplit: PerSplitStagedRows, opts: WriteParquetSplitsOptions): Promise; //# sourceMappingURL=writers.d.ts.map