Skip to content

Commit 86669ea

Browse files
author
James Cor
committed
fewer schema allocations
1 parent 18ec94a commit 86669ea

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

sql/plan/tablealias.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
// TableAlias is a node that acts as a table with a given name.
2222
type TableAlias struct {
2323
*UnaryNode
24-
name string
25-
comment string
26-
id sql.TableId
27-
cols sql.ColSet
28-
sch sql.Schema
24+
name string
25+
comment string
26+
id sql.TableId
27+
cols sql.ColSet
28+
sch sql.Schema
29+
cachedSch bool
2930
}
3031

3132
var _ sql.RenameableNode = (*TableAlias)(nil)
@@ -34,13 +35,7 @@ var _ sql.CollationCoercible = (*TableAlias)(nil)
3435

3536
// NewTableAlias returns a new Table alias node.
3637
func NewTableAlias(name string, node sql.Node) *TableAlias {
37-
childSchema := node.Schema()
38-
schema := make(sql.Schema, len(childSchema))
39-
for i, col := range childSchema {
40-
newCol := *col
41-
newCol.Source = name
42-
schema[i] = &newCol
43-
}
38+
schema := make(sql.Schema, len(node.Schema()))
4439
ret := &TableAlias{
4540
UnaryNode: &UnaryNode{Child: node},
4641
name: name,
@@ -99,6 +94,14 @@ func (t *TableAlias) Comment() string {
9994
// Schema implements the Node interface. TableAlias alters the schema of its child element to rename the source of
10095
// columns to the alias.
10196
func (t *TableAlias) Schema() sql.Schema {
97+
if !t.cachedSch {
98+
for i, col := range t.Child.Schema() {
99+
newCol := *col
100+
newCol.Source = t.name
101+
t.sch[i] = &newCol
102+
}
103+
t.cachedSch = true
104+
}
102105
return t.sch
103106
}
104107

0 commit comments

Comments
 (0)