Skip to content

Commit 5dd2153

Browse files
authored
Merge pull request #2181 from dolthub/daylon/distinct-on
Added DISTINCT ON handling
2 parents 342a4f2 + 6d8cde3 commit 5dd2153

4 files changed

Lines changed: 127 additions & 9 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ require (
99
github.com/dolthub/dolt/go v0.40.5-0.20260108000424-ed62ee89285b
1010
github.com/dolthub/eventsapi_schema v0.0.0-20250915094920-eadfd39051ca
1111
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
12-
github.com/dolthub/go-mysql-server v0.20.1-0.20260108224859-4dd05696542e
12+
github.com/dolthub/go-mysql-server v0.20.1-0.20260110001532-5eb9ccaba8a1
1313
github.com/dolthub/pg_query_go/v6 v6.0.0-20251215122834-fb20be4254d1
1414
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216
15-
github.com/dolthub/vitess v0.0.0-20260108222406-f8a2587c4954
15+
github.com/dolthub/vitess v0.0.0-20260109110924-205efc8530f1
1616
github.com/fatih/color v1.13.0
1717
github.com/goccy/go-json v0.10.2
1818
github.com/gogo/protobuf v1.3.2

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
238238
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
239239
github.com/dolthub/go-icu-regex v0.0.0-20250916051405-78a38d478790 h1:zxMsH7RLiG+dlZ/y0LgJHTV26XoiSJcuWq+em6t6VVc=
240240
github.com/dolthub/go-icu-regex v0.0.0-20250916051405-78a38d478790/go.mod h1:F3cnm+vMRK1HaU6+rNqQrOCyR03HHhR1GWG2gnPOqaE=
241-
github.com/dolthub/go-mysql-server v0.20.1-0.20260108224859-4dd05696542e h1:OKp5W4bgSJIAKESEtBtyOWF85HwvE7UIe+T204v5EN0=
242-
github.com/dolthub/go-mysql-server v0.20.1-0.20260108224859-4dd05696542e/go.mod h1:2bgnal91FRGVdJyirmUSMCd1Tp3Ci8dTDsrJWkTp/hs=
241+
github.com/dolthub/go-mysql-server v0.20.1-0.20260110001532-5eb9ccaba8a1 h1:n2BXaNdJZWPu7Bpeeb0gSxG0fHqfFWCiO2Agcyygndo=
242+
github.com/dolthub/go-mysql-server v0.20.1-0.20260110001532-5eb9ccaba8a1/go.mod h1:ZODUC6XlqCx3DgHl6x7cWwMR0uq7XzVU0adsJvjZJTY=
243243
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
244244
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
245245
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
@@ -250,8 +250,8 @@ github.com/dolthub/pg_query_go/v6 v6.0.0-20251215122834-fb20be4254d1 h1:GY17cGA4
250250
github.com/dolthub/pg_query_go/v6 v6.0.0-20251215122834-fb20be4254d1/go.mod h1:qnrZP3/1slFl2Bq5yw38HLOsArZareGwdpEceriblLc=
251251
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 h1:JWkKRE4EHUcEVQCMRBej8DYxjYjRz/9MdF/NNQh0o70=
252252
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA=
253-
github.com/dolthub/vitess v0.0.0-20260108222406-f8a2587c4954 h1:VN2ZjnYPyxcAN/XetvcdumFbI2Ad/Gb47Qwdo9REY3A=
254-
github.com/dolthub/vitess v0.0.0-20260108222406-f8a2587c4954/go.mod h1:FLWqdXsAeeBQyFwDjmBVu0GnbjI2MKeRf3tRVdJEKlI=
253+
github.com/dolthub/vitess v0.0.0-20260109110924-205efc8530f1 h1:souetbYNBRHrt9y990VGD1jkzCIQ0jC+gxMdFOEjL+g=
254+
github.com/dolthub/vitess v0.0.0-20260109110924-205efc8530f1/go.mod h1:FLWqdXsAeeBQyFwDjmBVu0GnbjI2MKeRf3tRVdJEKlI=
255255
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
256256
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
257257
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=

server/ast/select_clause.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package ast
1616

1717
import (
18-
"github.com/cockroachdb/errors"
1918
"github.com/dolthub/go-mysql-server/sql/expression"
2019

2120
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
@@ -160,8 +159,17 @@ PostJoinRewrite:
160159
}
161160
}
162161
}
162+
distinct := node.Distinct
163+
var distinctOn vitess.Exprs
163164
if len(node.DistinctOn) > 0 {
164-
return nil, errors.Errorf("DISTINCT ON is not yet supported")
165+
distinct = true
166+
distinctOn = make(vitess.Exprs, len(node.DistinctOn))
167+
for i, expr := range node.DistinctOn {
168+
distinctOn[i], err = nodeExpr(ctx, expr)
169+
if err != nil {
170+
return nil, err
171+
}
172+
}
165173
}
166174
where, err := nodeWhere(ctx, node.Where)
167175
if err != nil {
@@ -180,7 +188,10 @@ PostJoinRewrite:
180188
return nil, err
181189
}
182190
return &vitess.Select{
183-
QueryOpts: vitess.QueryOpts{Distinct: node.Distinct},
191+
QueryOpts: vitess.QueryOpts{
192+
Distinct: distinct,
193+
DistinctOn: distinctOn,
194+
},
184195
SelectExprs: selectExprs,
185196
From: from,
186197
Where: where,

testing/go/select_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,113 @@ import (
2323
// TestSelect covers SELECT syntax not covered by our MySQL select tests
2424
func TestSelect(t *testing.T) {
2525
RunScripts(t, []ScriptTest{
26+
{
27+
Name: "SELECT DISTINCT ON",
28+
SetUpScript: []string{
29+
"CREATE TABLE test (v1 INT4, v2 INT4);",
30+
"INSERT INTO test VALUES (1, 3), (1, 4), (2, 3), (2, 4);",
31+
"CREATE TABLE test2 (v1 INT4, v2 INT4, v3 INT4);",
32+
"INSERT INTO test2 VALUES (1, 3, 5), (2, 3, 5), (1, 4, 5), (2, 4, 5);",
33+
},
34+
Assertions: []ScriptTestAssertion{
35+
{
36+
Query: "SELECT * FROM test ORDER BY v1, v2;",
37+
Expected: []sql.Row{
38+
{1, 3},
39+
{1, 4},
40+
{2, 3},
41+
{2, 4},
42+
},
43+
},
44+
{
45+
Query: "SELECT DISTINCT * FROM test ORDER BY v1, v2;",
46+
Expected: []sql.Row{
47+
{1, 3},
48+
{1, 4},
49+
{2, 3},
50+
{2, 4},
51+
},
52+
},
53+
{
54+
Query: "SELECT DISTINCT ON(v1) * FROM test ORDER BY v1, v2;",
55+
Expected: []sql.Row{
56+
{1, 3},
57+
{2, 3},
58+
},
59+
},
60+
{
61+
Query: "SELECT DISTINCT ON(v2) * FROM test ORDER BY v2, v1;",
62+
Expected: []sql.Row{
63+
{1, 3},
64+
{1, 4},
65+
},
66+
},
67+
{
68+
Query: "SELECT DISTINCT ON(v1) * FROM test ORDER BY v2, v1;",
69+
ExpectedErr: sql.ErrDistinctOnMatchOrderBy.Message,
70+
},
71+
{
72+
Query: "SELECT DISTINCT ON(v2) * FROM test ORDER BY v2 DESC, v1 DESC;",
73+
Expected: []sql.Row{
74+
{2, 4},
75+
{2, 3},
76+
},
77+
},
78+
{
79+
Query: "SELECT DISTINCT ON(v2, v1) * FROM test2 ORDER BY v1, v2;",
80+
Expected: []sql.Row{
81+
{1, 3, 5},
82+
{1, 4, 5},
83+
{2, 3, 5},
84+
{2, 4, 5},
85+
},
86+
},
87+
{
88+
Query: "SELECT DISTINCT ON(v2, v1) * FROM test2 ORDER BY v1, v2 DESC;",
89+
Expected: []sql.Row{
90+
{1, 4, 5},
91+
{1, 3, 5},
92+
{2, 4, 5},
93+
{2, 3, 5},
94+
},
95+
},
96+
{
97+
Query: "SELECT DISTINCT ON(v2, v1) * FROM test2 ORDER BY v1, v2 LIMIT 1;",
98+
Expected: []sql.Row{
99+
{1, 3, 5},
100+
},
101+
},
102+
{
103+
Query: "SELECT DISTINCT ON(v2, v1) * FROM test2 ORDER BY v1, v2 DESC LIMIT 1;",
104+
Expected: []sql.Row{
105+
{1, 4, 5},
106+
},
107+
},
108+
{
109+
Query: "SELECT DISTINCT ON(v1, v2, v3) * FROM test2 ORDER BY v1, v2;",
110+
Expected: []sql.Row{
111+
{1, 3, 5},
112+
{1, 4, 5},
113+
{2, 3, 5},
114+
{2, 4, 5},
115+
},
116+
},
117+
{
118+
Query: "SELECT DISTINCT ON(v3) v1 FROM test2;",
119+
Expected: []sql.Row{
120+
{1},
121+
},
122+
},
123+
{
124+
Query: "SELECT DISTINCT ON(v1, v3) * FROM test2 ORDER BY v1, v2;",
125+
ExpectedErr: sql.ErrDistinctOnMatchOrderBy.Message,
126+
},
127+
{
128+
Query: "SELECT DISTINCT ON(v2) * FROM test2 ORDER BY v1, v2;",
129+
ExpectedErr: sql.ErrDistinctOnMatchOrderBy.Message,
130+
},
131+
},
132+
},
26133
{
27134
Name: "select values",
28135
Assertions: []ScriptTestAssertion{

0 commit comments

Comments
 (0)