Skip to content

Commit 27648af

Browse files
author
James Cor
committed
exempt processlist lower casing through aliases
1 parent e44b780 commit 27648af

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

enginetest/enginetests.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,95 @@ func TestInfoSchema(t *testing.T, h Harness) {
359359
)
360360
})
361361

362+
t.Run("information_schema.processlist projection with alias case", func(t *testing.T) {
363+
e := mustNewEngine(t, h)
364+
defer e.Close()
365+
366+
if IsServerEngine(e) {
367+
t.Skip("skipping for server engine as the processlist returned from server differs")
368+
}
369+
p := sqle.NewProcessList()
370+
p.AddConnection(1, "localhost")
371+
372+
ctx := NewContext(h)
373+
ctx.Session.SetClient(sql.Client{Address: "localhost", User: "root"})
374+
ctx.Session.SetConnectionId(1)
375+
ctx.ProcessList = p
376+
ctx.SetCurrentDatabase("")
377+
378+
p.ConnectionReady(ctx.Session)
379+
380+
ctx, err := p.BeginQuery(ctx, "SELECT foo")
381+
require.NoError(t, err)
382+
383+
p.AddConnection(2, "otherhost")
384+
sess2 := sql.NewBaseSessionWithClientServer("localhost", sql.Client{Address: "otherhost", User: "root"}, 2)
385+
sess2.SetCurrentDatabase("otherdb")
386+
p.ConnectionReady(sess2)
387+
ctx2 := sql.NewContext(context.Background(), sql.WithPid(2), sql.WithSession(sess2))
388+
ctx2, err = p.BeginQuery(ctx2, "SELECT bar")
389+
require.NoError(t, err)
390+
p.EndQuery(ctx2)
391+
392+
TestQueryWithContext(t, ctx, e, h,
393+
"SELECT id, uSeR, hOST FROM information_schema.processlist pl ORDER BY id",
394+
[]sql.Row{
395+
{uint64(1), "root", "localhost"},
396+
{uint64(2), "root", "otherhost"},
397+
},
398+
sql.Schema{
399+
{Name: "id", Type: types.Uint64},
400+
{Name: "uSeR", Type: types.MustCreateString(sqltypes.VarChar, 96, sql.Collation_Information_Schema_Default)},
401+
{Name: "hOST", Type: types.MustCreateString(sqltypes.VarChar, 783, sql.Collation_Information_Schema_Default)},
402+
},
403+
nil, nil,
404+
)
405+
})
406+
407+
t.Run("information_schema.processlist projection with aliased join case", func(t *testing.T) {
408+
e := mustNewEngine(t, h)
409+
defer e.Close()
410+
411+
if IsServerEngine(e) {
412+
t.Skip("skipping for server engine as the processlist returned from server differs")
413+
}
414+
p := sqle.NewProcessList()
415+
p.AddConnection(1, "localhost")
416+
417+
ctx := NewContext(h)
418+
ctx.Session.SetClient(sql.Client{Address: "localhost", User: "root"})
419+
ctx.Session.SetConnectionId(1)
420+
ctx.ProcessList = p
421+
ctx.SetCurrentDatabase("")
422+
423+
p.ConnectionReady(ctx.Session)
424+
425+
ctx, err := p.BeginQuery(ctx, "SELECT foo")
426+
require.NoError(t, err)
427+
428+
p.AddConnection(2, "otherhost")
429+
sess2 := sql.NewBaseSessionWithClientServer("localhost", sql.Client{Address: "otherhost", User: "root"}, 2)
430+
sess2.SetCurrentDatabase("otherdb")
431+
p.ConnectionReady(sess2)
432+
ctx2 := sql.NewContext(context.Background(), sql.WithPid(2), sql.WithSession(sess2))
433+
ctx2, err = p.BeginQuery(ctx2, "SELECT bar")
434+
require.NoError(t, err)
435+
p.EndQuery(ctx2)
436+
437+
TestQueryWithContext(t, ctx, e, h,
438+
"SELECT id, uSeR, hOST FROM information_schema.processlist pl join information_schema.schemata on true ORDER BY id limit 1",
439+
[]sql.Row{
440+
{uint64(1), "root", "localhost"},
441+
},
442+
sql.Schema{
443+
{Name: "id", Type: types.Uint64},
444+
{Name: "uSeR", Type: types.MustCreateString(sqltypes.VarChar, 96, sql.Collation_Information_Schema_Default)},
445+
{Name: "hOST", Type: types.MustCreateString(sqltypes.VarChar, 783, sql.Collation_Information_Schema_Default)},
446+
},
447+
nil, nil,
448+
)
449+
})
450+
362451
for _, tt := range queries.SkippedInfoSchemaQueries {
363452
t.Run(tt.Query, func(t *testing.T) {
364453
t.Skip()

sql/planbuilder/scalar.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/dolthub/go-mysql-server/sql/expression/function/json"
3131
"github.com/dolthub/go-mysql-server/sql/fulltext"
3232
"github.com/dolthub/go-mysql-server/sql/plan"
33+
"github.com/dolthub/go-mysql-server/sql/transform"
3334
"github.com/dolthub/go-mysql-server/sql/types"
3435
)
3536

@@ -130,7 +131,25 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
130131
}
131132
b.handleErr(err)
132133
}
133-
c = c.withOriginal(v.Name.String())
134+
// Look past table aliases
135+
// TODO: also find it through joins??? wtf
136+
var origTbl string
137+
if inScope.node != nil {
138+
transform.Inspect(inScope.node, func(node sql.Node) bool {
139+
switch n := node.(type) {
140+
case *plan.TableAlias:
141+
if n.Name() == c.table {
142+
if child, ok := n.Child.(sql.Nameable); ok {
143+
origTbl = child.Name()
144+
}
145+
}
146+
return false
147+
}
148+
return true
149+
})
150+
}
151+
152+
c = c.withOriginal(origTbl, v.Name.String())
134153
return c.scalarGf()
135154
case *ast.FuncExpr:
136155
name := v.Name.Lowered()

sql/planbuilder/scope.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,12 @@ func (c scopeColumn) unwrapGetFieldAliasId() columnId {
612612
return c.id
613613
}
614614

615-
func (c scopeColumn) withOriginal(col string) scopeColumn {
615+
func (c scopeColumn) withOriginal(origTbl, col string) scopeColumn {
616616
// info schema columns always presented as uppercase, except for processlist
617617
// can't reference information_schema.ProcessListTableName because of import cycles
618-
if !strings.EqualFold(c.db, sql.InformationSchemaDatabaseName) || (strings.EqualFold(c.db, sql.InformationSchemaDatabaseName) && strings.EqualFold(c.table, "processlist")) {
618+
if !strings.EqualFold(c.db, sql.InformationSchemaDatabaseName) ||
619+
(strings.EqualFold(c.db, sql.InformationSchemaDatabaseName) && strings.EqualFold(c.table, "processlist")) ||
620+
(strings.EqualFold(c.db, sql.InformationSchemaDatabaseName) && strings.EqualFold(origTbl, "processlist")) {
619621
c.originalCol = col
620622
}
621623
return c

0 commit comments

Comments
 (0)