-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve_create_like.go
executable file
·86 lines (79 loc) · 2.54 KB
/
resolve_create_like.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
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package analyzer
import (
"fmt"
"strings"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/plan"
)
func resolveCreateLike(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
ct, ok := n.(*plan.CreateTable)
if !ok || ct.Like() == nil {
return n, nil
}
resolvedLikeTable, ok := ct.Like().(*plan.ResolvedTable)
if !ok {
return nil, fmt.Errorf("attempted to resolve CREATE LIKE, expected `ResolvedTable` but received `%T`", ct.Like())
}
likeTable := resolvedLikeTable.Table
var idxDefs []*plan.IndexDefinition
if indexableTable, ok := likeTable.(sql.IndexedTable); ok {
indexes, err := indexableTable.GetIndexes(ctx)
if err != nil {
return nil, err
}
for _, index := range indexes {
if index.IsGenerated() {
continue
}
constraint := sql.IndexConstraint_None
if index.IsUnique() {
constraint = sql.IndexConstraint_Unique
}
columns := make([]sql.IndexColumn, len(index.Expressions()))
for i, col := range index.Expressions() {
//TODO: find a better way to get only the column name if the table is present
col = strings.TrimPrefix(col, indexableTable.Name()+".")
columns[i] = sql.IndexColumn{
Name: col,
Length: 0,
}
}
idxDefs = append(idxDefs, &plan.IndexDefinition{
IndexName: index.ID(),
Using: sql.IndexUsing_Default,
Constraint: constraint,
Columns: columns,
Comment: index.Comment(),
})
}
}
origSch := likeTable.Schema()
newSch := make(sql.Schema, len(origSch))
for i, col := range origSch {
tempCol := *col
tempCol.Source = ct.Name()
newSch[i] = &tempCol
}
var pkOrdinals []int
if pkTable, ok := likeTable.(sql.PrimaryKeyTable); ok {
pkOrdinals = pkTable.PrimaryKeySchema().PkOrdinals
}
tableSpec := &plan.TableSpec{
Schema: sql.NewPrimaryKeySchema(newSch, pkOrdinals...),
IdxDefs: idxDefs,
}
return plan.NewCreateTable(ct.Database(), ct.Name(), ct.IfNotExists(), ct.Temporary(), tableSpec), nil
}