Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/deparser/__tests__/pretty/show-quoting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ it('rejects an overlong SHOW identifier that cannot be split', () => {
expect(() => Deparser.deparse({ VariableShowStmt: { name: `${'x'.repeat(64)}..` } }))
.toThrow('SHOW name cannot be split into identifiers of at most 63 bytes');
});

it('bounds encoding work for a SHOW name with many dot boundaries', () => {
const encoder = new TextEncoder();
const constructor = jest.spyOn(globalThis, 'TextEncoder').mockImplementation(() => ({
encode(segment: string) {
if (segment.length > 63) throw new Error('Unbounded SHOW encoding');
return encoder.encode(segment);
}
} as TextEncoder));
const name = `${'a.'.repeat(100000)}.z`;
const started = Date.now();
try {
expect(Deparser.deparse({ VariableShowStmt: { name } })).toMatch(/^SHOW /);
expect(Date.now() - started).toBeLessThan(10000);
} finally {
constructor.mockRestore();
}
});
4 changes: 2 additions & 2 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4450,12 +4450,12 @@ export class Deparser implements DeparserVisitor {
const boundaries = new Map<number, number>();
for (let start = node.name.length - 1; start >= 0; start--) {
if (start > 0 && node.name[start - 1] !== '.') continue;
if (encoder.encode(node.name.slice(start)).length <= 63) {
if (node.name.length - start <= 63 && encoder.encode(node.name.slice(start)).length <= 63) {
boundaries.set(start, node.name.length);
continue;
}
for (let end = start + 1; end < node.name.length - 1; end++) {
if (encoder.encode(node.name.slice(start, end)).length > 63) break;
if (end - start > 63 || encoder.encode(node.name.slice(start, end)).length > 63) break;
if (node.name[end] === '.' && boundaries.has(end + 1)) {
boundaries.set(start, end);
}
Expand Down
Loading