Metadata-Version: 2.4
Name: wedata-dq
Version: 1.0.0
Summary: WeData Data Quality Expectations - Databricks DLT compatible decorators for PySpark
Home-page: https://git.woa.com/wedata/wedata-dq
Author: WeData Team
Author-email: wedata@tencent.com
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: pyspark>=3.3.0

# WeData DQ Expectations Demo 4 — wedata.dq 包

## 概述

本 Demo 演示如何通过 **pip install wedata-dq** 安装包后，使用 `@expect` 装饰器语法进行数据质量验证。

对标 Databricks DLT 的 `@dlt.expect()` 装饰器，WeData 提供自己的 `wedata.dq` 包，用户只需：

```python
from wedata.dq import table, expect, expect_or_drop, expect_or_fail

@table
@expect("valid_age", "age BETWEEN 18 AND 100", severity="warn")
@expect("positive_amount", "amount > 0", severity="error")
def transform_orders():
    return spark.sql("SELECT * FROM raw_orders")
```

## 目录结构

```
wedata_demo_4/
├── wedata/                    # ⭐ wedata.dq Python 包源码
│   ├── __init__.py            # 包入口，版本信息
│   └── dq/
│       ├── __init__.py        # 导出 table / expect / expect_or_drop / expect_or_fail
│       ├── decorators.py      # 装饰器定义（规则注册）
│       └── runtime.py         # 运行时引擎（规则执行 + 指标写入）
├── pyproject.toml             # 构建配置
├── setup.cfg                  # 包元数据（name=wedata-dq, version=1.0.0）
├── demo_pipeline.py           # 演示脚本（使用 wedata.dq 包）
├── requirements.txt           # 依赖: pyspark
├── run_demo.sh                # 一键运行脚本
└── README.md                  # 本文件
```

## 快速开始

### 方式 1: 一键运行

```bash
cd wedata_demo_4/
bash run_demo.sh
```

### 方式 2: 手动运行

```bash
cd wedata_demo_4/
pip install pyspark
python demo_pipeline.py
```

## 生产环境安装方式（DLC 引擎）

### 构建 wheel 包

```bash
cd wedata_demo_4/
pip install build
python -m build
# 输出: dist/wedata_dq-1.0.0-py3-none-any.whl
```

### 上传到外网

**方式A: COS 直接上传（最快）**

将 `dist/wedata_dq-1.0.0-py3-none-any.whl` 上传到 COS 桶（如 `wedata-dq-1258344699.cos.ap-guangzhou.myqcloud.com/release/`），用户脚本中 pip install 即可。

**方式B: 腾讯内网 PyPI 源**

上传到 https://mirrors.tencent.com/repository/pypi/tencent_pypi/（需联系基础架构同学开通权限）。

### 用户 DLC 脚本中使用

```python
# DLC 用户脚本开头，pip install 安装 wedata-dq 包
import subprocess

# 方式A: 从 COS 直接安装
subprocess.check_call([
    "pip", "install",
    "https://wedata-dq-1258344699.cos.ap-guangzhou.myqcloud.com/release/wedata_dq-1.0.0-py3-none-any.whl"
])

# 方式B: 从腾讯内网 PyPI 源安装
subprocess.check_call([
    "pip", "install", "-i", "https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple/",
    "--trusted-host", "mirrors.tencent.com",
    "wedata-dq"
])

# 安装后直接使用
from wedata.dq import table, expect, expect_or_drop, expect_or_fail, init_runtime

@table
@expect("valid_age", "age BETWEEN 18 AND 100", severity="warn")
def transform_customers():
    return spark.sql("SELECT * FROM raw_customers")
```

## 核心设计

### 装饰器对标 Databricks DLT

| Databricks DLT | WeData wedata.dq |
|---|---|
| `@dlt.table` | `@table` |
| `@dlt.expect("rule", "expr")` | `@expect("rule", "expr")` |
| `@dlt.expect_or_drop("rule", "expr")` | `@expect_or_drop("rule", "expr")` |
| `@dlt.expect_or_fail("rule", "expr")` | `@expect_or_fail("rule", "expr")` |

### 三种违规策略

| 策略 | 装饰器 | 行为 |
|------|--------|------|
| **warn** | `@expect` | 记录违规行，不阻断数据流 |
| **drop** | `@expect_or_drop` | 自动丢弃违规行 |
| **fail** | `@expect_or_fail` | 发现违规则抛异常，终止作业 |

### 执行流程

```
用户脚本 import wedata.dq
  → @expect 装饰器在编译期注册规则
    → @table 装饰器在运行期拦截函数调用
      → apply_expectations() 应用规则
        → F.expr() 编译 SQL 约束
          → 统计通过/失败行数
            → 按策略处理 (warn/drop/fail)
              → 写入 wedata_dq_expectations 监控表
```

### 与 Demo 2/3 的区别

| 对比维度 | Demo 2 (方案2) | Demo 3 (方案3) | **Demo 4 (新方案)** |
|---------|---------------|---------------|-------------------|
| 包形式 | 独立脚本内嵌类 | Java 注入源码 | **标准 Python 包** |
| 安装方式 | 无（脚本自带） | Java 拼接注入 | **pip install wedata-dq** |
| 分发渠道 | ❌ 复制代码 | ❌ 依赖 Java 注入器 | **✅ COS / PyPI / --py-files** |
| 用户导入 | `wedata_dlt = WedataExpectations()` | 不需要 import | **`from wedata.dq import expect`** |
| 对标 DLT | 需手动创建实例 | 源码内联 | **`import wedata.dq` 即可** |
| 版本管理 | ❌ 无 | ❌ 改模板文件 | **✅ pip 版本号管理** |

## 测试数据说明

| 表 | 行数 | 质量问题 |
|----|------|---------|
| raw_customers | 5 | Charlie 邮箱为空(drop), David 年龄/国家码不合法(warn) |
| raw_orders | 6 | #103 customer_id 为空(fail), #104 金额为负(error), #105 状态不合法(warn) |

预期结果:
- customers 表: 4 行（Charlie 被 drop）
- orders 表: 不存在（#103 触发 fail，整表失败）
- 监控表: 包含所有规则的通过/失败统计
