forked from goccy/go-zetasqlite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wildcard_table.go
191 lines (167 loc) · 4.44 KB
/
wildcard_table.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package internal
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
"time"
"github.com/goccy/go-zetasql/types"
)
const tableSuffixColumnName = "_TABLE_SUFFIX"
func (c *Catalog) isWildcardTable(path []string) bool {
if len(path) == 0 {
return false
}
lastPath := path[len(path)-1]
if len(lastPath) == 0 {
return false
}
lastChar := lastPath[len(lastPath)-1]
return lastChar == '*'
}
type WildcardTable struct {
spec *TableSpec
tables []*TableSpec
prefix string
}
func (t *WildcardTable) existsColumn(table *TableSpec, column string) bool {
for _, col := range table.Columns {
if col.Name == column {
return true
}
}
return false
}
func (t *WildcardTable) FormatSQL(ctx context.Context) (string, error) {
queries := make([]string, 0, len(t.tables))
for _, table := range t.tables {
var columns []string
for _, column := range t.spec.Columns {
if column.Name == tableSuffixColumnName {
continue
}
if t.existsColumn(table, column.Name) {
columns = append(columns, fmt.Sprintf("`%s`", column.Name))
} else {
columns = append(columns, fmt.Sprintf("NULL as %s", column.Name))
}
}
fullName := strings.Join(table.NamePath, ".")
if len(fullName) <= len(t.prefix) {
return "", fmt.Errorf("failed to find table suffix from %s", fullName)
}
tableSuffix := fullName[len(t.prefix):]
encodedSuffix, err := EncodeGoValue(types.StringType(), tableSuffix)
if err != nil {
return "", err
}
queries = append(queries,
fmt.Sprintf(
"SELECT %s, '%s' as _TABLE_SUFFIX FROM `%s`",
strings.Join(columns, ","),
encodedSuffix,
table.TableName(),
),
)
}
return strings.Join(queries, " UNION ALL "), nil
}
func (t *WildcardTable) Name() string {
return strings.Join(t.spec.NamePath, ".")
}
func (t *WildcardTable) FullName() string {
return t.spec.TableName()
}
func (t *WildcardTable) NumColumns() int {
return len(t.spec.Columns)
}
func (t *WildcardTable) Column(idx int) types.Column {
column := t.spec.Columns[idx]
typ, err := column.Type.ToZetaSQLType()
if err != nil {
return nil
}
return types.NewSimpleColumn(
strings.Join(t.spec.NamePath, "."), column.Name, typ,
)
}
func (t *WildcardTable) PrimaryKey() []int {
return nil
}
func (t *WildcardTable) FindColumnByName(name string) types.Column {
for _, col := range t.spec.Columns {
if col.Name == name {
typ, err := col.Type.ToZetaSQLType()
if err != nil {
return nil
}
return types.NewSimpleColumn(
t.spec.TableName(), col.Name, typ,
)
}
}
return nil
}
func (t *WildcardTable) IsValueTable() bool {
return false
}
func (t *WildcardTable) SerializationID() int64 {
return 0
}
func (t *WildcardTable) CreateEvaluatorTableIterator(columnIdxs []int) (*types.EvaluatorTableIterator, error) {
return nil, nil
}
func (t *WildcardTable) AnonymizationInfo() *types.AnonymizationInfo {
return nil
}
func (t *WildcardTable) SupportsAnonymization() bool {
return false
}
func (t *WildcardTable) TableTypeName(mode types.ProductMode) string {
return ""
}
func (c *Catalog) createWildcardTable(path []string) (types.Table, error) {
name := strings.Join(path, "_")
name = strings.TrimRight(name, "*")
re, err := regexp.Compile(name)
if err != nil {
return nil, fmt.Errorf("failed to compile %s: %w", name, err)
}
matchedSpecs := make([]*TableSpec, 0, len(c.tableMap))
for name, spec := range c.tableMap {
if re.MatchString(name) {
matchedSpecs = append(matchedSpecs, spec)
}
}
sort.Slice(matchedSpecs, func(i, j int) bool {
return matchedSpecs[i].CreatedAt.UnixNano() > matchedSpecs[j].CreatedAt.UnixNano()
})
if len(matchedSpecs) == 0 {
return nil, fmt.Errorf("failed to find matched tables by wildcard")
}
spec := matchedSpecs[0]
wildcardTable := new(TableSpec)
*wildcardTable = *spec
wildcardTable.NamePath = append([]string{}, spec.NamePath...)
wildcardTable.Columns = append(wildcardTable.Columns, &ColumnSpec{
Name: tableSuffixColumnName,
Type: &Type{Kind: types.STRING},
})
lastNamePath := spec.NamePath[len(spec.NamePath)-1]
lastNamePath = lastNamePath[:len(path)-1]
wildcardTable.NamePath[len(spec.NamePath)-1] = fmt.Sprintf(
"%s_wildcard_%d", lastNamePath, time.Now().Unix(),
)
// firstIdentifier may be omitted, so we need to check it.
prefix := name
firstIdentifier := spec.NamePath[0]
if !strings.HasPrefix(prefix, firstIdentifier+".") {
prefix = firstIdentifier + "." + prefix
}
return &WildcardTable{
spec: wildcardTable,
tables: matchedSpecs,
prefix: prefix,
}, nil
}