Skip to content

[Phase2-1.4b] ServerSideGroupByStrategy 實作 (MSSQL 整合測試) #95

@cct08311github

Description

@cct08311github

Parent Epic

#90

依賴

任務

實作 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 都能翻譯
  • 聚合 Expressiong.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.SqlServer package
  • 連線字串:Server=localhost;Database=WTM_Analysis_Test;User=sa;Password=BmsDocker2024!;TrustServerCertificate=true
  • 環境變數 WTM_TEST_MSSQL=1 啟用

複雜度

(Phase 2 最複雜的單一任務)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0Critical priorityanalysis-modeAnalysis Mode featureenhancementNew feature or requestin-progressWork currently in progressintegration-testRequires integration test infrastructureperformancePerformance improvementphase-2Phase 2 roadmap

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions