Expand description
Schema inference and type mapping.
§Three-Tier Inference
The tier is selected automatically based on the data source:
| Tier | Source | Strategy |
|---|---|---|
| Exact | Arrow IPC, Parquet | Types read from file metadata — zero guessing. |
| Structural | JSON | Full scan of all objects. Per-column type widening (see below). |
| Heuristic | CSV | Header row for names, first 1 000 rows sampled for types. Ambiguous → TEXT. |
All tiers can be bypassed with an explicit schema override from the caller.
§Type Widening Rules (Structural / Heuristic Tiers)
When a JSON or CSV column contains mixed types across rows, the widening chain determines the final type:
Null < Bool < Int < BigInt < Double < Date < Timestamp < TextSpecific rules (implemented in resolve_type):
- All null → TEXT (safe catch-all).
- Uniform non-null → that type, unchanged.
- Mixed numeric (Int /
BigInt/ Double) → widest numeric type seen. - Any other mix (e.g. Bool + Int, Date + Text) → TEXT.
§Arrow / Parquet Type Mapping
The exact-tier mapping from Arrow types to Hyper SQL types is handled by
crate::ingest_arrow::arrow_type_to_hyper. Key mappings:
| Arrow | Hyper |
|---|---|
| Int16/Int32/Int64 | SMALLINT/INT/BIGINT |
| UInt16/UInt32/UInt64 | INT/BIGINT/BIGINT (promoted to signed) |
| Float32/Float64 | DOUBLE PRECISION |
| Utf8/LargeUtf8 | TEXT |
| Date32/Date64 | DATE |
| Timestamp(_, None/Some) | TIMESTAMP / TIMESTAMPTZ |
| Decimal128/Decimal256 | NUMERIC |
Structs§
- Column
Schema - A column’s name, Hyper SQL type (as a string like
"INT"or"DOUBLE PRECISION"), and nullability. This is the internal schema representation shared across all ingest and table-creation paths.
Functions§
- apply_
schema_ override - Overlay a user-provided override (
{"column_name": "TYPE", ...}) on top of an already-inferred column list, preserving the inferred column order and replacing only the types listed in the override. - build_
table_ def - Build a hyperdb-api
TableDefinitionfrom a list ofColumnSchema. - infer_
csv_ schema - Infer a
ColumnSchemafor each CSV column (Tier 3). - infer_
json_ schema - Infer a
ColumnSchemafor each key in a JSON array of objects (Tier 2). - map_
hyper_ type - Map a user-facing type name string (e.g.
"INT","NUMERIC(12,2)") to ahyperdb_api::SqlType. AcceptsPostgreSQLaliases (INT4,FLOAT8,VARCHAR) so schema overrides from diverse sources work without normalization. - normalize_
schema_ param - Normalize a raw MCP
schemaparameter value into the column-name → type map expected byapply_schema_overrideandparse_schema_override. - parse_
schema_ override - Parse a user-provided schema override (
{"column_name": "TYPE", ...}) into aVec<ColumnSchema>. Validates each type name againstmap_hyper_typeand rejects unknown types early. All override columns are marked nullable. - widen_
csv_ numeric_ columns - Second-pass streaming widen: re-read the given CSV source and, for columns
the first-pass inference classified as
INT,BIGINT, orDOUBLE PRECISION, promote the type if a value outside its current range appears anywhere in the file (not just the first 1 000 rows).