diff --git a/go/test/endtoend/vtgate/lookup_test.go b/go/test/endtoend/vtgate/lookup_test.go index deb3710cb7d..141eea3d3b1 100644 --- a/go/test/endtoend/vtgate/lookup_test.go +++ b/go/test/endtoend/vtgate/lookup_test.go @@ -21,13 +21,12 @@ import ( "fmt" "testing" - "vitess.io/vitess/go/test/endtoend/utils" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "vitess.io/vitess/go/mysql" + "vitess.io/vitess/go/test/endtoend/utils" + "vitess.io/vitess/go/vt/log" ) func TestUnownedLookupInsertNull(t *testing.T) { @@ -581,3 +580,13 @@ func TestUnicodeLooseMD5CaseInsensitive(t *testing.T) { utils.AssertMatches(t, conn, "SELECT id1, id2 from t4 where id2 = 'Test'", `[[INT64(1) VARCHAR("test")]]`) } + +func TestJoinWithPredicateAndJoinOnDifferentVindex(t *testing.T) { + conn, closer := start(t) + defer closer() + + qr := utils.Exec(t, conn, "vexplain select /*vt+ planner=gen4 */ t4.id1 from t4, t3 where t4.id2 = 'foo' and t4.id1 = t3.id6") + log.Errorf("plan: \n%s\n", qr.Rows[0][0].ToString()) + + utils.Exec(t, conn, "select t4.id1 from t4, t3 where t4.id2 = 'foo' and t4.id1 = t3.id6") +} diff --git a/go/vt/vtgate/planbuilder/testdata/select_cases.json b/go/vt/vtgate/planbuilder/testdata/select_cases.json index 4f9b77d9b19..0f9a60c0733 100644 --- a/go/vt/vtgate/planbuilder/testdata/select_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/select_cases.json @@ -8063,5 +8063,102 @@ "user.user_metadata" ] } + }, + { + "comment": "join query with lookup and join on different vindex column", + "query": "select u.id from user u, user_metadata um where u.name = 'foo' and u.id = um.user_id", + "v3-plan": { + "QueryType": "SELECT", + "Original": "select u.id from user u, user_metadata um where u.name = 'foo' and u.id = um.user_id", + "Instructions": { + "OperatorType": "Join", + "Variant": "Join", + "JoinColumnIndexes": "L:0", + "JoinVars": { + "u_id": 0 + }, + "TableName": "`user`_user_metadata", + "Inputs": [ + { + "OperatorType": "Route", + "Variant": "Equal", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select u.id from `user` as u where 1 != 1", + "Query": "select u.id from `user` as u where u.`name` = 'foo'", + "Table": "`user`", + "Values": [ + "VARCHAR(\"foo\")" + ], + "Vindex": "name_user_map" + }, + { + "OperatorType": "Route", + "Variant": "EqualUnique", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select 1 from user_metadata as um where 1 != 1", + "Query": "select 1 from user_metadata as um where um.user_id = :u_id", + "Table": "user_metadata", + "Values": [ + ":u_id" + ], + "Vindex": "user_index" + } + ] + } + }, + "gen4-plan": { + "QueryType": "SELECT", + "Original": "select u.id from user u, user_metadata um where u.name = 'foo' and u.id = um.user_id", + "Instructions": { + "OperatorType": "VindexLookup", + "Variant": "Equal", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "Values": [ + "VARCHAR(\"foo\")" + ], + "Vindex": "name_user_map", + "Inputs": [ + { + "OperatorType": "Route", + "Variant": "IN", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select `name`, keyspace_id from name_user_vdx where 1 != 1", + "Query": "select `name`, keyspace_id from name_user_vdx where `name` in ::__vals", + "Table": "name_user_vdx", + "Values": [ + "::name" + ], + "Vindex": "user_index" + }, + { + "OperatorType": "Route", + "Variant": "ByDestination", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select u.id from `user` as u, user_metadata as um where 1 != 1", + "Query": "select u.id from `user` as u, user_metadata as um where u.`name` = 'foo' and u.id = um.user_id", + "Table": "`user`, user_metadata" + } + ] + }, + "TablesUsed": [ + "user.user", + "user.user_metadata" + ] + } } ]