Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
300 lines (282 loc) · 8.67 KB
/
Copy pathdiff.go
File metadata and controls
300 lines (282 loc) · 8.67 KB
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
// diff — compare one table across two servers (the drift gate).
//
// Made for the "is the replica current?" question: schema, row count, time
// bounds, and a numeric fingerprint, side by side. Identical → exit 0;
// any difference → exit 1, so a cron line can gate on it. The B side is a
// profile name or an ANONYMOUS URI — for an authenticated ad-hoc B, save a
// profile first (sparrow connect ... --name b).
package main
import (
"context"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/flight"
"github.com/apache/arrow-go/v18/arrow/flight/flightsql"
"github.com/apache/arrow-go/v18/arrow/memory"
)
type diffCheck struct {
Check string `json:"check"`
A string `json:"a"`
B string `json:"b"`
Same bool `json:"same"`
}
type diffReport struct {
Table string `json:"table"`
A string `json:"a"`
B string `json:"b"`
Checks []diffCheck `json:"checks"`
Same bool `json:"same"`
}
func cmdDiff(args []string) error {
fs := newFlagSet("diff", `usage: sparrow diff <table> --against <profile-or-uri> [flags]
compare the table on two servers: schema, COUNT(*), --time MIN/MAX bounds,
and a numeric fingerprint (COUNT+AVG of up to 4 shared numeric columns).
identical → exit 0 · any drift → exit 1, so cron can gate on it
examples: sparrow diff series_data --against gizmo --time period
sparrow diff trades --against grpc+tls://replica:443 -o json`)
cf := addConnFlags(fs)
against := fs.String("against", "", "B side: profile name or anonymous grpc URI (required)")
timeCol := fs.String("time", "", "temporal column: compare MIN/MAX bounds")
output := fs.String("o", "", `output: "json" for a machine-readable report`)
pos := parseFlags(fs, args)
if len(pos) != 1 || *against == "" {
return usagef("usage: sparrow diff <table> --against <profile-or-uri> [--time col] [-o json]")
}
jsonOut := false
switch strings.ToLower(*output) {
case "":
case "json":
jsonOut = true
default:
return usagef(`diff -o supports only "json"`)
}
table := pos[0]
texpr := tableExpr(table)
pa, aname, err := cf.resolve()
if err != nil {
return err
}
pb, bname, err := resolveProfile(*against, Profile{Auth: "none"})
if err != nil {
return err
}
// two independent dials — each carries its OWN auth metadata context
base, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
ca, ctxA, err := dial(base, pa)
if err != nil {
return connError{fmt.Errorf("A (%s): %w", aname, err)}
}
defer ca.Close()
cb, ctxB, err := dial(base, pb)
if err != nil {
return connError{fmt.Errorf("B (%s): %w", bname, err)}
}
defer cb.Close()
rep := diffReport{Table: table,
A: fmt.Sprintf("%s (%s)", pa.URI, aname),
B: fmt.Sprintf("%s (%s)", pb.URI, bname)}
if !jsonOut {
fmt.Printf("sparrow diff %s\nA: %-10s %s\nB: %-10s %s\n\n", table, aname, pa.URI, bname, pb.URI)
}
emit := func(c diffCheck) {
rep.Checks = append(rep.Checks, c)
if jsonOut {
return
}
if c.Same {
fmt.Printf(" ✓ %-12s %s\n", c.Check, c.A)
} else {
fmt.Printf(" ✗ %-12s A %s · B %s\n", c.Check, c.A, c.B)
}
}
// ── schema (LIMIT 0 both sides) ─────────────────────────────────────
sa, errA := diffSchema(ctxA, ca, texpr)
sb, errB := diffSchema(ctxB, cb, texpr)
if errA != nil || errB != nil {
side, name, err := "A", aname, errA
if errA == nil {
side, name, err = "B", bname, errB
}
return fmt.Errorf("diff: %s (%s): table %s is not queryable: %v", side, name, table, err)
}
amap, bmap := fieldTypes(sa), fieldTypes(sb)
var added, removed, retyped []string
for _, f := range sb.Fields() {
if _, ok := amap[f.Name]; !ok {
added = append(added, f.Name)
}
}
for _, f := range sa.Fields() {
bt, ok := bmap[f.Name]
if !ok {
removed = append(removed, f.Name)
} else if at := amap[f.Name]; at != bt {
retyped = append(retyped, fmt.Sprintf("%s: %s→%s", f.Name, at, bt))
}
}
if len(added)+len(removed)+len(retyped) == 0 {
emit(diffCheck{Check: "schema", Same: true,
A: fmt.Sprintf("%d columns, identical", sa.NumFields()), B: "same"})
} else {
var parts []string
if len(added) > 0 {
parts = append(parts, "adds "+strings.Join(added, ","))
}
if len(removed) > 0 {
parts = append(parts, "drops "+strings.Join(removed, ","))
}
if len(retyped) > 0 {
parts = append(parts, "retypes "+strings.Join(retyped, ", "))
}
emit(diffCheck{Check: "schema", Same: false,
A: fmt.Sprintf("%d columns", sa.NumFields()), B: strings.Join(parts, " · ")})
}
// ── row count ───────────────────────────────────────────────────────
compare := func(name, sql string, render func(row []string) string, numeric bool) {
ra, errA := queryRow(ctxA, ca, sql)
rb, errB := queryRow(ctxB, cb, sql)
switch {
case errA != nil:
emit(diffCheck{Check: name, Same: false, A: "error: " + firstLine(errA), B: renderOr(rb, render)})
case errB != nil:
emit(diffCheck{Check: name, Same: false, A: renderOr(ra, render), B: "error: " + firstLine(errB)})
default:
same := rowsEqual(ra, rb, numeric)
c := diffCheck{Check: name, Same: same, A: render(ra), B: render(rb)}
if same {
c.B = "same"
}
emit(c)
}
}
compare("rows", "SELECT COUNT(*) FROM "+texpr, func(r []string) string {
if len(r) > 0 {
return groupDigits(r[0])
}
return "(empty)"
}, false)
// ── time bounds (--time) ────────────────────────────────────────────
if *timeCol != "" {
tc := quoteIdent(*timeCol)
compare("time", fmt.Sprintf("SELECT MIN(%s), MAX(%s) FROM %s", tc, tc, texpr),
func(r []string) string {
if len(r) >= 2 {
return r[0] + " → " + r[1]
}
return "(empty)"
}, false)
}
// ── numeric fingerprint: COUNT+AVG of ≤4 shared numeric columns ─────
var fpCols []string
for _, f := range sa.Fields() {
if len(fpCols) >= 4 {
break
}
if isNumericType(f.Type.ID()) && bmap[f.Name] == amap[f.Name] {
if _, ok := bmap[f.Name]; ok {
fpCols = append(fpCols, f.Name)
}
}
}
for _, c := range fpCols {
q := quoteIdent(c)
compare("Σ "+c, fmt.Sprintf("SELECT COUNT(%s), AVG(%s) FROM %s", q, q, texpr),
func(r []string) string {
if len(r) >= 2 {
return "count " + groupDigits(r[0]) + " · avg " + r[1]
}
return "(empty)"
}, true)
}
// ── verdict ─────────────────────────────────────────────────────────
same, differ := 0, 0
for _, c := range rep.Checks {
if c.Same {
same++
} else {
differ++
}
}
rep.Same = differ == 0
if jsonOut {
b, _ := json.MarshalIndent(rep, "", " ")
fmt.Println(string(b))
} else {
fmt.Println()
if rep.Same {
fmt.Printf("%d checks — identical\n", same)
} else {
fmt.Printf("%d same · %d differ — drift detected\n", same, differ)
}
}
if !rep.Same {
return fmt.Errorf("diff: %d difference(s) between %s and %s", differ, aname, bname)
}
return nil
}
func diffSchema(ctx context.Context, cl *flightsql.Client, texpr string) (*arrow.Schema, error) {
info, err := cl.Execute(ctx, "SELECT * FROM "+texpr+" LIMIT 0")
if err != nil {
return nil, err
}
if len(info.Schema) > 0 {
if sc, err := flight.DeserializeSchema(info.Schema, memory.DefaultAllocator); err == nil {
return sc, nil
}
}
for _, ep := range info.Endpoint {
rdr, err := cl.DoGet(ctx, ep.Ticket)
if err != nil {
return nil, err
}
sc := rdr.Schema()
for rdr.Next() {
}
rdr.Release()
if sc != nil {
return sc, nil
}
}
return nil, fmt.Errorf("no schema in FlightInfo or stream")
}
func fieldTypes(s *arrow.Schema) map[string]string {
out := make(map[string]string, s.NumFields())
for _, f := range s.Fields() {
out[f.Name] = f.Type.String()
}
return out
}
func renderOr(row []string, render func([]string) string) string {
if row == nil {
return "(unavailable)"
}
return render(row)
}
// rowsEqual compares result rows; numeric cells get a relative tolerance so
// engine-specific float summation order doesn't read as drift.
func rowsEqual(a, b []string, numeric bool) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] == b[i] {
continue
}
if numeric {
fa, ea := strconv.ParseFloat(a[i], 64)
fb, eb := strconv.ParseFloat(b[i], 64)
if ea == nil && eb == nil &&
math.Abs(fa-fb) <= 1e-9*math.Max(1, math.Max(math.Abs(fa), math.Abs(fb))) {
continue
}
}
return false
}
return true
}