Skip to content

Commit 8cbd988

Browse files
author
James Cor
committed
Merge branch 'main' into james/lazy
2 parents 486f6d5 + d8e3189 commit 8cbd988

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-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: 26 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,9 @@ 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+
135+
origTbl := b.getOrigTblName(inScope.node, c.table)
136+
c = c.withOriginal(origTbl, v.Name.String())
134137
return c.scalarGf()
135138
case *ast.FuncExpr:
136139
name := v.Name.Lowered()
@@ -400,6 +403,28 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
400403
return nil
401404
}
402405

406+
func (b *Builder) getOrigTblName(node sql.Node, alias string) string {
407+
if node == nil {
408+
return ""
409+
}
410+
// Look past table aliases
411+
var origTbl string
412+
transform.Inspect(node, func(n sql.Node) bool {
413+
switch nn := n.(type) {
414+
case *plan.TableAlias:
415+
if nn.Name() == alias {
416+
if child, ok := nn.Child.(sql.Nameable); ok {
417+
origTbl = child.Name()
418+
}
419+
}
420+
return false
421+
default:
422+
return true
423+
}
424+
})
425+
return origTbl
426+
}
427+
403428
// getJsonValueTypeLiteral converts a type coercion string into a literal
404429
// expression with the zero type of the coercion (see json_value function).
405430
func (b *Builder) getJsonValueTypeLiteral(e sql.Expression) sql.Expression {

sql/planbuilder/scope.go

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

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

0 commit comments

Comments
 (0)