Skip to content

Commit 18ec94a

Browse files
author
James Cor
committed
cache schemas
1 parent 3693a55 commit 18ec94a

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

sql/plan/project.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Project struct {
3535
// a RowIter.
3636
IncludesNestedIters bool
3737
deps sql.ColSet
38+
39+
sch sql.Schema
40+
cachedSch bool
3841
}
3942

4043
var _ sql.Expressioner = (*Project)(nil)
@@ -47,6 +50,7 @@ func NewProject(expressions []sql.Expression, child sql.Node) *Project {
4750
return &Project{
4851
UnaryNode: UnaryNode{child},
4952
Projections: expressions,
53+
sch: make(sql.Schema, len(expressions)),
5054
}
5155
}
5256

@@ -122,14 +126,16 @@ func ExprDeps(exprs ...sql.Expression) sql.ColSet {
122126

123127
// Schema implements the Node interface.
124128
func (p *Project) Schema() sql.Schema {
125-
var s = make(sql.Schema, len(p.Projections))
126-
for i, expr := range p.Projections {
127-
s[i] = transform.ExpressionToColumn(expr, AliasSubqueryString(expr))
128-
if gf := unwrapGetField(expr); gf != nil {
129-
s[i].Default = findDefault(p.Child, gf)
129+
if !p.cachedSch {
130+
p.cachedSch = true
131+
for i, expr := range p.Projections {
132+
p.sch[i] = transform.ExpressionToColumn(expr, AliasSubqueryString(expr))
133+
if gf := unwrapGetField(expr); gf != nil {
134+
p.sch[i].Default = findDefault(p.Child, gf)
135+
}
130136
}
131137
}
132-
return s
138+
return p.sch
133139
}
134140

135141
// Resolved implements the Resolvable interface.

sql/plan/tablealias.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type TableAlias struct {
2525
comment string
2626
id sql.TableId
2727
cols sql.ColSet
28+
sch sql.Schema
2829
}
2930

3031
var _ sql.RenameableNode = (*TableAlias)(nil)
@@ -33,7 +34,18 @@ var _ sql.CollationCoercible = (*TableAlias)(nil)
3334

3435
// NewTableAlias returns a new Table alias node.
3536
func NewTableAlias(name string, node sql.Node) *TableAlias {
36-
ret := &TableAlias{UnaryNode: &UnaryNode{Child: node}, name: name}
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+
}
44+
ret := &TableAlias{
45+
UnaryNode: &UnaryNode{Child: node},
46+
name: name,
47+
sch: schema,
48+
}
3749
if tin, ok := node.(TableIdNode); ok {
3850
ret.id = tin.Id()
3951
ret.cols = tin.Columns()
@@ -87,14 +99,7 @@ func (t *TableAlias) Comment() string {
8799
// Schema implements the Node interface. TableAlias alters the schema of its child element to rename the source of
88100
// columns to the alias.
89101
func (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
96-
}
97-
return copy
102+
return t.sch
98103
}
99104

100105
// WithChildren implements the Node interface.
@@ -118,21 +123,22 @@ func (t *TableAlias) CollationCoercibility(ctx *sql.Context) (collation sql.Coll
118123
return sql.Collation_binary, 7
119124
}
120125

121-
func (t TableAlias) String() string {
126+
func (t *TableAlias) String() string {
122127
pr := sql.NewTreePrinter()
123128
_ = pr.WriteNode("TableAlias(%s)", t.name)
124129
_ = pr.WriteChildren(t.Child.String())
125130
return pr.String()
126131
}
127132

128-
func (t TableAlias) DebugString() string {
133+
func (t *TableAlias) DebugString() string {
129134
pr := sql.NewTreePrinter()
130135
_ = pr.WriteNode("TableAlias(%s)", t.name)
131136
_ = pr.WriteChildren(sql.DebugString(t.Child))
132137
return pr.String()
133138
}
134139

135-
func (t TableAlias) WithName(name string) sql.Node {
136-
t.name = name
137-
return &t
140+
func (t *TableAlias) WithName(name string) sql.Node {
141+
nt := *t
142+
nt.name = name
143+
return &nt
138144
}

0 commit comments

Comments
 (0)