Skip to content

Commit afb7521

Browse files
committed
passing all tests except query plan tests
1 parent 0787112 commit afb7521

File tree

8 files changed

+64
-84
lines changed

8 files changed

+64
-84
lines changed

enginetest/enginetests.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -865,44 +865,8 @@ func TestOrderByGroupBy(t *testing.T, harness Harness) {
865865
}
866866
}
867867
require.Equal(t, rowCount, 3)
868-
869-
// TODO: this should error; the order by doesn't count towards ONLY_FULL_GROUP_BY
870-
_, rowIter, _, err = e.Query(ctx, "select id, team from members group by team order by id")
871-
require.NoError(t, err)
872-
rowCount = 0
873-
for {
874-
row, err = rowIter.Next(ctx)
875-
if err == io.EOF {
876-
break
877-
}
878-
rowCount++
879-
require.NoError(t, err)
880-
881-
var val int64
882-
switch v := row[0].(type) {
883-
case int64:
884-
val = v
885-
case int32:
886-
val = int64(v)
887-
default:
888-
panic(fmt.Sprintf("unexpected type %T", v))
889-
}
890-
891-
team, ok, err := sql.Unwrap[string](ctx, row[1])
892-
require.True(t, ok)
893-
require.NoError(t, err)
894-
switch team {
895-
case "red":
896-
require.True(t, val == 3 || val == 4)
897-
case "orange":
898-
require.True(t, val == 5 || val == 6 || val == 7)
899-
case "purple":
900-
require.True(t, val == 8)
901-
default:
902-
panic("received non-existent team")
903-
}
904-
}
905-
require.Equal(t, rowCount, 3)
868+
869+
AssertErr(t, e, harness, "select id, team from members group by team order by id", nil, analyzererrors.ErrValidationGroupBy)
906870
})
907871
}
908872

enginetest/queries/column_alias_queries.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ var ColumnAliasQueries = []ScriptTest{
210210
Query: "SELECT 1 as a, (select a) as b;",
211211
Expected: []sql.Row{{1, 1}},
212212
},
213+
{
214+
Query: `SELECT 1 as a, (select a union select a) as b;`,
215+
Expected: []sql.Row{{1, 1}},
216+
},
213217
{
214218
Query: "SELECT 1 as a, (select a) as b from dual;",
215219
Expected: []sql.Row{{1, 1}},
@@ -226,6 +230,22 @@ var ColumnAliasQueries = []ScriptTest{
226230
Query: "SELECT 1 as a, (select a) from xy;",
227231
Expected: []sql.Row{{1, 1}, {1, 1}, {1, 1}, {1, 1}},
228232
},
233+
{
234+
// https://github.com/dolthub/dolt/issues/4256
235+
Query: `SELECT *, (select i union select i) as a from mytable;`,
236+
Expected: []sql.Row{
237+
{1, "first row", 1},
238+
{2, "second row", 2},
239+
{3, "third row", 3}},
240+
},
241+
{
242+
Query: "select 1 as b, (select b group by b order by b) order by 1;",
243+
Expected: []sql.Row{{1, 1}},
244+
},
245+
{
246+
Query: `select 1 as a, (select b), 0 as b;`,
247+
ExpectedErr: sql.ErrColumnNotFound,
248+
},
229249
},
230250
},
231251
{
@@ -245,39 +265,15 @@ var ColumnAliasQueries = []ScriptTest{
245265
},
246266
{
247267
Name: "various broken alias queries",
268+
Skip: true,
248269
Assertions: []ScriptTestAssertion{
249-
{
250-
// The second query in the union subquery returns "x" instead of mytable.i
251-
// https://github.com/dolthub/dolt/issues/4256
252-
Query: `SELECT *, (select i union select i) as a from mytable;`,
253-
Expected: []sql.Row{
254-
{1, "first row", 1},
255-
{2, "second row", 2},
256-
{3, "third row", 3}},
257-
},
258-
{
259-
Query: `SELECT 1 as a, (select a union select a) as b;`,
260-
Expected: []sql.Row{{1, 1}},
261-
},
262-
{
263-
// GMS executes this query, but it is not valid because of the forward ref of alias b.
264-
// GMS should return an error about an invalid forward-ref.
265-
Skip: true,
266-
Query: `select 1 as a, (select b), 0 as b;`,
267-
ExpectedErr: sql.ErrColumnNotFound,
268-
},
269270
{
270271
// GMS returns "expression 'dt.two' doesn't appear in the group by expressions", but MySQL will execute
271272
// this query.
272273
Query: "select 1 as a, one + 1 as mod1, dt.* from mytable as t1, (select 1, 2 from mytable) as dt (one, two) where dt.one > 0 group by one;",
273274
// column names: a, mod1, one, two
274275
Expected: []sql.Row{{1, 2, 1, 2}},
275276
},
276-
{
277-
// GMS returns `ambiguous column or alias name "b"` on both cases of `group by b` and `group by 1` inside subquery, but MySQL executes.
278-
Query: "select 1 as b, (select b group by b order by b) order by 1;",
279-
Expected: []sql.Row{{1, 1}},
280-
},
281277
},
282278
},
283279
}

enginetest/queries/information_schema_queries.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var InfoSchemaQueries = []QueryTest{
4646
table_name, index_name, comment, non_unique, GROUP_CONCAT(column_name ORDER BY seq_in_index) AS COLUMNS
4747
FROM information_schema.statistics
4848
WHERE table_schema='mydb' AND table_name='mytable' AND index_name!="PRIMARY"
49-
GROUP BY index_name;`,
49+
GROUP BY index_name, comment, non_unique;`,
5050
ExpectedColumns: sql.Schema{
5151
{
5252
Name: "TABLE_NAME",
@@ -274,7 +274,7 @@ var InfoSchemaQueries = []QueryTest{
274274
WHERE FILE_TYPE = 'UNDO LOG'
275275
AND FILE_NAME IS NOT NULL
276276
AND LOGFILE_GROUP_NAME IS NOT NULL
277-
GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE
277+
GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE, TOTAL_EXTENTS, INITIAL_SIZE, EXTRA
278278
ORDER BY LOGFILE_GROUP_NAME
279279
`,
280280
Expected: nil,

enginetest/queries/json_scripts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ var JsonScripts = []ScriptTest{
516516
},
517517
Assertions: []ScriptTestAssertion{
518518
{
519-
Query: "SELECT pk, JSON_ARRAYAGG(field) FROM (SELECT * FROM j ORDER BY pk) as sub GROUP BY field ORDER BY pk",
519+
Query: "SELECT pk, JSON_ARRAYAGG(field) FROM (SELECT * FROM j ORDER BY pk) as sub GROUP BY pk, field ORDER BY pk",
520520
Expected: []sql.Row{
521521
{1, types.MustJSON(`[{"key1": {"key": "value"}}]`)},
522522
{2, types.MustJSON(`[{"key1": "value1", "key2": "value2"}]`)},

enginetest/queries/sysbench_plans.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/analyzer/validation_rules.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
253253
checkParent := false
254254
var project *plan.Project
255255
var having *plan.Having
256-
var filter *plan.Filter
257256
transform.Inspect(n, func(n sql.Node) bool {
258257
defer func() {
259258
parent = n
@@ -290,17 +289,16 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
290289
exprs := make([]sql.Expression, 0)
291290
exprs = append(exprs, n.GroupByExprs...)
292291
if having != nil {
293-
if eq, ok := having.Cond.(*expression.Equals); ok {
294-
exprs = append(exprs, eq.Children()...)
295-
}
292+
exprs = append(exprs, getEqualsDependencies(having.Cond)...)
296293
}
297-
if filter != nil {
294+
possibleJoin := n.Child
295+
if filter, ok := n.Child.(*plan.Filter); ok {
296+
possibleJoin = filter.Child
297+
exprs = append(exprs, getEqualsDependencies(filter.Expression)...)
298298
}
299-
if join, ok := n.Child.(*plan.JoinNode); ok {
299+
if join, ok := possibleJoin.(*plan.JoinNode); ok {
300300
isJoin = true
301-
if eq, ok := join.Filter.(*expression.Equals); ok {
302-
exprs = append(exprs, eq.Children()...)
303-
}
301+
exprs = append(exprs, getEqualsDependencies(join.Filter)...)
304302
}
305303
for _, expr := range exprs {
306304
sql.Inspect(expr, func(expr sql.Expression) bool {
@@ -310,6 +308,9 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
310308
}
311309
groupBys[exprStr] = true
312310

311+
if nameable, ok := expr.(sql.Nameable); ok {
312+
groupBys[strings.ToLower(nameable.Name())] = true
313+
}
313314
_, isAlias := expr.(*expression.Alias)
314315
return isAlias
315316
})
@@ -334,8 +335,6 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
334335
}
335336
case *plan.Project:
336337
project = n
337-
case *plan.Filter:
338-
filter = n
339338
case *plan.Having:
340339
having = n
341340
}
@@ -345,6 +344,26 @@ func validateGroupBy(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scop
345344
return n, transform.SameTree, err
346345
}
347346

347+
func getEqualsDependencies(expr sql.Expression) []sql.Expression {
348+
exprs := make([]sql.Expression, 0)
349+
sql.Inspect(expr, func(expr sql.Expression) bool {
350+
switch expr := expr.(type) {
351+
case *expression.And:
352+
return true
353+
case *expression.Equals:
354+
for _, e := range expr.Children() {
355+
if and, ok := e.(*expression.And); ok {
356+
exprs = append(exprs, getEqualsDependencies(and)...)
357+
} else if _, ok := e.(*expression.Literal); !ok {
358+
exprs = append(exprs, e)
359+
}
360+
}
361+
}
362+
return false
363+
})
364+
return exprs
365+
}
366+
348367
func getSelectExprs(project *plan.Project, selectDeps []sql.Expression, groupBys map[string]bool) []sql.Expression {
349368
if project == nil {
350369
return selectDeps
@@ -392,6 +411,12 @@ func expressionReferencesOnlyGroupBys(groupBys map[string]bool, expr sql.Express
392411
return false
393412
}
394413

414+
if nameable, ok := expr.(sql.Nameable); ok {
415+
if groupBys[strings.ToLower(nameable.Name())] {
416+
return false
417+
}
418+
}
419+
395420
if len(expr.Children()) == 0 {
396421
valid = false
397422
return false

sql/plan/group_by.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (g *GroupBy) String() string {
140140
}
141141

142142
_ = pr.WriteChildren(
143-
fmt.Sprintf("Select Dependencies(%s)", strings.Join(selectDeps, ", ")),
143+
fmt.Sprintf("SelectDeps(%s)", strings.Join(selectDeps, ", ")),
144144
fmt.Sprintf("Grouping(%s)", strings.Join(grouping, ", ")),
145145
g.Child.String(),
146146
)

sql/plan/subquery.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,6 @@ func (s *Subquery) IsNullable() bool {
502502
}
503503

504504
func (s *Subquery) String() string {
505-
//pr := sql.NewTreePrinter()
506-
//_ = pr.WriteNode("Subquery")
507-
//children := []string{fmt.Sprintf("cacheable: %t", s.canCacheResults()), s.Query.String()}
508-
//_ = pr.WriteChildren(children...)
509-
//return pr.String()
510505
return fmt.Sprintf("Subquery(%s)", s.QueryString)
511506
}
512507

0 commit comments

Comments
 (0)