Skip to main content

autodml

Auto Modeling Definition Language — 用纯 Python / YAML / 直连数据库三种方式定义、校验、导出语义层模型(mdl.json)的 Python 库。

为什么

语义层(semantic layer)是 AI 生成 SQL 前的“业务翻译层”:逻辑表、计算列、表关系、指标。本库在纯 Python 生态内完成 MDL 的**生成(反射活库)→ 校验(lint)→ 导出(JSON/YAML)**闭环。

安装

pip install autodml            # 核心(YAML/JSON/校验)
pip install "autodml[db]"      # + SQLAlchemy 反射
pip install "autodml[dev]"     # + pytest

快速上手

1. 纯 Python 定义

from autodml import Manifest, Model, Column, Relationship, Metric

m = (
    Manifest(manifest="shop")
    .add(
        Model(name="customers", refSql="public.customers", columns=[
            Column(name="id", type="integer", isPrimary=True, notNull=True),
            Column(name="name", type="string"),
        ]),
        Model(name="orders", refSql="public.orders", columns=[
            Column(name="id", type="integer", isPrimary=True, notNull=True),
            Column(name="customer_id", type="integer"),
            Column(name="amount", type="decimal"),
        ]),
        Relationship(
            name="orders_customer_fk", relationType="manyToOne",
            left={"modelName": "orders", "columnName": "customer_id"},
            right={"modelName": "customers", "columnName": "id"},
        ),
        Metric(name="total_revenue", baseModel="orders",
               expression="SUM(amount)", type="decimal"),
    )
)

from autodml.validate import validate
print(validate(m))          # OK / 逐条 issue

from autodml.serialize import write_json
write_json(m, "mdl.json")   # camelCase 输出

2. 从活库反射

from autodml.introspect import reflect

m = reflect(
    "postgresql://user:***@localhost:5432/mydb",
    schema="public",
    include=["orders", "customers", "order_*"],
    exclude=["*_tmp"],
    infer_relationships=True,   # FK -> manyToOne/oneToOne 推断
)

3. YAML 目录

proj/
  project.yml
  models/customers.yml
  models/orders.yml
  relationships.yml
  metrics.yml
  views/active_customers.yml
# models/orders.yml
name: orders
refSql: public.orders
columns:
  - name: id
    type: integer
    isPrimary: true
    notNull: true
  - name: customer_id
    type: integer

CLI

autodml init ./proj                     # 脚手架
autodml validate ./proj                 # lint(error 退出码 1)
autodml export ./proj -o mdl.json       # 导出 mdl.json
autodml export ./proj -o out/ --format yaml-dir
autodml reflect "postgresql://..." --schema public -o mdl.json

MDL 核心概念

概念 说明
Model 逻辑表:refSql 指向物理表,columns + calculations
Column 列,type 用 MDL 类型(string/integer/decimal/…)
Calculation 计算列(expression)
Relationship 表关系:oneToOne / oneToMany / manyToOne / manyToMany,左右 side
Metric 指标:baseModel + expression(如 SUM(amount))
View 查询态视图:statement 是基于模型名的 SQL

校验规则(v0.1)

  • 命名(标识符合法性、保留字、_tmp/_backup 后缀警告)
  • 类型(MDL 类型白名单)
  • 重复(model/column/calculation/relationship 重名)
  • 引用完整性(relationship 指向的 model/column 存在、metric 的 baseModel 存在、view 语句里的标识符已知)
  • 结构(refSql/tableReference 二选一、表达式非空)

error 阻断(ValidationError),warning 只提示。

设计边界(v0.1)

  • 序列化输出 camelCase mdl.json;复杂特性(nested struct、enumLabels、view 的 dimensions/measures 高级声明)留到后续版本
  • 反射用 SQLAlchemy inspect,理论上支持所有 SQLAlchemy 方言(PostgreSQL/MySQL/SQLite/DuckDB…)
  • manyToOne/oneToOne 推断基于 PK 约束;非规范库建议手工在 YAML 里覆盖

开发

git clone … && cd autodml
python3 -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/python -m pytest tests/ -q

License

MIT

Metadata

Release files for autodml 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autodml 0.1.1
File Size Uploaded
autodml-0.1.1.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autodml 0.1.1
File Interpreter ABI Platform
autodml-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 35.1 kB

Release files / autodml-0.1.1.tar.gz

Download URL autodml-0.1.1.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e0dd7956093aa3fff1fa028cbdfd056d0c5f1d6e15b86a548e856c315b3a37e8
BLAKE2b-256 checksum
How to use checksums
ed130d2e17042a03657e6fd7c62a706b27581b434c38af4f4723f127c874019d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / autodml-0.1.1-py3-none-any.whl

Download URL autodml-0.1.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b39b8ed32e635c2434946d4c63268430a13057d8b3cc86c9bc8c2ecdc9cc40a4
BLAKE2b-256 checksum
How to use checksums
df66c3f6a1276009862d1fccf75e10f3d7238436903cf4218dc1e3f74603ef71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

0.1.2

2 release files

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page