-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_differ.go
392 lines (350 loc) · 9.97 KB
/
table_differ.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package nebulatest
import (
"fmt"
"log"
"strings"
"time"
"github.com/vesoft-inc/nebula-go/graph"
)
type TableDiffer struct {
DifferError
Response *graph.ExecutionResponse
}
func (d *TableDiffer) Diff(result string) {
real := printResult(d.Response)
result = strings.TrimSpace(result)
real = strings.TrimSpace(real)
if real != result {
d.err = fmt.Errorf("expected:\n%s, real:\n%s", result, real)
} else {
d.err = nil
}
}
const (
kColumnTypeEmpty = iota
kColumnTypeBool
kColumnTypeInteger
kColumnTypeID
kColumnTypeSinglePrecision
kColumnTypeDoublePrecision
kColumnTypeStr
kColumnTypeTimestamp
kColumnTypeYear
kColumnTypeMonth
kColumnTypeDate
kColumnTypeDatetime
)
func printResult(response *graph.ExecutionResponse) string {
widths, formats := computeColumnWidths(response)
if len(widths) == 0 {
return ""
}
sum := 0
for _, width := range widths {
sum += width
}
len := sum + 3*len(widths) + 1
headerLine := strings.Repeat("=", len)
rowLine := strings.Repeat("-", len)
builder := strings.Builder{}
builder.WriteString(headerLine)
builder.WriteString("\n|")
builder.WriteString(printHeader(response.GetColumnNames(), widths))
builder.WriteString(headerLine)
builder.WriteString("\n")
builder.WriteString(printData(response.GetRows(), rowLine, widths, formats))
return builder.String()
}
func computeColumnWidths(resp *graph.ExecutionResponse) (widths []int, formats []string) {
widths = make([]int, len(resp.ColumnNames))
for idx, columnName := range resp.ColumnNames {
widths[idx] = len(string(columnName))
}
formats = make([]string, len(widths))
if len(widths) == 0 || len(resp.Rows) == 0 {
return
}
types := make([]int, len(widths))
for idx := range widths {
types[idx] = kColumnTypeEmpty
formats[idx] = " "
}
for rowIdx, row := range resp.Rows {
if len(widths) != len(row.GetColumns()) {
log.Fatalf("Wrong number of columns(%d) in row(%d), expected %d", len(row.GetColumns()), rowIdx, len(widths))
}
for idx, column := range row.GetColumns() {
genFmt := types[idx] == kColumnTypeEmpty
if column.IsSetBoolVal() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeBool
} else {
if types[idx] != kColumnTypeBool {
log.Fatalf("%s is not bool column type", columnTypeString(types[idx]))
}
}
if widths[idx] < 5 {
widths[idx] = 5
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%ds |", widths[idx])
}
} else if column.IsSetInteger() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeInteger
} else {
if types[idx] != kColumnTypeInteger {
log.Fatalf("%s is not integer column type", columnTypeString(types[idx]))
}
}
val := column.GetInteger()
len := len(fmt.Sprintf("%d", val))
if widths[idx] < len {
widths[idx] = len
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%dd |", widths[idx])
}
} else if column.IsSetId() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeID
} else {
if types[idx] != kColumnTypeID {
log.Fatalf("%s is not id column type", columnTypeString(types[idx]))
}
}
val := column.GetId()
len := len(fmt.Sprintf("%d", val))
if widths[idx] < len {
widths[idx] = len
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%dd |", widths[idx])
}
} else if column.IsSetSinglePrecision() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeSinglePrecision
} else {
if types[idx] != kColumnTypeSinglePrecision {
log.Fatalf("%s is not single precision column type", columnTypeString(types[idx]))
}
}
val := column.GetSinglePrecision()
len := len(fmt.Sprintf("%f", val))
if widths[idx] < len {
widths[idx] = len
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%df |", widths[idx])
}
} else if column.IsSetDoublePrecision() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeDoublePrecision
} else {
if types[idx] != kColumnTypeDoublePrecision {
log.Fatalf("%s is not double precision column type", columnTypeString(types[idx]))
}
}
val := column.GetDoublePrecision()
len := len(fmt.Sprintf("%f", val))
if widths[idx] < len {
widths[idx] = len
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%df |", widths[idx])
}
} else if column.IsSetStr() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeStr
} else {
if types[idx] != kColumnTypeStr {
log.Fatalf("%s is not str column type", columnTypeString(types[idx]))
}
}
val := column.GetStr()
len := len(string(val))
if widths[idx] < len {
widths[idx] = len
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%ds |", widths[idx])
}
} else if column.IsSetTimestamp() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeTimestamp
} else {
if types[idx] != kColumnTypeTimestamp {
log.Fatalf("%s is not timestamp column type", columnTypeString(types[idx]))
}
}
if widths[idx] < 19 {
widths[idx] = 19
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%%dd-%%02d-%%02d %%02d:%%02d:%%02d |", widths[idx]-15)
}
} else if column.IsSetYear() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeYear
} else {
if types[idx] != kColumnTypeYear {
log.Fatalf("%s is not year column type", columnTypeString(types[idx]))
}
}
if widths[idx] < 4 {
widths[idx] = 4
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%-%dd |", widths[idx])
}
} else if column.IsSetMonth() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeMonth
} else {
if types[idx] != kColumnTypeMonth {
log.Fatalf("%s is not month column type", columnTypeString(types[idx]))
}
}
if widths[idx] < 7 {
widths[idx] = 7
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%%dd/%%02d |", widths[idx]-3)
}
} else if column.IsSetDate() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeDate
} else {
if types[idx] != kColumnTypeDate {
types[idx] = kColumnTypeDate
}
}
if widths[idx] < 10 {
widths[idx] = 10
genFmt = true
}
if genFmt {
formats[idx] = fmt.Sprintf(" %%%dd/%%02d/%%02d |", widths[idx]-6)
}
} else if column.IsSetDatetime() {
if types[idx] == kColumnTypeEmpty {
types[idx] = kColumnTypeDatetime
} else {
if types[idx] != kColumnTypeDatetime {
log.Fatalf("%s is not datetime column type", columnTypeString(types[idx]))
}
}
formats[idx] = fmt.Sprintf(" %%%dd/%%02d/%%02d %%02d:%%02d:%%02d.%%03d%%03d |", widths[idx]-22)
} else {
if types[idx] != kColumnTypeEmpty {
log.Fatalf("Wrong column type: %s", columnTypeString(types[idx]))
}
}
}
}
return
}
func columnTypeString(columnType int) string {
switch columnType {
case kColumnTypeEmpty:
return "Empty"
case kColumnTypeBool:
return "Bool"
case kColumnTypeInteger:
return "Integer"
case kColumnTypeID:
return "ID"
case kColumnTypeSinglePrecision:
return "Single precision"
case kColumnTypeDoublePrecision:
return "Double precision"
case kColumnTypeStr:
return "Str"
case kColumnTypeTimestamp:
return "Timestamp"
case kColumnTypeYear:
return "Year"
case kColumnTypeMonth:
return "Month"
case kColumnTypeDate:
return "Date"
case kColumnTypeDatetime:
return "Datetime"
default:
log.Printf("Invalid column type %d", columnType)
return ""
}
}
func printHeader(columnNames [][]byte, widths []int) string {
if len(columnNames) == 0 {
return ""
}
builder := strings.Builder{}
for idx, columnName := range columnNames {
format := fmt.Sprintf(" %%-%ds |", widths[idx])
builder.WriteString(fmt.Sprintf(format, string(columnName)))
}
builder.WriteString("\n")
return builder.String()
}
func printData(rows []*graph.RowValue, rowLine string, widths []int, formats []string) string {
if len(rows) == 0 {
return ""
}
builder := strings.Builder{}
for _, row := range rows {
builder.WriteString("|")
for colIdx, column := range row.GetColumns() {
var str string
if column.IsSetBoolVal() {
if column.GetBoolVal() {
str = fmt.Sprintf(formats[colIdx], "true")
} else {
str = fmt.Sprintf(formats[colIdx], "false")
}
} else if column.IsSetInteger() {
str = fmt.Sprintf(formats[colIdx], column.GetInteger())
} else if column.IsSetId() {
str = fmt.Sprintf(formats[colIdx], column.GetId())
} else if column.IsSetSinglePrecision() {
str = fmt.Sprintf(formats[colIdx], column.GetSinglePrecision())
} else if column.IsSetDoublePrecision() {
str = fmt.Sprintf(formats[colIdx], column.GetDoublePrecision())
} else if column.IsSetStr() {
str = fmt.Sprintf(formats[colIdx], column.GetStr())
} else if column.IsSetTimestamp() {
timestamp := column.GetTimestamp()
tm := time.Unix(int64(timestamp), 0)
str = fmt.Sprintf(formats[colIdx], tm.Year()+1900, tm.Month()+1, tm.Day(), tm.Hour(), tm.Minute(), tm.Second())
} else if column.IsSetYear() {
str = fmt.Sprintf(formats[colIdx], column.GetYear())
} else if column.IsSetMonth() {
month := column.GetMonth()
str = fmt.Sprintf(formats[colIdx], month.GetYear(), month.GetMonth())
} else if column.IsSetDate() {
date := column.GetDate()
str = fmt.Sprintf(formats[colIdx], date.GetYear(), date.GetMonth(), date.GetDay())
} else if column.IsSetDatetime() {
dt := column.GetDatetime()
str = fmt.Sprintf(formats[colIdx], dt.GetYear(), dt.GetMonth(), dt.GetDay(), dt.GetHour(), dt.GetMinute(), dt.GetSecond(), dt.GetMicrosec())
} else {
format := fmt.Sprintf(" %%-%dc |", widths[colIdx])
str = fmt.Sprintf(format, " ")
}
builder.WriteString(str)
}
builder.WriteString("\n")
builder.WriteString(rowLine)
builder.WriteString("\n")
}
return builder.String()
}