From 7cf22a72771f8dfc4ebc9cf76ddf0c93529e1e62 Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:01:36 +0200 Subject: [PATCH] fix: don't forget DISTINCT for derived tables (#15672) Signed-off-by: Andres Taylor --- .../planbuilder/operators/SQL_builder.go | 1 + .../planbuilder/testdata/from_cases.json | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index f6f934d4a3f..9eb27c6bed6 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -656,6 +656,7 @@ func buildDerivedSelect(op *Horizon, qb *queryBuilder, sel *sqlparser.Select) er sel.GroupBy = opQuery.GroupBy sel.Having = mergeHaving(sel.Having, opQuery.Having) sel.SelectExprs = opQuery.SelectExprs + sel.Distinct = opQuery.Distinct qb.addTableExpr(op.Alias, op.Alias, TableID(op), &sqlparser.DerivedTable{ Select: sel, }, nil, op.ColumnAliases) diff --git a/go/vt/vtgate/planbuilder/testdata/from_cases.json b/go/vt/vtgate/planbuilder/testdata/from_cases.json index 567a3b5f254..6c977b04401 100644 --- a/go/vt/vtgate/planbuilder/testdata/from_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/from_cases.json @@ -409,6 +409,28 @@ ] } }, + { + "comment": "DISTINCT inside derived table", + "query": "select * from (select distinct name from user) as t", + "plan": { + "QueryType": "SELECT", + "Original": "select * from (select distinct name from user) as t", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select `name` from (select `name` from `user` where 1 != 1) as t where 1 != 1", + "Query": "select `name` from (select distinct `name` from `user`) as t", + "Table": "`user`" + }, + "TablesUsed": [ + "user.user" + ] + } + }, { "comment": "',' join unsharded", "query": "select u1.a, u2.a from unsharded u1, unsharded u2",