Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: throw error when insert values is correlated subquery #31049

Merged
merged 7 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
12 changes: 12 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7140,6 +7140,18 @@ func (s *testSuite1) TestInsertValuesWithSubQuery(c *C) {
tk.MustQuery("select * from t2").Check(testkit.Rows("2 4 2"))
tk.MustExec("insert into t2 set a = 3, b = 5, c = b")
tk.MustQuery("select * from t2").Check(testkit.Rows("2 4 2", "3 5 5"))

// issue #30626
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int)")
// TODO: should insert success and get (81,1) from the table
err := tk.ExecToErr("insert into t values ( 81, ( select ( SELECT '1' AS `c0` WHERE '1' >= `subq_0`.`c0` ) as `c1` FROM ( SELECT '1' AS `c0` ) AS `subq_0` ) );")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Insert's SET operation or VALUES_LIST doesn't support complex subqueries now")
err = tk.ExecToErr("insert into t set a = 81, b = (select ( SELECT '1' AS `c0` WHERE '1' >= `subq_0`.`c0` ) as `c1` FROM ( SELECT '1' AS `c0` ) AS `subq_0` );")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Insert's SET operation or VALUES_LIST doesn't support complex subqueries now")

}

func (s *testSuite1) TestDIVZeroInPartitionExpr(c *C) {
Expand Down
15 changes: 13 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3234,10 +3234,15 @@ func (b *PlanBuilder) buildSetValuesOfInsert(ctx context.Context, insert *ast.In
if _, ok := assign.Expr.(*ast.SubqueryExpr); ok {
usingPlan = LogicalTableDual{}.Init(b.ctx, b.getSelectOffset())
}
expr, _, err := b.rewriteWithPreprocess(ctx, assign.Expr, usingPlan, nil, nil, true, checkRefColumn)
expr, np, err := b.rewriteWithPreprocess(ctx, assign.Expr, usingPlan, nil, nil, true, checkRefColumn)
if err != nil {
return err
}
if np != nil {
if _, ok := np.(*LogicalTableDual); !ok {
return errors.New("Insert's SET operation or VALUES_LIST doesn't support complex subqueries now")
}
}
if insertPlan.AllAssignmentsAreConstant {
_, isConstant := expr.(*expression.Constant)
insertPlan.AllAssignmentsAreConstant = isConstant
Expand Down Expand Up @@ -3314,7 +3319,13 @@ func (b *PlanBuilder) buildValuesListOfInsert(ctx context.Context, insert *ast.I
if _, ok := valueItem.(*ast.SubqueryExpr); ok {
usingPlan = LogicalTableDual{}.Init(b.ctx, b.getSelectOffset())
}
expr, _, err = b.rewriteWithPreprocess(ctx, valueItem, usingPlan, nil, nil, true, checkRefColumn)
var np LogicalPlan
expr, np, err = b.rewriteWithPreprocess(ctx, valueItem, usingPlan, nil, nil, true, checkRefColumn)
if np != nil {
if _, ok := np.(*LogicalTableDual); !ok {
return errors.New("Insert's SET operation or VALUES_LIST doesn't support complex subqueries now")
}
}
}
if err != nil {
return err
Expand Down