-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsanitizeRowsOnParseError.test.ts
More file actions
291 lines (263 loc) · 10.8 KB
/
Copy pathsanitizeRowsOnParseError.test.ts
File metadata and controls
291 lines (263 loc) · 10.8 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
import { describe, it, expect } from "vitest";
import {
INVALID_UTF16_SENTINEL,
isClickHouseJsonParseError,
parseRowNumberFromError,
sanitizeRows,
sanitizeUnknownInPlace,
} from "~/v3/eventRepository/sanitizeRowsOnParseError.server";
const HIGH_SURROGATE = "\uD800";
const LOW_SURROGATE = "\uDC00";
describe("isClickHouseJsonParseError", () => {
it("recognises ClickHouse's parse-error string", () => {
const err = new Error(
"Cannot parse JSON object here: {...}: (while reading the value of key attributes): (at row 15)\n: While executing ParallelParsingBlockInputFormat. "
);
expect(isClickHouseJsonParseError(err)).toBe(true);
});
it("returns false for unrelated errors", () => {
expect(isClickHouseJsonParseError(new Error("Connection refused"))).toBe(false);
expect(
isClickHouseJsonParseError(
new Error("Size of JSON object at position 999 is extremely large.")
)
).toBe(false);
});
it("returns false for null / undefined / strings", () => {
expect(isClickHouseJsonParseError(null)).toBe(false);
expect(isClickHouseJsonParseError(undefined)).toBe(false);
expect(isClickHouseJsonParseError("Cannot parse JSON object")).toBe(true);
});
});
describe("parseRowNumberFromError", () => {
it("extracts the row index from a typical ClickHouse error message", () => {
expect(
parseRowNumberFromError(
"Cannot parse JSON object here: { ... }: (while reading the value of key attributes): (at row 1942)\n: While executing ParallelParsingBlockInputFormat."
)
).toBe(1942);
});
it("returns null when no row index is present", () => {
expect(parseRowNumberFromError("Some other error without a row hint")).toBeNull();
});
it("returns the first match when multiple `at row N` substrings exist", () => {
expect(parseRowNumberFromError("at row 1, oops also at row 2")).toBe(1);
});
});
describe("sanitizeUnknownInPlace", () => {
it("returns the string unchanged when it has no surrogates", () => {
const result = sanitizeUnknownInPlace("hello world");
expect(result).toEqual({ value: "hello world", fixed: 0 });
});
it("replaces a lone-surrogate string with the sentinel", () => {
const result = sanitizeUnknownInPlace(`prefix ${HIGH_SURROGATE} suffix`);
expect(result.value).toBe(INVALID_UTF16_SENTINEL);
expect(result.fixed).toBe(1);
});
it("leaves valid surrogate pairs (emoji) intact", () => {
const result = sanitizeUnknownInPlace("hello 😀 world");
expect(result.value).toBe("hello 😀 world");
expect(result.fixed).toBe(0);
});
it("walks nested objects and mutates string leaves in place", () => {
const row = {
id: "row-1",
attributes: {
ai: {
prompt: { messages: `bad ${HIGH_SURROGATE} string` },
usage: { input_tokens: 42 },
},
clean: "untouched",
},
};
const result = sanitizeUnknownInPlace(row);
expect(result.fixed).toBe(1);
expect((row.attributes.ai.prompt as any).messages).toBe(INVALID_UTF16_SENTINEL);
expect(row.attributes.clean).toBe("untouched");
expect((row.attributes.ai.usage as any).input_tokens).toBe(42);
expect(row.id).toBe("row-1");
});
it("walks arrays recursively", () => {
const value = ["ok", `bad ${LOW_SURROGATE} value`, "also ok", { nested: `also bad ${HIGH_SURROGATE}` }];
const result = sanitizeUnknownInPlace(value);
expect(result.fixed).toBe(2);
expect(value[1]).toBe(INVALID_UTF16_SENTINEL);
expect((value[3] as any).nested).toBe(INVALID_UTF16_SENTINEL);
expect(value[0]).toBe("ok");
expect(value[2]).toBe("also ok");
});
it("leaves non-string primitives untouched", () => {
expect(sanitizeUnknownInPlace(42)).toEqual({ value: 42, fixed: 0 });
expect(sanitizeUnknownInPlace(true)).toEqual({ value: true, fixed: 0 });
expect(sanitizeUnknownInPlace(null)).toEqual({ value: null, fixed: 0 });
expect(sanitizeUnknownInPlace(undefined)).toEqual({ value: undefined, fixed: 0 });
});
// ─── Out-of-range integers (TRI-9755) ──────────────────────────────────────
// ClickHouse's JSON(max_dynamic_paths) column rejects bare integer tokens
// outside [Int64.MIN, UInt64.MAX]. Such Numbers serialise as bare integer
// form via JSON.stringify (no exponent, since |value| < 1e21) so they reach
// ClickHouse as unquoted oversized ints. Sanitizer replaces them with the
// string form, which ClickHouse's dynamic JSON column accepts as a String
// subtype on that path.
it("replaces an integer-valued Number above UInt64.MAX with its string form", () => {
// 117039831458782870000 is the actual prod value (Google Plus ID after
// upstream JS-Number precision loss from 117039831458782873093).
const result = sanitizeUnknownInPlace(117039831458782870000);
expect(result.value).toBe("117039831458782870000");
expect(result.fixed).toBe(1);
});
it("catches the float64 boundary at exactly 2**64 (UInt64.MAX + 1)", () => {
// float64 cannot represent UInt64.MAX (2^64 - 1) exactly — the literal
// 18446744073709551615 in JS source rounds to 2^64. JSON.stringify
// emits this Number as "18446744073709552000", which exceeds UInt64.MAX
// and trips ClickHouse. Regression for the BigInt-based comparison;
// a naïve `value > 18446744073709551615` would let this pass.
const result = sanitizeUnknownInPlace(2 ** 64);
expect(result.value).toBe("18446744073709552000");
expect(result.fixed).toBe(1);
});
it("replaces an integer-valued Number below Int64.MIN with its string form", () => {
// -9223372036854775809 is the first failing negative; in float64 it
// rounds to the same representation as Int64.MIN (-9223372036854775808),
// but for completeness we check a clearly-out-of-range negative.
const result = sanitizeUnknownInPlace(-1e20);
expect(result.value).toBe("-100000000000000000000");
expect(result.fixed).toBe(1);
});
it("leaves safe integers and boundary values untouched", () => {
// 42 — safe integer
expect(sanitizeUnknownInPlace(42)).toEqual({ value: 42, fixed: 0 });
// Number.MAX_SAFE_INTEGER (2^53 - 1) — JSON.stringify still emits as integer
expect(sanitizeUnknownInPlace(Number.MAX_SAFE_INTEGER)).toEqual({
value: Number.MAX_SAFE_INTEGER,
fixed: 0,
});
// 2^63 (Int64.MAX + 1) — still fits in UInt64, CH accepts it
expect(sanitizeUnknownInPlace(2 ** 63)).toEqual({ value: 2 ** 63, fixed: 0 });
});
it("leaves non-integer numbers untouched (floats, NaN, Infinity)", () => {
// Numbers with a fractional part — emitted with `.` in JSON
expect(sanitizeUnknownInPlace(3.14)).toEqual({ value: 3.14, fixed: 0 });
// Very large float-form (>= 1e21) — JSON.stringify uses exponent form,
// CH parses as Float64 successfully
expect(sanitizeUnknownInPlace(1e25)).toEqual({ value: 1e25, fixed: 0 });
// NaN / Infinity — JSON.stringify emits `null`, so harmless on the wire
expect(sanitizeUnknownInPlace(Number.NaN)).toEqual({ value: Number.NaN, fixed: 0 });
expect(sanitizeUnknownInPlace(Number.POSITIVE_INFINITY)).toEqual({
value: Number.POSITIVE_INFINITY,
fixed: 0,
});
});
it("finds an oversized integer nested deep inside the actual scan-social-profiles shape", () => {
const row = {
output: {
data: {
profiles: [
{ module: "linktree", query: "x@example.com" },
{
module: "poshmark",
spec_format: [
{
platform_variables: [
{
key: "gp_id",
proper_key: "Gp Id",
// The actual prod value — bare JSON integer > UInt64.MAX
value: 117039831458782870000,
type: "int",
},
],
},
],
},
],
},
},
};
const result = sanitizeUnknownInPlace(row);
expect(result.fixed).toBe(1);
expect(
(row.output.data.profiles[1].spec_format![0].platform_variables[0] as any).value
).toBe("117039831458782870000");
// Untouched neighbours
expect(row.output.data.profiles[0].module).toBe("linktree");
expect(row.output.data.profiles[1].spec_format![0].platform_variables[0].type).toBe("int");
});
});
describe("sanitizeRows", () => {
function makeRow(suffix: string, badField?: string) {
return {
id: `row-${suffix}`,
attributes: { foo: badField ?? "clean" },
};
}
it("sanitizes every row that has bad strings", () => {
const rows = [
makeRow("0", `bad-0-${HIGH_SURROGATE}`),
makeRow("1", `bad-1-${HIGH_SURROGATE}`),
makeRow("2", "clean"),
makeRow("3", `bad-3-${HIGH_SURROGATE}`),
];
const result = sanitizeRows(rows);
expect(rows[0].attributes.foo).toBe(INVALID_UTF16_SENTINEL);
expect(rows[1].attributes.foo).toBe(INVALID_UTF16_SENTINEL);
expect(rows[2].attributes.foo).toBe("clean");
expect(rows[3].attributes.foo).toBe(INVALID_UTF16_SENTINEL);
expect(result.rowsTouched).toBe(3);
expect(result.fieldsSanitized).toBe(3);
});
it("returns zero counts when no row has bad strings", () => {
const rows = [makeRow("0"), makeRow("1"), makeRow("2")];
const result = sanitizeRows(rows);
expect(result).toEqual({ rowsTouched: 0, fieldsSanitized: 0 });
});
it("returns zero counts for an empty batch", () => {
expect(sanitizeRows([])).toEqual({ rowsTouched: 0, fieldsSanitized: 0 });
});
it("counts multiple sanitized fields on the same row as one rowTouched but multiple fields", () => {
const rows = [
{
id: "r0",
attributes: {
a: `bad ${HIGH_SURROGATE}`,
b: `also bad ${LOW_SURROGATE}`,
c: "fine",
},
},
];
const result = sanitizeRows(rows);
expect(result.rowsTouched).toBe(1);
expect(result.fieldsSanitized).toBe(2);
});
it("counts surrogate fixes and out-of-range integer fixes together (TRI-9755)", () => {
const rows = [
{
id: "r0",
attributes: {
surrogate: `bad ${HIGH_SURROGATE}`,
bigint: 117039831458782870000,
clean: "fine",
safe: 42,
},
},
{
id: "r1",
attributes: {
bigint: -1e20,
clean: "still fine",
},
},
{
id: "r2",
attributes: { clean: "no fixes needed" },
},
];
const result = sanitizeRows(rows);
expect(result.rowsTouched).toBe(2);
expect(result.fieldsSanitized).toBe(3);
expect(rows[0].attributes.surrogate).toBe(INVALID_UTF16_SENTINEL);
expect(rows[0].attributes.bigint).toBe("117039831458782870000");
expect(rows[0].attributes.safe).toBe(42);
expect(rows[1].attributes.bigint).toBe("-100000000000000000000");
});
});