diff --git a/go/vt/vtgate/planbuilder/operators/SQL_builder.go b/go/vt/vtgate/planbuilder/operators/SQL_builder.go index 07fa5fbbd9d..3393c96f863 100644 --- a/go/vt/vtgate/planbuilder/operators/SQL_builder.go +++ b/go/vt/vtgate/planbuilder/operators/SQL_builder.go @@ -471,6 +471,7 @@ func buildDerived(op *Derived, qb *queryBuilder) error { 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 cb233af88b8..23fde4fc79f 100644 --- a/go/vt/vtgate/planbuilder/testdata/from_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/from_cases.json @@ -790,6 +790,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",