Skip to content

Commit

Permalink
planner: throw error when insert values is correlated subquery (pingc…
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 authored Dec 28, 2021
1 parent 57b2908 commit 3ea7a1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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
19 changes: 17 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3234,10 +3234,17 @@ 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 {
// See issue#30626 and the related tests in function TestInsertValuesWithSubQuery for more details.
// This is a TODO and we will support it later.
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 +3321,15 @@ 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 {
// See issue#30626 and the related tests in function TestInsertValuesWithSubQuery for more details.
// This is a TODO and we will support it later.
return errors.New("Insert's SET operation or VALUES_LIST doesn't support complex subqueries now")
}
}
}
if err != nil {
return err
Expand Down

0 comments on commit 3ea7a1d

Please sign in to comment.