/** * SQL reference extractors for lineage and generated-query validation. * * `extractTablesFromSql` intentionally stays lightweight for manifest lineage. * `analyzeSqlReferences` uses node-sql-parser for the stricter Tier-2 agent * validation path, where relation and column references must be checked against * an inspected context pack before SQL is executed. */ export interface SqlParseResult { /** External table dependencies (CTEs excluded) */ tables: string[]; /** CTE names defined in this query */ ctes: string[]; /** ref() calls found in the SQL */ refs: string[]; /** @metric() references found in the SQL */ metricRefs: string[]; /** @dim() references found in the SQL */ dimensionRefs: string[]; } export interface SqlColumnReference { column: string; tableAlias?: string; relation?: string; unqualified: boolean; /** True when this is a legal reference to a select-list alias. */ outputAliasReference?: boolean; } /** An equality join condition `left.col = right.col`, aliases resolved to relations. */ export interface SqlJoinCondition { leftRelation?: string; leftColumn: string; rightRelation?: string; rightColumn: string; joinType?: string; } /** An aggregate function reference in the SELECT list, e.g. `SUM(o.amount)`. */ export interface SqlAggregateReference { func: string; distinct: boolean; column?: string; relation?: string; } /** * References owned by one SELECT scope. Keeping aliases scoped prevents an * inner CTE alias from being treated as a peer of an outer alias during * ambiguity validation. */ export interface SqlReferenceScope { id: string; columns: SqlColumnReference[]; aliasToRelation: Record; outputAliases: string[]; } export interface SqlReferenceAnalysis { parsed: boolean; statementTypes: string[]; tables: string[]; ctes: string[]; /** Query-internal FROM/JOIN subquery aliases, never physical relations. */ derivedRelations: string[]; columns: SqlColumnReference[]; /** Equality join conditions (for grain / fan-out analysis). Empty when unparsed. */ joins: SqlJoinCondition[]; /** Aggregate function references in the SELECT list. Empty when unparsed. */ aggregates: SqlAggregateReference[]; aliasToRelation: Record; scopes: SqlReferenceScope[]; error?: string; } export interface SqlAnalyticalSignature { version: 1; statementType: 'select'; canonicalAst: string; positionalParameters: number[]; } /** Parser-owned identity for one named SELECT output expression. */ export interface SqlOutputExpressionSignature { version: 1; outputAlias: string; canonicalExpression: string; operators: string[]; columns: string[]; aggregateFunctions: string[]; } export interface GeneratedAnalyticalOutputSignature extends SqlOutputExpressionSignature { /** Aggregate calls and their parser-resolved physical inputs for this output only. */ aggregateInputs: SqlAggregateReference[]; } /** Parser-owned semantic facts for a generated analytical SELECT. */ export interface GeneratedAnalyticalSqlSignatureV1 { version: 1; canonicalAst: string; outputs: GeneratedAnalyticalOutputSignature[]; groupByColumns: string[]; filterExpression?: string; orderBy: Array<{ expression: string; direction: 'asc' | 'desc'; }>; limit?: { kind: 'literal'; value: number; } | { kind: 'parameter'; value: string; }; sourceRelations: string[]; joins: SqlJoinCondition[]; setOperations: string[]; hasWindow: boolean; positionalParameters: number[]; } /** * Extract table references from a SQL string. * * Identifies tables in FROM and JOIN clauses, filters out CTE definitions, * and detects ref("block_name") calls. */ export declare function extractTablesFromSql(sql: string): SqlParseResult; export declare function analyzeSqlReferences(sql: string, dialect?: string): SqlReferenceAnalysis; /** * Parser-owned same-plan authority for bounded SQL repair. The complete SELECT * AST is retained (including projections, predicates, grouping, joins, * aggregates, ordering, and bounds); only parser locations and identifier * quoting/case are normalized. Unsupported or multi-statement SQL has no * signature and therefore cannot be automatically repaired. */ export declare function buildSqlAnalyticalSignature(sql: string, dialect?: string): SqlAnalyticalSignature | undefined; /** * Return an exact expression-tree signature for one unambiguous named output. * Relation aliases are intentionally excluded from the expression identity; * callers must bind physical relations separately. Column names, functions, * operators, CASE predicates, literals, and nesting remain in the signature. */ export declare function buildSqlOutputExpressionSignature(sql: string, outputAlias: string, dialect?: string): SqlOutputExpressionSignature | undefined; export declare function buildGeneratedAnalyticalSqlSignature(sql: string, dialect?: string): GeneratedAnalyticalSqlSignatureV1 | undefined; //# sourceMappingURL=sql-parser.d.ts.map