import "../../shared/src/valita.js";
import { orderingSchema, conditionSchema } from "./ast.js";
import { rowSchema } from "./data.js";
import { record, number, array, object, union, unknown, null as _null, literal, boolean, string } from "@badrap/valita";
const rowCountsByQuerySchema = record(number());
const rowCountsBySourceSchema = record(rowCountsByQuerySchema);
const rowsByQuerySchema = record(array(rowSchema));
const rowsBySourceSchema = record(rowsByQuerySchema);
const costEstimateJSONSchema = object({
  startupCost: number(),
  scanEst: number(),
  cost: number(),
  returnedRows: number(),
  selectivity: number(),
  limit: number().optional()
});
const plannerConstraintSchema = record(union(unknown(), _null()));
const attemptStartEventJSONSchema = object({
  type: literal("attempt-start"),
  attemptNumber: number(),
  totalAttempts: number()
});
const connectionCostsEventJSONSchema = object({
  type: literal("connection-costs"),
  attemptNumber: number(),
  costs: array(
    object({
      connection: string(),
      cost: number(),
      costEstimate: costEstimateJSONSchema,
      pinned: boolean(),
      constraints: record(union(plannerConstraintSchema, _null())),
      constraintCosts: record(costEstimateJSONSchema)
    })
  )
});
const connectionSelectedEventJSONSchema = object({
  type: literal("connection-selected"),
  attemptNumber: number(),
  connection: string(),
  cost: number(),
  isRoot: boolean()
});
const constraintsPropagatedEventJSONSchema = object({
  type: literal("constraints-propagated"),
  attemptNumber: number(),
  connectionConstraints: array(
    object({
      connection: string(),
      constraints: record(union(plannerConstraintSchema, _null())),
      constraintCosts: record(costEstimateJSONSchema)
    })
  )
});
const joinTypeSchema = union(
  literal("semi"),
  literal("flipped"),
  literal("unflippable")
);
const planCompleteEventJSONSchema = object({
  type: literal("plan-complete"),
  attemptNumber: number(),
  totalCost: number(),
  flipPattern: number(),
  joinStates: array(
    object({
      join: string(),
      type: joinTypeSchema
    })
  )
});
const planFailedEventJSONSchema = object({
  type: literal("plan-failed"),
  attemptNumber: number(),
  reason: string()
});
const bestPlanSelectedEventJSONSchema = object({
  type: literal("best-plan-selected"),
  bestAttemptNumber: number(),
  totalCost: number(),
  flipPattern: number(),
  joinStates: array(
    object({
      join: string(),
      type: joinTypeSchema
    })
  )
});
const nodeTypeSchema = union(
  literal("connection"),
  literal("join"),
  literal("fan-out"),
  literal("fan-in"),
  literal("terminus")
);
const nodeCostEventJSONSchema = object({
  type: literal("node-cost"),
  attemptNumber: number().optional(),
  nodeType: nodeTypeSchema,
  node: string(),
  branchPattern: array(number()),
  downstreamChildSelectivity: number(),
  costEstimate: costEstimateJSONSchema,
  filters: conditionSchema.optional(),
  ordering: orderingSchema.optional(),
  joinType: joinTypeSchema.optional()
});
const nodeConstraintEventJSONSchema = object({
  type: literal("node-constraint"),
  attemptNumber: number().optional(),
  nodeType: nodeTypeSchema,
  node: string(),
  branchPattern: array(number()),
  constraint: union(plannerConstraintSchema, _null()).optional(),
  from: string()
});
const planDebugEventJSONSchema = union(
  attemptStartEventJSONSchema,
  connectionCostsEventJSONSchema,
  connectionSelectedEventJSONSchema,
  constraintsPropagatedEventJSONSchema,
  planCompleteEventJSONSchema,
  planFailedEventJSONSchema,
  bestPlanSelectedEventJSONSchema,
  nodeCostEventJSONSchema,
  nodeConstraintEventJSONSchema
);
const analyzeQueryResultSchema = object({
  warnings: array(string()),
  syncedRows: record(array(rowSchema)).optional(),
  syncedRowCount: number(),
  start: number(),
  /** @deprecated Use start + elapsed instead */
  end: number(),
  elapsed: number().optional(),
  afterPermissions: string().optional(),
  /** @deprecated Use readRowCountsByQuery */
  vendedRowCounts: rowCountsBySourceSchema.optional(),
  /** @deprecated Use readRows */
  vendedRows: rowsBySourceSchema.optional(),
  sqlitePlans: record(array(string())).optional(),
  readRows: rowsBySourceSchema.optional(),
  readRowCountsByQuery: rowCountsBySourceSchema.optional(),
  readRowCount: number().optional(),
  dbScansByQuery: rowCountsBySourceSchema.optional(),
  joinPlans: array(planDebugEventJSONSchema).optional()
});
export {
  analyzeQueryResultSchema,
  attemptStartEventJSONSchema,
  bestPlanSelectedEventJSONSchema,
  connectionSelectedEventJSONSchema,
  nodeConstraintEventJSONSchema,
  planFailedEventJSONSchema,
  rowCountsByQuerySchema,
  rowCountsBySourceSchema,
  rowsByQuerySchema,
  rowsBySourceSchema
};
//# sourceMappingURL=analyze-query-result.js.map
