Skip to content

Commit 5ac69ec

Browse files
[release-18.0] In the same sqltypes.Type, Copy expression types to avoid weight_strings and derived tables (#15069) (#15129)
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
1 parent 2934b0a commit 5ac69ec

File tree

3 files changed

+205
-5
lines changed

3 files changed

+205
-5
lines changed

go/test/endtoend/vtgate/queries/union/union_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ func TestUnionDistinct(t *testing.T) {
7373
t.Skip()
7474
mcmp.AssertMatches("select 1 from dual where 1 IN (select 1 as col union select 2)", "[[INT64(1)]]")
7575
})
76+
mcmp.AssertMatches(`SELECT 1 from t1 UNION SELECT 2 from t1`, `[[INT64(1)] [INT64(2)]]`)
77+
mcmp.AssertMatches(`SELECT 5 from t1 UNION SELECT 6 from t1`, `[[INT64(5)] [INT64(6)]]`)
78+
mcmp.AssertMatchesNoOrder(`SELECT id1 from t1 UNION SELECT id2 from t1`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`)
79+
mcmp.AssertMatchesNoOrder(`SELECT 1 from t1 UNION SELECT id2 from t1`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`)
80+
mcmp.AssertMatchesNoOrder(`SELECT 5 from t1 UNION SELECT id2 from t1`, `[[INT64(5)] [INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`)
81+
mcmp.AssertMatchesNoOrder(`SELECT id1 from t1 UNION SELECT 2 from t1`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)]]`)
82+
mcmp.AssertMatchesNoOrder(`SELECT id1 from t1 UNION SELECT 5 from t1`, `[[INT64(1)] [INT64(2)] [INT64(3)] [INT64(4)] [INT64(5)]]`)
83+
mcmp.Exec(`select curdate() from t1 union select 3 from t1`)
84+
mcmp.Exec(`select curdate() from t1 union select id1 from t1`)
7685
})
7786

7887
}

go/vt/vtgate/planbuilder/operators/union_merging.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,31 @@ func createMergedUnion(
188188
cols := make(sqlparser.SelectExprs, len(lhsExprs))
189189
noDeps := len(lhsExprs) != len(rhsExprs)
190190
for idx, col := range lhsExprs {
191-
ae, ok := col.(*sqlparser.AliasedExpr)
191+
lae, ok := col.(*sqlparser.AliasedExpr)
192192
if !ok {
193193
cols[idx] = col
194194
noDeps = true
195195
continue
196196
}
197-
col := sqlparser.NewColName(ae.ColumnName())
197+
col := sqlparser.NewColName(lae.ColumnName())
198198
cols[idx] = aeWrap(col)
199199
if noDeps {
200200
continue
201201
}
202202

203-
deps := ctx.SemTable.RecursiveDeps(ae.Expr)
204-
ae, ok = rhsExprs[idx].(*sqlparser.AliasedExpr)
203+
deps := ctx.SemTable.RecursiveDeps(lae.Expr)
204+
rae, ok := rhsExprs[idx].(*sqlparser.AliasedExpr)
205205
if !ok {
206206
noDeps = true
207207
continue
208208
}
209-
deps = deps.Merge(ctx.SemTable.RecursiveDeps(ae.Expr))
209+
deps = deps.Merge(ctx.SemTable.RecursiveDeps(rae.Expr))
210+
rt, _, foundR := ctx.SemTable.TypeForExpr(rae.Expr)
211+
lt, _, foundL := ctx.SemTable.TypeForExpr(lae.Expr)
212+
if foundR && foundL && rt == lt {
213+
ctx.SemTable.CopySemanticInfo(rae.Expr, col)
214+
ctx.SemTable.CopySemanticInfo(lae.Expr, col)
215+
}
210216
ctx.SemTable.Recursive[col] = deps
211217
}
212218

go/vt/vtgate/planbuilder/testdata/union_cases.json

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,5 +1495,190 @@
14951495
"user.user"
14961496
]
14971497
}
1498+
},
1499+
{
1500+
"comment": "Select literals from table union Select literals from table",
1501+
"query": "SELECT 1 from user UNION SELECT 2 from user",
1502+
"plan": {
1503+
"QueryType": "SELECT",
1504+
"Original": "SELECT 1 from user UNION SELECT 2 from user",
1505+
"Instructions": {
1506+
"OperatorType": "Distinct",
1507+
"Collations": [
1508+
"0"
1509+
],
1510+
"Inputs": [
1511+
{
1512+
"OperatorType": "Route",
1513+
"Variant": "Scatter",
1514+
"Keyspace": {
1515+
"Name": "user",
1516+
"Sharded": true
1517+
},
1518+
"FieldQuery": "select 1 from `user` where 1 != 1 union select 2 from `user` where 1 != 1",
1519+
"Query": "select 1 from `user` union select 2 from `user`",
1520+
"Table": "`user`"
1521+
}
1522+
]
1523+
},
1524+
"TablesUsed": [
1525+
"user.user"
1526+
]
1527+
}
1528+
},
1529+
{
1530+
"comment": "Select column from table union Select literals from table",
1531+
"query": "select col1 from user union select 3 from user",
1532+
"plan": {
1533+
"QueryType": "SELECT",
1534+
"Original": "select col1 from user union select 3 from user",
1535+
"Instructions": {
1536+
"OperatorType": "Distinct",
1537+
"Collations": [
1538+
"(0:1)"
1539+
],
1540+
"ResultColumns": 1,
1541+
"Inputs": [
1542+
{
1543+
"OperatorType": "Route",
1544+
"Variant": "Scatter",
1545+
"Keyspace": {
1546+
"Name": "user",
1547+
"Sharded": true
1548+
},
1549+
"FieldQuery": "select col1, weight_string(col1) from (select col1 from `user` where 1 != 1 union select 3 from `user` where 1 != 1) as dt where 1 != 1",
1550+
"Query": "select col1, weight_string(col1) from (select col1 from `user` union select 3 from `user`) as dt",
1551+
"Table": "`user`"
1552+
}
1553+
]
1554+
},
1555+
"TablesUsed": [
1556+
"user.user"
1557+
]
1558+
}
1559+
},
1560+
{
1561+
"comment": "Select literals from table union Select column from table",
1562+
"query": "select 3 from user union select col1 from user",
1563+
"plan": {
1564+
"QueryType": "SELECT",
1565+
"Original": "select 3 from user union select col1 from user",
1566+
"Instructions": {
1567+
"OperatorType": "Distinct",
1568+
"Collations": [
1569+
"(0:1)"
1570+
],
1571+
"ResultColumns": 1,
1572+
"Inputs": [
1573+
{
1574+
"OperatorType": "Route",
1575+
"Variant": "Scatter",
1576+
"Keyspace": {
1577+
"Name": "user",
1578+
"Sharded": true
1579+
},
1580+
"FieldQuery": "select `3`, weight_string(`3`) from (select 3 from `user` where 1 != 1 union select col1 from `user` where 1 != 1) as dt where 1 != 1",
1581+
"Query": "select `3`, weight_string(`3`) from (select 3 from `user` union select col1 from `user`) as dt",
1582+
"Table": "`user`"
1583+
}
1584+
]
1585+
},
1586+
"TablesUsed": [
1587+
"user.user"
1588+
]
1589+
}
1590+
},
1591+
{
1592+
"comment": "Select literals from table union Select now() from table",
1593+
"query": "select 3 from user union select now() from user",
1594+
"plan": {
1595+
"QueryType": "SELECT",
1596+
"Original": "select 3 from user union select now() from user",
1597+
"Instructions": {
1598+
"OperatorType": "Distinct",
1599+
"Collations": [
1600+
"(0:1)"
1601+
],
1602+
"ResultColumns": 1,
1603+
"Inputs": [
1604+
{
1605+
"OperatorType": "Route",
1606+
"Variant": "Scatter",
1607+
"Keyspace": {
1608+
"Name": "user",
1609+
"Sharded": true
1610+
},
1611+
"FieldQuery": "select `3`, weight_string(`3`) from (select 3 from `user` where 1 != 1 union select now() from `user` where 1 != 1) as dt where 1 != 1",
1612+
"Query": "select `3`, weight_string(`3`) from (select 3 from `user` union select now() from `user`) as dt",
1613+
"Table": "`user`"
1614+
}
1615+
]
1616+
},
1617+
"TablesUsed": [
1618+
"user.user"
1619+
]
1620+
}
1621+
},
1622+
{
1623+
"comment": "Select now() from table union Select literals from table",
1624+
"query": "select now() from user union select 3 from user",
1625+
"plan": {
1626+
"QueryType": "SELECT",
1627+
"Original": "select now() from user union select 3 from user",
1628+
"Instructions": {
1629+
"OperatorType": "Distinct",
1630+
"Collations": [
1631+
"(0:1)"
1632+
],
1633+
"ResultColumns": 1,
1634+
"Inputs": [
1635+
{
1636+
"OperatorType": "Route",
1637+
"Variant": "Scatter",
1638+
"Keyspace": {
1639+
"Name": "user",
1640+
"Sharded": true
1641+
},
1642+
"FieldQuery": "select `now()`, weight_string(`now()`) from (select now() from `user` where 1 != 1 union select 3 from `user` where 1 != 1) as dt where 1 != 1",
1643+
"Query": "select `now()`, weight_string(`now()`) from (select now() from `user` union select 3 from `user`) as dt",
1644+
"Table": "`user`"
1645+
}
1646+
]
1647+
},
1648+
"TablesUsed": [
1649+
"user.user"
1650+
]
1651+
}
1652+
},
1653+
{
1654+
"comment": "Select now() from table union Select column from table",
1655+
"query": "select now() from user union select id from user",
1656+
"plan": {
1657+
"QueryType": "SELECT",
1658+
"Original": "select now() from user union select id from user",
1659+
"Instructions": {
1660+
"OperatorType": "Distinct",
1661+
"Collations": [
1662+
"(0:1)"
1663+
],
1664+
"ResultColumns": 1,
1665+
"Inputs": [
1666+
{
1667+
"OperatorType": "Route",
1668+
"Variant": "Scatter",
1669+
"Keyspace": {
1670+
"Name": "user",
1671+
"Sharded": true
1672+
},
1673+
"FieldQuery": "select `now()`, weight_string(`now()`) from (select now() from `user` where 1 != 1 union select id from `user` where 1 != 1) as dt where 1 != 1",
1674+
"Query": "select `now()`, weight_string(`now()`) from (select now() from `user` union select id from `user`) as dt",
1675+
"Table": "`user`"
1676+
}
1677+
]
1678+
},
1679+
"TablesUsed": [
1680+
"user.user"
1681+
]
1682+
}
14981683
}
14991684
]

0 commit comments

Comments
 (0)