Skip to content

Commit 801b1d3

Browse files
authored
planner: skip pure-constant generated column substitution (#67473)
close #66859
1 parent a5ad421 commit 801b1d3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pkg/planner/core/casetest/join/join_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,14 @@ JOIN
320320
` └─Selection cop[tikv] not(isnull(test.t3_issue60076.b))`,
321321
` └─TableFullScan cop[tikv] table:t3_issue60076 keep order:false, stats:pseudo`))
322322
tk.MustQuery(`show warnings`).Check(testkit.Rows())
323+
324+
tk.MustExec(`drop table if exists issue66859_t0, issue66859_t1, issue66859_t2`)
325+
tk.MustExec(`create table issue66859_t0(c0 int)`)
326+
tk.MustExec(`create table issue66859_t1 like issue66859_t0`)
327+
tk.MustExec(`create index i0 on issue66859_t1((5))`)
328+
tk.MustExec(`insert into issue66859_t0(c0) values(-1)`)
329+
tk.MustQuery(`select /* issue:66859 */ issue66859_t0.c0 as ref0, issue66859_t1.c0 as ref2
330+
from issue66859_t0 left join issue66859_t1 on issue66859_t0.c0 = issue66859_t1.c0
331+
where 5 >= issue66859_t0.c0`).Check(testkit.Rows("-1 <nil>"))
323332
})
324333
}

pkg/planner/core/rule_generate_column_substitute.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func collectGenerateColumn(lp base.LogicalPlan, exprToColumn ExprColumnMap) {
8181
s := ds.Schema().Columns
8282
col := expression.ColInfo2Col(s, colInfo)
8383
if col != nil && col.GetType(ectx).PartialEqual(col.VirtualExpr.GetType(ectx), lp.SCtx().GetSessionVars().EnableUnsafeSubstitute) {
84+
// Replacing a literal with a generated column that is itself a pure constant is not
85+
// semantically neutral across outer joins, because null-augmentation can turn the
86+
// generated column into NULL while the original literal stays constant.
87+
if len(expression.ExtractColumns(col.VirtualExpr)) == 0 {
88+
continue
89+
}
8490
exprToColumn[col.VirtualExpr] = col
8591
}
8692
}

0 commit comments

Comments
 (0)