Skip to content

Commit 122fbb2

Browse files
[release-21.0] fix: remove database qualifier after building query in operator to sql (vitessio#18602) (vitessio#18604)
Signed-off-by: Harshit Gangal <[email protected]> Co-authored-by: Harshit Gangal <[email protected]>
1 parent c896dd9 commit 122fbb2

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

go/test/endtoend/vtgate/queries/orderby/orderby_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ func TestSimpleOrderBy(t *testing.T) {
5353
mcmp.AssertMatches(`SELECT id2 FROM t1 ORDER BY id2 ASC`, `[[INT64(5)] [INT64(6)] [INT64(7)] [INT64(8)] [INT64(9)] [INT64(10)]]`)
5454
}
5555

56+
// TestQueryWithDBQualifier tests that we remove the db qualifier in the plan output that is sent down to the database.
57+
func TestQueryWithDBQualifier(t *testing.T) {
58+
mcmp, closer := start(t)
59+
defer closer()
60+
61+
mcmp.Exec("insert into t1(id1, id2) values (0,10),(1,9),(2,8),(3,7),(4,6),(5,5)")
62+
mcmp.Exec(`SELECT ks_orderby.t1.id1, ks_orderby.t1.id2 FROM ks_orderby.t1 ORDER BY ks_orderby.t1.id2 ASC, ks_orderby.t1.id1 desc`)
63+
}
64+
5665
func TestOrderBy(t *testing.T) {
5766
mcmp, closer := start(t)
5867
defer closer()

go/vt/vtgate/planbuilder/operators/SQL_builder.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func ToSQL(ctx *plancontext.PlanningContext, op Operator) (_ sqlparser.Statement
5252
if ctx.SemTable != nil {
5353
q.sortTables()
5454
}
55+
sqlparser.RemoveKeyspaceIgnoreSysSchema(q.stmt)
5556
return q.stmt, q.dmlOperator, nil
5657
}
5758

@@ -384,15 +385,6 @@ func (ts *tableSorter) Swap(i, j int) {
384385
ts.sel.From[i], ts.sel.From[j] = ts.sel.From[j], ts.sel.From[i]
385386
}
386387

387-
func removeKeyspaceFromSelectExpr(expr sqlparser.SelectExpr) {
388-
switch expr := expr.(type) {
389-
case *sqlparser.AliasedExpr:
390-
sqlparser.RemoveKeyspaceInCol(expr.Expr)
391-
case *sqlparser.StarExpr:
392-
expr.TableName.Qualifier = sqlparser.NewIdentifierCS("")
393-
}
394-
}
395-
396388
func stripDownQuery(from, to sqlparser.SelectStatement) {
397389
switch node := from.(type) {
398390
case *sqlparser.Select:
@@ -407,9 +399,6 @@ func stripDownQuery(from, to sqlparser.SelectStatement) {
407399
toNode.Comments = node.Comments
408400
toNode.Limit = node.Limit
409401
toNode.SelectExprs = node.SelectExprs
410-
for _, expr := range toNode.SelectExprs {
411-
removeKeyspaceFromSelectExpr(expr)
412-
}
413402
case *sqlparser.Union:
414403
toNode, ok := to.(*sqlparser.Union)
415404
if !ok {
@@ -653,8 +642,6 @@ func buildFilter(op *Filter, qb *queryBuilder) {
653642
func buildDerived(op *Horizon, qb *queryBuilder) {
654643
buildQuery(op.Source, qb)
655644

656-
sqlparser.RemoveKeyspaceInCol(op.Query)
657-
658645
stmt := qb.stmt
659646
qb.stmt = nil
660647
switch sel := stmt.(type) {
@@ -705,7 +692,6 @@ func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) {
705692
func buildHorizon(op *Horizon, qb *queryBuilder) {
706693
buildQuery(op.Source, qb)
707694
stripDownQuery(op.Query, qb.asSelectStatement())
708-
sqlparser.RemoveKeyspaceInCol(qb.stmt)
709695
}
710696

711697
func buildRecursiveCTE(op *RecurseCTE, qb *queryBuilder) {

go/vt/vtgate/planbuilder/operators/route.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@ func createProjection(ctx *plancontext.PlanningContext, src Operator, derivedNam
551551
}
552552

553553
func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int {
554-
removeKeyspaceFromSelectExpr(expr)
555-
556554
if reuse {
557555
offset := r.FindCol(ctx, expr.Expr, true)
558556
if offset != -1 {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4868,5 +4868,48 @@
48684868
"user.user"
48694869
]
48704870
}
4871+
},
4872+
{
4873+
"comment": "order by and project pushed under route having database qualifier - it should be removed in final query",
4874+
"query": "select user.user.col from user.user order by user.user.id",
4875+
"plan": {
4876+
"QueryType": "SELECT",
4877+
"Original": "select user.user.col from user.user order by user.user.id",
4878+
"Instructions": {
4879+
"OperatorType": "Route",
4880+
"Variant": "Scatter",
4881+
"Keyspace": {
4882+
"Name": "user",
4883+
"Sharded": true
4884+
},
4885+
"FieldQuery": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` where 1 != 1",
4886+
"OrderBy": "(1|2) ASC",
4887+
"Query": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` order by `user`.id asc",
4888+
"ResultColumns": 1,
4889+
"Table": "`user`"
4890+
},
4891+
"TablesUsed": [
4892+
"user.user"
4893+
]
4894+
}
4895+
},
4896+
{
4897+
"comment": "order by and project pushed under route having information_schema database qualifier - it should not be removed in final query",
4898+
"query": "select information_schema.table.col from information_schema.table order by information_schema.table.name",
4899+
"plan": {
4900+
"QueryType": "SELECT",
4901+
"Original": "select information_schema.table.col from information_schema.table order by information_schema.table.name",
4902+
"Instructions": {
4903+
"OperatorType": "Route",
4904+
"Variant": "DBA",
4905+
"Keyspace": {
4906+
"Name": "main",
4907+
"Sharded": false
4908+
},
4909+
"FieldQuery": "select information_schema.`table`.col from information_schema.`table` where 1 != 1",
4910+
"Query": "select information_schema.`table`.col from information_schema.`table` order by information_schema.`table`.`name` asc",
4911+
"Table": "information_schema.`table`"
4912+
}
4913+
}
48714914
}
48724915
]

0 commit comments

Comments
 (0)