@@ -25,6 +25,7 @@ type TableAlias struct {
2525 comment string
2626 id sql.TableId
2727 cols sql.ColSet
28+ sch sql.Schema
2829}
2930
3031var _ sql.RenameableNode = (* TableAlias )(nil )
@@ -33,7 +34,10 @@ var _ sql.CollationCoercible = (*TableAlias)(nil)
3334
3435// NewTableAlias returns a new Table alias node.
3536func NewTableAlias (name string , node sql.Node ) * TableAlias {
36- ret := & TableAlias {UnaryNode : & UnaryNode {Child : node }, name : name }
37+ ret := & TableAlias {
38+ UnaryNode : & UnaryNode {Child : node },
39+ name : name ,
40+ }
3741 if tin , ok := node .(TableIdNode ); ok {
3842 ret .id = tin .Id ()
3943 ret .cols = tin .Columns ()
@@ -87,14 +91,16 @@ func (t *TableAlias) Comment() string {
8791// Schema implements the Node interface. TableAlias alters the schema of its child element to rename the source of
8892// columns to the alias.
8993func (t * TableAlias ) Schema () sql.Schema {
90- childSchema := t .Child .Schema ()
91- copy := make (sql.Schema , len (childSchema ))
92- for i , col := range childSchema {
93- colCopy := * col
94- colCopy .Source = t .name
95- copy [i ] = & colCopy
94+ if t .sch == nil {
95+ childSchema := t .Child .Schema ()
96+ t .sch = make (sql.Schema , len (childSchema ))
97+ for i , col := range childSchema {
98+ newCol := * col
99+ newCol .Source = t .name
100+ t .sch [i ] = & newCol
101+ }
96102 }
97- return copy
103+ return t . sch
98104}
99105
100106// WithChildren implements the Node interface.
@@ -118,21 +124,22 @@ func (t *TableAlias) CollationCoercibility(ctx *sql.Context) (collation sql.Coll
118124 return sql .Collation_binary , 7
119125}
120126
121- func (t TableAlias ) String () string {
127+ func (t * TableAlias ) String () string {
122128 pr := sql .NewTreePrinter ()
123129 _ = pr .WriteNode ("TableAlias(%s)" , t .name )
124130 _ = pr .WriteChildren (t .Child .String ())
125131 return pr .String ()
126132}
127133
128- func (t TableAlias ) DebugString () string {
134+ func (t * TableAlias ) DebugString () string {
129135 pr := sql .NewTreePrinter ()
130136 _ = pr .WriteNode ("TableAlias(%s)" , t .name )
131137 _ = pr .WriteChildren (sql .DebugString (t .Child ))
132138 return pr .String ()
133139}
134140
135- func (t TableAlias ) WithName (name string ) sql.Node {
136- t .name = name
137- return & t
141+ func (t * TableAlias ) WithName (name string ) sql.Node {
142+ nt := * t
143+ nt .name = name
144+ return & nt
138145}
0 commit comments