// Status: the wire contract. The implemented resources are in
// `crates/pqbench-model-v1/src/` (re-exported as `pqbench::v1`). The modelling
// rules are
// `docs/concepts/data-modelling.md`; where this contract differs from them, they
// win.
//
// pqbench IPC messages, in protobuf (AIP style).
//
// The data the commands pass are these resource messages: one resource per
// NDJSON line, using the proto3 JSON mapping, except that field names are the
// names as spelled here (snake_case), not the mapping's lowerCamelCase — a
// generator sets `json_name` to match — that a count or a size is a JSON
// number, not the mapping's string, and that an instant is integer milliseconds
// since the Unix epoch, the unit the logs already store. An enum is its value
// name. One `Schema` line, for example:
//
//   {"name":"catalogs/dbx_samples/schemas/nyctaxi","comment":"NYC taxi data"}
//
// There is no envelope: no `kind`, no per-document `version`. A stage reads
// exactly one resource type, and the `name`'s collection segment identifies it
// (`…/tables/{t}` is a `Table`, `…/blobs/{service}/{locator}` is a `Blob`,
// `…/columns/{column}` is a `Column`). A stream is one type; a genuinely mixed
// input is a `oneof`, never a tag on a resource.
//
// The API version is the package's (`pqbench.v1`), not a segment of the `name`;
// a name is self-describing by its collection segment, and no document carries
// a version of its own.
//
// An id segment is any string that is one URI path segment and decodes back to
// what it names — a plain name, a URL-encoded path (`/` as `%2F`), a dotted
// column path, or a number. None of these ids is user-specified, so AIP-122
// asks only that the format be documented (below); its character-set advice
// comes from RFC-1034, the DNS spec, and does not apply here.
//
// Resource names are relative (AIP-122) and are pqbench's own identifiers, not
// the backend's URL: the endpoint is Unity's or Iceberg's, and the dialect maps
// `name` to that backend's REST URL — `/api/2.1/unity-catalog/tables/{c}.{s}.{t}`
// for Unity, `/namespaces/{s}/tables/{t}` for Iceberg. A dotted FQN is an
// input-only lookup alias; it never appears as a field.
//
// AIP's field annotations are deliberately absent: the documents are
// reimplemented as native Rust, so the type system carries what the annotations
// would describe — an absent value is an `Option`, an output-only field is not
// an input parameter — and there is no generator to read them. The AIP shape
// (names, patterns, field names) still applies.
//
// Credentials are session state, not resource data: a reader resolves a table's
// vended lease in memory and no resource carries a key.
//
// A table a catalog did not name — one read by storage path — is still a
// resource: it takes the `local` catalog and schema (`catalogs/local/schemas/
// local/tables/{id}`), so every file has a canonical name.
//
// A blob is storage-rooted: `blobs/{service}/{locator}` (the service is
// `s3`, `file`, …; the locator URL-encoded), so a bare object or a local path
// names a resource like any other. A table references it through a `TableFile`
// association, not as its parent (AIP-124).

syntax = "proto3";

package pqbench.v1;

// A per-column value the log stores as JSON — a number, a string, an object, or
// null — and a partition value that may be null.
import "google/protobuf/struct.proto";

// ---------------------------------------------------------------------------
// Enums (AIP-126): UPPER_SNAKE_CASE, an `_UNSPECIFIED` zero value.
// ---------------------------------------------------------------------------

enum CatalogType {
  CATALOG_TYPE_UNSPECIFIED = 0;
  CATALOG_TYPE_MANAGED = 1;
  CATALOG_TYPE_FOREIGN = 2;
  CATALOG_TYPE_SYSTEM = 3;
}

enum TableFormat {
  TABLE_FORMAT_UNSPECIFIED = 0;
  TABLE_FORMAT_DELTA = 1;
  TABLE_FORMAT_ICEBERG = 2;
}

// ---------------------------------------------------------------------------
// Resources (AIP-121/122/123). First field is `name`; `uid`, when present, is
// an output-only UUID4.
// ---------------------------------------------------------------------------

// The metastore: the entity above catalogs. There can be several — the Unity
// metastore, the Iceberg metastore — so it is a collection, e.g.
// `metastores/unity`.
message Metastore {
  // Format: metastores/{metastore}
  string name = 1;

  string uid = 2;

  // The user-set metastore name.
  string display_name = 3;

  string cloud_provider = 4;

  string region = 5;
}

message Catalog {
  // Format: catalogs/{catalog}
  string name = 1;

  string uid = 2;

  string display_name = 3;

  CatalogType catalog_type = 4;

  string comment = 5;

  string owner = 6;
}

message Schema {
  // Format: catalogs/{catalog}/schemas/{schema}
  string name = 1;

  string uid = 2;

  string comment = 3;

  // The schema's storage URI, when it has one.
  string uri = 4;

  map<string, string> properties = 5;

  // The user-facing name the endpoint reports for the schema.
  string display_name = 6;
}

message Table {
  // Format: catalogs/{catalog}/schemas/{schema}/tables/{table}
  string name = 1;

  string uid = 2;

  // The storage root URI.
  string uri = 3;

  TableFormat format = 4;

  int64 snapshot_version = 5;

  repeated string partition_columns = 6;

  // The schema, in order. A repeated field follows its element type (AIP-140).
  repeated DeclaredColumn declared_columns = 7;

  map<string, string> properties = 8;

  int64 file_count = 9;

  int64 partition_count = 10;

  // Removed: vended credentials are session state, not resource data.
  reserved 11;
  reserved "credentials";

  // The user-facing name the endpoint reports for the table.
  string display_name = 12;

  // When the table was created, milliseconds since the Unix epoch: the Delta
  // log's `metaData.createdTime`, else the catalog record's `created_at`.
  optional int64 create_time = 13;
}

// A column of a table's schema. Not a resource: the declared column is a
// bounded value on its `Table`, while a file's copy of it is the `Column`
// resource the analyses hang off.
message DeclaredColumn {
  // The column's name within the schema (AIP-122 reserves `name` for resources).
  string column = 1;

  string data_type = 2;

  bool nullable = 3;

  int64 position = 4;
}

// A storage blob: the bytes a data file lives in — a local file or an S3
// object, named by its service. It is storage-rooted and top-level; a table
// references it through a `TableFile` association (AIP-124), not as a parent.
message Blob {
  // Format: blobs/{service}/{locator}
  // `{service}` is the storage service (`s3`, `file`, …); `{locator}` is what
  // it names — `bucket/key` for `s3` (`s3://bucket/key` → `bucket%2Fkey`),
  // `tmp/x.parquet` for `file` — URL-encoded to one segment so it decodes back.
  string name = 1;

  int64 size_bytes = 2;

  // Rows in the file, from its footer. Absent when the footer was not read.
  optional int64 row_count = 8;

  // The storage class or tier (`STANDARD`, `STANDARD_IA`, …), when known.
  string storage_class = 3;

  // Whether reading the object needs a restore first. Absent when the storage
  // service has no such state.
  optional bool restore_required = 9;

  // The writer that produced the file, as its footer names it (`created_by`),
  // e.g. `parquet-mr version 1.13.1`. Absent when the footer names none.
  string writer = 7;

  int64 row_group_count = 4;

  int64 column_count = 5;

  // The probability the table file naming this blob was kept with. Absent
  // means unsampled.
  optional double sample_rate = 6;
}

// A table's reference to one `Blob`: a row of the table's log (the Delta
// `add`). The table owns it, so its canonical parent is the table; the blob is
// storage-owned. This is the AIP-124 association resource between them.
message TableFile {
  // Format: catalogs/{catalog}/schemas/{schema}/tables/{table}/files/{file}
  // `{file}` is the table-relative path, URL-encoded to one segment.
  string name = 1;

  // The blob holding the bytes, a `pqbench.v1.Blob` name.
  string blob = 2;

  int64 size_bytes = 3;

  // The log's partition values; a null value is JSON null.
  map<string, google.protobuf.Value> partition_values = 4;

  // The Delta add action's statistics, when the log carries them.
  FileStats stats = 5;

  // Removed: vended credentials are session state, not resource data.
  reserved 6;
  reserved "credentials";

  // The path relative to the table's storage root, normalized to a
  // `/`-separated path with percent escapes decoded.
  string path = 7;

  // The write time the log records for the file, in milliseconds since the
  // Unix epoch: Delta's `add.modificationTime`, or the commit time of the
  // Iceberg snapshot that added the file. Absent when the log records none.
  optional int64 modification_time = 8;

  // The probability a `sample files` stage kept this record with; its inverse
  // is the weight a sum over the stream applies. Absent means unsampled.
  optional double sample_rate = 9;
}

// One partition of a table's snapshot: the active files that share its
// partition values, folded into totals. An unpartitioned table has one.
message Partition {
  // Format: catalogs/{catalog}/schemas/{schema}/tables/{table}/partitions/{partition}
  // `{partition}` is the Hive-style path of the values (`k=v/k2=v2`, in the
  // table's partition column order; `/`, `=` and `%` in a key or value
  // percent-escaped; a null value is `__HIVE_DEFAULT_PARTITION__`), URL-encoded
  // to one segment. A table without partition columns has `unpartitioned`, as
  // do removed files whose `remove` action records no partition values.
  string name = 1;

  // The log's partition values; a null value is JSON null.
  map<string, google.protobuf.Value> partition_values = 2;

  // Changes when a file is added to or removed from the partition: an
  // order-independent hash of each file's path and size, 16 hex digits.
  string etag = 3;

  int64 file_count = 4;

  // Sum of the files' sizes, as the log claims them.
  int64 size_bytes = 5;

  // Sum of the files' record counts; absent when a file has none.
  optional int64 record_count = 6;

  int64 smallest_file_size_bytes = 7;

  int64 largest_file_size_bytes = 8;

  // The oldest and newest file by the log's modification time (ties fall to
  // the path). Absent when the log records no per-file time.
  TableFile first_file = 9;

  TableFile last_file = 10;

  // The file a measurement reads for the partition: the one whose path and
  // size hash lowest, so it stays the same while the partition holds it.
  TableFile sample_file = 11;

  // Files the snapshot removed that no active file holds and that are still
  // stored, and billed: Delta's tombstones until a vacuum, and Iceberg's
  // deleted manifest entries and superseded snapshots until an expire.
  optional int64 removed_file_count = 12;

  // Sum of the removed files' sizes.
  optional int64 removed_size_bytes = 13;

  // Merge-on-read delete files the snapshot still applies: Iceberg position
  // and equality deletes, and a v3 deletion vector. Absent when the partition
  // holds none, and for a format without delete files (Delta).
  optional int64 delete_file_count = 14;

  // Sum of the delete files' sizes.
  optional int64 delete_size_bytes = 15;

  // When `process-partition.sh` last finished at this etag, milliseconds since
  // the Unix epoch. Absent until the driver's store has processed the
  // partition.
  optional int64 last_processed_time = 16;

  // The cheapest monthly storage cost across the candidate classes and tiers
  // of the partition's stored price. Absent until priced, and for files with
  // no storage class.
  optional double lowest_cost_per_month = 17;

  // The dearest monthly storage cost across the candidate classes and tiers
  // of the partition's stored price. Absent until priced, and for files with
  // no storage class.
  optional double highest_cost_per_month = 18;
}

// A partition's storage price at one etag, the partition's singleton child
// (AIP-156): the storage classes of the files `get partition-price` looked at,
// and what the partition's bytes cost in each.
message PartitionPrice {
  // Format: catalogs/{catalog}/schemas/{schema}/tables/{table}/partitions/{partition}/price
  string name = 1;

  // The partition's etag when it was priced.
  string partition_etag = 2;

  // The storage classes of the partition's oldest, sample and newest files,
  // without repeats, in that order. Lifecycle moves objects by age, so one
  // class covers the partition and two mean a transition inside it. Empty
  // when the files are not S3 objects.
  repeated string storage_classes = 3;

  // The partition's bytes priced in each class it shows: one entry per class,
  // and one per access sub-tier for a class that prices by access. With one
  // class this is the cost; with two they bound it.
  repeated StorageTierCost storage_costs = 4;

  // The partition's removed bytes priced the same way.
  repeated StorageTierCost removed_storage_costs = 5;

  // Whether any sampled file needs a restore before it can be read. Absent when
  // no sampled file is an S3 object.
  optional bool restore_required = 6;
}

// The totals of the partitions under one catalog, schema or table, its
// singleton child (AIP-156), from `summarize partitions`. A partition counts
// once, whether the fold read a live listing or the driver's newest stored
// version.
message PartitionStats {
  // Format: catalogs/{catalog}/partitionStats
  // Format: catalogs/{catalog}/schemas/{schema}/partitionStats
  // Format: catalogs/{catalog}/schemas/{schema}/tables/{table}/partitionStats
  string name = 1;

  // The partitions.
  int64 partition_count = 2;

  // Those whose `process-partition.sh` finished at their etag.
  int64 processed_partition_count = 12;

  reserved 10, 11; // was version_counts and failed_version_count

  // The partitions' files and bytes, from their listings.
  int64 file_count = 3;

  int64 size_bytes = 4;

  // The bytes removed from the logs but not vacuumed, where the logs report
  // them.
  int64 removed_size_bytes = 5;

  // The partitions with a monthly cost: priced, and their files have a
  // storage class.
  int64 priced_partition_count = 6;

  // Those partitions' monthly cost, summed over their cheapest and their
  // dearest class or access tier. Absent when none has a cost.
  optional double lowest_cost_per_month = 7;

  optional double highest_cost_per_month = 8;

  // When the newest file of any partition was written.
  optional int64 last_modification_time = 9;
}

// One candidate monthly storage cost: a class with one price, or one access
// sub-tier of a class that chooses its price by access.
message StorageTierCost {
  string storage_class = 1;

  // The access sub-tier, for a class that prices by access.
  optional string access_tier = 2;

  // The S3 list price, in dollars per gibibyte per month (us-east-1).
  double dollars_per_gibibyte_per_month = 3;

  double cost_per_month = 4;
}

// A file's copy of a declared column: the physical unit the analyses read. The
// collection is `columns` and the path says `column`, so the resource takes the
// bare name `Column`. The `{column}` id segment is the column path (e.g.
// `a.b`); a dot is valid in a URI path segment.
message Column {
  // Format:
  // blobs/{service}/{locator}/columns/{column}
  string name = 1;

  // The declared column this file column is a copy of.
  string declared_column = 2;

  int64 position = 3;

  string physical_data_type = 4;

  int64 row_group_count = 5;

  // The table whose file was measured, when measured through a table file.
  string table = 6;

  // The column's byte mass, when the footer was measured.
  Bytemass mass = 7;

  // The probability the table file naming this column was kept with, when a
  // `sample files` stage chose it. Absent means unsampled, weight one.
  optional double sample_rate = 8;

  int32 max_definition_level = 9;
  int32 max_repetition_level = 10;

  // The size in bytes of the file this column was measured in, when known.
  int64 size_bytes = 11;

  // The storage class of the file this column was measured in, when known.
  string storage_class = 12;
}

// A column's byte mass (the footer's `ColumnMetaData`), folded over the column's
// chunks. A value on `Column`, not a resource; the per-chunk breakdown is not
// carried.
message Bytemass {
  // The leaf column's logical type, as the Parquet spec names it (`STRING`,
  // `DECIMAL(10,2)`, `TIMESTAMP(MICROS,UTC)`, …). Absent when the footer's
  // schema records none. A footer fact: it costs no page read.
  string logical_type = 21;

  int64 compressed_bytes = 2;
  int64 uncompressed_bytes = 3;
  // The file's serialized Thrift footer and its eight-byte trailer, in bytes:
  // the metadata a file pays for once, so every column of the file carries the
  // same value. A footer fact: the length comes with the footer read, so no
  // extra byte is fetched. Absent only when the caller did not supply it.
  int64 footer_bytes = 24;
  // The file's bytes outside every column chunk: the footer above, the page
  // indexes, bloom filters, key-value metadata (an `ARROW:schema`, say), and
  // any other object bytes no chunk accounts for. `size_bytes` − Σ
  // `compressed_bytes`; a file fact repeated on each of its columns.
  int64 unaccounted_bytes = 25;
  string codec = 4;
  repeated string encodings = 5;
  repeated string data_encodings = 15;
  // The chunk is dictionary-encoded: a dictionary page offset, dictionary data
  // pages, or a dictionary encoding in the chunk's encoding set. A writer that
  // omits the offset (parquet-mr 1.8.3) still sets this.
  bool dictionary = 6;
  // Bytes from the dictionary page offset to the first data page, when the
  // footer records an offset that locates the page.
  int64 dictionary_bytes = 16;

  // Data pages the footer counts, split by how they encode their values; a
  // dictionary page counts as neither. Absent when the writer recorded no page
  // encoding statistics. Footer facts: no page read.
  optional int64 plain_data_page_count = 22;
  optional int64 dictionary_data_page_count = 23;

  // Every chunk carries a page index (a column index and an offset index), so
  // the column can be pruned at page granularity.
  bool page_index = 20;
  int64 unencoded_bytes = 17;
  int64 value_count = 7;
  int64 null_count = 8;
  int64 distinct_count = 9;
  string first_value = 10;
  string last_value = 11;

  // Derived.
  double bytes_per_value = 12;
  double compression_ratio = 13;
  double null_fraction = 14;
  double bytes_per_row = 18;
  double dictionary_fraction = 19;
}

// A codec sweep of a column.
message CompressionReport {
  // Format: …/columns/{column}/compression/{codec}
  string name = 1;

  string codec = 2;

  int64 compressed_bytes = 3;
  double ratio = 4;
  double compress_megabytes_per_second = 5;
  double decompress_megabytes_per_second = 6;
}

// A storage suggestion for a column or for a file, as `lint` measures it.
// `lint` measures each candidate against a control written by the same writer
// at the same codec and level. A file-wide knob (say the page size of every
// column) yields one suggestion for the file: its `name` is
// `{blob}/suggestions/{rule}` and its `column` is empty. The fields below are
// the wire contract.
message Suggestion {
  // Format:
  // blobs/{service}/{locator}/columns/{column}/suggestions/{rule}
  // Format (a file-wide knob):
  // blobs/{service}/{locator}/suggestions/{rule}
  string name = 1;

  // The stable rule id.
  string rule = 2;

  // The column's dotted leaf path; empty on a file-wide knob.
  string column = 3;

  // The table whose file was measured, when measured through a table file.
  string table = 4;

  // Was the partition a partition-level rule measured. `lint` reads Parquet
  // files, so a partition-level rule no longer exists.
  reserved 15; // was `partition`
  reserved "partition";

  // The probability the table file naming this column was kept with, when a
  // `sample files` stage chose it. Absent means unsampled, weight one.
  optional double sample_rate = 5;

  // The size in bytes of the file this column was measured in, when known.
  int64 size_bytes = 6;

  // The application that wrote the file, copied from the blob's `writer`, so
  // the remedy can be read without the file record.
  string writer = 10;

  // One-line explanation: the detection evidence, then the control's verdict.
  string message = 7;

  // The change that recovers the bytes, in the terms of the file's writer.
  string remedy = 8;

  // Estimated bytes saved, conservative across compression levels. Present
  // when the control measured a win.
  optional int64 savings_bytes = 9;

  // Rows the lint pass decoded, not the rows it asked for: the sample is one
  // whole representative row group.
  optional int64 sample_row_count = 12;

  // Whole row groups the lint pass decoded: the one group whose row count is
  // closest to the file's average.
  optional int64 sample_row_group_count = 13;
}

// ---------------------------------------------------------------------------
// Value types (not resources).
// ---------------------------------------------------------------------------

message FileStats {
  int64 record_count = 1;
  double bytes_per_row = 2;
}

// ---------------------------------------------------------------------------
// Documents that are not resources.
// ---------------------------------------------------------------------------

// One rewrite `experiment` measured: the codec settings and the resulting size.
message ExperimentTrial {
  repeated string rewrites = 1;
  int64 size_bytes = 2;
  double ratio = 3;
}
