tachibana22 commented on PR #1152: URL: https://github.com/apache/incubator-seata-go/pull/1152#issuecomment-5476958796
本 PR(#1152)接手 #1120,完成了 SQL Parser 向上游官方(`[github.com/pingcap/tidb/pkg/parser](https://github.com/pingcap/tidb/pkg/parser)`)的迁移,并解决了此前 Review 中提出的阻断性与安全性问题。 ### 一、 变更背景与核心目标 1. **解耦非官方 Fork**:将核心 SQL 解析依赖由停止维护的 `[github.com/arana-db/parser](https://github.com/arana-db/parser)` 切换回上游官方 `[github.com/pingcap/tidb/pkg/parser](https://github.com/pingcap/tidb/pkg/parser)`。 2. **生产代码与 `test_driver` 解耦**:执行器与 Undo Builder 不再直接依赖 `*test_driver.ParamMarkerExpr`,统一基于 `ast.ParamMarkerExpr` 接口与抽象辅助方法进行参数提取。 3. **AST 遍历全分支对齐与参数等价性**:补全 Undo Log Builder 对 13 种复杂 AST 节点的递归遍历,确保多子查询、Case When 等场景下参数顺序一致,防止回滚数据错位损坏。 4. **Go 1.25 工具链与破坏性变更登记**:由于上游 Parser 的 `go.mod` 声明要求 Go 1.25,同步提升最低版本并在变更日志中显式声明 Breaking Change,同时适配语法门禁。 ### 二、 改动文件详细说明 (File-by-File Details) #### 1. 版本与变更声明 (Documentation) - **`changes/dev.md` / `changes/dev_zh.md`** - 在 `### breaking change:` 模块中关联 `[#1104]`,显著标注将最低 Go 版本从 1.20 提升至 1.25 的 `action required` 说明。 - 在 `### optimize:` 中关联 `[#1104]`,登记将 Parser 依赖迁移回官方仓库的优化项。 #### 2. SQL Parser 核心抽象与排序 (Parser Factory) - **`pkg/datasource/sql/parser/parser_factory.go`** - **Driver 初始化注释**:保留 `_ "[github.com/pingcap/tidb/pkg/parser/test_driver](https://github.com/pingcap/tidb/pkg/parser/test_driver)"` 匿名导入并补充架构注释,说明其用于向 TiDB Parser 注册实例化 `ParamMarkerExpr` 的全局驱动。 - **接口抽象提取**:新增 `getParamMarkerOffset` 与导出的 `GetParamMarkerOrder` 函数,优先通过接口断言提取 `Offset` / `Order`,兜底采用反射读取未导出字段,消除业务代码对具体驱动结构体的硬编码依赖。 - **确定性排序**:将 `assignParamMarkerOrders` 中的排序改为 `sort.SliceStable`,并进行切片容量预分配,严格按物理文本偏移量(Offset)为占位符赋予 `Order`。 - **`pkg/datasource/sql/parser/parse_factory_test.go`** - 新增 `TestAssignParamMarkerOrders_EquivalenceMatrix`,覆盖 `Case When`、多子查询嵌套 `JOIN`、`INSERT ... ON DUPLICATE KEY UPDATE` 等复杂 SQL 的参数占位符顺序等价性验证。 - 新增 `TestGetParamMarkerOrder_NonMarkerNode`,验证非参数节点传入时的防御性降级。 #### 3. AT 执行器与锁键生成 (AT Executors) - **`pkg/datasource/sql/exec/at/base_executor.go`** - 移除 `test_driver` 依赖,重构 `traversalArgs` 为 `case ast.ParamMarkerExpr`,统一通过 `parser.GetParamMarkerOrder(expr)` 获取参数索引。 - 清理 `buildSelectFields` 中未使用的 `ctx context.Context` 参数为 `_`。 - **`pkg/datasource/sql/exec/at/base_executor_test.go`** - 新增 `TestBaseExecutorBuildSelectArgs`,验证 `baseExecutor` 在多条件 `BETWEEN`、`IN`、`LIMIT` SQL 下能精准提取并还原 `selectArgs`。 - **`pkg/datasource/sql/exec/at/select_for_update_executor.go`** - 将 `buildLockKey` 中的 `bytes.Buffer` 优化为 `strings.Builder`,提升内存分配性能。 - 增加空主键防御校验(`len(columnNames) == 0` 时直接返回空字符串)。 - 移除冗余的反射方法调用,改用统一的 `reflectx.GetElemDataValue` 与 `getSqlNullValue` 提取底层值,增强锁键构造的鲁棒性。 - **`pkg/datasource/sql/exec/at/select_for_update_executor_test.go`** - 新增 `TestBuildLockKey_EmptyPrimaryKey`,覆盖无主键元数据时的锁键生成路径。 - **`pkg/datasource/sql/exec/at/multi_sequential_executor.go` / `multi_sequential_executor_test.go`** - 将 `ast` 与 `format` 的 import 路径平滑迁移至 `[github.com/pingcap/tidb/pkg/parser](https://github.com/pingcap/tidb/pkg/parser)`。 - **`pkg/datasource/sql/exec/at/insert_executor_test.go`** - 将不推荐的 `gomonkey` import 替换为 `gomock`,规范测试依赖。 #### 4. Undo Log 构建引擎 (Undo Log Builder) - **`pkg/datasource/sql/undo/builder/basic_undo_log_builder.go`** - 移除 `test_driver` 依赖。 - 重构并补齐 `traversalArgs` 的 AST 递归分支,对齐 `base_executor.go` 的 13 种节点类型:涵盖 `BinaryOperationExpr`、`BetweenExpr`、`PatternInExpr`、`ParenthesesExpr`、`Join`、`UnaryOperationExpr`、`FuncCallExpr`、`SubqueryExpr`、`ExistsSubqueryExpr`、`CompareSubqueryExpr`、`PatternLikeOrIlikeExpr`、`IsNullExpr`、`CaseExpr` 以及 `ast.ParamMarkerExpr`。 - 清理 `buildWhereConditionByPKs` 中未使用的 `dbType` 参数。 - **`pkg/datasource/sql/undo/builder/basic_undo_log_builder_test.go`** - 新增 `TestBasicUndoLogBuilder_BuildSelectArgs_ComplexAST` 测试矩阵,覆盖 6 种包含复合函数、嵌套子查询与 Case When 的复杂 SQL 参数提取场景。 #### 5. 网络层工具链适配 (Remoting / Toolchain Fix) - **`pkg/remoting/grpc/listener.go`** - 将单个 channel 监听的 `for { select { case <-ticker.C: ... } }` 语法重构成 Go 1.25 / 新版 `golangci-lint` 推荐的 `for range ticker.C` 语法,确保升至 Go 1.25 后的 CI 构建与静态分析检查通过。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
