/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Printing steps (#933): each step as its canonical form of jq * (`Query Editor Spec.md` §4.7), built as a `JqType` program and printed by * east's `printJq`, one segment per line of the pipeline, with the span of * every step and condition. * * A step prints for the shape of the rows it takes — `.status.type` for a * variant's case, `sort_by(-.total)` for a number — so the program is laid * out a step at a time, each prefix checked by east's `checkJq`, whose stages * are the shapes. A step that does not check leaves the shape as it was, so * the steps after it still print; the whole program's check reports it. * * @packageDocumentation */ import { type CheckJqResult, type EastType, type JqNode } from "@elaraai/east"; import { type StepField } from "./fields.js"; import { type Shape } from "./shape.js"; import { type Condition, type Step, type StepQuery } from "./values.js"; /** A printed step's range of text. */ export interface StepRange { /** The step's id. */ readonly id: string; /** Its first offset. */ readonly from: number; /** The offset after its last. */ readonly to: number; } /** A printed condition's range of text, and the ranges of its field and its value. */ export interface ConditionRange { /** The step it is in. */ readonly stepId: string; /** The condition's id. */ readonly condId: string; /** Its first offset. */ readonly from: number; /** The offset after its last. */ readonly to: number; /** Where its field's read is, and its value's literal. */ readonly parts: { readonly field?: readonly [number, number]; readonly value?: readonly [number, number]; }; } /** * A step's slot: what a check's diagnostic is placed on, besides a * condition. `dataset`, `key` and `fields` are a Look up's; `by` and * `agg-field` (a total's field, with the total's id) a Group and total's; * `pick-field` (with the field shown's id) a Show only fields'; `n` a Keep * the first's; `field` the field of a Sort, a Fill, an Open each list or a * Take part of a date; `value` a Fill's; `via` a List every part's; `range` * and `fixed` (with the input's name) a Try the model's. */ export type StepSlot = "dataset" | "key" | "fields" | "by" | "agg-field" | "pick-field" | "n" | "field" | "value" | "via" | "range" | "fixed"; /** A printed slot's range of text. */ export interface SlotRange { /** The step it is in. */ readonly stepId: string; /** The slot. */ readonly slot: StepSlot; /** The total, field shown, field brought in or input it is for. */ readonly id?: string; /** Its first offset. */ readonly from: number; /** The offset after its last. */ readonly to: number; } /** Steps printed as canonical jq. */ export interface PrintedSteps { /** The program's text. */ readonly text: string; /** The program, or `null` when a jq step's text breaks it. */ readonly program: JqNode | null; /** Where the data source's read is. */ readonly source: { readonly from: number; readonly to: number; }; /** Each printed step's range, in order; an unfinished step is not printed. */ readonly steps: readonly StepRange[]; /** Each printed condition's range, groups included. */ readonly conds: readonly ConditionRange[]; /** Each printed slot's range. */ readonly slots: readonly SlotRange[]; } /** One step laid out: the shape it takes and gives, and whether it printed and checked. */ export interface LaidOutStep { /** The step. */ readonly step: Step; /** The shape of the rows it takes. */ readonly before: Shape; /** The shape it gives: its input's when it is unfinished or does not check. */ readonly after: Shape; /** Whether it is in the program: unfinished steps are left out. */ readonly printed: boolean; /** Whether it checked in the program before it. */ readonly checked: boolean; } /** A segment of the program, as the counting program reads it. */ export interface LaidOutSegment { /** The step it is part of; `undefined` for the prologue and the source. */ readonly stepId: string | undefined; /** Its text. */ readonly text: string; /** Whether it is the source, or a step's last segment, after which the step's shape holds. */ readonly last: boolean; } /** A query laid out: the printed program, each step's shapes, and the program's check. */ export interface StepLayout { /** The printed program. */ readonly printed: PrintedSteps; /** The shape the data source gives. */ readonly source: Shape; /** Each step, in order. */ readonly steps: readonly LaidOutStep[]; /** The shape the query gives. */ readonly final: Shape; /** The whole program's check, against the root. */ readonly check: CheckJqResult; /** The program's segments, in order. */ readonly segments: readonly LaidOutSegment[]; } /** What separates two segments of the pipeline layout. */ export declare const SEP = "\n| "; /** * The fields jq reads a step's field through, in a shape. * * @param shape - the shape * @param ref - how the step names the field * @param whole - read a variant as its whole value, not its case * @returns the fields read */ export declare function pathFor(shape: Shape, ref: string, whole?: boolean): readonly string[]; /** * The shape of each item of a list field, for the conditions inside has any * where. * * @param field - the list field * @returns the items as rows of their record, or {@link UNKNOWN} */ export declare function itemShape(field: StepField | undefined): Shape; /** * Whether a condition of an all filter narrows the rows to a case, and so * prints in a `select` of its own before the rest: a variant field is one of * its cases. * * @param condition - a top-level condition of the filter * @param shape - the rows it tests * @returns the variant field and the case, or `undefined` */ export declare function narrowingOf(condition: Condition, shape: Shape): { field: string; case: string | undefined; } | undefined; /** The name a step gives a field made from another: the field's own, or the step's name for it. */ export declare function nameFor(shape: Shape, ref: string): string; /** The scalar fields of a tree's node, which List every part keeps. */ export declare function scalarFieldsOf(record: EastType): string[]; /** The name of a variable a step binds, from a name it would like: the name, or `value` when jq cannot read it as one. */ export declare function variableName(name: string): string; /** * Whether a finished step can be printed: a Look up's dataset must be one jq * can bind as a variable. * * @param step - the step * @returns whether it prints */ export declare function printable(step: Step): boolean; /** * What the steps know of the shape a step gives, besides its type: the cases * its rows are narrowed to, a Keep the first bound, and what a row is called. * * @param step - the step * @param before - the shape it takes * @returns the narrowed fields, the bound and the noun */ export declare function knownAfter(step: Step, before: Shape): { narrowed: ReadonlyMap; limit: number | undefined; noun: string; }; /** The shape a stage of a checked prefix gives, found by where the segment starts. */ export declare function stageAt(check: CheckJqResult, from: number): { type: EastType; multiplicity: CheckJqResult["stages"][number]["multiplicity"]; } | undefined; /** * Lays a query out: prints each finished step for the shape before it, * checking each prefix against the root, and checks the whole program. * * @param query - the query * @param root - the root's type: a struct of the data sources * @returns the printed program, each step's shapes, and the program's check */ export declare function layOutSteps(query: StepQuery, root: EastType): StepLayout; /** * Prints a query's steps as canonical jq (`Query Editor Spec.md` §4.7), with * the span of every step and condition. * * @param query - the query * @param root - the root's type: a struct of the data sources * @returns the program's text, the program, and the ranges * * @remarks * The program starts with `.ds as $ds` for each dataset a finished Look up * reads, then the data source; each finished step follows on its own line. * Unfinished steps are left out. A jq step's text is printed as written. */ export declare function printSteps(query: StepQuery, root: EastType): PrintedSteps; //# sourceMappingURL=print.d.ts.map