-
Notifications
You must be signed in to change notification settings - Fork 2
/
autocomplete.go
254 lines (242 loc) · 4.51 KB
/
autocomplete.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"log"
"regexp"
"strings"
)
var keywordsAutocomlete []string
func initAutocomlete() {
keywords := []string{
"ADD COLUMN",
"AFTER",
"ALL",
"ALTER TABLE",
"ANY",
"ARRAY",
"ARRAY JOIN",
"AS",
"ASC",
"ASYNC",
"ATTACH PART",
"ATTACH PARTITION",
"ATTACH",
"BY",
"CASE",
"CHECK TABLE",
"CLEAR COLUMN",
"COLLATE",
"COORDINATE",
"COPY",
"CREATE",
"CROSS",
"DATABASE",
"DATABASES",
"DEDUPLICATE",
"DESC",
"DESCRIBE",
"DESCRIBE TABLE",
"DETACH",
"DETACH PARTITION",
"DISTINCT",
"DROP",
"DROP COLUMN",
"DROP PARTITION",
"ELSE",
"END",
"ENGINE",
"EXISTS",
"EXISTS TABLE",
"FETCH PARTITION",
"FINAL",
"FORMAT",
"FREEZE PARTITION",
"FROM",
"FULL",
"GROUP BY",
"GLOBAL",
"HAVING",
"IF EXISTS",
"IF NOT EXISTS",
"IN PARTITION",
"INNER",
"INSERT INTO",
"INTO",
"JOIN",
"KILL QUERY",
"LEFT",
"LEFT ARRAY JOIN",
"LIKE",
"LIMIT",
"LOCAL",
"MATERIALIZED",
"MODIFY COLUMN",
"MODIFY PRIMARY KEY",
"NAME",
"NOT",
"ON",
"OPTIMIZE TABLE",
"ORDER",
"ORDER BY",
"OUTFILE",
"PARTITION",
"POPULATE",
"PREWHERE",
"RENAME TABLE",
// "RESHARD",
"RIGHT",
"SELECT",
"SHOW",
"SET",
"SETTINGS",
"SAMPLE",
"SHOW CREATE TABLE",
"SHOW PROCESSLIST",
"SYNC",
"TABLE",
"TABLES",
"TEMPORARY",
"TEST",
"THEN",
"TO",
"TOTALS",
"UNION",
"UNION ALL",
"USE",
"USING",
"VALUES",
"VIEW",
"WHEN",
"WHERE",
"WITH",
"AggregatingMergeTree",
"Buffer",
"CollapsingMergeTree",
"Distributed",
"File",
"Join",
"Kafka",
"Log",
"MaterializedView",
"Memory",
"Merge",
"MergeTree",
"Null",
"ReplacingMergeTree",
"ReplicatedAggregatingMergeTree",
"ReplicatedCollapsingMergeTree",
"ReplicatedMergeTree",
"ReplicatedReplacingMergeTree",
"ReplicatedSummingMergeTree",
"Set",
"SummingMergeTree",
"TinyLog",
"View",
// https://github.com/yandex/ClickHouse/blob/master/dbms/src/DataStreams/FormatFactory.cpp
"BlockTabSeparated",
"CapnProto",
"CSV",
"CSVWithNames",
"JSON",
"JSONCompact",
"JSONEachRow",
"Native",
"Null",
"Null",
"ODBCDriver",
"Pretty",
"PrettyCompact",
"PrettyCompactMonoBlock",
"PrettyCompactNoEscapes",
"PrettyNoEscapes",
"PrettySpace",
"PrettySpaceNoEscapes",
"RowBinary",
formatTabSeparated,
"TabSeparatedRaw",
"TabSeparatedWithNames",
"TabSeparatedWithNamesAndTypes",
"TSKV",
"TSV",
"TSVRaw",
"TSVWithNames",
"TSVWithNamesAndTypes",
"Values",
formatVertical,
"VerticalRaw",
"XML",
"Array",
"Boolean",
"Date",
"DateTime",
"Enum",
"Expression",
"FixedString",
"Float32",
"Float64",
"Int8",
"Int16",
"Int32",
"Int64",
"Nullable",
"Set",
"String",
"Tuple",
"UInt8",
"UInt16",
"UInt32",
"UInt64",
}
query := `
SELECT concat('dictGet', t, '(\'', name, '\',\'', n,'\',' ,replaceRegexpAll(key,'([A-Za-z0-9]+)','to\\1(id)'), ')') as n2
FROM system.dictionaries ARRAY JOIN attribute.names as n, attribute.types as t
UNION ALL
SELECT DISTINCT name FROM (
SELECT name FROM system.functions
UNION ALL
SELECT concat(name,'If') FROM system.functions WHERE is_aggregate=1
UNION ALL
SELECT concat(name,'Array') FROM system.functions WHERE is_aggregate=1
UNION ALL
SELECT concat(name,'Merge') FROM system.functions WHERE is_aggregate=1
UNION ALL
SELECT concat(name,'State') FROM system.functions WHERE is_aggregate=1
UNION ALL
SELECT concat(name,'MergeState') FROM system.functions WHERE is_aggregate=1
UNION ALL
SELECT name FROM system.tables
UNION ALL
SELECT name FROM system.columns
UNION ALL
SELECT name FROM system.databases
UNION ALL
SELECT name FROM system.settings
) ORDER BY name`
data, err := serviceRequest(query)
if err != nil {
keywordsAutocomlete = keywords
log.Println(err)
}
for _, element := range data {
keywords = append(keywords, element[0])
}
keywordsAutocomlete = keywords
// spew.Dump(keywordsAutocomlete)
}
var lastWordRegexp = regexp.MustCompile("(^.*)\\b(\\w+)$")
func clickhouseComleter(line string) (c []string) {
matches := lastWordRegexp.FindStringSubmatch(line)
if len(matches) == 3 {
lastWord := matches[2]
prefix := matches[1]
for _, n := range keywordsAutocomlete {
// possible improvements:
// sort keywords by popularity
// make it context aware (?)
// use more effective search (like binary tree) than simple iterations through all the keywords
if strings.HasPrefix(strings.ToLower(n), strings.ToLower(lastWord)) {
c = append(c, prefix+n)
}
}
}
return
}