Repository navigation
Description
The PREPARE flow keeps one parsed statement AST across EXECUTEs, and prepared statements are re-bound whenever needRebuild fires (computation_wrapper.go:1478-1494 — forced on every EXECUTE while any live CN is below MORPCVersion32 in a rolling upgrade; also on schema change / sql_mode change / retry invalidation). BuildPlan at prepare time (mysql_cmd_executor.go:3107 buildPlanWithAuthorization; retained at 3170-3176; PrepareStmt does not reject *tree.Load) mutates the shared AST — buildLoad/bindLoad end with:
stmt.Param.Tail.ColumnList = nil
stmt.Param.Tail.Assignments = nil(pkg/sql/plan/build_load.go:701-702; pkg/sql/plan/bind_load.go:232-233).
On the re-bind of the SAME statement (freshPreparedCloneStatement returns the retained original AST for everything except CLONE TABLE, mysql_cmd_executor.go:3259-3284):
- getExternalProject now sees len(ColumnList)==0 and falls into getNormalExternalProject (build_load.go:119-124): EVERY table column is treated as file-provided. A LOAD ... (c1, c2) into a wider table now demands one file field per table column — data lands in the wrong columns with the rest defaulted. No error is raised.
- SET expressions and user-variable columns vanish with Tail.Assignments.
- InitNullMap's re-bind guard — added by fix(sql): retain external NULLIF rules across plan rebuilds #30095 specifically so that PREPARE or a transaction retry keeps its rules — is DEAD on this path: it requires
len(param.Tail.Assignments) > 0(build_load.go:1126-1129), but the very bind that produced the NullMap nil-ed the Assignments slice at 701-702, so the guard never holds and NullMap is rebuilt empty: every NULLIF sentinel is lost.
This is the LOAD DATA face of #30094 ('repeated external-table plan binding loses NULLIF rules'), whose fix (#30095) landed the guard for the CREATE EXTERNAL TABLE DDL path (where Assignments survive) and whose unit test exercised InitNullMap directly — but #30094 also required regression coverage for 'actual parsed ... and LOAD ASTs', and the complete buildLoad/bindLoad double-bind path still destroys the rules on current main: the three tests below FAIL on ccfca4f. The extra capability here is silent wrong-column loading, which is strictly worse than the NULLIF-only loss on the external-table path: prepared/rebound LOADs rewrite data into wrong columns with default-fill instead of erroring.
Consequences (reproduced): second bind of the statement LOAD DATA INLINE ... (n_nationkey, n_regionkey, n_name, @v) SET n_comment=NULLIF(@v,'NULL') produces TbColToDataCol where n_regionkey maps to input position 2 instead of 1 (all table columns shifted), the __mo_load_user_var raw column is gone, and the NULLIF case expression disappears from the load projection; for field-referencing NULLIF (SET n_comment=NULLIF(n_comment,'NULL')) param.NullMap is empty after the re-bind.
Fix direction: make bindLoad/buildLoad not mutate the shared AST (work on a copied Tail: copy ColumnList/Assignments before consuming), or make InitNullMap's guard key off the surviving NullMap alone (param.NullMap != nil => keep) AND preserve ColumnList for re-binds; add the double-bind regression test both for legacy buildLoad and modern BuildPlan.
Full reproduction, exactly as Loom ran it
Save as pkg/sql/plan/loom_prepared_load_rebind_test.go, then run from the repo root:
CGO_CFLAGS="-I$(pwd)/cgo -I$(pwd)/thirdparties/install/include" CGO_LDFLAGS="-L$(pwd)/cgo -L$(pwd)/thirdparties/install/lib" go test ./pkg/sql/plan -run 'TestPreparedLoadRebind' -count=1 -v
--- test file ---
// Copyright 2026 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"testing"
"github.com/matrixorigin/matrixone/pkg/sql/parsers"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/stretchr/testify/require"
)
// A PREPARE-built LOAD DATA statement is re-bound on EXECUTE (and on any
// needRebuild trigger: rolling upgrade below MORPCVersion32, schema change,
// sql_mode change). Binding must therefore not consume the shared AST — or
// the plan built from the SAME statement must be identical.
//
// bindLoad/buildLoad (bind_load.go:232-233, build_load.go:701-702) set
// stmt.Param.Tail.ColumnList = nil and stmt.Param.Tail.Assignments = nil
// after the first bind, and InitNullMap's re-bind guard
// (build_load.go:1126-1129) requires len(param.Tail.Assignments) > 0 —
// which the same code just destroyed — so on the second bind the column
// list, SET expressions and NULLIF rules are all lost.
//
// The tests below bind one parsed statement twice, like PREPARE then EXECUTE.
// Expected (correct) behavior: the second bind yields the same plan.
// Actual: FAIL — the re-bound plan mapped every table column as file-provided.
func TestPreparedLoadRebindKeepsColumnListAndNullIfLegacy(t *testing.T) {
ctx := NewMockCompilerContext(true, newPlanTestProcess(t))
statement, err := parsers.ParseOne(ctx.GetContext(), dialect.MYSQL, issue29849LoadSQL, 1)
require.NoError(t, err)
defer statement.Free()
load, ok := statement.(*tree.Load)
require.True(t, ok)
nation := ctx.tables["nation"]
require.NotNil(t, nation)
nation.Name2ColIndex = make(map[string]int32, len(nation.Cols))
for i, column := range nation.Cols {
nation.Name2ColIndex[column.Name] = int32(i)
}
// first bind: PREPARE
firstPlan, err := buildLoad(load, ctx, false)
require.NoError(t, err)
assertIssue29849LoadUserVariablePlan(t, firstPlan)
// second bind of the SAME statement: EXECUTE / needRebuild paths re-bind
// the retained AST. The plans must be identical. FAIL here = the defect
// is reproduced: the re-bound plan lost the column list and NULLIF.
secondPlan, err := buildLoad(load, ctx, false)
require.NoError(t, err)
assertIssue29849LoadUserVariablePlan(t, secondPlan)
}
func TestPreparedLoadRebindKeepsColumnListAndNullIfModern(t *testing.T) {
ctx := NewMockCompilerContext(true, newPlanTestProcess(t))
statement, err := parsers.ParseOne(ctx.GetContext(), dialect.MYSQL, issue29849LoadSQL, 1)
require.NoError(t, err)
defer statement.Free()
load, ok := statement.(*tree.Load)
require.True(t, ok)
// PREPARE builds through the modern binder...
firstPlan, err := BuildPlan(ctx, load, false)
require.NoError(t, err)
assertIssue29849LoadUserVariablePlan(t, firstPlan)
// ...and EXECUTE re-binds the same retained AST. FAIL here = defect.
secondPlan, err := BuildPlan(ctx, load, false)
require.NoError(t, err)
assertIssue29849LoadUserVariablePlan(t, secondPlan)
}
// TestPreparedLoadRebindKeepsNullMap uses a field-referencing NULLIF (the
// only kind recorded in NullMap) and asserts the NULLIF rules of the
// retained statement survive the second bind.
func TestPreparedLoadRebindKeepsNullMap(t *testing.T) {
const loadSQL = "LOAD DATA INLINE FORMAT='csv', DATA='1,2,alpha,comment' INTO TABLE nation FIELDS TERMINATED BY ',' (n_nationkey, n_regionkey, n_name, n_comment) SET n_comment=NULLIF(n_comment, 'NULL')"
ctx := NewMockCompilerContext(true, newPlanTestProcess(t))
statement, err := parsers.ParseOne(ctx.GetContext(), dialect.MYSQL, loadSQL, 1)
require.NoError(t, err)
defer statement.Free()
load, ok := statement.(*tree.Load)
require.True(t, ok)
nation := ctx.tables["nation"]
nation.Name2ColIndex = make(map[string]int32, len(nation.Cols))
for i, column := range nation.Cols {
nation.Name2ColIndex[column.Name] = int32(i)
}
_, err = buildLoad(load, ctx, false)
require.NoError(t, err)
require.NotEmpty(t, load.Param.NullMap, "first bind must build the NULLIF rules")
// The PREPARE/EXECUTE re-bind binds the consumed AST.
_, err = buildLoad(load, ctx, false)
require.NoError(t, err)
// FAIL here = the defect is reproduced: the re-bind lost the NULLIF
// rules of the retained (PREPARE) statement. InitNullMap's guard is dead
// because the first bind nil-ed Tail.Assignments.
require.NotEmpty(t, load.Param.NullMap,
"re-bind rebuilt NullMap empty: every NULLIF sentinel is lost (InitNullMap guard at build_load.go:1126-1129 is dead because Assignments was nil-ed)")
}Found by Loom bug hunt (module: custom-de41a00730; tests: unit, white-box, concurrency; severity: S1)
Activity
Ariznawlll commentedon Nov 29, 2026
结论
测试通过。同一 LOAD AST 重绑定后,显式输入列顺序、用户变量、SET NULLIF 和 NullMap 均保留。
关键证据
版本与环境
922815d74fe664dc9f4ea7bfc4c4e8ac3b279cd0;本次 Linux CI 从官方 main 检出的实际版本为231b68c2d6421e9d212538866e0f825afd6c13e8,两处分别核对完整git_version()。CI 比本地多了 Release merge spill buffers on panic #32160,核对差异仅为 spill sinker 及其两个测试文件。以下 CI 的正式 SQL、SDK 和源码测试确实在 CI 版本上重新执行;不把 MOTR commit 当作 MO 版本,也不将 main 结果外推为 4.2-dev 通过。步骤、断言与实际结果
TestPreparedLoadRebindKeepsColumnListAndNullIfLegacy、TestPreparedLoadRebindKeepsColumnListAndNullIfModern、TestPreparedLoadRebindKeepsNullMap,各三轮,9/9 根测试通过。@v的 NULLIF 和直接字段 NULLIF;重复执行、变更 sql_mode、ALTER 触发后精确行集和 NULL 位置均正确。这里覆盖同一 AST 的 legacy/modern 双次绑定及真实 schema/sql_mode 重绑定;没有宣称完成异构版本滚动升级。该升级路径只是原报告的重绑定触发示例,产品实现修复和本次断言目标是重绑定保留输入规则。
回归与 CI
qa-acceptance-main中版本、正式结果、原生测试和 SDK 日志。本轮整体正式 MOTR 63/63(21 个用例各三轮)、0 失败;63 次不全属于本 Issue。待解决点
本 Issue 上述原始问题与相关边界没有未通过项。该结论不扩展为其他 Issue、大数据性能或未执行的生产拓扑验收。MOTR 回归已经提交,按用户允许先验收关闭、随后合入 PR 的流程处理。
最终结论:测试通过。原始复现、上述扩展、对应 MOTR 回归及 Linux CI 均已完成;本 Issue 验收范围内未通过项:无。按授权关闭 #31315。