/
resolve_create_select.go
112 lines (96 loc) · 3.39 KB
/
resolve_create_select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package analyzer
import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/transform"
)
// todo this should be split into two rules. The first should be in
// planbuilder and only bind the child select, strip/merge schemas.
// a second rule should finalize analysis of the source/dest nodes
// (skipping passthrough rule).
func resolveCreateSelect(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
ct, ok := n.(*plan.CreateTable)
if !ok || ct.Select() == nil {
return n, transform.SameTree, nil
}
analyzedSelect, err := a.Analyze(ctx, ct.Select(), scope)
if err != nil {
return nil, transform.SameTree, err
}
// We don't want to carry any information about keys, constraints, defaults, etc. from a `create table as select`
// statement. When the underlying select node is a table, we must remove all such info from its schema. The only
// exception is NOT NULL constraints, which we leave alone.
selectSchema := stripSchema(analyzedSelect.Schema())
mergedSchema := mergeSchemas(ct.PkSchema().Schema, selectSchema)
newSch := make(sql.Schema, len(mergedSchema))
for i, col := range mergedSchema {
tempCol := *col
tempCol.Source = ct.Name()
newSch[i] = &tempCol
}
pkOrdinals := make([]int, 0)
for i, col := range newSch {
if col.PrimaryKey {
pkOrdinals = append(pkOrdinals, i)
}
}
newSpec := &plan.TableSpec{
Schema: sql.NewPrimaryKeySchema(newSch, pkOrdinals...),
}
newCreateTable := plan.NewCreateTable(ct.Database(), ct.Name(), ct.IfNotExists(), ct.Temporary(), newSpec)
analyzedCreate, err := a.Analyze(ctx, newCreateTable, scope)
if err != nil {
return nil, transform.SameTree, err
}
return plan.NewTableCopier(ct.Database(), StripPassthroughNodes(analyzedCreate), StripPassthroughNodes(analyzedSelect), plan.CopierProps{}), transform.NewTree, nil
}
// stripSchema removes all non-type information from a schema, such as the key info, default value, etc.
func stripSchema(schema sql.Schema) sql.Schema {
sch := schema.Copy()
for i := range schema {
sch[i].Default = nil
sch[i].Generated = nil
sch[i].AutoIncrement = false
sch[i].PrimaryKey = false
sch[i].Source = ""
sch[i].Comment = ""
}
return sch
}
// mergeSchemas takes in the table spec of the CREATE TABLE and merges it with the schema used by the
// select query. The ultimate structure for the new table will be [CREATE TABLE exclusive columns, columns with the same
// name, SELECT exclusive columns]
func mergeSchemas(inputSchema sql.Schema, selectSchema sql.Schema) sql.Schema {
if inputSchema == nil {
return selectSchema
}
// Get the matching columns between the two via name
matchingColumns := make([]*sql.Column, 0)
leftExclusive := make([]*sql.Column, 0)
for _, col := range inputSchema {
found := false
for _, col2 := range selectSchema {
if col.Name == col2.Name {
matchingColumns = append(matchingColumns, col)
found = true
}
}
if !found {
leftExclusive = append(leftExclusive, col)
}
}
rightExclusive := make([]*sql.Column, 0)
for _, col := range selectSchema {
found := false
for _, col2 := range inputSchema {
if col.Name == col2.Name {
found = true
break
}
}
if !found {
rightExclusive = append(rightExclusive, col)
}
}
return append(append(leftExclusive, matchingColumns...), rightExclusive...)
}