-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Labels
P0Critical priorityCritical priorityanalysis-modeAnalysis Mode featureAnalysis Mode featureenhancementNew feature or requestNew feature or requestin-progressWork currently in progressWork currently in progressintegration-testRequires integration test infrastructureRequires integration test infrastructureperformancePerformance improvementPerformance improvementphase-2Phase 2 roadmapPhase 2 roadmap
Description
Parent Epic
依賴
- [Phase2-1.4a] IGroupByStrategy 介面 + GroupByStrategyResolver #94 (1.4a IGroupByStrategy 介面)
任務
實作 server-side GroupBy 策略,讓 DB 執行 GROUP BY + 聚合,避免 Take(50K) 全表載入。
實作
新增 Analysis/ServerSideGroupByStrategy.cs
核心:將動態維度/度量轉為 EF Core IQueryable.GroupBy().Select() Expression Tree。
public class ServerSideGroupByStrategy : IGroupByStrategy
{
public List<Dictionary<string, object?>> Execute<TModel>(
IQueryable<TModel> query, AnalysisQueryRequest req,
Dictionary<string, AnalysisFieldMeta> wl)
{
// 1. 建構 GroupBy key Expression(匿名型別或 Tuple)
// 2. 建構 Select 投影 Expression(維度 + 聚合函式)
// 3. 執行 IQueryable → DB 做 GROUP BY
// 4. 轉為 List<Dictionary<string, object?>>
}
}技術挑戰
- 動態 GroupBy key:維度數量不固定(1-3),需動態建構 Expression
- 方案 A:
new { Key1 = x.Field1, Key2 = x.Field2 }匿名型別(需 Reflection.Emit) - 方案 B:Tuple<string, string, string>(固定 3 slots,未用的填 "")
- 方案 C:組合為 string key(DB 端 CONCAT),但 Oracle CONCAT 限 2 參數
- 建議方案 B:Tuple 最安全,EF Core 兩個 provider 都能翻譯
- 方案 A:
- 聚合 Expression:
g.Sum(x => x.Amount)需動態建構Expression<Func<TModel, decimal>> - Oracle 限制:複雜 GroupBy + Select 可能 fallback to client evaluation → 需 catch 並降級
MSSQL/Oracle 兼容
| 操作 | MSSQL | Oracle | 策略 |
|---|---|---|---|
| GroupBy(Tuple) | ✅ | ✅(需驗證) | Tuple 方案 |
| Sum/Count/Avg/Max/Min | ✅ | ✅ | EF Core 標準翻譯 |
| Take(MaxRows) | ✅ TOP N | ✅ FETCH FIRST N | EF Core 標準翻譯 |
驗證
- SQLite 單元測試:基本 GroupBy 正確性
- MSSQL 整合測試(
[TestCategory("IntegrationMssql")]):- 單維度 + 單度量
- 3 維度 + 3 度量(最大組合)
- 空結果集
- 截斷行為(>10K groups)
- 驗證實際生成的 SQL(用
ToQueryString())
測試基礎設施
- Test 專案加入
Microsoft.EntityFrameworkCore.SqlServerpackage - 連線字串:
Server=localhost;Database=WTM_Analysis_Test;User=sa;Password=BmsDocker2024!;TrustServerCertificate=true - 環境變數
WTM_TEST_MSSQL=1啟用
複雜度
高(Phase 2 最複雜的單一任務)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P0Critical priorityCritical priorityanalysis-modeAnalysis Mode featureAnalysis Mode featureenhancementNew feature or requestNew feature or requestin-progressWork currently in progressWork currently in progressintegration-testRequires integration test infrastructureRequires integration test infrastructureperformancePerformance improvementPerformance improvementphase-2Phase 2 roadmapPhase 2 roadmap