Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions go/test/endtoend/vtgate/queries/orderby/orderby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func TestSimpleOrderBy(t *testing.T) {
mcmp.AssertMatches(`SELECT id2 FROM t1 ORDER BY id2 ASC`, `[[INT64(5)] [INT64(6)] [INT64(7)] [INT64(8)] [INT64(9)] [INT64(10)]]`)
}

// TestQueryWithDBQualifier tests that we remove the db qualifier in the plan output that is sent down to the database.
func TestQueryWithDBQualifier(t *testing.T) {
mcmp, closer := start(t)
defer closer()

mcmp.Exec("insert into t1(id1, id2) values (0,10),(1,9),(2,8),(3,7),(4,6),(5,5)")
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`)
}

func TestOrderBy(t *testing.T) {
mcmp, closer := start(t)
defer closer()
Expand Down
16 changes: 1 addition & 15 deletions go/vt/vtgate/planbuilder/operators/SQL_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func ToSQL(ctx *plancontext.PlanningContext, op Operator) (_ sqlparser.Statement
if ctx.SemTable != nil {
q.sortTables()
}
sqlparser.RemoveKeyspaceIgnoreSysSchema(q.stmt)
return q.stmt, q.dmlOperator, nil
}

Expand Down Expand Up @@ -395,15 +396,6 @@ func (ts *tableSorter) Swap(i, j int) {
ts.sel.From[i], ts.sel.From[j] = ts.sel.From[j], ts.sel.From[i]
}

func removeKeyspaceFromSelectExpr(expr sqlparser.SelectExpr) {
switch expr := expr.(type) {
case *sqlparser.AliasedExpr:
sqlparser.RemoveKeyspaceInCol(expr.Expr)
case *sqlparser.StarExpr:
expr.TableName.Qualifier = sqlparser.NewIdentifierCS("")
}
}

func stripDownQuery(from, to sqlparser.TableStatement) {
switch node := from.(type) {
case *sqlparser.Select:
Expand All @@ -418,9 +410,6 @@ func stripDownQuery(from, to sqlparser.TableStatement) {
toNode.Comments = node.Comments
toNode.Limit = node.Limit
toNode.SelectExprs = node.SelectExprs
for _, expr := range toNode.SelectExprs.Exprs {
removeKeyspaceFromSelectExpr(expr)
}
case *sqlparser.Union:
toNode, ok := to.(*sqlparser.Union)
if !ok {
Expand Down Expand Up @@ -666,8 +655,6 @@ func buildFilter(op *Filter, qb *queryBuilder) {
func buildDerived(op *Horizon, qb *queryBuilder) {
buildQuery(op.Source, qb)

sqlparser.RemoveKeyspaceInCol(op.Query)

stmt := qb.stmt
qb.stmt = nil
switch sel := stmt.(type) {
Expand Down Expand Up @@ -718,7 +705,6 @@ func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) {
func buildHorizon(op *Horizon, qb *queryBuilder) {
buildQuery(op.Source, qb)
stripDownQuery(op.Query, qb.asSelectStatement())
sqlparser.RemoveKeyspaceInCol(qb.stmt)
}

func buildRecursiveCTE(op *RecurseCTE, qb *queryBuilder) {
Expand Down
2 changes: 0 additions & 2 deletions go/vt/vtgate/planbuilder/operators/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,6 @@ func createProjection(ctx *plancontext.PlanningContext, src Operator, derivedNam
}

func (r *Route) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int {
removeKeyspaceFromSelectExpr(expr)

if reuse {
offset := r.FindCol(ctx, expr.Expr, true)
if offset != -1 {
Expand Down
45 changes: 45 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -5074,5 +5074,50 @@
"user.user"
]
}
},
{
"comment": "order by and project pushed under route having database qualifier - it should be removed in final query",
"query": "select user.user.col from user.user order by user.user.id",
"plan": {
"Type": "Scatter",
"QueryType": "SELECT",
"Original": "select user.user.col from user.user order by user.user.id",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` where 1 != 1",
"OrderBy": "(1|2) ASC",
"Query": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` order by `user`.id asc",
"ResultColumns": 1,
"Table": "`user`"
},
"TablesUsed": [
"user.user"
]
}
},
{
"comment": "order by and project pushed under route having information_schema database qualifier - it should not be removed in final query",
"query": "select information_schema.table.col from information_schema.table order by information_schema.table.name",
"plan": {
"Type": "Passthrough",
"QueryType": "SELECT",
"Original": "select information_schema.table.col from information_schema.table order by information_schema.table.name",
"Instructions": {
"OperatorType": "Route",
"Variant": "DBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select information_schema.`table`.col from information_schema.`table` where 1 != 1",
"Query": "select information_schema.`table`.col from information_schema.`table` order by information_schema.`table`.`name` asc",
"Table": "information_schema.`table`"
}
}
}
]
Loading