Skip to content

Commit

Permalink
Cherry-pick b327743 with conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitess-bot[bot] committed Mar 22, 2024
1 parent a19be92 commit 4408ecb
Show file tree
Hide file tree
Showing 8 changed files with 2,421 additions and 0 deletions.
19 changes: 19 additions & 0 deletions go/test/endtoend/vtgate/queries/reference/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ func TestReferenceRouting(t *testing.T) {
`[[INT64(0)]]`,
)

t.Run("Complex reference query", func(t *testing.T) {
utils.SkipIfBinaryIsBelowVersion(t, 20, "vtgate")
// Verify a complex query using reference tables with a left join having a derived table with an order by clause works as intended.
utils.AssertMatches(
t,
conn,
`SELECT t.id FROM (
SELECT zd.id, zd.zip_id
FROM `+shardedKeyspaceName+`.zip_detail AS zd
WHERE zd.id IN (2)
ORDER BY zd.discontinued_at
LIMIT 1
) AS t
LEFT JOIN `+shardedKeyspaceName+`.zip_detail AS t0 ON t.zip_id = t0.zip_id
ORDER BY t.id`,
`[[INT64(2)]]`,
)
})

// UPDATE should route an unqualified zip_detail to unsharded keyspace.
utils.Exec(t, conn,
"UPDATE zip_detail SET discontinued_at = NULL WHERE id = 2")
Expand Down
21 changes: 21 additions & 0 deletions go/vt/vtgate/planbuilder/operators/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,28 @@ func addColumnToInput(operator ops.Operator, expr *sqlparser.AliasedExpr, addToG
case *Limit:
return addColumnToInput(op.Source, expr, addToGroupBy)
case *Ordering:
<<<<<<< HEAD
return addColumnToInput(op.Source, expr, addToGroupBy)
=======
derivedName, src, added, offset := addMultipleColumnsToInput(ctx, op.Source, reuse, addToGroupBy, exprs)
if added {
op.Source = src
}
return derivedName, op, added, offset

case *LockAndComment:
derivedName, src, added, offset := addMultipleColumnsToInput(ctx, op.Source, reuse, addToGroupBy, exprs)
if added {
op.Source = src
}
return derivedName, op, added, offset

case *Horizon:
// if the horizon has an alias, then it is a derived table,
// we have to add a new projection and can't build on this one
return op.Alias, op, false, nil

>>>>>>> b327743a25 (Fix aliasing in routes that have a derived table (#15550))
case selectExpressions:
if op.isDerived() {
// if the only thing we can push to is a derived table,
Expand Down
Loading

0 comments on commit 4408ecb

Please sign in to comment.