{"version":3,"file":"query.mjs","names":[],"sources":["../src/query.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type {\n  DiscoveredColumnReference,\n  SQLCommenterTag,\n  TableReference,\n} from \"./sql/analyzer.js\";\nimport {\n  PostgresExplainStageSchema,\n  type PostgresExplainStage,\n} from \"./sql/database.js\";\nimport type { Nudge } from \"./sql/nudges.js\";\n\nexport type IndexRecommendation = {\n  name?: string;\n  schema: string;\n  table: string;\n  columns: Array<{\n    schema: string;\n    table: string;\n    column: string;\n    sort?: unknown;\n    where?: unknown;\n  }>;\n  where?: string;\n  definition: string;\n};\n\nexport type LiveQueryOptimization =\n  | { state: \"waiting\" }\n  | { state: \"optimizing\" }\n  | { state: \"not_supported\"; reason: string }\n  | {\n      state: \"improvements_available\";\n      cost: number;\n      optimizedCost: number;\n      costReductionPercentage: number;\n      indexRecommendations: IndexRecommendation[];\n      triedIndexNames?: Record<string, string>;\n      indexesUsed: string[];\n      explainPlan?: PostgresExplainStage;\n      optimizedExplainPlan?: PostgresExplainStage;\n    }\n  | {\n      state: \"no_improvement_found\";\n      cost: number;\n      indexesUsed: string[];\n      explainPlan?: PostgresExplainStage;\n    }\n  | { state: \"timeout\"; cost?: number }\n  | { state: \"error\"; error: string };\n\nexport type QueryHash = string;\n\nexport const RecentQuery = z.object({\n  formattedQuery: z.string(),\n  displayQuery: z.string().optional(),\n  username: z.string(),\n  query: z.string(),\n  meanTime: z.number(),\n  calls: z.string(),\n  rows: z.string(),\n  topLevel: z.boolean(),\n  isSystemQuery: z.boolean(),\n  isSelectQuery: z.boolean(),\n  isIntrospection: z.boolean(),\n  isTargetlessSelectQuery: z.boolean(),\n  tableReferences: z.array(z.custom<TableReference>()),\n  columnReferences: z.array(z.custom<DiscoveredColumnReference>()),\n  tags: z.array(z.custom<SQLCommenterTag>()),\n  nudges: z.array(z.custom<Nudge>()).optional().default([]),\n  hash: z.string(),\n  // Coarser identity than `hash`: the fingerprint after the SELECT target list\n  // is stripped of bare column refs and sorted, so queries differing only in\n  // which plain columns they select collapse together. Computed by the analyzer\n  // (RecentQuery.normalizedHash); optional so older analyzer clients that don't\n  // send it still parse.\n  normalizedHash: z.string().optional(),\n  firstSeen: z.number().optional(),\n  optimization: z.custom<LiveQueryOptimization>().optional(),\n  explainPlan: PostgresExplainStageSchema.optional(),\n});\n\nexport type RecentQuery = z.infer<typeof RecentQuery>;\n\nexport const OptimizedQuery = RecentQuery.extend({\n  optimization: z.custom<LiveQueryOptimization>(),\n});\n\nexport type OptimizedQuery = z.infer<typeof OptimizedQuery>;\n\nexport type OptimizationResult = {\n  hash: QueryHash;\n  optimization: LiveQueryOptimization;\n};\n"],"mappings":";;;;AAqDA,MAAa,cAAc,EAAE,OAAO;CAClC,gBAAgB,EAAE,QAAQ;CAC1B,cAAc,EAAE,QAAQ,CAAC,UAAU;CACnC,UAAU,EAAE,QAAQ;CACpB,OAAO,EAAE,QAAQ;CACjB,UAAU,EAAE,QAAQ;CACpB,OAAO,EAAE,QAAQ;CACjB,MAAM,EAAE,QAAQ;CAChB,UAAU,EAAE,SAAS;CACrB,eAAe,EAAE,SAAS;CAC1B,eAAe,EAAE,SAAS;CAC1B,iBAAiB,EAAE,SAAS;CAC5B,yBAAyB,EAAE,SAAS;CACpC,iBAAiB,EAAE,MAAM,EAAE,QAAwB,CAAC;CACpD,kBAAkB,EAAE,MAAM,EAAE,QAAmC,CAAC;CAChE,MAAM,EAAE,MAAM,EAAE,QAAyB,CAAC;CAC1C,QAAQ,EAAE,MAAM,EAAE,QAAe,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;CACzD,MAAM,EAAE,QAAQ;CAMhB,gBAAgB,EAAE,QAAQ,CAAC,UAAU;CACrC,WAAW,EAAE,QAAQ,CAAC,UAAU;CAChC,cAAc,EAAE,QAA+B,CAAC,UAAU;CAC1D,aAAa,2BAA2B,UAAU;CACnD,CAAC;AAIF,MAAa,iBAAiB,YAAY,OAAO,EAC/C,cAAc,EAAE,QAA+B,EAChD,CAAC"}