Skip to content

Commit

Permalink
Merge pull request vitessio#9855 from planetscale/fix-9854
Browse files Browse the repository at this point in the history
Fix `__sq_has_values1` error with `PulloutSubquery`
  • Loading branch information
systay authored Mar 10, 2022
2 parents fd36aed + 122272b commit f6f0ae0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions go/test/endtoend/vtgate/queries/subquery/subquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func TestSubqueriesHasValues(t *testing.T) {
utils.AssertMatches(t, conn, `SELECT id2 FROM t1 WHERE id1 NOT IN (SELECT id1 FROM t1 WHERE id1 > 10) ORDER BY id2`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)] [INT64(5)] [INT64(6)]]`)
}

func TestSubqueriesExists(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

defer utils.Exec(t, conn, `delete from t1`)
utils.Exec(t, conn, "insert into t1(id1, id2) values (0,1),(1,2),(2,3),(3,4),(4,5),(5,6)")
utils.AssertMatches(t, conn, `SELECT id2 FROM t1 WHERE EXISTS (SELECT id1 FROM t1 WHERE id1 > 0)`, `[[INT64(1)] [INT64(5)] [INT64(2)] [INT64(3)] [INT64(4)] [INT64(6)]]`)
}

func TestQueryAndSubQWithLimit(t *testing.T) {
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ func (es *ExtractedSubquery) GetHasValuesArg() string {
func (es *ExtractedSubquery) updateAlternative() {
switch original := es.Original.(type) {
case *ExistsExpr:
es.alternative = NewArgument(es.argName)
es.alternative = NewArgument(es.hasValuesArg)
case *Subquery:
es.alternative = NewArgument(es.argName)
case *ComparisonExpr:
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func (r *rewriter) rewriteExistsSubquery(cursor *sqlparser.Cursor, node *sqlpars
}

r.inSubquery++
argName := r.reservedVars.ReserveHasValuesSubQuery()
semTableSQ.SetArgName(argName)
hasValuesArg := r.reservedVars.ReserveHasValuesSubQuery()
semTableSQ.SetHasValuesArg(hasValuesArg)
cursor.Replace(semTableSQ)
return nil
}
Expand Down

0 comments on commit f6f0ae0

Please sign in to comment.