# Python CLI schema generation: TypeScript reference Use the existing TypeScript implementation as the reference: - [init](../packages/cli/src/commands/init.ts): table selection and dataset serving. - [dataset generator](../packages/cli/src/generators/dataset-generator.ts): discovery, type mapping, semantic names and suggested measures. - [generate datasets](../packages/cli/src/commands/generate-datasets.ts): regeneration and overwrite protection. - [generator tests](../packages/cli/src/generators/dataset-generator.test.ts): authoring behavior. ## Shared contract `hypequery init --path analytics --tables orders,customers` uses the actual ClickHouse catalog. `--exclude-tables` omits tables; all tables are selected by default (`--all-tables` is accepted explicitly). `--skip-connection` creates an explicit offline demo. Connections use `CLICKHOUSE_*` environment variables. Passwords and tokens are never written to definitions or schema snapshots. Both languages preserve physical-column mappings, use camelCase semantic fields, suggest `totalCount` plus `total` / `avg` for numeric value columns, and exclude identifiers and coordinates from sum/average suggestions. Those aggregates are suggestions, not inferred business metrics. Tenant-looking names are reported, never silently converted to tenant policy. Serving requires auth. ## Python accuracy rules Read `system.tables` and `system.columns` with bound database/table parameters and readonly settings. Record exact source names/types in `schema.json`. Report unsupported arrays/maps/tuples/aggregate states rather than assuming string. Unwrap Nullable/LowCardinality for scalar mapping. Fail before writes on semantic name collisions, invalid identifiers, missing tables or no supported scalar fields. `totalCount` counts the first supported non-nullable column, so it counts every row while staying column-backed for deployment and relationship joins. If no supported non-nullable column exists, Python omits `totalCount`, generates `nonNullCount` over the first supported column and warns that it excludes NULLs. The generated example query uses the count actually emitted. This is a deliberate correction to the TypeScript-style nullable fallback: a count of non-null values must not be labelled a total row count. `time_key` prefers an actual timestamp `created_at`, otherwise the sole timestamp. Multiple alternatives require explicit review. Catalog metadata cannot infer business relationships or trusted tenant boundaries. Qualify sources with the actual database to avoid silently querying a different source when runtime connection settings change. Refuse existing output paths and roll back newly written files on failure. Discovery does not seed or modify data. ## Regeneration contract `hypequery generate datasets` (also accepted as TypeScript's `generate:datasets`) writes `datasets.py` in the current directory, where Python init places it beside `app.py`. TypeScript defaults to `analytics/datasets.ts` because its init uses that layout; Python follows its own init layout instead. `--output` selects a file; `--path` selects a directory. Table inclusion and exclusion reuse init's metadata discovery. Repeat the original selection on every regeneration. `--check` and `--diff` never create directories or change files; drift or a missing file exits 1. Equal contents exit 0 without changing the modification time. Existing differing files require `--force`; force cannot be combined with either read-only option. Writes use a private temporary file, exclusive creation or atomic replacement, and cleanup on failure. Symlink destinations and parents are refused. Replacement keeps the existing file's permissions; new files use the umask default. Filesystems without hard links fall back to exclusive creation. Generation replaces definitions as a whole, including authored tenant_key settings, custom measures and relationships; review the diff before forcing. As in TypeScript, `--force` is the author's decision and is never refused for content. When the replaced file visibly configured `tenant_key`, the command warns that generated definitions do not; detection is best-effort and never executes authored code. Writes compare content and file identity with the pre-discovery snapshot; changes or a newly created destination abort even with `--force`. A per-output lock directory coordinates CLI writers through validation and replacement. It does not lock ordinary editors: avoid editing definitions during regeneration. On a crashed invocation, remove a leftover `.datasets.py.lock` directory only after confirming its owner (whose PID is recorded inside and reported on contention) stopped. Competing generators may create shared parent directories without causing failure. Tenant-column inference and automatic auth configuration remain unimplemented. The `schema.json` from init is explicitly an initial discovery snapshot, not a current catalog cache. Regeneration only updates the requested Python file. ## Remaining TypeScript-only scope chDB, explicit tenant-column generation and context-auth scaffolding remain separate Python work. Do not claim these capabilities as implemented in Python.