docs/agents/planner/window_frame_replace_notes.md
Background:
CTE + derived table + outer join + window queries failing in planner with
Can't find column ... in schema.ORDER BY expression referencing a derived column, andRANGE frame whose bound expressions were derived from the window ORDER BY items.Root cause:
ReplaceExprColumns.LogicalWindow.ReplaceExprColumns updated:
PartitionBy,OrderBy.Frame.Start/End.CalcFuncs and CompareCols.RANGE ... CURRENT ROW, those frame expressions can still hold the pre-elimination column identity from the child projection.ResolveIndices then sees a stale window-frame column ID and fails even though a same-named column still exists in the child schema.Related follow-up:
ResolveExprAndReplace previously rewrote scalar-function arguments in place.Implementation choice:
ResolveExprAndReplace copy-on-write for ScalarFunction so replacement does not mutate an existing expression tree in place.LogicalWindow.ReplaceExprColumns to rewrite frame CalcFuncs and CompareCols together with PartitionBy and OrderBy.Regression coverage:
TestWindowWithOuterJoinAndCTE in pkg/planner/core/casetest/windows/window_with_exist_subquery_test.go
reproduces the reported CTE + outer join + window + RANGE/current-row shape and also covers
a nearby CURRENT ROW ... UNBOUNDED FOLLOWING variant so both frame-bound replacement paths stay exercised.Validation commands:
make failpoint-enablego test -v -run '^TestWindowWithOuterJoinAndCTE$' -tags='intest,deadlock' -count=1 ./pkg/planner/core/casetest/windowsmake failpoint-disablemake lint