mongo-2-sql 0.1.3__tar.gz → 0.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/PKG-INFO +122 -3
  2. mongo_2_sql-0.1.3/src/mongo_2_sql.egg-info/PKG-INFO → mongo_2_sql-0.1.4/README.md +591 -494
  3. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/pyproject.toml +3 -2
  4. mongo_2_sql-0.1.4/src/mongo_2_sql/core/cte_manager.py +72 -0
  5. mongo_2_sql-0.1.4/src/mongo_2_sql/core/cte_stage_base.py +101 -0
  6. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/expr_base.py +4 -4
  7. mongo_2_sql-0.1.4/src/mongo_2_sql/core/expr_parser_base.py +353 -0
  8. mongo_2_sql-0.1.4/src/mongo_2_sql/core/expression_resolver.py +101 -0
  9. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/stage_base.py +18 -15
  10. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/stage_loader.py +1 -0
  11. mongo_2_sql-0.1.4/src/mongo_2_sql/renderer/sqlite.py +234 -0
  12. mongo_2_sql-0.1.4/src/mongo_2_sql/stages/add_fields/__init__.py +9 -0
  13. mongo_2_sql-0.1.4/src/mongo_2_sql/stages/add_fields/stage.py +137 -0
  14. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/group/expr.py +7 -34
  15. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/group/stage.py +57 -12
  16. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/match/expr.py +9 -40
  17. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/project/expr.py +126 -14
  18. mongo_2_sql-0.1.4/src/mongo_2_sql/stages/project/stage.py +203 -0
  19. mongo_2_sql-0.1.3/README.md → mongo_2_sql-0.1.4/src/mongo_2_sql.egg-info/PKG-INFO +613 -473
  20. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql.egg-info/SOURCES.txt +9 -1
  21. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql.egg-info/requires.txt +1 -0
  22. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/tests/test.py +2 -1
  23. mongo_2_sql-0.1.4/tests/test1.py +45 -0
  24. mongo_2_sql-0.1.4/tests/test_complex_pipelines.py +287 -0
  25. mongo_2_sql-0.1.3/src/mongo_2_sql/renderer/sqlite.py +0 -131
  26. mongo_2_sql-0.1.3/src/mongo_2_sql/stages/project/stage.py +0 -85
  27. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/LICENSE +0 -0
  28. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/setup.cfg +0 -0
  29. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/__init__.py +0 -0
  30. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/converter.py +0 -0
  31. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/__init__.py +0 -0
  32. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/ast_base.py +0 -0
  33. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/expr_registry.py +0 -0
  34. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/core/pipeline_runtime.py +0 -0
  35. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/main.py +0 -0
  36. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/renderer/__init__.py +0 -0
  37. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/__init__.py +0 -0
  38. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/group/__init__.py +0 -0
  39. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/limit/__init__.py +0 -0
  40. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/limit/stage.py +0 -0
  41. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/lookup/__init__.py +0 -0
  42. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/lookup/stage.py +0 -0
  43. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/match/__init__.py +0 -0
  44. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/match/stage.py +0 -0
  45. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/project/__init__.py +0 -0
  46. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/sort/__init__.py +0 -0
  47. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/sort/stage.py +0 -0
  48. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/unwind/__init__.py +0 -0
  49. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/stages/unwind/stage.py +0 -0
  50. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql/utils.py +0 -0
  51. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql.egg-info/dependency_links.txt +0 -0
  52. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql.egg-info/entry_points.txt +0 -0
  53. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/src/mongo_2_sql.egg-info/top_level.txt +0 -0
  54. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.4}/tests/test_all_examples.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mongo-2-sql
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: A library to convert MongoDB aggregation pipelines to SQL parser
5
5
  Author-email: Bao Bingbo <baob2@outlook.com>
6
6
  License: MIT License
@@ -17,12 +17,50 @@ Requires-Python: >=3.10
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
19
  Requires-Dist: typing-extensions>=4.0.0; python_version < "3.10"
20
+ Requires-Dist: sqlparse>=0.5.5
20
21
  Dynamic: license-file
21
22
 
22
23
  # Mongo to SQL
23
24
 
24
25
  Mongo to SQL 是一个将 MongoDB 聚合管道转换为 SQL 查询语句的工具。
25
26
 
27
+ ## 版本更新 v0.1.4
28
+
29
+ ### 新增功能
30
+
31
+ #### 1. CTE(公用表表达式)统一处理
32
+ - **$addFields、$project、$group 阶段现在使用 CTE 方式处理**
33
+ - 将复杂的聚合管道拆分为多个SQL片段,使用 WITH 子句拼接
34
+ - 自动管理 CTE 命名和依赖关系
35
+ - 适用于多次投影、多层分组等复杂场景
36
+
37
+ #### 2. 表达式解析器统一架构
38
+ - **ExpressionResolver**: 统一的表达式解析器入口,避免 stage 之间相互 import
39
+ - **BaseExpressionParser**: 基础表达式解析器基类,减少代码重复
40
+ - 提供通用操作符集合、字段引用解析、值转换等基础功能
41
+
42
+ #### 3. SQL 格式化输出
43
+ - 使用 sqlparse 库自动格式化 SQL
44
+ - SELECT、FROM、WHERE、GROUP BY 等关键字独占一行
45
+ - 字段列表缩进对齐,提高可读性
46
+ - WHERE 子句添加注释提示可扩展性
47
+
48
+ #### 4. Project 阶段增强
49
+ - 支持字段重命名语法: `{"new_name": "$old_name"}`
50
+ - 聚合后字段引用自动处理(无需添加表前缀)
51
+ - 支持包含模式和排除模式
52
+
53
+ #### 5. Stage 解耦架构
54
+ - 各 stage 表达式解析器通过统一的 ExpressionResolver 进行交互
55
+ - 避免模块之间的循环依赖
56
+ - 更易于扩展和维护
57
+
58
+ ### 问题修复
59
+
60
+ - **修复 `_id` 字段双引号问题**: 不再使用 `"_id"`,改为 `_id`
61
+ - **修复聚合后字段引用**: 聚合后的字段不再错误添加表前缀
62
+ - **修复模块导入路径**: 统一使用相对导入,避免 "attempted relative import beyond top-level package" 错误
63
+
26
64
  ## 功能特性
27
65
 
28
66
  - **阶段基础方法支持**: 支持 `$match`, `$project`, `$group`, `$sort`, `$limit`, `$lookup`, `$unwind` 等常用阶段
@@ -111,6 +149,52 @@ ORDER BY count DESC
111
149
  LIMIT 10
112
150
  ```
113
151
 
152
+ ### CTE 处理(复杂聚合管道)
153
+
154
+ 当管道包含多次 $project、$group 或 $addFields 阶段时,系统会自动使用 CTE 方式处理:
155
+
156
+ ```python
157
+ from mongo_2_sql import convert_mongo_pipeline_to_sql
158
+
159
+ # 复杂管道:多次投影和分组
160
+ pipeline = [
161
+ { "$match": { "status": "active", "age": { "$gte": 18 } } },
162
+ { "$project": { "name": 1, "category": 1, "amount": 1 } },
163
+ { "$group": { "_id": "$category", "totalAmount": { "$sum": "$amount" }, "count": { "$sum": 1 } } },
164
+ { "$project": { "_id": 1, "totalAmount": 1, "count": 1 } },
165
+ { "$match": { "totalAmount": { "$gte": 18 } } },
166
+ { "$sort": { "totalAmount": -1 } },
167
+ { "$limit": 5 }
168
+ ]
169
+
170
+ sql, metadata = convert_mongo_pipeline_to_sql(pipeline, "transactions")
171
+ print(sql)
172
+ ```
173
+
174
+ 输出(自动使用 CTE 格式化):
175
+ ```sql
176
+ WITH cte_1 AS
177
+ (SELECT t.category AS _id,
178
+ SUM(t.amount) AS totalAmount,
179
+ COUNT(*) AS COUNT
180
+ FROM transactions AS t
181
+ WHERE 1=1
182
+ AND (t.status = 'active')
183
+ AND (t.age >= 18)
184
+ GROUP BY t.category),
185
+ cte_2 AS
186
+ (SELECT t._id AS _id,
187
+ t.totalAmount AS totalAmount,
188
+ t.count AS COUNT
189
+ FROM cte_1 AS t)
190
+ SELECT *
191
+ FROM cte_2 AS t
192
+ WHERE 1=1
193
+ AND (t.totalAmount >= 18)
194
+ ORDER BY t.totalAmount DESC
195
+ LIMIT 5
196
+ ```
197
+
114
198
  ### 面向对象用法
115
199
 
116
200
  ```python
@@ -297,7 +381,27 @@ FROM products AS t
297
381
  GROUP BY t.category
298
382
  ```
299
383
 
300
- ### 示例 5: 字符串连接
384
+ ### 示例 5: 字段重命名
385
+
386
+ ```python
387
+ pipeline = [
388
+ {
389
+ "$project": {
390
+ "product_name": "$name",
391
+ "total_value": { "$multiply": ["$price", "$quantity"] }
392
+ }
393
+ }
394
+ ]
395
+ ```
396
+
397
+ ```sql
398
+ SELECT
399
+ t.name AS product_name,
400
+ (t.price * t.quantity) AS total_value
401
+ FROM products AS t
402
+ ```
403
+
404
+ ### 示例 6: 字符串连接
301
405
 
302
406
  ```python
303
407
  pipeline = [
@@ -319,7 +423,7 @@ SELECT
319
423
  FROM users AS t
320
424
  ```
321
425
 
322
- ### 示例 6: JOIN 查询
426
+ ### 示例 7: JOIN 查询
323
427
 
324
428
  ```python
325
429
  pipeline = [
@@ -479,6 +583,21 @@ MIT License - 详见 [LICENSE](LICENSE) 文件
479
583
 
480
584
  ## 变更日志
481
585
 
586
+ ### v0.1.4 (2025-02-13)
587
+
588
+ **新增功能:**
589
+ - CTE 统一处理框架,支持 $addFields、$project、$group 阶段切片
590
+ - ExpressionResolver 统一表达式解析器,避免 stage 间耦合
591
+ - BaseExpressionParser 基础解析器类,减少代码重复
592
+ - SQL 格式化输出(使用 sqlparse)
593
+ - Project 阶段字段重命名支持
594
+ - Stage 解耦架构设计
595
+
596
+ **问题修复:**
597
+ - 修复 `_id` 字段双引号问题
598
+ - 修复聚合后字段引用错误
599
+ - 修复模块导入路径问题
600
+
482
601
  ### v0.1.3 (2025-02-07)
483
602
 
484
603
  **改进:**