-
Notifications
You must be signed in to change notification settings - Fork 2
/
query_generator.go
185 lines (157 loc) · 3.94 KB
/
query_generator.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
package nqo
import (
"bytes"
"github.com/cockroachdb/cockroach/pkg/util"
)
type table int
const (
nation table = iota
region
part
supplier
partsupp
customer
orders
lineitem
numTables
)
var tableNames = [...]string{
nation: "nation",
region: "region",
part: "part",
supplier: "supplier",
partsupp: "partsupp",
customer: "customer",
orders: "orders",
lineitem: "lineitem",
}
type joinPath struct {
pk table
fk table
cond string
}
type tabSet = util.FastIntSet
type joinCondKey struct {
t1 table
t2 table
}
var paths = []joinPath{
{region, nation, "r_regionkey = n_regionkey"},
{nation, supplier, "n_nationkey = s_nationkey"},
{nation, customer, "n_nationkey = c_nationkey"},
{part, partsupp, "p_partkey = ps_partkey"},
{part, lineitem, "p_partkey = l_partkey"},
{supplier, partsupp, "s_suppkey = ps_suppkey"},
{supplier, lineitem, "s_suppkey = l_suppkey"},
{partsupp, lineitem, "ps_partkey = l_partkey AND l_suppkey = ps_suppkey"},
{customer, orders, "c_custkey = o_custkey"},
{orders, lineitem, "o_orderkey = l_orderkey"},
}
var joinsWith map[table]*tabSet
var joinCondition map[joinCondKey]string
func makeJoinsWith() {
joinsWith = make(map[table]*tabSet, numTables)
for i := table(0); i < numTables; i++ {
joinsWith[i] = &tabSet{}
}
joinCondition = make(map[joinCondKey]string, 2*len(paths))
for i := range paths {
path := &paths[i]
joinsWith[path.pk].Add(int(path.fk))
joinsWith[path.fk].Add(int(path.pk))
joinCondition[joinCondKey{path.pk, path.fk}] = path.cond
joinCondition[joinCondKey{path.fk, path.pk}] = path.cond
}
}
func generate(n int) [][]table {
tables := tabSet{}
for i := 0; i < int(numTables); i++ {
tables.Add(i)
}
return generateImpl(n, tables, tabSet{}, nil)
}
func generateImpl(n int, tables tabSet, join tabSet, joinSlice []table) [][]table {
if n <= 0 {
return [][]table{joinSlice}
}
var out [][]table
tables.ForEach(func(i int) {
if !join.Contains(i) {
newJoin := join.Copy()
newJoin.Add(i)
var jw tabSet
newJoin.ForEach(func (j int) {
jw.UnionWith(*joinsWith[table(j)])
})
newTables := jw.Difference(newJoin)
newJoinSlice := make([]table, len(joinSlice))
copy(newJoinSlice, joinSlice)
newJoinSlice = append(newJoinSlice, table(i))
out = append(out, generateImpl(n-1, newTables, newJoin, newJoinSlice)...)
}
})
return out
}
func GenerateJoins(max int) [][]table {
makeJoinsWith()
var out [][]table
for i := 1; i <= max; i++ {
out = append(out, generate(i)...)
}
return out
}
func GenerateQueries(max int) []string {
joins := GenerateJoins(max)
queries := make([]string, 0, len(joins))
for _, join := range joins {
query := make([]bytes.Buffer, 1)
query[0].WriteString("SELECT * FROM ")
if len(join) > 0 {
query[0].WriteString(tableNames[join[0]])
}
for i := 1; i < len(join); i++ {
for j := range query {
q := &query[j]
q.WriteString(" JOIN ")
q.WriteString(tableNames[join[i]])
q.WriteString(" ON ")
}
// Get all the conditions that match with the preceding tables in
// the join.
var conditions []string
for j := 0; j < i; j++ {
if cond, ok := joinCondition[joinCondKey{join[j], join[i]}]; ok {
conditions = append(conditions, cond)
}
}
var oldQuery []string
if len(conditions) > 1 {
// Save the old query.
oldQuery = make([]string, 0, len(query))
for j := 0; j < len(query); j++ {
q := &query[j]
oldQuery = append(oldQuery, q.String())
}
}
for j := 0; j < len(query); j++ {
q := &query[j]
q.WriteString(conditions[0])
}
if len(conditions) > 1 {
// Create new sets of queries for each condition.
for k := 1; k < len(conditions); k++ {
for _, s := range oldQuery {
var newQuery bytes.Buffer
newQuery.WriteString(s)
newQuery.WriteString(conditions[k])
query = append(query, newQuery)
}
}
}
}
for i := range query {
queries = append(queries, query[i].String())
}
}
return queries
}