Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,12 @@ func (b *PlanBuilder) buildSelection(ctx context.Context, p base.LogicalPlan, wh
cnfItems := expression.SplitCNFItems(expr)
for _, item := range cnfItems {
if con, ok := item.(*expression.Constant); ok && expression.ConstExprConsiderPlanCache(con, useCache) {
ret, _, err := expression.EvalBool(b.ctx.GetExprCtx().GetEvalCtx(), expression.CNFExprs{con}, chunk.Row{})
// Constant predicate folding during planning should not inherit the
// outer statement's truncate handling. For view expansion inside DML,
// expressions like `WHERE ''` are planned as part of a SELECT and
// should not be rejected as invalid view definitions.
exprCtx := exprctx.CtxWithHandleTruncateErrLevel(b.ctx.GetExprCtx(), errctx.LevelIgnore)
ret, _, err := expression.EvalBool(exprCtx.GetEvalCtx(), expression.CNFExprs{con}, chunk.Row{})
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/integrationtest/r/executor/insert.result
Original file line number Diff line number Diff line change
Expand Up @@ -2443,3 +2443,15 @@ ts
DROP TABLE t;
SET @@time_zone = @old_time_zone;
SET @@sql_mode = @old_sql_mode;
SET @old_sql_mode_65832 = @@sql_mode;
SET sql_mode = 'STRICT_TRANS_TABLES';
CREATE TABLE t_65832_0(c0 BLOB(304));
CREATE TABLE t_65832_1(c0 CHAR DEFAULT '0');
CREATE VIEW v_65832(c0) AS SELECT 0.9699394901011086 FROM t_65832_0 INNER JOIN t_65832_0 AS t0_alias ON t_65832_0.c0 WHERE '';
INSERT INTO t_65832_1 VALUES ('v') ON DUPLICATE KEY UPDATE c0=(SELECT true FROM v_65832 CROSS JOIN t_65832_0 ON (true));
SELECT * FROM t_65832_1;
c0
v
DROP VIEW v_65832;
DROP TABLE t_65832_0, t_65832_1;
SET sql_mode = @old_sql_mode_65832;
Expand Down
18 changes: 18 additions & 0 deletions tests/integrationtest/t/executor/insert.test
Original file line number Diff line number Diff line change
Expand Up @@ -1783,3 +1783,21 @@ DROP TABLE t;
SET @@time_zone = @old_time_zone;
SET @@sql_mode = @old_sql_mode;
--disable_warnings

# Regression test for issue #65832:
# INSERT ... ON DUPLICATE KEY UPDATE with a subquery referencing a view whose
# SELECT definition contains a constant boolean expression (e.g. WHERE '') should
# not return ErrViewInvalid when the outer statement is an INSERT in strict SQL mode.
# Root cause: planner-time constant folding inherited the outer INSERT's
# truncate handling, causing WHERE '' to raise ErrTruncatedWrongVal and then
# (incorrectly) surface as ErrViewInvalid during view expansion.
SET @old_sql_mode_65832 = @@sql_mode;
SET sql_mode = 'STRICT_TRANS_TABLES';
CREATE TABLE t_65832_0(c0 BLOB(304));
CREATE TABLE t_65832_1(c0 CHAR DEFAULT '0');
CREATE VIEW v_65832(c0) AS SELECT 0.9699394901011086 FROM t_65832_0 INNER JOIN t_65832_0 AS t0_alias ON t_65832_0.c0 WHERE '';
INSERT INTO t_65832_1 VALUES ('v') ON DUPLICATE KEY UPDATE c0=(SELECT true FROM v_65832 CROSS JOIN t_65832_0 ON (true));
SELECT * FROM t_65832_1;
DROP VIEW v_65832;
DROP TABLE t_65832_0, t_65832_1;
SET sql_mode = @old_sql_mode_65832;
Loading