import type { AstNodeMetadata } from "./types"; /** * A mapping of AST (Abstract Syntax Tree) node types to their corresponding TypeScript types. * This object acts as a type registry for the various components of the NQL (Notation Query Language) AST. */ export declare const NqlAstTypes: { agg_function: NqlAggFunction; binary_op: NqlBinaryOp; case: NqlCase; function: NqlFunction; literal: NqlLit; string_literal: NqlLit; long_literal: NqlLit; double_literal: NqlLit; boolean_literal: NqlLit; timestamp_literal: NqlLit; column: NqlIdentColumn; table: NqlIdentTable; join: NqlJoin; type_spec: NqlTypeSpec; operator: NqlOp; window: NqlWindow; unknown: NqlUnknown; select: NqlSelect; create_materialized_view: NqlCreateMaterializedView; explain: NqlExplain; array: NqlArrayType; binary: NqlBinaryType; bool: NqlBooleanType; double: NqlDoubleType; long: NqlLongType; interval: NqlIntervalType; object: NqlObjectType; other: NqlOtherType; string: NqlStringType; timestamp: NqlTimestampType; }; /** * Represents the logical operators that can be used to combine constraints in a NQL query. */ type NqlTemplateConstraintLogicalOperator = "ANY_OF" | "ALL_OF" | "NONE_OF"; /** * Defines a constraint that can be applied in a placeholder node. A constraint can consist of * individual conditions or groups of nested conditions combined using a logical operator. * * @template T The type of conditions or nested constraint groups contained within this constraint. */ interface NqlTemplateConstraint { operator: NqlTemplateConstraintLogicalOperator; constraints: (T | NqlTemplateConstraintGroup)[]; } /** * Represents a group of constraints combined under a single logical operator. This allows for * nested and complex logical expressions within a NQL query. * * @extends NqlTemplateConstraint Inherits the structure of NqlConstraint for nested grouping. * @template T The type of conditions or nested constraint groups contained within this constraint group. */ interface NqlTemplateConstraintGroup extends NqlTemplateConstraint { } /** * Represents the keys from the NqlAstTypes object, used to identify the type of AST node. */ type NqlAstType = keyof typeof NqlAstTypes; /** * Extracts the TypeScript type(s) associated with a given NQL AST type or array of AST types. * This utility type dynamically maps one or more NqlAstType keys to their corresponding types. * * @template T A single NqlAstType or an array of NqlAstType values. */ type ExtractNqlType = T extends NqlAstType[] ? { [K in keyof T]: T[K] extends NqlAstType ? (typeof NqlAstTypes)[T[K]] : never; } : T extends NqlAstType ? (typeof NqlAstTypes)[T] : never; /** * Extracts the TypeScript type(s) associated with the expected type(s) of a `NqlPlaceholder`. * This utility type conditionally checks if a given type `T` is a `NqlPlaceholder` with a generic * type parameter `U`. If `T` is a `NqlPlaceholder`, it uses the `ExtractNqlType` utility to map * `U` (the generic type parameter of the `NqlPlaceholder`) to its corresponding TypeScript type(s). * If `T` is not a `NqlPlaceholder`, the type resolves to `never`. * * This type is useful for type inference and ensuring type safety when working with AST nodes * that may be placeholders, enabling conditional type logic based on the structure of the AST. * * @template T - The type to check against `NqlPlaceholder`. This can be any type within the * `NqlAst` hierarchy, or more broadly, any type in the system. * * @returns The extracted TypeScript type(s) if `T` is a `NqlPlaceholder`, otherwise `never`. * * @example * // Given a placeholder type with expected type 'string_literal': * type PlaceholderStringLiteral = NqlPlaceholder<'string_literal'>; * * // The extracted type would be `NqlLit`: * type ExtractedType = ExtractPlaceholderType; * * @example * // When the type is not a `NqlPlaceholder`, `never` is returned: * type NonPlaceholderType = NqlAst; // Assuming NqlAst is not a placeholder. * type ExtractedType = ExtractPlaceholderType; // `never` */ type ExtractPlaceholderType = T extends NqlPlaceholder ? ExtractNqlType : never; /** * Represents a placeholder within a NQL query template. Placeholders are used to dynamically insert * values, columns, or other AST nodes into a query based on certain constraints and expectations. * * @template T A single NqlAstType or an array of NqlAstType values indicating the expected type(s) * for the AST node(s) that can fill this placeholder. */ interface NqlPlaceholder { type: "placeholder"; identifier: string; description?: string; expectedType: T; optional: boolean; cardinality: "one" | "many"; constraints?: NqlTemplateConstraint>; nql: string; } type Quantifier = "all" | "distinct"; interface NqlAggFunction { nql: string; as?: string; args: NqlAst[]; name: string; quantifier: Quantifier | null; type: "agg_function"; } interface NqlBinaryOp { nql: string; as?: string; name: string; left: NqlAst; right: NqlAst; type: "binary_op"; } interface NqlCase { nql: string; as?: string; args: CaseArg[]; type: "case"; } type CaseArg = NqlWhen | NqlElse; interface NqlWhen { nql: string; condition: NqlAst; value: NqlAst; type: "when"; } interface NqlElse { nql: string; value: NqlAst; type: "else"; } interface NqlFunction { nql: string; as?: string; args: NqlAst[]; name: string; type: "function"; } interface NqlArrayType { type: "array"; items: NqlSimpleType | null; } interface NqlBinaryType { type: "binary"; format: NqlBinaryTypeFormat | null; } type NqlBinaryTypeFormat = "geometry"; interface NqlBooleanType { type: "bool"; } interface NqlDoubleType { type: "double"; } interface NqlLongType { type: "long"; } interface NqlIntervalType { type: "interval"; period: NqlIntervalTypePeriod; } type NqlIntervalTypePeriod = "day" | "day_hour" | "day_minute" | "day_second" | "hour" | "hour_minute" | "hour_second" | "minute" | "minute_second" | "month" | "second" | "year" | "year_month"; interface NqlObjectType { type: "object"; properties: NqlSimpleType[]; } interface NqlOtherType { type: "other"; } interface NqlStringType { type: "string"; } interface NqlTimestampType { type: "timestamp"; } type NqlSimpleType = NqlArrayType | NqlBinaryType | NqlBooleanType | NqlDoubleType | NqlLongType | NqlIntervalType | NqlObjectType | NqlOtherType | NqlStringType | NqlTimestampType; interface NqlLit { nql: string; as?: string; value_type: T; value: string | null; type: "literal"; } interface NqlIdentColumn { nql: string; as?: string; db: string | null; schema: string | null; table: string; column: string; type: "column"; } interface NqlIdentTable { nql: string; as?: string; db: string | null; schema: string | null; table: string; type: "table"; } type NqlIdent = NqlIdentColumn | NqlIdentTable; type NqlJoinConditionType = "none" | "on" | "using"; type NqlJoinType = "comma" | "cross" | "full" | "inner" | "left" | "left_semi" | "right"; interface NqlJoin { condition: NqlAst | null; condition_type: NqlJoinConditionType; left: NqlAst; join_type: NqlJoinType; right: NqlAst; type: "join"; } interface NqlTypeSpec { nql: string; value: string; type: "type_spec"; } interface NqlOp { nql: string; as?: string; args: NqlAst[]; name: string; type: "operator"; } interface NqlWindow { nql: string; lower_bound: NqlAst | null; name: NqlIdent | null; order_by: NqlAst[]; partitions: NqlAst[]; upper_bound: NqlAst | null; type: "window"; } interface NqlUnknown { nql: string; type: "unknown"; } interface NqlBudget { amount: { currency: string; value: number; }; period: { type: "calendar_daily" | "calendar_monthly"; }; } interface NqlSelect { nql: string; as?: string; budget: NqlBudget | null; columns: NqlAst[]; from: NqlAst | null; group_by: NqlAst[]; having: NqlAst | null; is_distinct: boolean; limit: NqlAst | null; order_by: NqlAst[]; qualify: NqlAst | null; where: NqlAst | null; windows: NqlAst[]; with: NqlAst[] | null; type: "select"; } interface NqlCreateMaterializedView { nql: string; name: string; select: NqlSelect; type: "create_materialized_view"; } interface NqlExplain { nql: string; query: NqlAst; type: "explain"; } interface NqlAstList extends Array { type: "ast_list"; } export declare function isAstArray(n: NqlAst): n is NqlAstList; type NqlAst = { type: string; ast_metadata?: AstNodeMetadata; } & (NqlAstList | NqlAggFunction | NqlBinaryOp | NqlCase | NqlFunction | NqlLit | NqlIdent | NqlJoin | NqlTypeSpec | NqlOp | NqlWindow | NqlUnknown | NqlSelect | NqlCreateMaterializedView | NqlExplain | NqlWhen | NqlElse | NqlPlaceholder); export type { ExtractPlaceholderType, NqlAggFunction, NqlAst, NqlAstList, NqlAstType, NqlBinaryOp, NqlBudget, NqlCase, NqlCreateMaterializedView, NqlElse, NqlExplain, NqlFunction, NqlIdent, NqlIdentColumn, NqlIdentTable, NqlJoin, NqlJoinConditionType, NqlJoinType, NqlLit, NqlOp, NqlPlaceholder, NqlSelect, NqlSimpleType, NqlTemplateConstraint, NqlTemplateConstraintGroup, NqlTemplateConstraintLogicalOperator, NqlTypeSpec, NqlUnknown, NqlWhen, NqlWindow, };