[release-15.0] BugFix: Unsharded query using a derived table and a dual table (#12484) (#12492)

* BugFix: Unsharded query using a derived table and a dual table (#12484)

* test: add failing test for a query in an unsharded keyspace using a derived table and reference table

Signed-off-by: Manan Gupta <manan@planetscale.com>

* feat: fix logic to check if a single unsharded keyspace is used

Signed-off-by: Manan Gupta <manan@planetscale.com>

* feat: fix test output

Signed-off-by: Manan Gupta <manan@planetscale.com>

---------

Signed-off-by: Manan Gupta <manan@planetscale.com>

* feat: remove test files

Signed-off-by: Manan Gupta <manan@planetscale.com>

---------

Signed-off-by: Manan Gupta <manan@planetscale.com>
This commit is contained in:
Manan Gupta 2023-02-27 21:21:42 +05:30 коммит произвёл GitHub
Родитель b55eae3b93
Коммит 9f173734bb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 46 добавлений и 1 удалений

Просмотреть файл

@ -7870,5 +7870,43 @@
"main.dual"
]
}
},
{
"comment": "query with a derived table and dual table in unsharded keyspace",
"query": "SELECT * FROM unsharded_a AS t1 JOIN (SELECT trim((SELECT MAX(name) FROM unsharded_a)) AS name) AS t2 WHERE t1.name >= t2.name ORDER BY t1.name ASC LIMIT 1;",
"v3-plan": {
"QueryType": "SELECT",
"Original": "SELECT * FROM unsharded_a AS t1 JOIN (SELECT trim((SELECT MAX(name) FROM unsharded_a)) AS name) AS t2 WHERE t1.name >= t2.name ORDER BY t1.name ASC LIMIT 1;",
"Instructions": {
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select * from unsharded_a as t1 join (select trim((select max(`name`) from unsharded_a where 1 != 1)) as `name` from dual where 1 != 1) as t2 where 1 != 1",
"Query": "select * from unsharded_a as t1 join (select trim((select max(`name`) from unsharded_a)) as `name` from dual) as t2 where t1.`name` >= t2.`name` order by t1.`name` asc limit 1",
"Table": "unsharded_a, dual"
}
},
"gen4-plan": {
"QueryType": "SELECT",
"Original": "SELECT * FROM unsharded_a AS t1 JOIN (SELECT trim((SELECT MAX(name) FROM unsharded_a)) AS name) AS t2 WHERE t1.name >= t2.name ORDER BY t1.name ASC LIMIT 1;",
"Instructions": {
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select * from unsharded_a as t1 join (select trim((select max(`name`) from unsharded_a where 1 != 1)) as `name` from dual where 1 != 1) as t2 where 1 != 1",
"Query": "select * from unsharded_a as t1 join (select trim((select max(`name`) from unsharded_a)) as `name` from dual) as t2 where t1.`name` >= t2.`name` order by t1.`name` asc limit 1",
"Table": "dual, unsharded_a"
},
"TablesUsed": [
"main.dual",
"main.unsharded_a"
]
}
}
]

Просмотреть файл

@ -372,7 +372,7 @@ func (st *SemTable) SingleUnshardedKeyspace() (*vindexes.Keyspace, []*vindexes.T
for _, table := range st.Tables {
vindexTable := table.GetVindexTable()
if vindexTable == nil || vindexTable.Type != "" {
if vindexTable == nil {
_, isDT := table.getExpr().Expr.(*sqlparser.DerivedTable)
if isDT {
// derived tables are ok, as long as all real tables are from the same unsharded keyspace
@ -381,6 +381,13 @@ func (st *SemTable) SingleUnshardedKeyspace() (*vindexes.Keyspace, []*vindexes.T
}
return nil, nil
}
if vindexTable.Type != "" {
// A reference table is not an issue when seeing if a query is going to an unsharded keyspace
if vindexTable.Type == vindexes.TypeReference {
continue
}
return nil, nil
}
name, ok := table.getExpr().Expr.(sqlparser.TableName)
if !ok {
return nil, nil